Skip to content

Commit 65a3cc2

Browse files
bors[bot]mcrakhman
andauthored
Merge #4717
4717: Implementation of lazy assits r=matklad a=mcrakhman Co-authored-by: Mikhail Rakhmanov <[email protected]>
2 parents 794f6da + 6cd2e04 commit 65a3cc2

14 files changed

+210
-85
lines changed

crates/ra_ide/src/lib.rs

+17-22
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub use crate::{
7777
};
7878

7979
pub use hir::Documentation;
80-
pub use ra_assists::{AssistConfig, AssistId};
80+
pub use ra_assists::{Assist, AssistConfig, AssistId, ResolvedAssist};
8181
pub use ra_db::{
8282
Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRootId,
8383
};
@@ -142,14 +142,6 @@ pub struct AnalysisHost {
142142
db: RootDatabase,
143143
}
144144

145-
#[derive(Debug)]
146-
pub struct Assist {
147-
pub id: AssistId,
148-
pub label: String,
149-
pub group_label: Option<String>,
150-
pub source_change: SourceChange,
151-
}
152-
153145
impl AnalysisHost {
154146
pub fn new(lru_capacity: Option<usize>) -> AnalysisHost {
155147
AnalysisHost { db: RootDatabase::new(lru_capacity) }
@@ -470,20 +462,23 @@ impl Analysis {
470462
self.with_db(|db| completion::completions(db, config, position).map(Into::into))
471463
}
472464

473-
/// Computes assists (aka code actions aka intentions) for the given
465+
/// Computes resolved assists with source changes for the given position.
466+
pub fn resolved_assists(
467+
&self,
468+
config: &AssistConfig,
469+
frange: FileRange,
470+
) -> Cancelable<Vec<ResolvedAssist>> {
471+
self.with_db(|db| ra_assists::Assist::resolved(db, config, frange))
472+
}
473+
474+
/// Computes unresolved assists (aka code actions aka intentions) for the given
474475
/// position.
475-
pub fn assists(&self, config: &AssistConfig, frange: FileRange) -> Cancelable<Vec<Assist>> {
476-
self.with_db(|db| {
477-
ra_assists::Assist::resolved(db, config, frange)
478-
.into_iter()
479-
.map(|assist| Assist {
480-
id: assist.assist.id,
481-
label: assist.assist.label,
482-
group_label: assist.assist.group.map(|it| it.0),
483-
source_change: assist.source_change,
484-
})
485-
.collect()
486-
})
476+
pub fn unresolved_assists(
477+
&self,
478+
config: &AssistConfig,
479+
frange: FileRange,
480+
) -> Cancelable<Vec<Assist>> {
481+
self.with_db(|db| Assist::unresolved(db, config, frange))
487482
}
488483

489484
/// Computes the set of diagnostics for the given file.

crates/rust-analyzer/src/config.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ pub struct ClientCapsConfig {
123123
pub code_action_literals: bool,
124124
pub work_done_progress: bool,
125125
pub code_action_group: bool,
126+
pub resolve_code_action: bool,
126127
}
127128

128129
impl Default for Config {
@@ -336,7 +337,11 @@ impl Config {
336337

337338
let code_action_group =
338339
experimental.get("codeActionGroup").and_then(|it| it.as_bool()) == Some(true);
339-
self.client_caps.code_action_group = code_action_group
340+
self.client_caps.code_action_group = code_action_group;
341+
342+
let resolve_code_action =
343+
experimental.get("resolveCodeAction").and_then(|it| it.as_bool()) == Some(true);
344+
self.client_caps.resolve_code_action = resolve_code_action;
340345
}
341346
}
342347
}

crates/rust-analyzer/src/diagnostics/snapshots/rust_analyzer__diagnostics__to_proto__tests__snap_multi_line_fix.snap

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ expression: diag
6565
fixes: [
6666
CodeAction {
6767
title: "return the expression directly",
68+
id: None,
6869
group: None,
6970
kind: Some(
7071
"quickfix",

crates/rust-analyzer/src/diagnostics/snapshots/rust_analyzer__diagnostics__to_proto__tests__snap_rustc_unused_variable.snap

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ expression: diag
5050
fixes: [
5151
CodeAction {
5252
title: "consider prefixing with an underscore",
53+
id: None,
5354
group: None,
5455
kind: Some(
5556
"quickfix",

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

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ fn map_rust_child_diagnostic(
145145
} else {
146146
MappedRustChildDiagnostic::SuggestedFix(lsp_ext::CodeAction {
147147
title: rd.message.clone(),
148+
id: None,
148149
group: None,
149150
kind: Some("quickfix".to_string()),
150151
edit: Some(lsp_ext::SnippetWorkspaceEdit {

crates/rust-analyzer/src/lsp_ext.rs

+18
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ pub struct JoinLinesParams {
9797
pub ranges: Vec<Range>,
9898
}
9999

100+
pub enum ResolveCodeActionRequest {}
101+
102+
impl Request for ResolveCodeActionRequest {
103+
type Params = ResolveCodeActionParams;
104+
type Result = Option<SnippetWorkspaceEdit>;
105+
const METHOD: &'static str = "experimental/resolveCodeAction";
106+
}
107+
108+
/// Params for the ResolveCodeActionRequest
109+
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
110+
#[serde(rename_all = "camelCase")]
111+
pub struct ResolveCodeActionParams {
112+
pub code_action_params: lsp_types::CodeActionParams,
113+
pub id: String,
114+
}
115+
100116
pub enum OnEnter {}
101117

102118
impl Request for OnEnter {
@@ -202,6 +218,8 @@ impl Request for CodeActionRequest {
202218
pub struct CodeAction {
203219
pub title: String,
204220
#[serde(skip_serializing_if = "Option::is_none")]
221+
pub id: Option<String>,
222+
#[serde(skip_serializing_if = "Option::is_none")]
205223
pub group: Option<String>,
206224
#[serde(skip_serializing_if = "Option::is_none")]
207225
pub kind: Option<String>,

crates/rust-analyzer/src/main_loop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ fn on_request(
509509
.on::<lsp_ext::Runnables>(handlers::handle_runnables)?
510510
.on::<lsp_ext::InlayHints>(handlers::handle_inlay_hints)?
511511
.on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)?
512+
.on::<lsp_ext::ResolveCodeActionRequest>(handlers::handle_resolve_code_action)?
512513
.on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)?
513514
.on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol)?
514515
.on::<lsp_types::request::WorkspaceSymbol>(handlers::handle_workspace_symbol)?

crates/rust-analyzer/src/main_loop/handlers.rs

+58-19
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use ra_project_model::TargetKind;
2525
use ra_syntax::{AstNode, SyntaxKind, TextRange, TextSize};
2626
use serde::{Deserialize, Serialize};
2727
use serde_json::to_value;
28-
use stdx::format_to;
28+
use stdx::{format_to, split1};
2929

3030
use crate::{
3131
cargo_target_spec::CargoTargetSpec,
@@ -701,37 +701,27 @@ pub fn handle_formatting(
701701
}]))
702702
}
703703

704-
pub fn handle_code_action(
705-
snap: GlobalStateSnapshot,
706-
params: lsp_types::CodeActionParams,
707-
) -> Result<Option<Vec<lsp_ext::CodeAction>>> {
708-
let _p = profile("handle_code_action");
709-
// We intentionally don't support command-based actions, as those either
710-
// requires custom client-code anyway, or requires server-initiated edits.
711-
// Server initiated edits break causality, so we avoid those as well.
712-
if !snap.config.client_caps.code_action_literals {
713-
return Ok(None);
714-
}
715-
704+
fn handle_fixes(
705+
snap: &GlobalStateSnapshot,
706+
params: &lsp_types::CodeActionParams,
707+
res: &mut Vec<lsp_ext::CodeAction>,
708+
) -> Result<()> {
716709
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
717710
let line_index = snap.analysis().file_line_index(file_id)?;
718711
let range = from_proto::text_range(&line_index, params.range);
719-
let frange = FileRange { file_id, range };
720-
721712
let diagnostics = snap.analysis().diagnostics(file_id)?;
722-
let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
723713

724714
let fixes_from_diagnostics = diagnostics
725715
.into_iter()
726716
.filter_map(|d| Some((d.range, d.fix?)))
727717
.filter(|(diag_range, _fix)| diag_range.intersect(range).is_some())
728718
.map(|(_range, fix)| fix);
729-
730719
for fix in fixes_from_diagnostics {
731720
let title = fix.label;
732721
let edit = to_proto::snippet_workspace_edit(&snap, fix.source_change)?;
733722
let action = lsp_ext::CodeAction {
734723
title,
724+
id: None,
735725
group: None,
736726
kind: Some(lsp_types::code_action_kind::QUICKFIX.into()),
737727
edit: Some(edit),
@@ -747,13 +737,62 @@ pub fn handle_code_action(
747737
}
748738
res.push(fix.action.clone());
749739
}
740+
Ok(())
741+
}
742+
743+
pub fn handle_code_action(
744+
snap: GlobalStateSnapshot,
745+
params: lsp_types::CodeActionParams,
746+
) -> Result<Option<Vec<lsp_ext::CodeAction>>> {
747+
let _p = profile("handle_code_action");
748+
// We intentionally don't support command-based actions, as those either
749+
// requires custom client-code anyway, or requires server-initiated edits.
750+
// Server initiated edits break causality, so we avoid those as well.
751+
if !snap.config.client_caps.code_action_literals {
752+
return Ok(None);
753+
}
754+
755+
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
756+
let line_index = snap.analysis().file_line_index(file_id)?;
757+
let range = from_proto::text_range(&line_index, params.range);
758+
let frange = FileRange { file_id, range };
759+
let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
750760

751-
for assist in snap.analysis().assists(&snap.config.assist, frange)?.into_iter() {
752-
res.push(to_proto::code_action(&snap, assist)?.into());
761+
handle_fixes(&snap, &params, &mut res)?;
762+
763+
if snap.config.client_caps.resolve_code_action {
764+
for (index, assist) in
765+
snap.analysis().unresolved_assists(&snap.config.assist, frange)?.into_iter().enumerate()
766+
{
767+
res.push(to_proto::unresolved_code_action(&snap, assist, index)?);
768+
}
769+
} else {
770+
for assist in snap.analysis().resolved_assists(&snap.config.assist, frange)?.into_iter() {
771+
res.push(to_proto::resolved_code_action(&snap, assist)?);
772+
}
753773
}
774+
754775
Ok(Some(res))
755776
}
756777

778+
pub fn handle_resolve_code_action(
779+
snap: GlobalStateSnapshot,
780+
params: lsp_ext::ResolveCodeActionParams,
781+
) -> Result<Option<lsp_ext::SnippetWorkspaceEdit>> {
782+
let _p = profile("handle_resolve_code_action");
783+
let file_id = from_proto::file_id(&snap, &params.code_action_params.text_document.uri)?;
784+
let line_index = snap.analysis().file_line_index(file_id)?;
785+
let range = from_proto::text_range(&line_index, params.code_action_params.range);
786+
let frange = FileRange { file_id, range };
787+
788+
let assists = snap.analysis().resolved_assists(&snap.config.assist, frange)?;
789+
let (id_string, index) = split1(&params.id, ':').unwrap();
790+
let index = index.parse::<usize>().unwrap();
791+
let assist = &assists[index];
792+
assert!(assist.assist.id.0 == id_string);
793+
Ok(to_proto::resolved_code_action(&snap, assist.clone())?.edit)
794+
}
795+
757796
pub fn handle_code_lens(
758797
snap: GlobalStateSnapshot,
759798
params: lsp_types::CodeLensParams,

crates/rust-analyzer/src/to_proto.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use ra_db::{FileId, FileRange};
33
use ra_ide::{
44
Assist, CompletionItem, CompletionItemKind, Documentation, FileSystemEdit, Fold, FoldKind,
55
FunctionSignature, Highlight, HighlightModifier, HighlightTag, HighlightedRange, Indel,
6-
InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget, ReferenceAccess, Runnable,
7-
RunnableKind, Severity, SourceChange, SourceFileEdit, TextEdit,
6+
InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget, ReferenceAccess,
7+
ResolvedAssist, Runnable, RunnableKind, Severity, SourceChange, SourceFileEdit, TextEdit,
88
};
99
use ra_syntax::{SyntaxKind, TextRange, TextSize};
1010
use ra_vfs::LineEndings;
@@ -623,20 +623,36 @@ fn main() <fold>{
623623
}
624624
}
625625

626-
pub(crate) fn code_action(
626+
pub(crate) fn unresolved_code_action(
627627
snap: &GlobalStateSnapshot,
628628
assist: Assist,
629+
index: usize,
629630
) -> Result<lsp_ext::CodeAction> {
630631
let res = lsp_ext::CodeAction {
631632
title: assist.label,
632-
group: if snap.config.client_caps.code_action_group { assist.group_label } else { None },
633+
id: Some(format!("{}:{}", assist.id.0.to_owned(), index.to_string())),
634+
group: assist.group.filter(|_| snap.config.client_caps.code_action_group).map(|gr| gr.0),
633635
kind: Some(String::new()),
634-
edit: Some(snippet_workspace_edit(snap, assist.source_change)?),
636+
edit: None,
635637
command: None,
636638
};
637639
Ok(res)
638640
}
639641

642+
pub(crate) fn resolved_code_action(
643+
snap: &GlobalStateSnapshot,
644+
assist: ResolvedAssist,
645+
) -> Result<lsp_ext::CodeAction> {
646+
let change = assist.source_change;
647+
unresolved_code_action(snap, assist.assist, 0).and_then(|it| {
648+
Ok(lsp_ext::CodeAction {
649+
id: None,
650+
edit: Some(snippet_workspace_edit(snap, change)?),
651+
..it
652+
})
653+
})
654+
}
655+
640656
pub(crate) fn runnable(
641657
snap: &GlobalStateSnapshot,
642658
file_id: FileId,

docs/dev/lsp-extensions.md

+24
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,30 @@ Invoking code action at this position will yield two code actions for importing
9797
* Is a fixed two-level structure enough?
9898
* Should we devise a general way to encode custom interaction protocols for GUI refactorings?
9999

100+
## Lazy assists with `ResolveCodeAction`
101+
102+
**Issue:** https://github.com/microsoft/language-server-protocol/issues/787
103+
104+
**Client Capability** `{ "resolveCodeAction": boolean }`
105+
106+
If this capability is set, the assists will be computed lazily. Thus `CodeAction` returned from the server will only contain `id` but not `edit` or `command` fields. The only exclusion from the rule is the diagnostic edits.
107+
108+
After the client got the id, it should then call `experimental/resolveCodeAction` command on the server and provide the following payload:
109+
110+
```typescript
111+
interface ResolveCodeActionParams {
112+
id: string;
113+
codeActionParams: lc.CodeActionParams;
114+
}
115+
```
116+
117+
As a result of the command call the client will get the respective workspace edit (`lc.WorkspaceEdit`).
118+
119+
### Unresolved Questions
120+
121+
* Apply smarter filtering for ids?
122+
* Upon `resolveCodeAction` command only call the assits which should be resolved and not all of them?
123+
100124
## Parent Module
101125

102126
**Issue:** https://github.com/microsoft/language-server-protocol/issues/1002

0 commit comments

Comments
 (0)