From 7633b26841f7d6cd103c7ee45bd89fec8b6e46c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Dzivjak?= Date: Thu, 23 Nov 2023 00:34:25 +0100 Subject: [PATCH] add return --- helix-term/src/application.rs | 117 ++++++++++++++++++++-------------- 1 file changed, 69 insertions(+), 48 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 21c56db44ce7..74c0a04f7a14 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -1180,58 +1180,79 @@ impl Application { let language_server = language_server!(); let offset_encoding = language_server.offset_encoding(); - if params.external.unwrap_or_default() { - self.jobs - .callback(crate::open_external_url_callback(params.uri)); - return; - } - - let path = match params.uri.to_file_path() { - Ok(path) => path, - Err(_) => { - log::error!("Unsupported file URI: {}", params.uri); - return; + let success = match params { + lsp::ShowDocumentParams { + external: Some(true), + uri, + .. + } => { + self.jobs.callback(crate::open_external_url_callback(uri)); + true } - }; - - let action = match params.take_focus { - Some(true) => helix_view::editor::Action::Replace, - _ => helix_view::editor::Action::HorizontalSplit, - }; + lsp::ShowDocumentParams { + uri, + selection, + take_focus, + .. + } => { + match uri.to_file_path() { + Ok(path) => { + let action = match take_focus { + Some(true) => helix_view::editor::Action::Replace, + _ => helix_view::editor::Action::HorizontalSplit, + }; - let doc = match self.editor.open(&path, action) { - Ok(id) => doc_mut!(self.editor, &id), - Err(err) => { - log::error!("failed to open path: {:?}: {:?}", params.uri, err); - return; + match self.editor.open(&path, action) { + Ok(id) => { + let doc = doc_mut!(self.editor, &id); + if let Some(range) = selection { + // TODO: convert inside server + if let Some(new_range) = lsp_range_to_range( + doc.text(), + range, + offset_encoding, + ) { + let view = view_mut!(self.editor); + + // we flip the range so that the cursor sits on the start of the symbol + // (for example start of the function). + doc.set_selection( + view.id, + Selection::single( + new_range.head, + new_range.anchor, + ), + ); + if action.align_view(view, doc.id()) { + align_view(doc, view, Align::Center); + } + } else { + log::warn!( + "lsp position out of bounds - {:?}", + range + ); + }; + }; + true + } + Err(err) => { + log::error!( + "failed to open path: {:?}: {:?}", + uri, + err + ); + false + } + } + } + Err(err) => { + log::error!("unsupported file URI: {}: {:?}", uri, err); + false + } + } } }; - - if let Some(range) = params.selection { - // TODO: convert inside server - let new_range = if let Some(new_range) = - lsp_range_to_range(doc.text(), range, offset_encoding) - { - new_range - } else { - log::warn!("lsp position out of bounds - {:?}", range); - return; - }; - - let view = view_mut!(self.editor); - - // we flip the range so that the cursor sits on the start of the symbol - // (for example start of the function). - doc.set_selection( - view.id, - Selection::single(new_range.head, new_range.anchor), - ); - if action.align_view(view, doc.id()) { - align_view(doc, view, Align::Center); - } - } - - Ok(serde_json::Value::Null) + Ok(json!(lsp::ShowDocumentResult { success })) } };