Skip to content

Commit fe29a9e

Browse files
bors[bot]matklad
andauthored
Merge #8494
8494: internal: unfork code paths for unresolved and resolved assist r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 15d3b6c + 460f0ef commit fe29a9e

File tree

5 files changed

+72
-101
lines changed

5 files changed

+72
-101
lines changed

crates/ide_assists/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ pub use assist_config::AssistConfig;
2828

2929
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3030
pub enum AssistKind {
31+
// FIXME: does the None variant make sense? Probably not.
3132
None,
33+
3234
QuickFix,
3335
Generate,
3436
Refactor,

crates/rust-analyzer/src/from_proto.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,27 @@ pub(crate) fn text_range(line_index: &LineIndex, range: lsp_types::Range) -> Tex
4242
TextRange::new(start, end)
4343
}
4444

45-
pub(crate) fn file_id(world: &GlobalStateSnapshot, url: &lsp_types::Url) -> Result<FileId> {
46-
world.url_to_file_id(url)
45+
pub(crate) fn file_id(snap: &GlobalStateSnapshot, url: &lsp_types::Url) -> Result<FileId> {
46+
snap.url_to_file_id(url)
4747
}
4848

4949
pub(crate) fn file_position(
50-
world: &GlobalStateSnapshot,
50+
snap: &GlobalStateSnapshot,
5151
tdpp: lsp_types::TextDocumentPositionParams,
5252
) -> Result<FilePosition> {
53-
let file_id = file_id(world, &tdpp.text_document.uri)?;
54-
let line_index = world.file_line_index(file_id)?;
53+
let file_id = file_id(snap, &tdpp.text_document.uri)?;
54+
let line_index = snap.file_line_index(file_id)?;
5555
let offset = offset(&line_index, tdpp.position);
5656
Ok(FilePosition { file_id, offset })
5757
}
5858

5959
pub(crate) fn file_range(
60-
world: &GlobalStateSnapshot,
60+
snap: &GlobalStateSnapshot,
6161
text_document_identifier: lsp_types::TextDocumentIdentifier,
6262
range: lsp_types::Range,
6363
) -> Result<FileRange> {
64-
let file_id = file_id(world, &text_document_identifier.uri)?;
65-
let line_index = world.file_line_index(file_id)?;
64+
let file_id = file_id(snap, &text_document_identifier.uri)?;
65+
let line_index = snap.file_line_index(file_id)?;
6666
let range = text_range(&line_index, range);
6767
Ok(FileRange { file_id, range })
6868
}
@@ -82,7 +82,7 @@ pub(crate) fn assist_kind(kind: lsp_types::CodeActionKind) -> Option<AssistKind>
8282
}
8383

8484
pub(crate) fn annotation(
85-
world: &GlobalStateSnapshot,
85+
snap: &GlobalStateSnapshot,
8686
code_lens: lsp_types::CodeLens,
8787
) -> Result<Annotation> {
8888
let data = code_lens.data.unwrap();
@@ -91,25 +91,25 @@ pub(crate) fn annotation(
9191
match resolve {
9292
lsp_ext::CodeLensResolveData::Impls(params) => {
9393
let file_id =
94-
world.url_to_file_id(&params.text_document_position_params.text_document.uri)?;
95-
let line_index = world.file_line_index(file_id)?;
94+
snap.url_to_file_id(&params.text_document_position_params.text_document.uri)?;
95+
let line_index = snap.file_line_index(file_id)?;
9696

9797
Ok(Annotation {
9898
range: text_range(&line_index, code_lens.range),
9999
kind: AnnotationKind::HasImpls {
100-
position: file_position(world, params.text_document_position_params)?,
100+
position: file_position(snap, params.text_document_position_params)?,
101101
data: None,
102102
},
103103
})
104104
}
105105
lsp_ext::CodeLensResolveData::References(params) => {
106-
let file_id = world.url_to_file_id(&params.text_document.uri)?;
107-
let line_index = world.file_line_index(file_id)?;
106+
let file_id = snap.url_to_file_id(&params.text_document.uri)?;
107+
let line_index = snap.file_line_index(file_id)?;
108108

109109
Ok(Annotation {
110110
range: text_range(&line_index, code_lens.range),
111111
kind: AnnotationKind::HasReferences {
112-
position: file_position(world, params)?,
112+
position: file_position(snap, params)?,
113113
data: None,
114114
},
115115
})

crates/rust-analyzer/src/handlers.rs

Lines changed: 40 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ use std::{
88
};
99

1010
use ide::{
11-
AnnotationConfig, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, Query,
12-
RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange, TextEdit,
11+
AnnotationConfig, AssistKind, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData,
12+
Query, RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange, TextEdit,
1313
};
1414
use ide_db::SymbolKind;
1515
use itertools::Itertools;
1616
use lsp_server::ErrorCode;
1717
use lsp_types::{
1818
CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
1919
CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
20-
CodeActionKind, CodeLens, CompletionItem, Diagnostic, DiagnosticTag, DocumentFormattingParams,
20+
CodeLens, CompletionItem, Diagnostic, DiagnosticTag, DocumentFormattingParams,
2121
DocumentHighlight, FoldingRange, FoldingRangeParams, HoverContents, Location, NumberOrString,
2222
Position, PrepareRenameResponse, Range, RenameParams, SemanticTokensDeltaParams,
2323
SemanticTokensFullDeltaResult, SemanticTokensParams, SemanticTokensRangeParams,
@@ -36,7 +36,7 @@ use crate::{
3636
diff::diff,
3737
from_proto,
3838
global_state::{GlobalState, GlobalStateSnapshot},
39-
line_index::{LineEndings, LineIndex},
39+
line_index::LineEndings,
4040
lsp_ext::{self, InlayHint, InlayHintsParams},
4141
lsp_utils::all_edits_are_disjoint,
4242
to_proto, LspError, Result,
@@ -982,86 +982,66 @@ pub(crate) fn handle_code_action(
982982
params: lsp_types::CodeActionParams,
983983
) -> Result<Option<Vec<lsp_ext::CodeAction>>> {
984984
let _p = profile::span("handle_code_action");
985-
// We intentionally don't support command-based actions, as those either
986-
// requires custom client-code anyway, or requires server-initiated edits.
987-
// Server initiated edits break causality, so we avoid those as well.
985+
988986
if !snap.config.code_action_literals() {
987+
// We intentionally don't support command-based actions, as those either
988+
// require either custom client-code or server-initiated edits. Server
989+
// initiated edits break causality, so we avoid those.
989990
return Ok(None);
990991
}
991992

992-
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
993-
let line_index = snap.file_line_index(file_id)?;
994-
let range = from_proto::text_range(&line_index, params.range);
995-
let frange = FileRange { file_id, range };
993+
let line_index =
994+
snap.file_line_index(from_proto::file_id(&snap, &params.text_document.uri)?)?;
995+
let frange = from_proto::file_range(&snap, params.text_document.clone(), params.range)?;
996996

997997
let mut assists_config = snap.config.assist();
998998
assists_config.allowed = params
999-
.clone()
1000999
.context
10011000
.only
1001+
.clone()
10021002
.map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect());
10031003

10041004
let mut res: Vec<lsp_ext::CodeAction> = Vec::new();
10051005

1006-
let include_quick_fixes = match &params.context.only {
1007-
Some(v) => v.iter().any(|it| {
1008-
it == &lsp_types::CodeActionKind::EMPTY || it == &lsp_types::CodeActionKind::QUICKFIX
1009-
}),
1006+
let include_quick_fixes = match &assists_config.allowed {
1007+
Some(v) => v.iter().any(|it| it == &AssistKind::None || it == &AssistKind::QuickFix),
10101008
None => true,
10111009
};
1012-
if include_quick_fixes {
1013-
add_quick_fixes(&snap, frange, &line_index, &mut res)?;
1014-
}
1015-
1016-
if snap.config.code_action_resolve() {
1017-
for (index, assist) in
1018-
snap.analysis.assists(&assists_config, false, frange)?.into_iter().enumerate()
1019-
{
1020-
res.push(to_proto::unresolved_code_action(&snap, params.clone(), assist, index)?);
1021-
}
1022-
} else {
1023-
for assist in snap.analysis.assists(&assists_config, true, frange)?.into_iter() {
1024-
res.push(to_proto::resolved_code_action(&snap, assist)?);
1025-
}
1026-
}
1010+
let code_action_resolve_cap = snap.config.code_action_resolve();
10271011

1028-
Ok(Some(res))
1029-
}
1012+
let mut assists = Vec::new();
10301013

1031-
fn add_quick_fixes(
1032-
snap: &GlobalStateSnapshot,
1033-
frange: FileRange,
1034-
line_index: &LineIndex,
1035-
acc: &mut Vec<lsp_ext::CodeAction>,
1036-
) -> Result<()> {
1037-
let diagnostics = snap.analysis.diagnostics(&snap.config.diagnostics(), frange.file_id)?;
1014+
// Fixes from native diagnostics.
1015+
if include_quick_fixes {
1016+
let diagnostics = snap.analysis.diagnostics(&snap.config.diagnostics(), frange.file_id)?;
1017+
assists.extend(
1018+
diagnostics
1019+
.into_iter()
1020+
.filter_map(|d| d.fix)
1021+
.filter(|fix| fix.target.intersect(frange.range).is_some()),
1022+
)
1023+
}
10381024

1039-
for fix in diagnostics
1040-
.into_iter()
1041-
.filter_map(|d| d.fix)
1042-
.filter(|fix| fix.target.intersect(frange.range).is_some())
1043-
{
1044-
if let Some(source_change) = fix.source_change {
1045-
let edit = to_proto::snippet_workspace_edit(&snap, source_change)?;
1046-
let action = lsp_ext::CodeAction {
1047-
title: fix.label.to_string(),
1048-
group: None,
1049-
kind: Some(CodeActionKind::QUICKFIX),
1050-
edit: Some(edit),
1051-
is_preferred: Some(false),
1052-
data: None,
1053-
};
1054-
acc.push(action);
1055-
}
1025+
// Assists proper.
1026+
assists.extend(snap.analysis.assists(&assists_config, !code_action_resolve_cap, frange)?);
1027+
for (index, assist) in assists.into_iter().enumerate() {
1028+
let resolve_data =
1029+
if code_action_resolve_cap { Some((index, params.clone())) } else { None };
1030+
let code_action = to_proto::code_action(&snap, assist, resolve_data)?;
1031+
res.push(code_action)
10561032
}
10571033

1034+
// Fixes from `cargo check`.
10581035
for fix in snap.check_fixes.get(&frange.file_id).into_iter().flatten() {
1036+
// FIXME: this mapping is awkward and shouldn't exist. Refactor
1037+
// `snap.check_fixes` to not convert to LSP prematurely.
10591038
let fix_range = from_proto::text_range(&line_index, fix.range);
10601039
if fix_range.intersect(frange.range).is_some() {
1061-
acc.push(fix.action.clone());
1040+
res.push(fix.action.clone());
10621041
}
10631042
}
1064-
Ok(())
1043+
1044+
Ok(Some(res))
10651045
}
10661046

10671047
pub(crate) fn handle_code_action_resolve(
@@ -1091,7 +1071,7 @@ pub(crate) fn handle_code_action_resolve(
10911071
let index = index.parse::<usize>().unwrap();
10921072
let assist = &assists[index];
10931073
assert!(assist.id.0 == id);
1094-
let edit = to_proto::resolved_code_action(&snap, assist.clone())?.edit;
1074+
let edit = to_proto::code_action(&snap, assist.clone(), None)?.edit;
10951075
code_action.edit = edit;
10961076
Ok(code_action)
10971077
}

crates/rust-analyzer/src/to_proto.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -843,40 +843,31 @@ pub(crate) fn code_action_kind(kind: AssistKind) -> lsp_types::CodeActionKind {
843843
}
844844
}
845845

846-
pub(crate) fn unresolved_code_action(
846+
pub(crate) fn code_action(
847847
snap: &GlobalStateSnapshot,
848-
code_action_params: lsp_types::CodeActionParams,
849848
assist: Assist,
850-
index: usize,
849+
resolve_data: Option<(usize, lsp_types::CodeActionParams)>,
851850
) -> Result<lsp_ext::CodeAction> {
852-
assert!(assist.source_change.is_none());
853-
let res = lsp_ext::CodeAction {
851+
let mut res = lsp_ext::CodeAction {
854852
title: assist.label.to_string(),
855853
group: assist.group.filter(|_| snap.config.code_action_group()).map(|gr| gr.0),
856854
kind: Some(code_action_kind(assist.id.1)),
857855
edit: None,
858856
is_preferred: None,
859-
data: Some(lsp_ext::CodeActionData {
860-
id: format!("{}:{}", assist.id.0, index.to_string()),
861-
code_action_params,
862-
}),
863-
};
864-
Ok(res)
865-
}
866-
867-
pub(crate) fn resolved_code_action(
868-
snap: &GlobalStateSnapshot,
869-
assist: Assist,
870-
) -> Result<lsp_ext::CodeAction> {
871-
let change = assist.source_change.unwrap();
872-
let res = lsp_ext::CodeAction {
873-
edit: Some(snippet_workspace_edit(snap, change)?),
874-
title: assist.label.to_string(),
875-
group: assist.group.filter(|_| snap.config.code_action_group()).map(|gr| gr.0),
876-
kind: Some(code_action_kind(assist.id.1)),
877-
is_preferred: None,
878857
data: None,
879858
};
859+
match (assist.source_change, resolve_data) {
860+
(Some(it), _) => res.edit = Some(snippet_workspace_edit(snap, it)?),
861+
(None, Some((index, code_action_params))) => {
862+
res.data = Some(lsp_ext::CodeActionData {
863+
id: format!("{}:{}", assist.id.0, index.to_string()),
864+
code_action_params,
865+
});
866+
}
867+
(None, None) => {
868+
stdx::never!("assist should always be resolved if client can't do lazy resolving")
869+
}
870+
};
880871
Ok(res)
881872
}
882873

crates/rust-analyzer/tests/rust-analyzer/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ fn main() {}
340340
}
341341
]
342342
},
343-
"isPreferred": false,
344343
"kind": "quickfix",
345344
"title": "Create module"
346345
}]),
@@ -411,7 +410,6 @@ fn main() {{}}
411410
}
412411
]
413412
},
414-
"isPreferred": false,
415413
"kind": "quickfix",
416414
"title": "Create module"
417415
}]),

0 commit comments

Comments
 (0)