Skip to content

Commit 7d2eb00

Browse files
committed
Switch to upstream protocol for resolving code action
Note that we have to maintain custom implementation on the client side: I don't see how to marry bulitin resolve support with groups and snippets.
1 parent ada5a88 commit 7d2eb00

File tree

15 files changed

+86
-91
lines changed

15 files changed

+86
-91
lines changed

crates/rust-analyzer/src/caps.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use serde_json::json;
1616
use crate::semantic_tokens;
1717

1818
pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabilities {
19-
let code_action_provider = code_action_capabilities(client_caps);
20-
2119
ServerCapabilities {
2220
text_document_sync: Some(TextDocumentSyncCapability::Options(TextDocumentSyncOptions {
2321
open_close: Some(true),
@@ -49,7 +47,7 @@ pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabiliti
4947
document_highlight_provider: Some(OneOf::Left(true)),
5048
document_symbol_provider: Some(OneOf::Left(true)),
5149
workspace_symbol_provider: Some(OneOf::Left(true)),
52-
code_action_provider: Some(code_action_provider),
50+
code_action_provider: Some(code_action_capabilities(client_caps)),
5351
code_lens_provider: Some(CodeLensOptions { resolve_provider: Some(true) }),
5452
document_formatting_provider: Some(OneOf::Left(true)),
5553
document_range_formatting_provider: None,
@@ -113,7 +111,7 @@ fn code_action_capabilities(client_caps: &ClientCapabilities) -> CodeActionProvi
113111
CodeActionKind::REFACTOR_INLINE,
114112
CodeActionKind::REFACTOR_REWRITE,
115113
]),
116-
resolve_provider: None,
114+
resolve_provider: Some(true),
117115
work_done_progress_options: Default::default(),
118116
})
119117
})

crates/rust-analyzer/src/config.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub struct ClientCapsConfig {
144144
pub code_action_literals: bool,
145145
pub work_done_progress: bool,
146146
pub code_action_group: bool,
147-
pub resolve_code_action: bool,
147+
pub code_action_resolve: bool,
148148
pub hover_actions: bool,
149149
pub status_notification: bool,
150150
pub signature_help_label_offsets: bool,
@@ -383,6 +383,14 @@ impl Config {
383383
}
384384
}
385385
}
386+
387+
if let Some(code_action) = &doc_caps.code_action {
388+
if let Some(resolve_support) = &code_action.resolve_support {
389+
if resolve_support.properties.iter().any(|it| it == "edit") {
390+
self.client_caps.code_action_resolve = true;
391+
}
392+
}
393+
}
386394
}
387395

388396
if let Some(window_caps) = caps.window.as_ref() {
@@ -400,7 +408,6 @@ impl Config {
400408
self.assist.allow_snippets(snippet_text_edit);
401409

402410
self.client_caps.code_action_group = get_bool("codeActionGroup");
403-
self.client_caps.resolve_code_action = get_bool("resolveCodeAction");
404411
self.client_caps.hover_actions = get_bool("hoverActions");
405412
self.client_caps.status_notification = get_bool("statusNotification");
406413
}

crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
fixes: [
3737
CodeAction {
3838
title: "consider prefixing with an underscore",
39-
id: None,
4039
group: None,
4140
kind: Some(
4241
CodeActionKind(
@@ -70,6 +69,7 @@
7069
is_preferred: Some(
7170
true,
7271
),
72+
data: None,
7373
},
7474
],
7575
},

crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
fixes: [
3737
CodeAction {
3838
title: "consider prefixing with an underscore",
39-
id: None,
4039
group: None,
4140
kind: Some(
4241
CodeActionKind(
@@ -70,6 +69,7 @@
7069
is_preferred: Some(
7170
true,
7271
),
72+
data: None,
7373
},
7474
],
7575
},

crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
fixes: [
3737
CodeAction {
3838
title: "consider prefixing with an underscore",
39-
id: None,
4039
group: None,
4140
kind: Some(
4241
CodeActionKind(
@@ -70,6 +69,7 @@
7069
is_preferred: Some(
7170
true,
7271
),
72+
data: None,
7373
},
7474
],
7575
},

crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
fixes: [
5252
CodeAction {
5353
title: "return the expression directly",
54-
id: None,
5554
group: None,
5655
kind: Some(
5756
CodeActionKind(
@@ -98,6 +97,7 @@
9897
is_preferred: Some(
9998
true,
10099
),
100+
data: None,
101101
},
102102
],
103103
},

crates/rust-analyzer/src/diagnostics/to_proto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ fn map_rust_child_diagnostic(
110110
} else {
111111
MappedRustChildDiagnostic::SuggestedFix(lsp_ext::CodeAction {
112112
title: rd.message.clone(),
113-
id: None,
114113
group: None,
115114
kind: Some(lsp_types::CodeActionKind::QUICKFIX),
116115
edit: Some(lsp_ext::SnippetWorkspaceEdit {
@@ -119,6 +118,7 @@ fn map_rust_child_diagnostic(
119118
document_changes: None,
120119
}),
121120
is_preferred: Some(true),
121+
data: None,
122122
})
123123
}
124124
}

crates/rust-analyzer/src/handlers.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -806,11 +806,11 @@ fn handle_fixes(
806806
let edit = to_proto::snippet_workspace_edit(&snap, fix.source_change)?;
807807
let action = lsp_ext::CodeAction {
808808
title: fix.label.to_string(),
809-
id: None,
810809
group: None,
811810
kind: Some(CodeActionKind::QUICKFIX),
812811
edit: Some(edit),
813812
is_preferred: Some(false),
813+
data: None,
814814
};
815815
res.push(action);
816816
}
@@ -852,11 +852,11 @@ pub(crate) fn handle_code_action(
852852

853853
handle_fixes(&snap, &params, &mut res)?;
854854

855-
if snap.config.client_caps.resolve_code_action {
855+
if snap.config.client_caps.code_action_resolve {
856856
for (index, assist) in
857857
snap.analysis.unresolved_assists(&snap.config.assist, frange)?.into_iter().enumerate()
858858
{
859-
res.push(to_proto::unresolved_code_action(&snap, assist, index)?);
859+
res.push(to_proto::unresolved_code_action(&snap, params.clone(), assist, index)?);
860860
}
861861
} else {
862862
for assist in snap.analysis.resolved_assists(&snap.config.assist, frange)?.into_iter() {
@@ -867,11 +867,16 @@ pub(crate) fn handle_code_action(
867867
Ok(Some(res))
868868
}
869869

870-
pub(crate) fn handle_resolve_code_action(
870+
pub(crate) fn handle_code_action_resolve(
871871
mut snap: GlobalStateSnapshot,
872-
params: lsp_ext::ResolveCodeActionParams,
873-
) -> Result<Option<lsp_ext::SnippetWorkspaceEdit>> {
874-
let _p = profile::span("handle_resolve_code_action");
872+
mut code_action: lsp_ext::CodeAction,
873+
) -> Result<lsp_ext::CodeAction> {
874+
let _p = profile::span("handle_code_action_resolve");
875+
let params = match code_action.data.take() {
876+
Some(it) => it,
877+
None => Err("can't resolve code action without data")?,
878+
};
879+
875880
let file_id = from_proto::file_id(&snap, &params.code_action_params.text_document.uri)?;
876881
let line_index = snap.analysis.file_line_index(file_id)?;
877882
let range = from_proto::text_range(&line_index, params.code_action_params.range);
@@ -888,7 +893,9 @@ pub(crate) fn handle_resolve_code_action(
888893
let index = index.parse::<usize>().unwrap();
889894
let assist = &assists[index];
890895
assert!(assist.assist.id.0 == id);
891-
Ok(to_proto::resolved_code_action(&snap, assist.clone())?.edit)
896+
let edit = to_proto::resolved_code_action(&snap, assist.clone())?.edit;
897+
code_action.edit = edit;
898+
Ok(code_action)
892899
}
893900

894901
pub(crate) fn handle_code_lens(

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,6 @@ pub struct JoinLinesParams {
113113
pub ranges: Vec<Range>,
114114
}
115115

116-
pub enum ResolveCodeActionRequest {}
117-
118-
impl Request for ResolveCodeActionRequest {
119-
type Params = ResolveCodeActionParams;
120-
type Result = Option<SnippetWorkspaceEdit>;
121-
const METHOD: &'static str = "experimental/resolveCodeAction";
122-
}
123-
124-
/// Params for the ResolveCodeActionRequest
125-
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
126-
#[serde(rename_all = "camelCase")]
127-
pub struct ResolveCodeActionParams {
128-
pub code_action_params: lsp_types::CodeActionParams,
129-
pub id: String,
130-
}
131-
132116
pub enum OnEnter {}
133117

134118
impl Request for OnEnter {
@@ -265,13 +249,18 @@ impl Request for CodeActionRequest {
265249
const METHOD: &'static str = "textDocument/codeAction";
266250
}
267251

252+
pub enum CodeActionResolveRequest {}
253+
impl Request for CodeActionResolveRequest {
254+
type Params = CodeAction;
255+
type Result = CodeAction;
256+
const METHOD: &'static str = "codeAction/resolve";
257+
}
258+
268259
#[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
269260
#[serde(rename_all = "camelCase")]
270261
pub struct CodeAction {
271262
pub title: String,
272263
#[serde(skip_serializing_if = "Option::is_none")]
273-
pub id: Option<String>,
274-
#[serde(skip_serializing_if = "Option::is_none")]
275264
pub group: Option<String>,
276265
#[serde(skip_serializing_if = "Option::is_none")]
277266
pub kind: Option<CodeActionKind>,
@@ -282,6 +271,16 @@ pub struct CodeAction {
282271
pub edit: Option<SnippetWorkspaceEdit>,
283272
#[serde(skip_serializing_if = "Option::is_none")]
284273
pub is_preferred: Option<bool>,
274+
275+
#[serde(skip_serializing_if = "Option::is_none")]
276+
pub data: Option<CodeActionData>,
277+
}
278+
279+
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
280+
#[serde(rename_all = "camelCase")]
281+
pub struct CodeActionData {
282+
pub code_action_params: lsp_types::CodeActionParams,
283+
pub id: String,
285284
}
286285

287286
#[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]

crates/rust-analyzer/src/main_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl GlobalState {
435435
.on::<lsp_ext::Runnables>(handlers::handle_runnables)
436436
.on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints)
437437
.on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)
438-
.on::<lsp_ext::ResolveCodeActionRequest>(handlers::handle_resolve_code_action)
438+
.on::<lsp_ext::CodeActionResolveRequest>(handlers::handle_code_action_resolve)
439439
.on::<lsp_ext::HoverRequest>(handlers::handle_hover)
440440
.on::<lsp_ext::ExternalDocs>(handlers::handle_open_docs)
441441
.on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)

0 commit comments

Comments
 (0)