From 545f562fe63cb47cac4298fc2e5de17e78f192ed Mon Sep 17 00:00:00 2001 From: misiasty3 Date: Fri, 10 Mar 2023 22:30:28 +0100 Subject: [PATCH 1/3] check if lsp supports symbol renaming before prompting the user --- helix-lsp/src/client.rs | 18 +++++++++--------- helix-term/src/commands/lsp.rs | 9 +++++++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index 9fa118fbda22..bf9eb1f6ef0b 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -1104,21 +1104,21 @@ impl Client { Some(self.call::(params)) } + pub fn supports_rename(&self) -> bool { + let capabilities = self.capabilities.get().unwrap(); + match capabilities.rename_provider { + Some(lsp::OneOf::Left(true)) | Some(lsp::OneOf::Right(_)) => true, + // None | Some(false) + _ => false, + } + } + pub fn rename_symbol( &self, text_document: lsp::TextDocumentIdentifier, position: lsp::Position, new_name: String, ) -> Option>> { - let capabilities = self.capabilities.get().unwrap(); - - // Return early if the language server does not support renaming. - match capabilities.rename_provider { - Some(lsp::OneOf::Left(true)) | Some(lsp::OneOf::Right(_)) => (), - // None | Some(false) - _ => return None, - }; - let params = lsp::RenameParams { text_document_position: lsp::TextDocumentPositionParams { text_document, diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 08519366b257..c954ed066b0f 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -1290,8 +1290,7 @@ pub fn rename_symbol(cx: &mut Context) { match language_server.rename_symbol(doc.identifier(), pos, input.to_string()) { Some(future) => future, None => { - cx.editor - .set_error("Language server does not support symbol renaming"); + cx.editor.set_error("Failed to rename symbol"); return; } }; @@ -1310,6 +1309,12 @@ pub fn rename_symbol(cx: &mut Context) { let language_server = language_server!(cx.editor, doc); let offset_encoding = language_server.offset_encoding(); + if !language_server.supports_rename() { + cx.editor + .set_error("Language server does not support symbol renaming"); + return; + } + let pos = doc.position(view.id, offset_encoding); match language_server.prepare_rename(doc.identifier(), pos) { From 49ecac832ff6a55e12948550cd2f09870ad17d40 Mon Sep 17 00:00:00 2001 From: misiasty3 <85675213+misiasty3@users.noreply.github.com> Date: Sun, 12 Mar 2023 22:09:13 +0100 Subject: [PATCH 2/3] cleaner code Co-authored-by: Poliorcetics --- helix-lsp/src/client.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index bf9eb1f6ef0b..1c0e22979489 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -1106,11 +1106,7 @@ impl Client { pub fn supports_rename(&self) -> bool { let capabilities = self.capabilities.get().unwrap(); - match capabilities.rename_provider { - Some(lsp::OneOf::Left(true)) | Some(lsp::OneOf::Right(_)) => true, - // None | Some(false) - _ => false, - } + matches!(capabilities.rename_provider, Some(lsp::OneOf::Left(true)) | Some(lsp::OneOf::Right(_))) } pub fn rename_symbol( From 977a361f529c15e5a8e90c022b6690e60208b496 Mon Sep 17 00:00:00 2001 From: misiasty3 Date: Sun, 12 Mar 2023 22:29:05 +0100 Subject: [PATCH 3/3] better error handling --- helix-lsp/src/client.rs | 9 ++++++++- helix-term/src/commands/lsp.rs | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index 1c0e22979489..5aee514e8a7f 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -1106,7 +1106,10 @@ impl Client { pub fn supports_rename(&self) -> bool { let capabilities = self.capabilities.get().unwrap(); - matches!(capabilities.rename_provider, Some(lsp::OneOf::Left(true)) | Some(lsp::OneOf::Right(_))) + matches!( + capabilities.rename_provider, + Some(lsp::OneOf::Left(true) | lsp::OneOf::Right(_)) + ) } pub fn rename_symbol( @@ -1115,6 +1118,10 @@ impl Client { position: lsp::Position, new_name: String, ) -> Option>> { + if !self.supports_rename() { + return None; + } + let params = lsp::RenameParams { text_document_position: lsp::TextDocumentPositionParams { text_document, diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index c954ed066b0f..094ced2853ef 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -1290,7 +1290,8 @@ pub fn rename_symbol(cx: &mut Context) { match language_server.rename_symbol(doc.identifier(), pos, input.to_string()) { Some(future) => future, None => { - cx.editor.set_error("Failed to rename symbol"); + cx.editor + .set_error("Language server does not support symbol renaming"); return; } };