Skip to content

Commit a05ce5a

Browse files
committed
Auto merge of rust-lang#13986 - MariaSolOs:limit-completions, r=Veykril
Add setting for limiting number of completions For rust-lang#13911.
2 parents 3c89945 + 064fcfa commit a05ce5a

File tree

7 files changed

+28
-1
lines changed

7 files changed

+28
-1
lines changed

crates/ide-completion/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct CompletionConfig {
1919
pub insert_use: InsertUseConfig,
2020
pub prefer_no_std: bool,
2121
pub snippets: Vec<Snippet>,
22+
pub limit: Option<usize>,
2223
}
2324

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

crates/ide-completion/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
7575
skip_glob_imports: true,
7676
},
7777
snippets: Vec::new(),
78+
limit: None,
7879
};
7980

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

crates/rust-analyzer/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ config_data! {
200200
completion_autoself_enable: bool = "true",
201201
/// Whether to add parenthesis and argument snippets when completing function.
202202
completion_callable_snippets: CallableCompletionDef = "\"fill_arguments\"",
203+
/// Maximum number of completions to return. If `None`, the limit is infinite.
204+
completion_limit: Option<usize> = "null",
203205
/// Whether to show postfix snippets like `dbg`, `if`, `not`, etc.
204206
completion_postfix_enable: bool = "true",
205207
/// Enables completions of private items and fields that are defined in the current workspace even if they are not visible at the current position.
@@ -1343,6 +1345,7 @@ impl Config {
13431345
.snippet_support?
13441346
)),
13451347
snippets: self.snippets.clone(),
1348+
limit: self.data.completion_limit,
13461349
}
13471350
}
13481351

crates/rust-analyzer/src/integrated_benchmarks.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ fn integrated_completion_benchmark() {
146146
},
147147
snippets: Vec::new(),
148148
prefer_no_std: false,
149+
limit: None,
149150
};
150151
let position =
151152
FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
@@ -184,6 +185,7 @@ fn integrated_completion_benchmark() {
184185
},
185186
snippets: Vec::new(),
186187
prefer_no_std: false,
188+
limit: None,
187189
};
188190
let position =
189191
FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };

crates/rust-analyzer/src/to_proto.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,14 @@ pub(crate) fn completion_items(
215215
let max_relevance = items.iter().map(|it| it.relevance().score()).max().unwrap_or_default();
216216
let mut res = Vec::with_capacity(items.len());
217217
for item in items {
218-
completion_item(&mut res, config, line_index, &tdpp, max_relevance, item)
218+
completion_item(&mut res, config, line_index, &tdpp, max_relevance, item);
219219
}
220+
221+
if let Some(limit) = config.completion().limit {
222+
res.sort_by(|item1, item2| item1.sort_text.cmp(&item2.sort_text));
223+
res.truncate(limit);
224+
}
225+
220226
res
221227
}
222228

docs/user/generated_config.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ with `self` prefixed to them when inside a method.
227227
--
228228
Whether to add parenthesis and argument snippets when completing function.
229229
--
230+
[[rust-analyzer.completion.limit]]rust-analyzer.completion.limit (default: `null`)::
231+
+
232+
--
233+
Maximum number of completions to return. If `None`, the limit is infinite.
234+
--
230235
[[rust-analyzer.completion.postfix.enable]]rust-analyzer.completion.postfix.enable (default: `true`)::
231236
+
232237
--

editors/code/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,15 @@
705705
"Do no snippet completions for callables."
706706
]
707707
},
708+
"rust-analyzer.completion.limit": {
709+
"markdownDescription": "Maximum number of completions to return. If `None`, the limit is infinite.",
710+
"default": null,
711+
"type": [
712+
"null",
713+
"integer"
714+
],
715+
"minimum": 0
716+
},
708717
"rust-analyzer.completion.postfix.enable": {
709718
"markdownDescription": "Whether to show postfix snippets like `dbg`, `if`, `not`, etc.",
710719
"default": true,

0 commit comments

Comments
 (0)