fix: Refactor apply_workspace_edit to remove assert

Fixes #698
pull/766/head
Blaž Hrastnik 3 years ago
parent 07be66c677
commit b02d872938

@ -2445,7 +2445,7 @@ fn apply_workspace_edit(
) { ) {
if let Some(ref changes) = workspace_edit.changes { if let Some(ref changes) = workspace_edit.changes {
log::debug!("workspace changes: {:?}", changes); log::debug!("workspace changes: {:?}", changes);
editor.set_error(String::from("Handling workspace changesis not implemented yet, see https://github.com/helix-editor/helix/issues/183")); editor.set_error(String::from("Handling workspace_edit.changes is not implemented yet, see https://github.com/helix-editor/helix/issues/183"));
return; return;
// 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 // 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
// TODO: find some example that uses workspace changes, and test it // TODO: find some example that uses workspace changes, and test it
@ -2463,8 +2463,30 @@ fn apply_workspace_edit(
match document_changes { match document_changes {
lsp::DocumentChanges::Edits(document_edits) => { lsp::DocumentChanges::Edits(document_edits) => {
for document_edit in document_edits { for document_edit in document_edits {
let (view, doc) = current!(editor); let path = document_edit
assert_eq!(doc.url().unwrap(), document_edit.text_document.uri); .text_document
.uri
.to_file_path()
.expect("unable to convert URI to filepath");
let current_view_id = view!(editor).id;
let doc = editor
.document_by_path_mut(path)
.expect("Document for document_changes not found");
// Need to determine a view for apply/append_changes_to_history
let selections = doc.selections();
let view_id = if selections.contains_key(&current_view_id) {
// use current if possible
current_view_id
} else {
// Hack: we take the first available view_id
selections
.keys()
.next()
.copied()
.expect("No view_id available")
};
let edits = document_edit let edits = document_edit
.edits .edits
.iter() .iter()
@ -2482,8 +2504,8 @@ fn apply_workspace_edit(
edits, edits,
offset_encoding, offset_encoding,
); );
doc.apply(&transaction, view.id); doc.apply(&transaction, view_id);
doc.append_changes_to_history(view.id); doc.append_changes_to_history(view_id);
} }
} }
lsp::DocumentChanges::Operations(operations) => { lsp::DocumentChanges::Operations(operations) => {

Loading…
Cancel
Save