on master help sign in
feat: resolve code action (#7677)
commit8977123f25baba838d2d16f8a40e78563c4ebf4a authorsigmaSd <bedisnbiba@gmail.com> dateFri 21 Jul 2023, 20:50 (3 years ago) parentd52b790
2 files changed+41−3
helix-lsp/src/client.rs+27−2 helix-term/src/commands/lsp.rs+14−1
helix-lsp/src/client.rs+27−2
@@ -7,8 +7,9 @@ use crate::{
77use helix_core::{find_workspace, path, syntax::LanguageServerFeature, ChangeSet, Rope};
88use helix_loader::{self, VERSION_AND_GIT_HASH};
99use lsp::{
10− notification::DidChangeWorkspaceFolders, DidChangeWorkspaceFoldersParams, OneOf,
11− PositionEncodingKind, WorkspaceFolder, WorkspaceFoldersChangeEvent,
10+ notification::DidChangeWorkspaceFolders, CodeActionCapabilityResolveSupport,
11+ DidChangeWorkspaceFoldersParams, OneOf, PositionEncodingKind, WorkspaceFolder,
12+ WorkspaceFoldersChangeEvent,
1213};
1314use lsp_types as lsp;
1415use parking_lot::Mutex;
@@ -609,6 +610,12 @@ impl Client {
609610 .collect(),
610611 },
611612 }),
613+ is_preferred_support: Some(true),
614+ disabled_support: Some(true),
615+ data_support: Some(true),
616+ resolve_support: Some(CodeActionCapabilityResolveSupport {
617+ properties: vec!["edit".to_owned(), "command".to_owned()],
618+ }),
612619 ..Default::default()
613620 }),
614621 publish_diagnostics: Some(lsp::PublishDiagnosticsClientCapabilities {
@@ -954,6 +961,24 @@ impl Client {
954961 Some(self.call::<lsp::request::ResolveCompletionItem>(completion_item))
955962 }
956963
964+ pub fn resolve_code_action(
965+ &self,
966+ code_action: lsp::CodeAction,
967+ ) -> Option<impl Future<Output = Result<Value>>> {
968+ let capabilities = self.capabilities.get().unwrap();
969+
970+ // Return early if the server does not support resolving code action.
971+ match capabilities.completion_provider {
972+ Some(lsp::CompletionOptions {
973+ resolve_provider: Some(true),
974+ ..
975+ }) => (),
976+ _ => return None,
977+ }
978+
979+ Some(self.call::<lsp::request::CodeActionResolveRequest>(code_action))
980+ }
981+
957982 pub fn text_document_signature_help(
958983 &self,
959984 text_document: lsp::TextDocumentIdentifier,
helix-term/src/commands/lsp.rs+14−1
@@ -743,7 +743,20 @@ pub fn code_action(cx: &mut Context) {
743743 }
744744 lsp::CodeActionOrCommand::CodeAction(code_action) => {
745745 log::debug!("code action: {:?}", code_action);
746− if let Some(ref workspace_edit) = code_action.edit {
746+ // we support lsp "codeAction/resolve" for `edit` and `command` fields
747+ let mut resolved_code_action = None;
748+ if code_action.edit.is_none() || code_action.command.is_none() {
749+ if let Some(future) = language_server.resolve_code_action(code_action.clone()) {
750+ if let Ok(response) = helix_lsp::block_on(future) {
751+ if let Ok(code_action) = serde_json::from_value::<CodeAction>(response) {
752+ resolved_code_action = Some(code_action);
753+ }
754+ }
755+ }
756+ }
757+ let resolved_code_action = resolved_code_action.as_ref().unwrap_or(code_action);
758+
759+ if let Some(ref workspace_edit) = resolved_code_action.edit {
747760 log::debug!("edit: {:?}", workspace_edit);
748761 let _ = apply_workspace_edit(editor, offset_encoding, workspace_edit);
749762 }