This commit is contained in:
Lukas Wirth 2023-12-08 13:19:34 +01:00
parent 6abba17a5b
commit 1475848250
15 changed files with 516 additions and 550 deletions

View file

@ -9,9 +9,7 @@ use syntax::{AstNode, SmolStr};
use crate::{
context::{CompletionContext, DotAccess, DotAccessKind, PathCompletionCtx, PathKind},
item::{Builder, CompletionItem, CompletionItemKind, CompletionRelevance},
render::{
compute_exact_name_match, compute_function_type_match, compute_ref_match, RenderContext,
},
render::{compute_exact_name_match, compute_ref_match, compute_type_match, RenderContext},
CallableSnippets,
};
@ -81,8 +79,30 @@ fn render(
.and_then(|trait_| trait_.containing_trait_or_trait_impl(ctx.db()))
.map_or(false, |trait_| completion.is_ops_trait(trait_));
let (has_dot_receiver, has_call_parens, cap) = match func_kind {
FuncKind::Function(&PathCompletionCtx {
kind: PathKind::Expr { .. },
has_call_parens,
..
}) => (false, has_call_parens, ctx.completion.config.snippet_cap),
FuncKind::Method(&DotAccess { kind: DotAccessKind::Method { has_parens }, .. }, _) => {
(true, has_parens, ctx.completion.config.snippet_cap)
}
FuncKind::Method(DotAccess { kind: DotAccessKind::Field { .. }, .. }, _) => {
(true, false, ctx.completion.config.snippet_cap)
}
_ => (false, false, None),
};
let complete_call_parens = cap
.filter(|_| !has_call_parens)
.and_then(|cap| Some((cap, params(ctx.completion, func, &func_kind, has_dot_receiver)?)));
item.set_relevance(CompletionRelevance {
type_match: compute_function_type_match(completion, &func),
type_match: if has_call_parens || complete_call_parens.is_some() {
compute_type_match(completion, &ret_type)
} else {
compute_type_match(completion, &func.ty(db))
},
exact_name_match: compute_exact_name_match(completion, &call),
is_op_method,
..ctx.completion_relevance()
@ -112,42 +132,9 @@ fn render(
.detail(detail)
.lookup_by(name.unescaped().to_smol_str());
match ctx.completion.config.snippet_cap {
Some(cap) => {
let complete_params = match func_kind {
FuncKind::Function(PathCompletionCtx {
kind: PathKind::Expr { .. },
has_call_parens: false,
..
}) => Some(false),
FuncKind::Method(
DotAccess {
kind:
DotAccessKind::Method { has_parens: false } | DotAccessKind::Field { .. },
..
},
_,
) => Some(true),
_ => None,
};
if let Some(has_dot_receiver) = complete_params {
if let Some((self_param, params)) =
params(ctx.completion, func, &func_kind, has_dot_receiver)
{
add_call_parens(
&mut item,
completion,
cap,
call,
escaped_call,
self_param,
params,
);
}
}
}
_ => (),
};
if let Some((cap, (self_param, params))) = complete_call_parens {
add_call_parens(&mut item, completion, cap, call, escaped_call, self_param, params);
}
match ctx.import_to_add {
Some(import_to_add) => {