fix: format literal lookup

This commit is contained in:
yue4u 2022-08-16 01:24:21 +09:00
parent 5076f50c7a
commit 91358bd937
7 changed files with 46 additions and 22 deletions

View file

@ -129,7 +129,7 @@ mod tests {
#[test]
fn literal_struct_completion_edit() {
check_edit(
"FooDesc {}",
"FooDesc{}",
r#"
struct FooDesc { pub bar: bool }
@ -154,7 +154,7 @@ fn baz() {
#[test]
fn literal_struct_impl_self_completion() {
check_edit(
"Self {}",
"Self{}",
r#"
struct Foo {
bar: u64,
@ -180,7 +180,7 @@ impl Foo {
);
check_edit(
"Self()",
"Self()",
r#"
mod submod {
pub struct Foo(pub u64);
@ -209,7 +209,7 @@ impl submod::Foo {
#[test]
fn literal_struct_completion_from_sub_modules() {
check_edit(
"submod::Struct {}",
"submod::Struct{}",
r#"
mod submod {
pub struct Struct {
@ -238,7 +238,7 @@ fn f() -> submod::Struct {
#[test]
fn literal_struct_complexion_module() {
check_edit(
"FooDesc {}",
"FooDesc{}",
r#"
mod _69latrick {
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(
Variant,
),
lookup: "Foo{}",
detail: "Foo { x: i32, y: i32 }",
},
]
@ -591,6 +592,7 @@ fn main() { Foo::Fo$0 }
kind: SymbolKind(
Variant,
),
lookup: "Foo()",
detail: "Foo(i32, i32)",
},
]
@ -707,7 +709,7 @@ fn main() { let _: m::Spam = S$0 }
kind: SymbolKind(
Variant,
),
lookup: "Spam::Bar()",
lookup: "Spam::Bar()",
detail: "m::Spam::Bar(i32)",
relevance: CompletionRelevance {
exact_name_match: false,

View file

@ -10,8 +10,8 @@ use crate::{
render::{
compute_ref_match, compute_type_match,
variant::{
format_literal_label, render_record_lit, render_tuple_lit, visible_fields,
RenderedLiteral,
format_literal_label, format_literal_lookup, render_record_lit, render_tuple_lit,
visible_fields, RenderedLiteral,
},
RenderContext,
},
@ -97,13 +97,20 @@ fn render(
if !should_add_parens {
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(
CompletionItemKind::SymbolKind(thing.symbol_kind()),
ctx.source_range(),
format_literal_label(&qualified_name, kind),
label,
);
item.lookup_by(lookup);
item.detail(rendered.detail);
match snippet_cap {
@ -111,9 +118,6 @@ fn render(
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));
let ty = thing.ty(db);

View file

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

View file

@ -6,7 +6,7 @@ use itertools::Itertools;
use crate::{
render::{
variant::{format_literal_label, visible_fields},
variant::{format_literal_label, format_literal_lookup, visible_fields},
RenderContext,
},
CompletionItem, CompletionItemKind,
@ -24,13 +24,16 @@ pub(crate) fn render_union_literal(
Some(p) => (p.unescaped().to_string(), p.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(
CompletionItemKind::SymbolKind(SymbolKind::Union),
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, 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(),
}
}
/// 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() {
cov_mark::check!(enum_variant_pattern_path);
check_edit(
"RecordVariant {}",
"RecordVariant{}",
r#"
enum Enum {
RecordVariant { field: u32 }