Add quantified trees to reduce autocomplete options

This commit is contained in:
Tavo Annus 2024-01-03 22:11:08 +02:00
parent bdbdd83ec1
commit a946970e2d
16 changed files with 300 additions and 94 deletions

View file

@ -162,10 +162,11 @@ impl Completions {
&mut self,
ctx: &CompletionContext<'_>,
expr: &hir::term_search::TypeTree,
path_ctx: &PathCompletionCtx,
) {
let item = render_type_tree(ctx, expr, path_ctx);
item.add_to(self, ctx.db);
match render_type_tree(ctx, expr) {
Some(item) => item.add_to(self, ctx.db),
None => (),
}
}
pub(crate) fn add_crate_roots(
@ -698,10 +699,10 @@ pub(super) fn complete_name_ref(
ctx: &CompletionContext<'_>,
NameRefContext { nameref, kind }: &NameRefContext,
) {
expr::complete_expr(acc, ctx);
match kind {
NameRefKind::Path(path_ctx) => {
flyimport::import_on_the_fly_path(acc, ctx, path_ctx);
expr::complete_expr(acc, ctx, path_ctx);
match &path_ctx.kind {
PathKind::Expr { expr_ctx } => {

View file

@ -332,7 +332,6 @@ pub(crate) fn complete_expr_path(
pub(crate) fn complete_expr(
acc: &mut Completions,
ctx: &CompletionContext<'_>,
path_ctx: &PathCompletionCtx,
) {
let _p = profile::span("complete_expr");
if !ctx.qualifier_ctx.none() {
@ -349,11 +348,15 @@ pub(crate) fn complete_expr(
sema: &ctx.sema,
scope: &ctx.scope,
goal: ty.clone(),
config: hir::term_search::TermSearchConfig { enable_borrowcheck: false },
config: hir::term_search::TermSearchConfig {
enable_borrowcheck: false,
many_alternatives_threshold: 1,
depth: 2,
},
};
let exprs = hir::term_search::term_search(term_search_ctx);
for expr in exprs {
acc.add_expr(ctx, &expr, path_ctx);
acc.add_expr(ctx, &expr);
}
}
}

View file

@ -17,7 +17,7 @@ use ide_db::{
imports::import_assets::LocatedImport,
RootDatabase, SnippetCap, SymbolKind,
};
use syntax::{format_smolstr, AstNode, SmolStr, SyntaxKind, TextRange};
use syntax::{ast, AstNode, SmolStr, SyntaxKind, TextRange};
use text_edit::TextEdit;
use crate::{
@ -275,21 +275,50 @@ pub(crate) fn render_resolution_with_import_pat(
pub(crate) fn render_type_tree(
ctx: &CompletionContext<'_>,
expr: &hir::term_search::TypeTree,
path_ctx: &PathCompletionCtx,
) -> Builder {
let mut item = CompletionItem::new(
CompletionItemKind::Snippet,
ctx.source_range(),
expr.gen_source_code(&ctx.scope),
);
) -> Option<Builder> {
let mut i = 1;
let mut snippet_formatter = |ty: &hir::Type| {
let arg_name = ty
.as_adt()
.and_then(|adt| adt.name(ctx.db).as_text())
.map(|s| stdx::to_lower_snake_case(s.as_str()))
.unwrap_or_else(|| String::from("_"));
let res = format!("${{{i}:{arg_name}}}");
i += 1;
res
};
let mut label_formatter = |ty: &hir::Type| {
ty.as_adt()
.and_then(|adt| adt.name(ctx.db).as_text())
.map(|s| stdx::to_lower_snake_case(s.as_str()))
.unwrap_or_else(|| String::from("_"))
};
let label = expr.gen_source_code(&ctx.scope, &mut label_formatter);
let source_range = match &ctx.expected_name {
Some(name_or_ref) => name_or_ref.syntax().text_range(),
None => match ctx.original_token.parent() {
Some(node) => match node.ancestors().find_map(|n| ast::Path::cast(n)) {
Some(path) => path.syntax().text_range(),
None => node.text_range(),
},
None => ctx.source_range(),
},
};
let mut item = CompletionItem::new(CompletionItemKind::Snippet, source_range, label);
let snippet = format!("{}$0", expr.gen_source_code(&ctx.scope, &mut snippet_formatter));
let edit = TextEdit::replace(source_range, snippet);
item.snippet_edit(ctx.config.snippet_cap?, edit);
item.set_relevance(crate::CompletionRelevance {
type_match: Some(crate::item::CompletionRelevanceTypeMatch::CouldUnify),
..Default::default()
});
path_ref_match(ctx, path_ctx, &expr.ty(ctx.sema.db), &mut item);
item
Some(item)
}
fn scope_def_to_name(

View file

@ -97,6 +97,11 @@ fn func(param0 @ (param1, param2): (i32, i32)) {
kw unsafe
kw while
kw while let
sn ifletlocal
sn letlocal
sn matcharm
sn param1
sn param2
"#]],
);
}
@ -238,9 +243,11 @@ fn complete_in_block() {
kw use
kw while
kw while let
sn false
sn macro_rules
sn pd
sn ppd
sn true
"#]],
)
}
@ -682,7 +689,9 @@ fn main() {
}
"#,
expect![[r#"
fn test() fn() -> Zulu
fn test() fn() -> Zulu
sn Zulu
sn Zulu::test()
"#]],
);
}

View file

@ -316,6 +316,15 @@ fn func() {
bn RecordV {} RecordV { field$1 }$0
bn TupleV() TupleV($1)$0
bn UnitV UnitV$0
sn ()
sn CONST
sn Enum::UnitV
sn STATIC
sn Unit
sn false
sn func()
sn function()
sn true
"#]],
);
}
@ -558,10 +567,12 @@ fn foo() {
}
"#,
expect![[r#"
bn A A$0
bn B {} B { r#type$1 }$0
bn struct {} r#struct { r#type$1 }$0
bn type r#type$0
bn A A$0
bn B {} B { r#type$1 }$0
bn struct {} r#struct { r#type$1 }$0
bn type r#type$0
sn Enum::A
sn Enum::r#type
"#]],
);
}
@ -586,6 +597,7 @@ fn f(t: Ty) {
"#,
expect![[r#"
ct ABC const ABC: Self
sn t
"#]],
);
@ -608,6 +620,7 @@ fn f(e: MyEnum) {
expect![[r#"
ct A pub const A: i32
ct B pub const B: i32
sn e
"#]],
);
@ -633,6 +646,7 @@ fn f(u: U) {
expect![[r#"
ct C pub const C: i32
ct D pub const D: i32
sn u
"#]],
);
@ -652,6 +666,7 @@ fn f(v: u32) {
"#,
expect![[r#"
ct MIN pub const MIN: Self
sn v
"#]],
);
}
@ -763,6 +778,7 @@ fn f(x: EnumAlias<u8>) {
expect![[r#"
bn Tuple() Tuple($1)$0
bn Unit Unit$0
sn x
"#]],
);
}

View file

@ -192,6 +192,8 @@ fn main() {
bt u32 u32
kw crate::
kw self::
sn Foo::default()
sn foo
"#]],
);
check(

View file

@ -1230,6 +1230,10 @@ fn here_we_go() {
"#,
expect![[r#"
st Bar (alias Qux) Bar
sn ()
sn false
sn here_we_go()
sn true
"#]],
);
}
@ -1284,6 +1288,10 @@ fn here_we_go() {
kw unsafe
kw while
kw while let
sn ()
sn false
sn here_we_go()
sn true
"#]],
);
}

View file

@ -70,18 +70,27 @@ fn fn_return_type() {
fn x<'lt, T, const C: usize>() -> $0
"#,
expect![[r#"
en Enum Enum
ma makro!() macro_rules! makro
en Enum Enum
ma makro!() macro_rules! makro
md module
st Record Record
st Tuple Tuple
st Unit Unit
st Record Record
st Tuple Tuple
st Unit Unit
tt Trait
tp T
un Union Union
bt u32 u32
un Union Union
bt u32 u32
kw crate::
kw self::
sn ()
sn C
sn CONST
sn Enum::UnitV
sn STATIC
sn Unit
sn false
sn function()
sn true
"#]],
);
}
@ -100,18 +109,27 @@ fn foo() -> B$0 {
}
"#,
expect![[r#"
en Enum Enum
ma makro!() macro_rules! makro
en Enum Enum
ma makro!() macro_rules! makro
md module
st Record Record
st Tuple Tuple
st Unit Unit
st Record Record
st Tuple Tuple
st Unit Unit
tt Trait
un Union Union
bt u32 u32
un Union Union
bt u32 u32
it ()
kw crate::
kw self::
sn ()
sn CONST
sn Enum::UnitV
sn STATIC
sn Unit
sn false
sn foo()
sn function()
sn true
"#]],
)
}
@ -204,18 +222,26 @@ fn f2(x: u64) -> $0 {
}
"#,
expect![[r#"
en Enum Enum
ma makro!() macro_rules! makro
en Enum Enum
ma makro!() macro_rules! makro
md module
st Record Record
st Tuple Tuple
st Unit Unit
st Record Record
st Tuple Tuple
st Unit Unit
tt Trait
un Union Union
bt u32 u32
un Union Union
bt u32 u32
it u64
kw crate::
kw self::
sn ()
sn CONST
sn Enum::UnitV
sn STATIC
sn Unit
sn false
sn function()
sn true
"#]],
);
}
@ -319,18 +345,27 @@ fn foo<'lt, T, const C: usize>() {
}
"#,
expect![[r#"
en Enum Enum
ma makro!() macro_rules! makro
en Enum Enum
ma makro!() macro_rules! makro
md module
st Record Record
st Tuple Tuple
st Unit Unit
st Record Record
st Tuple Tuple
st Unit Unit
tt Trait
tp T
un Union Union
bt u32 u32
un Union Union
bt u32 u32
kw crate::
kw self::
sn ()
sn C
sn CONST
sn Enum::UnitV
sn STATIC
sn Unit
sn false
sn function()
sn true
"#]],
);
check(
@ -341,14 +376,23 @@ fn foo<'lt, T, const C: usize>() {
}
"#,
expect![[r#"
en Enum Enum
ma makro!() macro_rules! makro
en Enum Enum
ma makro!() macro_rules! makro
md module
st Record Record
st Tuple Tuple
st Unit Unit
st Record Record
st Tuple Tuple
st Unit Unit
tt Trait
un Union Union
un Union Union
sn ()
sn C
sn CONST
sn Enum::UnitV
sn STATIC
sn Unit
sn false
sn function()
sn true
"#]],
);
}