From 0729913525a55cad3ffe9876c1eb05f7b880d22d Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 21 Jun 2021 15:14:28 +0200 Subject: Various keyword completion fixes --- crates/ide_completion/src/completions/keyword.rs | 6 +++++- crates/ide_completion/src/context.rs | 7 +++++-- crates/ide_completion/src/patterns.rs | 9 ++++++++- crates/ide_completion/src/tests/type_pos.rs | 16 ++++------------ 4 files changed, 22 insertions(+), 16 deletions(-) (limited to 'crates') diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs index e40ec6280..407f796ef 100644 --- a/crates/ide_completion/src/completions/keyword.rs +++ b/crates/ide_completion/src/completions/keyword.rs @@ -92,7 +92,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte } if !ctx.has_visibility_prev_sibling() - && (expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_record_field()) + && (expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_field()) { add_keyword("pub(crate)", "pub(crate) "); add_keyword("pub", "pub "); @@ -122,6 +122,10 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte add_keyword("union", "union $1 {\n $0\n}"); } + if ctx.expects_type() { + return; + } + if ctx.expects_expression() { if !has_block_expr_parent { add_keyword("unsafe", "unsafe {\n $0\n}"); diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index e49e434fa..4814bd64d 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs @@ -286,8 +286,11 @@ impl<'a> CompletionContext<'a> { ) } - pub(crate) fn expect_record_field(&self) -> bool { - matches!(self.completion_location, Some(ImmediateLocation::RecordField)) + pub(crate) fn expect_field(&self) -> bool { + matches!( + self.completion_location, + Some(ImmediateLocation::RecordField | ImmediateLocation::TupleField) + ) } pub(crate) fn in_use_tree(&self) -> bool { diff --git a/crates/ide_completion/src/patterns.rs b/crates/ide_completion/src/patterns.rs index 271409c38..757c9a3da 100644 --- a/crates/ide_completion/src/patterns.rs +++ b/crates/ide_completion/src/patterns.rs @@ -31,6 +31,7 @@ pub(crate) enum ImmediateLocation { Impl, Trait, RecordField, + TupleField, RefExpr, IdentPat, BlockExpr, @@ -187,7 +188,13 @@ pub(crate) fn determine_location( ast::SourceFile(_it) => ImmediateLocation::ItemList, ast::ItemList(_it) => ImmediateLocation::ItemList, ast::RefExpr(_it) => ImmediateLocation::RefExpr, - ast::RecordField(_it) => ImmediateLocation::RecordField, + ast::RecordField(it) => if it.ty().map_or(false, |it| it.syntax().text_range().contains(offset)) { + return None; + } else { + ImmediateLocation::RecordField + }, + ast::TupleField(_it) => ImmediateLocation::TupleField, + ast::TupleFieldList(_it) => ImmediateLocation::TupleField, ast::AssocItemList(it) => match it.syntax().parent().map(|it| it.kind()) { Some(IMPL) => ImmediateLocation::Impl, Some(TRAIT) => ImmediateLocation::Trait, diff --git a/crates/ide_completion/src/tests/type_pos.rs b/crates/ide_completion/src/tests/type_pos.rs index 2bfecdd08..1ab47b27e 100644 --- a/crates/ide_completion/src/tests/type_pos.rs +++ b/crates/ide_completion/src/tests/type_pos.rs @@ -23,7 +23,6 @@ macro_rules! makro {} #[test] fn record_field_ty() { - // FIXME: pub shouldnt show up here check_with( r#" struct Foo<'lt, T, const C: usize> { @@ -31,8 +30,6 @@ struct Foo<'lt, T, const C: usize> { } "#, expect![[r#" - kw pub(crate) - kw pub sp Self tp T tt Trait @@ -42,7 +39,7 @@ struct Foo<'lt, T, const C: usize> { md module st Foo<…> st Unit - ma makro!(…) macro_rules! makro + ma makro!(…) macro_rules! makro bt u32 "#]], ) @@ -50,12 +47,13 @@ struct Foo<'lt, T, const C: usize> { #[test] fn tuple_struct_field() { - // FIXME: pub should show up here check_with( r#" struct Foo<'lt, T, const C: usize>(f$0); "#, expect![[r#" + kw pub(crate) + kw pub sp Self tp T tt Trait @@ -65,7 +63,7 @@ struct Foo<'lt, T, const C: usize>(f$0); md module st Foo<…> st Unit - ma makro!(…) macro_rules! makro + ma makro!(…) macro_rules! makro bt u32 "#]], ) @@ -73,13 +71,11 @@ struct Foo<'lt, T, const C: usize>(f$0); #[test] fn fn_return_type() { - // FIXME: return shouldnt show up here check_with( r#" fn x<'lt, T, const C: usize>() -> $0 "#, expect![[r#" - kw return tp T tt Trait en Enum @@ -95,7 +91,6 @@ fn x<'lt, T, const C: usize>() -> $0 #[test] fn body_type_pos() { - // FIXME: return shouldnt show up here check_with( r#" fn foo<'lt, T, const C: usize>() { @@ -104,7 +99,6 @@ fn foo<'lt, T, const C: usize>() { } "#, expect![[r#" - kw return tp T tt Trait en Enum @@ -136,7 +130,6 @@ fn foo<'lt, T, const C: usize>() { #[test] fn completes_types_and_const_in_arg_list() { - // FIXME: return shouldnt show up here // FIXME: we should complete the lifetime here for now check_with( r#" @@ -147,7 +140,6 @@ trait Trait2 { fn foo<'lt, T: Trait2<$0>, const CONST_PARAM: usize>(_: T) {} "#, expect![[r#" - kw return ta Foo = type Foo; tp T cp CONST_PARAM -- cgit v1.2.3