Skip to content

Commit 0ffdabd

Browse files
Provide an option to hide deprecated items from completion
1 parent 0ae42bd commit 0ffdabd

File tree

9 files changed

+58
-5
lines changed

9 files changed

+58
-5
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ use crate::{
5050
};
5151

5252
/// Represents an in-progress set of completions being built.
53-
#[derive(Debug, Default)]
53+
#[derive(Debug)]
5454
pub struct Completions {
5555
buf: Vec<CompletionItem>,
56+
hide_deprecated: bool,
5657
}
5758

5859
impl From<Completions> for Vec<CompletionItem> {
@@ -70,13 +71,19 @@ impl Builder {
7071
}
7172

7273
impl Completions {
74+
pub(crate) fn new(hide_deprecated: bool) -> Completions {
75+
Completions { buf: Vec::new(), hide_deprecated }
76+
}
77+
7378
fn add(&mut self, item: CompletionItem) {
74-
self.buf.push(item)
79+
if !item.deprecated || !self.hide_deprecated {
80+
self.buf.push(item);
81+
}
7582
}
7683

7784
fn add_opt(&mut self, item: Option<CompletionItem>) {
7885
if let Some(item) = item {
79-
self.buf.push(item)
86+
self.add(item)
8087
}
8188
}
8289

crates/ide-completion/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct CompletionConfig {
2626
pub prefer_absolute: bool,
2727
pub snippets: Vec<Snippet>,
2828
pub limit: Option<usize>,
29+
pub hide_deprecated: bool,
2930
}
3031

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

crates/ide-completion/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub fn completions(
150150
trigger_character: Option<char>,
151151
) -> Option<Vec<CompletionItem>> {
152152
let (ctx, analysis) = &CompletionContext::new(db, position, config)?;
153-
let mut completions = Completions::default();
153+
let mut completions = Completions::new(config.hide_deprecated);
154154

155155
// prevent `(` from triggering unwanted completion noise
156156
if trigger_character == Some('(') {

crates/ide-completion/src/tests.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
8383
prefer_absolute: false,
8484
snippets: Vec::new(),
8585
limit: None,
86+
hide_deprecated: false,
8687
};
8788

8889
pub(crate) fn completion_list(ra_fixture: &str) -> String {
@@ -326,3 +327,26 @@ Some multi-line comment$0
326327
String::new(),
327328
);
328329
}
330+
331+
#[test]
332+
fn hide_deprecated_if_requested() {
333+
assert_eq!(
334+
completion_list_with_config(
335+
CompletionConfig { hide_deprecated: true, ..TEST_CONFIG },
336+
r#"
337+
#[deprecated]
338+
fn foo() {}
339+
340+
fn test() {
341+
foo$0
342+
}
343+
"#,
344+
false,
345+
None
346+
),
347+
"\
348+
fn test() fn()
349+
bt u32 u32
350+
",
351+
);
352+
}

crates/ide-completion/src/tests/flyimport.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn check(ra_fixture: &str, expect: Expect) {
1010
let (db, position) = crate::tests::position(ra_fixture);
1111
let (ctx, analysis) = crate::context::CompletionContext::new(&db, position, &config).unwrap();
1212

13-
let mut acc = crate::completions::Completions::default();
13+
let mut acc = crate::completions::Completions::new(false);
1414
if let CompletionAnalysis::Name(NameContext { kind: NameKind::IdentPat(pat_ctx), .. }) =
1515
&analysis
1616
{

crates/rust-analyzer/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ config_data! {
512512
completion_termSearch_enable: bool = false,
513513
/// Term search fuel in "units of work" for autocompletion (Defaults to 1000).
514514
completion_termSearch_fuel: usize = 1000,
515+
/// Whether to omit deprecated items from autocompletion. By default they are marked as deprecated but not hidden.
516+
completions_hideDeprecated: bool = false,
515517

516518
/// Controls file watching implementation.
517519
files_watcher: FilesWatcherDef = FilesWatcherDef::Client,
@@ -1432,6 +1434,7 @@ impl Config {
14321434
limit: self.completion_limit().to_owned(),
14331435
enable_term_search: self.completion_termSearch_enable().to_owned(),
14341436
term_search_fuel: self.completion_termSearch_fuel().to_owned() as u64,
1437+
hide_deprecated: *self.completions_hideDeprecated(),
14351438
}
14361439
}
14371440

crates/rust-analyzer/src/integrated_benchmarks.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ fn integrated_completion_benchmark() {
167167
prefer_absolute: false,
168168
snippets: Vec::new(),
169169
limit: None,
170+
hide_deprecated: false,
170171
};
171172
let position =
172173
FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
@@ -213,6 +214,7 @@ fn integrated_completion_benchmark() {
213214
prefer_absolute: false,
214215
snippets: Vec::new(),
215216
limit: None,
217+
hide_deprecated: false,
216218
};
217219
let position =
218220
FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
@@ -257,6 +259,7 @@ fn integrated_completion_benchmark() {
257259
prefer_absolute: false,
258260
snippets: Vec::new(),
259261
limit: None,
262+
hide_deprecated: false,
260263
};
261264
let position =
262265
FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };

docs/user/generated_config.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ Whether to enable term search based snippets like `Some(foo.bar().baz())`.
358358
--
359359
Term search fuel in "units of work" for autocompletion (Defaults to 1000).
360360
--
361+
[[rust-analyzer.completions.hideDeprecated]]rust-analyzer.completions.hideDeprecated (default: `false`)::
362+
+
363+
--
364+
Whether to omit deprecated items from autocompletion. By default they are marked as deprecated but not hidden.
365+
--
361366
[[rust-analyzer.diagnostics.disabled]]rust-analyzer.diagnostics.disabled (default: `[]`)::
362367
+
363368
--

editors/code/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,16 @@
11821182
}
11831183
}
11841184
},
1185+
{
1186+
"title": "completions",
1187+
"properties": {
1188+
"rust-analyzer.completions.hideDeprecated": {
1189+
"markdownDescription": "Whether to omit deprecated items from autocompletion. By default they are marked as deprecated but not hidden.",
1190+
"default": false,
1191+
"type": "boolean"
1192+
}
1193+
}
1194+
},
11851195
{
11861196
"title": "diagnostics",
11871197
"properties": {

0 commit comments

Comments
 (0)