unqualfied_path completions aren't responsible for pattern completions

This commit is contained in:
Lukas Wirth 2021-03-15 17:23:08 +01:00
parent 5f6d71cf0c
commit 4a0ab832f3
3 changed files with 120 additions and 120 deletions

View file

@ -11,11 +11,14 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
return; return;
} }
if let Some(ty) = &ctx.expected_type { 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| { super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| {
acc.add_qualified_variant_pat(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 // FIXME: ideally, we should look at the type we are matching against and
// suggest variants + auto-imports // suggest variants + auto-imports
@ -85,7 +88,7 @@ static FOO: E = E::X;
struct Bar { f: u32 } struct Bar { f: u32 }
fn foo() { fn foo() {
match E::X { $0 } match E::X { a$0 }
} }
"#, "#,
expect![[r#" expect![[r#"
@ -106,10 +109,11 @@ macro_rules! m { ($e:expr) => { $e } }
enum E { X } enum E { X }
fn foo() { fn foo() {
m!(match E::X { $0 }) m!(match E::X { a$0 })
} }
"#, "#,
expect![[r#" expect![[r#"
ev E::X ()
en E en E
ma m!() macro_rules! m ma m!() macro_rules! m
"#]], "#]],
@ -129,7 +133,7 @@ static FOO: E = E::X;
struct Bar { f: u32 } struct Bar { f: u32 }
fn foo() { fn foo() {
let $0 let a$0
} }
"#, "#,
expect![[r#" expect![[r#"
@ -147,7 +151,7 @@ enum E { X }
static FOO: E = E::X; static FOO: E = E::X;
struct Bar { f: u32 } struct Bar { f: u32 }
fn foo($0) { fn foo(a$0) {
} }
"#, "#,
expect![[r#" expect![[r#"
@ -163,7 +167,7 @@ fn foo($0) {
struct Bar { f: u32 } struct Bar { f: u32 }
fn foo() { fn foo() {
let $0 let a$0
} }
"#, "#,
expect![[r#" expect![[r#"
@ -179,7 +183,7 @@ fn foo() {
struct Foo { bar: String, baz: String } struct Foo { bar: String, baz: String }
struct Bar(String, String); struct Bar(String, String);
struct Baz; struct Baz;
fn outer($0) {} fn outer(a$0) {}
"#, "#,
expect![[r#" expect![[r#"
bn Foo Foo { bar$1, baz$2 }: Foo$0 bn Foo Foo { bar$1, baz$2 }: Foo$0
@ -196,7 +200,7 @@ struct Foo { bar: String, baz: String }
struct Bar(String, String); struct Bar(String, String);
struct Baz; struct Baz;
fn outer() { fn outer() {
let $0 let a$0
} }
"#, "#,
expect![[r#" expect![[r#"
@ -215,7 +219,7 @@ struct Bar(String, String);
struct Baz; struct Baz;
fn outer() { fn outer() {
match () { match () {
$0 a$0
} }
} }
"#, "#,
@ -239,7 +243,7 @@ use foo::*;
fn outer() { fn outer() {
match () { match () {
$0 a$0
} }
} }
"#, "#,
@ -258,7 +262,7 @@ fn outer() {
struct Foo(i32); struct Foo(i32);
fn main() { fn main() {
match Foo(92) { match Foo(92) {
$0(92) => (), a$0(92) => (),
} }
} }
"#, "#,
@ -281,7 +285,7 @@ struct Foo(i32);
impl Foo { impl Foo {
fn foo() { fn foo() {
match () { 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
"#]],
)
}
} }

View file

@ -6,7 +6,7 @@ use syntax::AstNode;
use crate::{CompletionContext, Completions}; use crate::{CompletionContext, Completions};
pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { 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; return;
} }
if ctx.record_lit_syntax.is_some() 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| { ctx.scope.process_all_names(&mut |name, res| {
if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res { if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
cov_mark::hit!(skip_lifetime_completion); 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] #[test]
fn completes_enum_variant_basic_expr() { fn completes_enum_variant_basic_expr() {
check( 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] #[test]
fn dont_complete_attr() { fn dont_complete_attr() {
check( check(

View file

@ -6,24 +6,6 @@ use itertools::Itertools;
use crate::{item::CompletionKind, render::RenderContext, CompletionItem, CompletionItemKind}; use crate::{item::CompletionKind, render::RenderContext, CompletionItem, CompletionItemKind};
fn visible_fields(
ctx: &RenderContext<'_>,
fields: &[hir::Field],
item: impl HasAttrs,
) -> Option<(Vec<hir::Field>, 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::<Vec<_>>();
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( pub(crate) fn render_struct_pat(
ctx: RenderContext<'_>, ctx: RenderContext<'_>,
strukt: hir::Struct, strukt: hir::Struct,
@ -148,3 +130,21 @@ fn render_tuple_as_pat(fields: &[hir::Field], name: &str, fields_omitted: bool)
name = name name = name
) )
} }
fn visible_fields(
ctx: &RenderContext<'_>,
fields: &[hir::Field],
item: impl HasAttrs,
) -> Option<(Vec<hir::Field>, 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::<Vec<_>>();
let fields_omitted =
n_fields - fields.len() > 0 || item.attrs(ctx.db()).by_key("non_exhaustive").exists();
Some((fields, fields_omitted))
}