Skip to content

Commit 008d513

Browse files
Prepare for omittiong parts of completion data that need to be resolved
1 parent 85e1214 commit 008d513

File tree

10 files changed

+99
-16
lines changed

10 files changed

+99
-16
lines changed

src/tools/rust-analyzer/crates/ide-completion/src/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use hir::ImportPathConfig;
88
use ide_db::{imports::insert_use::InsertUseConfig, SnippetCap};
99

10-
use crate::snippet::Snippet;
10+
use crate::{snippet::Snippet, CompletionFieldsToResolve};
1111

1212
#[derive(Clone, Debug, PartialEq, Eq)]
1313
pub struct CompletionConfig {
@@ -27,6 +27,7 @@ pub struct CompletionConfig {
2727
pub prefer_absolute: bool,
2828
pub snippets: Vec<Snippet>,
2929
pub limit: Option<usize>,
30+
pub fields_to_resolve: CompletionFieldsToResolve,
3031
}
3132

3233
#[derive(Clone, Debug, PartialEq, Eq)]

src/tools/rust-analyzer/crates/ide-completion/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,35 @@ pub use crate::{
3737
snippet::{Snippet, SnippetScope},
3838
};
3939

40+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
41+
pub struct CompletionFieldsToResolve {
42+
pub resolve_label_details: bool,
43+
pub resolve_tags: bool,
44+
pub resolve_detail: bool,
45+
pub resolve_documentation: bool,
46+
pub resolve_sort_text: bool,
47+
pub resolve_filter_text: bool,
48+
pub resolve_text_edit: bool,
49+
// FIXME: those are always resolved
50+
// pub resolve_additional_text_edits: bool,
51+
pub resolve_command: bool,
52+
}
53+
54+
impl CompletionFieldsToResolve {
55+
pub const fn empty() -> Self {
56+
Self {
57+
resolve_label_details: false,
58+
resolve_tags: false,
59+
resolve_detail: false,
60+
resolve_documentation: false,
61+
resolve_sort_text: false,
62+
resolve_filter_text: false,
63+
resolve_text_edit: false,
64+
resolve_command: false,
65+
}
66+
}
67+
}
68+
4069
//FIXME: split the following feature into fine-grained features.
4170

4271
// Feature: Magic Completions

src/tools/rust-analyzer/crates/ide-completion/src/tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ use test_fixture::ChangeFixture;
3737
use test_utils::assert_eq_text;
3838

3939
use crate::{
40-
resolve_completion_edits, CallableSnippets, CompletionConfig, CompletionItem,
41-
CompletionItemKind,
40+
resolve_completion_edits, CallableSnippets, CompletionConfig, CompletionFieldsToResolve,
41+
CompletionItem, CompletionItemKind,
4242
};
4343

4444
/// Lots of basic item definitions
@@ -84,6 +84,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
8484
prefer_absolute: false,
8585
snippets: Vec::new(),
8686
limit: None,
87+
fields_to_resolve: CompletionFieldsToResolve::empty(),
8788
};
8889

8990
pub(crate) fn completion_list(ra_fixture: &str) -> String {

src/tools/rust-analyzer/crates/ide/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ pub use ide_assists::{
119119
Assist, AssistConfig, AssistId, AssistKind, AssistResolveStrategy, SingleResolve,
120120
};
121121
pub use ide_completion::{
122-
CallableSnippets, CompletionConfig, CompletionItem, CompletionItemKind, CompletionRelevance,
123-
Snippet, SnippetScope,
122+
CallableSnippets, CompletionConfig, CompletionFieldsToResolve, CompletionItem,
123+
CompletionItemKind, CompletionRelevance, Snippet, SnippetScope,
124124
};
125125
pub use ide_db::{
126126
base_db::{Cancelled, CrateGraph, CrateId, FileChange, SourceRoot, SourceRootId},

src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use std::{
1212
use cfg::{CfgAtom, CfgDiff};
1313
use hir::Symbol;
1414
use ide::{
15-
AssistConfig, CallableSnippets, CompletionConfig, DiagnosticsConfig, ExprFillDefaultMode,
16-
GenericParameterHints, HighlightConfig, HighlightRelatedConfig, HoverConfig, HoverDocFormat,
17-
InlayFieldsToResolve, InlayHintsConfig, JoinLinesConfig, MemoryLayoutHoverConfig,
18-
MemoryLayoutHoverRenderKind, Snippet, SnippetScope, SourceRootId,
15+
AssistConfig, CallableSnippets, CompletionConfig, CompletionFieldsToResolve, DiagnosticsConfig,
16+
ExprFillDefaultMode, GenericParameterHints, HighlightConfig, HighlightRelatedConfig,
17+
HoverConfig, HoverDocFormat, InlayFieldsToResolve, InlayHintsConfig, JoinLinesConfig,
18+
MemoryLayoutHoverConfig, MemoryLayoutHoverRenderKind, Snippet, SnippetScope, SourceRootId,
1919
};
2020
use ide_db::{
2121
imports::insert_use::{ImportGranularity, InsertUseConfig, PrefixKind},
@@ -1391,6 +1391,7 @@ impl Config {
13911391
}
13921392

13931393
pub fn completion(&self, source_root: Option<SourceRootId>) -> CompletionConfig {
1394+
let client_capability_fields = self.completion_resolve_support_properties();
13941395
CompletionConfig {
13951396
enable_postfix_completions: self.completion_postfix_enable(source_root).to_owned(),
13961397
enable_imports_on_the_fly: self.completion_autoimport_enable(source_root).to_owned()
@@ -1415,6 +1416,16 @@ impl Config {
14151416
limit: self.completion_limit(source_root).to_owned(),
14161417
enable_term_search: self.completion_termSearch_enable(source_root).to_owned(),
14171418
term_search_fuel: self.completion_termSearch_fuel(source_root).to_owned() as u64,
1419+
fields_to_resolve: CompletionFieldsToResolve {
1420+
resolve_label_details: client_capability_fields.contains("labelDetails"),
1421+
resolve_tags: client_capability_fields.contains("tags"),
1422+
resolve_detail: client_capability_fields.contains("detail"),
1423+
resolve_documentation: client_capability_fields.contains("documentation"),
1424+
resolve_sort_text: client_capability_fields.contains("sortText"),
1425+
resolve_filter_text: client_capability_fields.contains("filterText"),
1426+
resolve_text_edit: client_capability_fields.contains("textEdit"),
1427+
resolve_command: client_capability_fields.contains("command"),
1428+
},
14181429
}
14191430
}
14201431

src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,9 +1019,11 @@ pub(crate) fn handle_completion(
10191019

10201020
let items = to_proto::completion_items(
10211021
&snap.config,
1022+
&completion_config.fields_to_resolve,
10221023
&line_index,
10231024
snap.file_version(position.file_id),
10241025
text_document_position,
1026+
completion_trigger_character,
10251027
items,
10261028
);
10271029

src/tools/rust-analyzer/crates/rust-analyzer/src/integrated_benchmarks.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
1313
use hir::ChangeWithProcMacros;
1414
use ide::{
15-
AnalysisHost, CallableSnippets, CompletionConfig, DiagnosticsConfig, FilePosition, TextSize,
15+
AnalysisHost, CallableSnippets, CompletionConfig, CompletionFieldsToResolve, DiagnosticsConfig,
16+
FilePosition, TextSize,
1617
};
1718
use ide_db::{
1819
imports::insert_use::{ImportGranularity, InsertUseConfig},
@@ -168,6 +169,7 @@ fn integrated_completion_benchmark() {
168169
snippets: Vec::new(),
169170
limit: None,
170171
add_semicolon_to_unit: true,
172+
fields_to_resolve: CompletionFieldsToResolve::empty(),
171173
};
172174
let position =
173175
FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
@@ -215,6 +217,7 @@ fn integrated_completion_benchmark() {
215217
snippets: Vec::new(),
216218
limit: None,
217219
add_semicolon_to_unit: true,
220+
fields_to_resolve: CompletionFieldsToResolve::empty(),
218221
};
219222
let position =
220223
FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
@@ -260,6 +263,7 @@ fn integrated_completion_benchmark() {
260263
snippets: Vec::new(),
261264
limit: None,
262265
add_semicolon_to_unit: true,
266+
fields_to_resolve: CompletionFieldsToResolve::empty(),
263267
};
264268
let position =
265269
FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };

src/tools/rust-analyzer/crates/rust-analyzer/src/lsp/capabilities.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,21 @@ impl ClientCapabilities {
458458
.into_iter()
459459
.flatten()
460460
.cloned()
461-
.collect::<FxHashSet<_>>()
461+
.collect()
462+
}
463+
464+
pub fn completion_resolve_support_properties(&self) -> FxHashSet<String> {
465+
self.0
466+
.text_document
467+
.as_ref()
468+
.and_then(|text| text.completion.as_ref())
469+
.and_then(|completion_caps| completion_caps.completion_item.as_ref())
470+
.and_then(|completion_item_caps| completion_item_caps.resolve_support.as_ref())
471+
.map(|resolve_support| resolve_support.properties.iter())
472+
.into_iter()
473+
.flatten()
474+
.cloned()
475+
.collect()
462476
}
463477

464478
pub fn hover_markdown_support(&self) -> bool {

src/tools/rust-analyzer/crates/rust-analyzer/src/lsp/ext.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ pub struct CompletionResolveData {
825825
pub position: lsp_types::TextDocumentPositionParams,
826826
pub imports: Vec<CompletionImport>,
827827
pub version: Option<i32>,
828+
pub completion_trigger_character: Option<char>,
828829
}
829830

830831
#[derive(Debug, Serialize, Deserialize)]

src/tools/rust-analyzer/crates/rust-analyzer/src/lsp/to_proto.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use std::{
66
};
77

88
use ide::{
9-
Annotation, AnnotationKind, Assist, AssistKind, Cancellable, CompletionItem,
10-
CompletionItemKind, CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit,
11-
Fold, FoldKind, Highlight, HlMod, HlOperator, HlPunct, HlRange, HlTag, Indel,
9+
Annotation, AnnotationKind, Assist, AssistKind, Cancellable, CompletionFieldsToResolve,
10+
CompletionItem, CompletionItemKind, CompletionRelevance, Documentation, FileId, FileRange,
11+
FileSystemEdit, Fold, FoldKind, Highlight, HlMod, HlOperator, HlPunct, HlRange, HlTag, Indel,
1212
InlayFieldsToResolve, InlayHint, InlayHintLabel, InlayHintLabelPart, InlayKind, Markup,
1313
NavigationTarget, ReferenceCategory, RenameError, Runnable, Severity, SignatureHelp,
1414
SnippetEdit, SourceChange, StructureNodeKind, SymbolKind, TextEdit, TextRange, TextSize,
@@ -227,9 +227,11 @@ pub(crate) fn snippet_text_edit_vec(
227227

228228
pub(crate) fn completion_items(
229229
config: &Config,
230+
fields_to_resolve: &CompletionFieldsToResolve,
230231
line_index: &LineIndex,
231232
version: Option<i32>,
232233
tdpp: lsp_types::TextDocumentPositionParams,
234+
completion_trigger_character: Option<char>,
233235
mut items: Vec<CompletionItem>,
234236
) -> Vec<lsp_types::CompletionItem> {
235237
if config.completion_hide_deprecated() {
@@ -239,7 +241,17 @@ pub(crate) fn completion_items(
239241
let max_relevance = items.iter().map(|it| it.relevance.score()).max().unwrap_or_default();
240242
let mut res = Vec::with_capacity(items.len());
241243
for item in items {
242-
completion_item(&mut res, config, line_index, version, &tdpp, max_relevance, item);
244+
completion_item(
245+
&mut res,
246+
config,
247+
fields_to_resolve,
248+
line_index,
249+
version,
250+
&tdpp,
251+
max_relevance,
252+
completion_trigger_character,
253+
item,
254+
);
243255
}
244256

245257
if let Some(limit) = config.completion(None).limit {
@@ -253,17 +265,20 @@ pub(crate) fn completion_items(
253265
fn completion_item(
254266
acc: &mut Vec<lsp_types::CompletionItem>,
255267
config: &Config,
268+
fields_to_resolve: &CompletionFieldsToResolve,
256269
line_index: &LineIndex,
257270
version: Option<i32>,
258271
tdpp: &lsp_types::TextDocumentPositionParams,
259272
max_relevance: u32,
273+
completion_trigger_character: Option<char>,
260274
item: CompletionItem,
261275
) {
262276
let insert_replace_support = config.insert_replace_support().then_some(tdpp.position);
263277
let ref_match = item.ref_match();
264278
let lookup = item.lookup().to_owned();
265279

266280
let mut additional_text_edits = Vec::new();
281+
let mut something_to_resolve = false;
267282

268283
// LSP does not allow arbitrary edits in completion, so we have to do a
269284
// non-trivial mapping here.
@@ -337,7 +352,12 @@ fn completion_item(
337352
})
338353
.collect::<Vec<_>>();
339354
if !imports.is_empty() {
340-
let data = lsp_ext::CompletionResolveData { position: tdpp.clone(), imports, version };
355+
let data = lsp_ext::CompletionResolveData {
356+
position: tdpp.clone(),
357+
imports,
358+
version,
359+
completion_trigger_character,
360+
};
341361
lsp_item.data = Some(to_value(data).unwrap());
342362
}
343363
}

0 commit comments

Comments
 (0)