From 1914b7723f2f77a7259c65c888107e95f00d0ba1 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 3 Mar 2021 21:58:48 +0100 Subject: Don't complete `super` unless its valid in paths --- crates/ide_completion/src/completions/attribute.rs | 3 ++- crates/ide_completion/src/completions/fn_param.rs | 27 ++++++++++------------ crates/ide_completion/src/completions/keyword.rs | 27 ++++++++++++++++------ 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) } fn complete_attribute_start(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) { - for attr_completion in ATTRIBUTES { + let is_inner = attribute.kind() == ast::AttrKind::Inner; + for attr_completion in ATTRIBUTES.iter().filter(|compl| is_inner || !compl.prefer_inner) { let mut item = CompletionItem::new( CompletionKind::Attribute, 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) return; } func.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| { - let text = param.syntax().text().to_string(); - params.entry(text).or_insert(param); - }) + if let Some(pat) = param.pat() { + let text = param.syntax().text().to_string(); + let lookup = pat.syntax().text().to_string(); + params.entry(text).or_insert(lookup); + } + }); }; for node in ctx.token.parent().ancestors() { @@ -50,18 +53,12 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) }; } - params - .into_iter() - .filter_map(|(label, param)| { - let lookup = param.pat()?.syntax().text().to_string(); - Some((label, lookup)) - }) - .for_each(|(label, lookup)| { - CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label) - .kind(CompletionItemKind::Binding) - .lookup_by(lookup) - .add_to(acc) - }); + params.into_iter().for_each(|(label, lookup)| { + CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label) + .kind(CompletionItemKind::Binding) + .lookup_by(lookup) + .add_to(acc) + }); } #[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 @@ //! Completes keywords. +use std::iter; + use syntax::SyntaxKind; use test_utils::mark; @@ -19,10 +21,14 @@ pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionC CompletionItem::new(CompletionKind::Keyword, source_range, "self") .kind(CompletionItemKind::Keyword) .add_to(acc); - CompletionItem::new(CompletionKind::Keyword, source_range, "super::") - .kind(CompletionItemKind::Keyword) - .insert_text("super::") - .add_to(acc); + if iter::successors(ctx.path_qual.clone(), |p| p.qualifier()) + .all(|p| p.segment().and_then(|s| s.super_token()).is_some()) + { + CompletionItem::new(CompletionKind::Keyword, source_range, "super::") + .kind(CompletionItemKind::Keyword) + .insert_text("super::") + .add_to(acc); + } } // Suggest .await syntax for types that implement Future trait @@ -204,8 +210,16 @@ mod tests { "#]], ); + // FIXME: `self` shouldn't be shown here and the check below check( r"use a::$0", + expect![[r#" + kw self + "#]], + ); + + check( + r"use super::$0", expect![[r#" kw self kw super:: @@ -215,9 +229,8 @@ mod tests { check( r"use a::{b, $0}", expect![[r#" - kw self - kw super:: - "#]], + kw self + "#]], ); } -- cgit v1.2.3 From 02e9440e2332beab2647bf61491c788ab84aa4df Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 3 Mar 2021 21:59:41 +0100 Subject: Complete `while let` --- crates/ide_completion/src/completions/keyword.rs | 8 ++++++++ crates/ide_completion/src/completions/qualified_path.rs | 12 +++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs index 17bd19522..03c6dd454 100644 --- a/crates/ide_completion/src/completions/keyword.rs +++ b/crates/ide_completion/src/completions/keyword.rs @@ -91,6 +91,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte if ctx.is_expr { add_keyword(ctx, acc, "match", "match $0 {}"); add_keyword(ctx, acc, "while", "while $0 {}"); + add_keyword(ctx, acc, "while let", "while let $1 = $0 {}"); add_keyword(ctx, acc, "loop", "loop {$0}"); add_keyword(ctx, acc, "if", "if $0 {}"); add_keyword(ctx, acc, "if let", "if let $1 = $0 {}"); @@ -269,6 +270,7 @@ mod tests { kw trait kw match kw while + kw while let kw loop kw if kw if let @@ -296,6 +298,7 @@ mod tests { kw trait kw match kw while + kw while let kw loop kw if kw if let @@ -323,6 +326,7 @@ mod tests { kw trait kw match kw while + kw while let kw loop kw if kw if let @@ -357,6 +361,7 @@ fn quux() -> i32 { expect![[r#" kw match kw while + kw while let kw loop kw if kw if let @@ -406,6 +411,7 @@ fn quux() -> i32 { kw trait kw match kw while + kw while let kw loop kw if kw if let @@ -565,6 +571,7 @@ pub mod future { expect![[r#" kw match kw while + kw while let kw loop kw if kw if let @@ -624,6 +631,7 @@ fn foo() { expect![[r#" kw match kw while + kw while let kw loop kw if kw if let diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs index 2afa6979e..72fb757b1 100644 --- a/crates/ide_completion/src/completions/qualified_path.rs +++ b/crates/ide_completion/src/completions/qualified_path.rs @@ -81,9 +81,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon return None; } match item { - hir::AssocItem::Function(func) => { - acc.add_function(ctx, func, None); - } + hir::AssocItem::Function(func) => acc.add_function(ctx, func, None), hir::AssocItem::Const(ct) => acc.add_const(ctx, ct), hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), } @@ -110,9 +108,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon continue; } match item { - hir::AssocItem::Function(func) => { - acc.add_function(ctx, func, None); - } + hir::AssocItem::Function(func) => acc.add_function(ctx, func, None), hir::AssocItem::Const(ct) => acc.add_const(ctx, ct), hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), } @@ -143,9 +139,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon // them. if seen.insert(item) { match item { - hir::AssocItem::Function(func) => { - acc.add_function(ctx, func, None); - } + hir::AssocItem::Function(func) => acc.add_function(ctx, func, None), hir::AssocItem::Const(ct) => acc.add_const(ctx, ct), hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), } -- cgit v1.2.3