From 4a0ab832f315344199bd7b7197fc8ae3ca2b7a1c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 15 Mar 2021 17:23:08 +0100 Subject: unqualfied_path completions aren't responsible for pattern completions --- crates/ide_completion/src/completions/pattern.rs | 116 ++++++++++++++++++--- .../src/completions/unqualified_path.rs | 88 +--------------- crates/ide_completion/src/render/pattern.rs | 36 +++---- 3 files changed, 120 insertions(+), 120 deletions(-) diff --git a/crates/ide_completion/src/completions/pattern.rs b/crates/ide_completion/src/completions/pattern.rs index 9282c3827..46cef58f0 100644 --- a/crates/ide_completion/src/completions/pattern.rs +++ b/crates/ide_completion/src/completions/pattern.rs @@ -11,10 +11,13 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { return; } - if let Some(ty) = &ctx.expected_type { - super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| { - acc.add_qualified_variant_pat(ctx, variant, path) - }); + if !ctx.is_irrefutable_pat_binding { + if let Some(ty) = ctx.expected_type.as_ref() { + super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| { + acc.add_qualified_variant_pat(ctx, variant, path.clone()); + acc.add_qualified_enum_variant(ctx, variant, path); + }); + } } // FIXME: ideally, we should look at the type we are matching against and @@ -85,7 +88,7 @@ static FOO: E = E::X; struct Bar { f: u32 } fn foo() { - match E::X { $0 } + match E::X { a$0 } } "#, expect![[r#" @@ -106,10 +109,11 @@ macro_rules! m { ($e:expr) => { $e } } enum E { X } fn foo() { - m!(match E::X { $0 }) + m!(match E::X { a$0 }) } "#, expect![[r#" + ev E::X () en E ma m!(…) macro_rules! m "#]], @@ -129,7 +133,7 @@ static FOO: E = E::X; struct Bar { f: u32 } fn foo() { - let $0 + let a$0 } "#, expect![[r#" @@ -147,7 +151,7 @@ enum E { X } static FOO: E = E::X; struct Bar { f: u32 } -fn foo($0) { +fn foo(a$0) { } "#, expect![[r#" @@ -163,7 +167,7 @@ fn foo($0) { struct Bar { f: u32 } fn foo() { - let $0 + let a$0 } "#, expect![[r#" @@ -179,7 +183,7 @@ fn foo() { struct Foo { bar: String, baz: String } struct Bar(String, String); struct Baz; -fn outer($0) {} +fn outer(a$0) {} "#, expect![[r#" bn Foo Foo { bar$1, baz$2 }: Foo$0 @@ -196,7 +200,7 @@ struct Foo { bar: String, baz: String } struct Bar(String, String); struct Baz; fn outer() { - let $0 + let a$0 } "#, expect![[r#" @@ -215,7 +219,7 @@ struct Bar(String, String); struct Baz; fn outer() { match () { - $0 + a$0 } } "#, @@ -239,7 +243,7 @@ use foo::*; fn outer() { match () { - $0 + a$0 } } "#, @@ -258,7 +262,7 @@ fn outer() { struct Foo(i32); fn main() { match Foo(92) { - $0(92) => (), + a$0(92) => (), } } "#, @@ -281,7 +285,7 @@ struct Foo(i32); impl Foo { fn foo() { match () { - $0 + a$0 } } } @@ -314,4 +318,86 @@ impl Foo { "#]], ) } + + #[test] + fn completes_enum_variant_matcharm() { + check( + r#" +enum Foo { Bar, Baz, Quux } + +fn main() { + let foo = Foo::Quux; + match foo { Qu$0 } +} +"#, + expect![[r#" + ev Foo::Bar () + ev Foo::Baz () + ev Foo::Quux () + en Foo + "#]], + ) + } + + #[test] + fn completes_enum_variant_matcharm_ref() { + check( + r#" +enum Foo { Bar, Baz, Quux } + +fn main() { + let foo = Foo::Quux; + match &foo { Qu$0 } +} +"#, + expect![[r#" + ev Foo::Bar () + ev Foo::Baz () + ev Foo::Quux () + en Foo + "#]], + ) + } + + #[test] + fn completes_enum_variant_iflet() { + check( + r#" +enum Foo { Bar, Baz, Quux } + +fn main() { + let foo = Foo::Quux; + if let Qu$0 = foo { } +} +"#, + expect![[r#" + ev Foo::Bar () + ev Foo::Baz () + ev Foo::Quux () + en Foo + "#]], + ) + } + + #[test] + fn completes_enum_variant_impl() { + check( + r#" +enum Foo { Bar, Baz, Quux } +impl Foo { + fn foo() { match Foo::Bar { Q$0 } } +} +"#, + expect![[r#" + ev Self::Bar () + ev Self::Baz () + ev Self::Quux () + ev Foo::Bar () + ev Foo::Baz () + ev Foo::Quux () + sp Self + en Foo + "#]], + ) + } } diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs index 044dfd160..5ef80f6a7 100644 --- a/crates/ide_completion/src/completions/unqualified_path.rs +++ b/crates/ide_completion/src/completions/unqualified_path.rs @@ -6,7 +6,7 @@ use syntax::AstNode; use crate::{CompletionContext, Completions}; pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { - if !(ctx.is_trivial_path || ctx.is_pat_binding_or_const) { + if !ctx.is_trivial_path { return; } if ctx.record_lit_syntax.is_some() @@ -23,10 +23,6 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC }); } - if ctx.is_pat_binding_or_const { - return; - } - ctx.scope.process_all_names(&mut |name, res| { if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res { cov_mark::hit!(skip_lifetime_completion); @@ -608,66 +604,6 @@ fn main() { $0 } ); } - #[test] - fn completes_enum_variant_matcharm() { - check( - r#" -enum Foo { Bar, Baz, Quux } - -fn main() { - let foo = Foo::Quux; - match foo { Qu$0 } -} -"#, - expect![[r#" - ev Foo::Bar () - ev Foo::Baz () - ev Foo::Quux () - en Foo - "#]], - ) - } - - #[test] - fn completes_enum_variant_matcharm_ref() { - check( - r#" -enum Foo { Bar, Baz, Quux } - -fn main() { - let foo = Foo::Quux; - match &foo { Qu$0 } -} -"#, - expect![[r#" - ev Foo::Bar () - ev Foo::Baz () - ev Foo::Quux () - en Foo - "#]], - ) - } - - #[test] - fn completes_enum_variant_iflet() { - check( - r#" -enum Foo { Bar, Baz, Quux } - -fn main() { - let foo = Foo::Quux; - if let Qu$0 = foo { } -} -"#, - expect![[r#" - ev Foo::Bar () - ev Foo::Baz () - ev Foo::Quux () - en Foo - "#]], - ) - } - #[test] fn completes_enum_variant_basic_expr() { check( @@ -700,28 +636,6 @@ fn f() -> m::E { V$0 } ) } - #[test] - fn completes_enum_variant_impl() { - check( - r#" -enum Foo { Bar, Baz, Quux } -impl Foo { - fn foo() { match Foo::Bar { Q$0 } } -} -"#, - expect![[r#" - ev Self::Bar () - ev Self::Baz () - ev Self::Quux () - ev Foo::Bar () - ev Foo::Baz () - ev Foo::Quux () - sp Self - en Foo - "#]], - ) - } - #[test] fn dont_complete_attr() { check( diff --git a/crates/ide_completion/src/render/pattern.rs b/crates/ide_completion/src/render/pattern.rs index ca2926125..b4e80f424 100644 --- a/crates/ide_completion/src/render/pattern.rs +++ b/crates/ide_completion/src/render/pattern.rs @@ -6,24 +6,6 @@ use itertools::Itertools; use crate::{item::CompletionKind, render::RenderContext, CompletionItem, CompletionItemKind}; -fn visible_fields( - ctx: &RenderContext<'_>, - fields: &[hir::Field], - item: impl HasAttrs, -) -> Option<(Vec, bool)> { - let module = ctx.completion.scope.module()?; - let n_fields = fields.len(); - let fields = fields - .into_iter() - .filter(|field| field.is_visible_from(ctx.db(), module)) - .copied() - .collect::>(); - - let fields_omitted = - n_fields - fields.len() > 0 || item.attrs(ctx.db()).by_key("non_exhaustive").exists(); - Some((fields, fields_omitted)) -} - pub(crate) fn render_struct_pat( ctx: RenderContext<'_>, strukt: hir::Struct, @@ -148,3 +130,21 @@ fn render_tuple_as_pat(fields: &[hir::Field], name: &str, fields_omitted: bool) name = name ) } + +fn visible_fields( + ctx: &RenderContext<'_>, + fields: &[hir::Field], + item: impl HasAttrs, +) -> Option<(Vec, bool)> { + let module = ctx.completion.scope.module()?; + let n_fields = fields.len(); + let fields = fields + .into_iter() + .filter(|field| field.is_visible_from(ctx.db(), module)) + .copied() + .collect::>(); + + let fields_omitted = + n_fields - fields.len() > 0 || item.attrs(ctx.db()).by_key("non_exhaustive").exists(); + Some((fields, fields_omitted)) +} -- cgit v1.2.3