aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-03-03 20:58:48 +0000
committerLukas Wirth <[email protected]>2021-03-03 20:58:48 +0000
commit1914b7723f2f77a7259c65c888107e95f00d0ba1 (patch)
tree203572db4c48694c2bdd00987edb9a4b412a0384 /crates/ide_completion
parentd0fa7abc5044471b951149aa35244620db847ff9 (diff)
Don't complete `super` unless its valid in paths
Diffstat (limited to 'crates/ide_completion')
-rw-r--r--crates/ide_completion/src/completions/attribute.rs3
-rw-r--r--crates/ide_completion/src/completions/fn_param.rs27
-rw-r--r--crates/ide_completion/src/completions/keyword.rs27
3 files changed, 34 insertions, 23 deletions
diff --git a/crates/ide_completion/src/completions/attribute.rs b/crates/ide_completion/src/completions/attribute.rs
index 3a5bc4381..cb05e85fc 100644
--- a/crates/ide_completion/src/completions/attribute.rs
+++ b/crates/ide_completion/src/completions/attribute.rs
@@ -39,7 +39,8 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
39} 39}
40 40
41fn complete_attribute_start(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) { 41fn complete_attribute_start(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) {
42 for attr_completion in ATTRIBUTES { 42 let is_inner = attribute.kind() == ast::AttrKind::Inner;
43 for attr_completion in ATTRIBUTES.iter().filter(|compl| is_inner || !compl.prefer_inner) {
43 let mut item = CompletionItem::new( 44 let mut item = CompletionItem::new(
44 CompletionKind::Attribute, 45 CompletionKind::Attribute,
45 ctx.source_range(), 46 ctx.source_range(),
diff --git a/crates/ide_completion/src/completions/fn_param.rs b/crates/ide_completion/src/completions/fn_param.rs
index 38e33a93e..1bcc8727f 100644
--- a/crates/ide_completion/src/completions/fn_param.rs
+++ b/crates/ide_completion/src/completions/fn_param.rs
@@ -25,9 +25,12 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
25 return; 25 return;
26 } 26 }
27 func.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| { 27 func.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| {
28 let text = param.syntax().text().to_string(); 28 if let Some(pat) = param.pat() {
29 params.entry(text).or_insert(param); 29 let text = param.syntax().text().to_string();
30 }) 30 let lookup = pat.syntax().text().to_string();
31 params.entry(text).or_insert(lookup);
32 }
33 });
31 }; 34 };
32 35
33 for node in ctx.token.parent().ancestors() { 36 for node in ctx.token.parent().ancestors() {
@@ -50,18 +53,12 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
50 }; 53 };
51 } 54 }
52 55
53 params 56 params.into_iter().for_each(|(label, lookup)| {
54 .into_iter() 57 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
55 .filter_map(|(label, param)| { 58 .kind(CompletionItemKind::Binding)
56 let lookup = param.pat()?.syntax().text().to_string(); 59 .lookup_by(lookup)
57 Some((label, lookup)) 60 .add_to(acc)
58 }) 61 });
59 .for_each(|(label, lookup)| {
60 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
61 .kind(CompletionItemKind::Binding)
62 .lookup_by(lookup)
63 .add_to(acc)
64 });
65} 62}
66 63
67#[cfg(test)] 64#[cfg(test)]
diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs
index eb81f9765..17bd19522 100644
--- a/crates/ide_completion/src/completions/keyword.rs
+++ b/crates/ide_completion/src/completions/keyword.rs
@@ -1,5 +1,7 @@
1//! Completes keywords. 1//! Completes keywords.
2 2
3use std::iter;
4
3use syntax::SyntaxKind; 5use syntax::SyntaxKind;
4use test_utils::mark; 6use test_utils::mark;
5 7
@@ -19,10 +21,14 @@ pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionC
19 CompletionItem::new(CompletionKind::Keyword, source_range, "self") 21 CompletionItem::new(CompletionKind::Keyword, source_range, "self")
20 .kind(CompletionItemKind::Keyword) 22 .kind(CompletionItemKind::Keyword)
21 .add_to(acc); 23 .add_to(acc);
22 CompletionItem::new(CompletionKind::Keyword, source_range, "super::") 24 if iter::successors(ctx.path_qual.clone(), |p| p.qualifier())
23 .kind(CompletionItemKind::Keyword) 25 .all(|p| p.segment().and_then(|s| s.super_token()).is_some())
24 .insert_text("super::") 26 {
25 .add_to(acc); 27 CompletionItem::new(CompletionKind::Keyword, source_range, "super::")
28 .kind(CompletionItemKind::Keyword)
29 .insert_text("super::")
30 .add_to(acc);
31 }
26 } 32 }
27 33
28 // Suggest .await syntax for types that implement Future trait 34 // Suggest .await syntax for types that implement Future trait
@@ -204,9 +210,17 @@ mod tests {
204 "#]], 210 "#]],
205 ); 211 );
206 212
213 // FIXME: `self` shouldn't be shown here and the check below
207 check( 214 check(
208 r"use a::$0", 215 r"use a::$0",
209 expect![[r#" 216 expect![[r#"
217 kw self
218 "#]],
219 );
220
221 check(
222 r"use super::$0",
223 expect![[r#"
210 kw self 224 kw self
211 kw super:: 225 kw super::
212 "#]], 226 "#]],
@@ -215,9 +229,8 @@ mod tests {
215 check( 229 check(
216 r"use a::{b, $0}", 230 r"use a::{b, $0}",
217 expect![[r#" 231 expect![[r#"
218 kw self 232 kw self
219 kw super:: 233 "#]],
220 "#]],
221 ); 234 );
222 } 235 }
223 236