on master help sign in
fix: Refactor apply_workspace_edit to remove assert
b02d872Blaž Hrastnik
helix-term/src/commands.rs+27−5
@@ -2445,7 +2445,7 @@ fn apply_workspace_edit(
2445) {
2445) {
2446 if let Some(ref changes) = workspace_edit.changes {
2446 if let Some(ref changes) = workspace_edit.changes {
2447 log::debug!("workspace changes: {:?}", changes);
2447 log::debug!("workspace changes: {:?}", changes);
2448− editor.set_error(String::from("Handling workspace changesis not implemented yet, see https://github.com/helix-editor/helix/issues/183"));
2448+ editor.set_error(String::from("Handling workspace_edit.changes is not implemented yet, see https://github.com/helix-editor/helix/issues/183"));
2449 return;
2449 return;
2450 // Not sure if it works properly, it'll be safer to just panic here to avoid breaking some parts of code on which code actions will be used
2450 // Not sure if it works properly, it'll be safer to just panic here to avoid breaking some parts of code on which code actions will be used
2451 // TODO: find some example that uses workspace changes, and test it
2451 // TODO: find some example that uses workspace changes, and test it
@@ -2463,8 +2463,30 @@ fn apply_workspace_edit(
2463 match document_changes {
2463 match document_changes {
2464 lsp::DocumentChanges::Edits(document_edits) => {
2464 lsp::DocumentChanges::Edits(document_edits) => {
2465 for document_edit in document_edits {
2465 for document_edit in document_edits {
2466− let (view, doc) = current!(editor);
2466+ let path = document_edit
2467− assert_eq!(doc.url().unwrap(), document_edit.text_document.uri);
2467+ .text_document
2468+ .uri
2469+ .to_file_path()
2470+ .expect("unable to convert URI to filepath");
2471+ let current_view_id = view!(editor).id;
2472+ let doc = editor
2473+ .document_by_path_mut(path)
2474+ .expect("Document for document_changes not found");
2475+
2476+ // Need to determine a view for apply/append_changes_to_history
2477+ let selections = doc.selections();
2478+ let view_id = if selections.contains_key(&current_view_id) {
2479+ // use current if possible
2480+ current_view_id
2481+ } else {
2482+ // Hack: we take the first available view_id
2483+ selections
2484+ .keys()
2485+ .next()
2486+ .copied()
2487+ .expect("No view_id available")
2488+ };
2489+
2468 let edits = document_edit
2490 let edits = document_edit
2469 .edits
2491 .edits
2470 .iter()
2492 .iter()
@@ -2482,8 +2504,8 @@ fn apply_workspace_edit(
2482 edits,
2504 edits,
2483 offset_encoding,
2505 offset_encoding,
2484 );
2506 );
2485− doc.apply(&transaction, view.id);
2507+ doc.apply(&transaction, view_id);
2486− doc.append_changes_to_history(view.id);
2508+ doc.append_changes_to_history(view_id);
2487 }
2509 }
2488 }
2510 }
2489 lsp::DocumentChanges::Operations(operations) => {
2511 lsp::DocumentChanges::Operations(operations) => {