Auto merge of #13028 - yue4u:fix/literal-lookup, r=jonas-schievink

fix: record completion filtering

close #12975
This commit is contained in:
bors 2022-08-16 13:28:00 +00:00
commit b6fae56e38
7 changed files with 46 additions and 22 deletions

View File

@ -129,7 +129,7 @@ mod tests {
#[test] #[test]
fn literal_struct_completion_edit() { fn literal_struct_completion_edit() {
check_edit( check_edit(
"FooDesc {}", "FooDesc{}",
r#" r#"
struct FooDesc { pub bar: bool } struct FooDesc { pub bar: bool }
@ -154,7 +154,7 @@ fn baz() {
#[test] #[test]
fn literal_struct_impl_self_completion() { fn literal_struct_impl_self_completion() {
check_edit( check_edit(
"Self {}", "Self{}",
r#" r#"
struct Foo { struct Foo {
bar: u64, bar: u64,
@ -180,7 +180,7 @@ impl Foo {
); );
check_edit( check_edit(
"Self()", "Self()",
r#" r#"
mod submod { mod submod {
pub struct Foo(pub u64); pub struct Foo(pub u64);
@ -209,7 +209,7 @@ impl submod::Foo {
#[test] #[test]
fn literal_struct_completion_from_sub_modules() { fn literal_struct_completion_from_sub_modules() {
check_edit( check_edit(
"submod::Struct {}", "submod::Struct{}",
r#" r#"
mod submod { mod submod {
pub struct Struct { pub struct Struct {
@ -238,7 +238,7 @@ fn f() -> submod::Struct {
#[test] #[test]
fn literal_struct_complexion_module() { fn literal_struct_complexion_module() {
check_edit( check_edit(
"FooDesc {}", "FooDesc{}",
r#" r#"
mod _69latrick { mod _69latrick {
pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool } pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }

View File

@ -565,6 +565,7 @@ fn main() { Foo::Fo$0 }
kind: SymbolKind( kind: SymbolKind(
Variant, Variant,
), ),
lookup: "Foo{}",
detail: "Foo { x: i32, y: i32 }", detail: "Foo { x: i32, y: i32 }",
}, },
] ]
@ -591,6 +592,7 @@ fn main() { Foo::Fo$0 }
kind: SymbolKind( kind: SymbolKind(
Variant, Variant,
), ),
lookup: "Foo()",
detail: "Foo(i32, i32)", detail: "Foo(i32, i32)",
}, },
] ]
@ -707,7 +709,7 @@ fn main() { let _: m::Spam = S$0 }
kind: SymbolKind( kind: SymbolKind(
Variant, Variant,
), ),
lookup: "Spam::Bar()", lookup: "Spam::Bar()",
detail: "m::Spam::Bar(i32)", detail: "m::Spam::Bar(i32)",
relevance: CompletionRelevance { relevance: CompletionRelevance {
exact_name_match: false, exact_name_match: false,

View File

@ -10,8 +10,8 @@ use crate::{
render::{ render::{
compute_ref_match, compute_type_match, compute_ref_match, compute_type_match,
variant::{ variant::{
format_literal_label, render_record_lit, render_tuple_lit, visible_fields, format_literal_label, format_literal_lookup, render_record_lit, render_tuple_lit,
RenderedLiteral, visible_fields, RenderedLiteral,
}, },
RenderContext, RenderContext,
}, },
@ -97,13 +97,20 @@ fn render(
if !should_add_parens { if !should_add_parens {
kind = StructKind::Unit; kind = StructKind::Unit;
} }
let label = format_literal_label(&qualified_name, kind);
let lookup = if qualified {
format_literal_lookup(&short_qualified_name.to_string(), kind)
} else {
format_literal_lookup(&qualified_name, kind)
};
let mut item = CompletionItem::new( let mut item = CompletionItem::new(
CompletionItemKind::SymbolKind(thing.symbol_kind()), CompletionItemKind::SymbolKind(thing.symbol_kind()),
ctx.source_range(), ctx.source_range(),
format_literal_label(&qualified_name, kind), label,
); );
item.lookup_by(lookup);
item.detail(rendered.detail); item.detail(rendered.detail);
match snippet_cap { match snippet_cap {
@ -111,9 +118,6 @@ fn render(
None => item.insert_text(rendered.literal), None => item.insert_text(rendered.literal),
}; };
if qualified {
item.lookup_by(format_literal_label(&short_qualified_name.to_string(), kind));
}
item.set_documentation(thing.docs(db)).set_deprecated(thing.is_deprecated(&ctx)); item.set_documentation(thing.docs(db)).set_deprecated(thing.is_deprecated(&ctx));
let ty = thing.ty(db); let ty = thing.ty(db);

View File

@ -8,7 +8,7 @@ use syntax::SmolStr;
use crate::{ use crate::{
context::{ParamContext, ParamKind, PathCompletionCtx, PatternContext}, context::{ParamContext, ParamKind, PathCompletionCtx, PatternContext},
render::{ render::{
variant::{format_literal_label, visible_fields}, variant::{format_literal_label, format_literal_lookup, visible_fields},
RenderContext, RenderContext,
}, },
CompletionItem, CompletionItemKind, CompletionItem, CompletionItemKind,
@ -34,9 +34,10 @@ pub(crate) fn render_struct_pat(
let (name, escaped_name) = (name.unescaped().to_smol_str(), name.to_smol_str()); let (name, escaped_name) = (name.unescaped().to_smol_str(), name.to_smol_str());
let kind = strukt.kind(ctx.db()); let kind = strukt.kind(ctx.db());
let label = format_literal_label(name.as_str(), kind); let label = format_literal_label(name.as_str(), kind);
let lookup = format_literal_lookup(name.as_str(), kind);
let pat = render_pat(&ctx, pattern_ctx, &escaped_name, kind, &visible_fields, fields_omitted)?; let pat = render_pat(&ctx, pattern_ctx, &escaped_name, kind, &visible_fields, fields_omitted)?;
Some(build_completion(ctx, label, pat, strukt)) Some(build_completion(ctx, label, lookup, pat, strukt))
} }
pub(crate) fn render_variant_pat( pub(crate) fn render_variant_pat(
@ -60,11 +61,14 @@ pub(crate) fn render_variant_pat(
} }
}; };
let (label, pat) = match path_ctx { let (label, lookup, pat) = match path_ctx {
Some(PathCompletionCtx { has_call_parens: true, .. }) => (name, escaped_name.to_string()), Some(PathCompletionCtx { has_call_parens: true, .. }) => {
(name.clone(), name, escaped_name.to_string())
}
_ => { _ => {
let kind = variant.kind(ctx.db()); let kind = variant.kind(ctx.db());
let label = format_literal_label(name.as_str(), kind); let label = format_literal_label(name.as_str(), kind);
let lookup = format_literal_lookup(name.as_str(), kind);
let pat = render_pat( let pat = render_pat(
&ctx, &ctx,
pattern_ctx, pattern_ctx,
@ -73,16 +77,17 @@ pub(crate) fn render_variant_pat(
&visible_fields, &visible_fields,
fields_omitted, fields_omitted,
)?; )?;
(label, pat) (label, lookup, pat)
} }
}; };
Some(build_completion(ctx, label, pat, variant)) Some(build_completion(ctx, label, lookup, pat, variant))
} }
fn build_completion( fn build_completion(
ctx: RenderContext<'_>, ctx: RenderContext<'_>,
label: SmolStr, label: SmolStr,
lookup: SmolStr,
pat: String, pat: String,
def: impl HasAttrs + Copy, def: impl HasAttrs + Copy,
) -> CompletionItem { ) -> CompletionItem {
@ -90,6 +95,7 @@ fn build_completion(
item.set_documentation(ctx.docs(def)) item.set_documentation(ctx.docs(def))
.set_deprecated(ctx.is_deprecated(def)) .set_deprecated(ctx.is_deprecated(def))
.detail(&pat) .detail(&pat)
.lookup_by(lookup)
.set_relevance(ctx.completion_relevance()); .set_relevance(ctx.completion_relevance());
match ctx.snippet_cap() { match ctx.snippet_cap() {
Some(snippet_cap) => item.insert_snippet(snippet_cap, pat), Some(snippet_cap) => item.insert_snippet(snippet_cap, pat),

View File

@ -6,7 +6,7 @@ use itertools::Itertools;
use crate::{ use crate::{
render::{ render::{
variant::{format_literal_label, visible_fields}, variant::{format_literal_label, format_literal_lookup, visible_fields},
RenderContext, RenderContext,
}, },
CompletionItem, CompletionItemKind, CompletionItem, CompletionItemKind,
@ -24,13 +24,16 @@ pub(crate) fn render_union_literal(
Some(p) => (p.unescaped().to_string(), p.to_string()), Some(p) => (p.unescaped().to_string(), p.to_string()),
None => (name.unescaped().to_string(), name.to_string()), None => (name.unescaped().to_string(), name.to_string()),
}; };
let label = format_literal_label(&name.to_smol_str(), StructKind::Record);
let lookup = format_literal_lookup(&name.to_smol_str(), StructKind::Record);
let mut item = CompletionItem::new( let mut item = CompletionItem::new(
CompletionItemKind::SymbolKind(SymbolKind::Union), CompletionItemKind::SymbolKind(SymbolKind::Union),
ctx.source_range(), ctx.source_range(),
format_literal_label(&name.to_smol_str(), StructKind::Record), label,
); );
item.lookup_by(lookup);
let fields = un.fields(ctx.db()); let fields = un.fields(ctx.db());
let (fields, fields_omitted) = visible_fields(ctx.completion, &fields, un)?; let (fields, fields_omitted) = visible_fields(ctx.completion, &fields, un)?;

View File

@ -94,3 +94,12 @@ pub(crate) fn format_literal_label(name: &str, kind: StructKind) -> SmolStr {
StructKind::Unit => name.into(), StructKind::Unit => name.into(),
} }
} }
/// Format a struct, etc. literal option for lookup used in completions filtering.
pub(crate) fn format_literal_lookup(name: &str, kind: StructKind) -> SmolStr {
match kind {
StructKind::Tuple => SmolStr::from_iter([name, "()"]),
StructKind::Record => SmolStr::from_iter([name, "{}"]),
StructKind::Unit => name.into(),
}
}

View File

@ -467,7 +467,7 @@ fn foo() {
fn completes_enum_variant_pat() { fn completes_enum_variant_pat() {
cov_mark::check!(enum_variant_pattern_path); cov_mark::check!(enum_variant_pattern_path);
check_edit( check_edit(
"RecordVariant {}", "RecordVariant{}",
r#" r#"
enum Enum { enum Enum {
RecordVariant { field: u32 } RecordVariant { field: u32 }