chore: optimise

This commit is contained in:
Mohammad Mustakim Ali 2024-02-12 23:11:06 +00:00
parent 24a3c42bd6
commit 0af14ef8c3
3 changed files with 50 additions and 65 deletions

View File

@ -211,8 +211,8 @@ pub enum CompletionRelevancePostfixMatch {
#[derive(Debug, Clone, Copy, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct CompletionRelevanceFn { pub struct CompletionRelevanceFn {
pub has_args: bool, pub has_params: bool,
pub has_self_arg: bool, pub has_self_param: bool,
pub return_type: CompletionRelevanceReturnType, pub return_type: CompletionRelevanceReturnType,
} }
@ -310,10 +310,10 @@ pub fn score(self) -> u32 {
// Bump Constructor or Builder methods with no arguments, // Bump Constructor or Builder methods with no arguments,
// over them tha with self arguments // over them tha with self arguments
if fn_score > 0 { if fn_score > 0 {
if !asf.has_args { if !asf.has_params {
// bump associated functions // bump associated functions
fn_score += 1; fn_score += 1;
} else if asf.has_self_arg { } else if asf.has_self_param {
// downgrade methods (below Constructor) // downgrade methods (below Constructor)
fn_score = 1; fn_score = 1;
} }

View File

@ -1420,8 +1420,8 @@ fn bar(self) { self.$0 }
is_definite: false, is_definite: false,
function: Some( function: Some(
CompletionRelevanceFn { CompletionRelevanceFn {
has_args: true, has_params: true,
has_self_arg: true, has_self_param: true,
return_type: Other, return_type: Other,
}, },
), ),
@ -1545,8 +1545,8 @@ fn foo(s: S) { s.$0 }
is_definite: false, is_definite: false,
function: Some( function: Some(
CompletionRelevanceFn { CompletionRelevanceFn {
has_args: true, has_params: true,
has_self_arg: true, has_self_param: true,
return_type: Other, return_type: Other,
}, },
), ),
@ -2207,8 +2207,8 @@ fn test() {
"fn(&self, u32) -> Bar", "fn(&self, u32) -> Bar",
Some( Some(
CompletionRelevanceFn { CompletionRelevanceFn {
has_args: true, has_params: true,
has_self_arg: true, has_self_param: true,
return_type: Other, return_type: Other,
}, },
), ),
@ -2217,8 +2217,8 @@ fn test() {
"fn(&self)", "fn(&self)",
Some( Some(
CompletionRelevanceFn { CompletionRelevanceFn {
has_args: true, has_params: true,
has_self_arg: true, has_self_param: true,
return_type: Other, return_type: Other,
}, },
), ),
@ -2227,8 +2227,8 @@ fn test() {
"fn(&self) -> Foo", "fn(&self) -> Foo",
Some( Some(
CompletionRelevanceFn { CompletionRelevanceFn {
has_args: true, has_params: true,
has_self_arg: true, has_self_param: true,
return_type: DirectConstructor, return_type: DirectConstructor,
}, },
), ),
@ -2237,8 +2237,8 @@ fn test() {
"fn(&self, u32) -> Foo", "fn(&self, u32) -> Foo",
Some( Some(
CompletionRelevanceFn { CompletionRelevanceFn {
has_args: true, has_params: true,
has_self_arg: true, has_self_param: true,
return_type: DirectConstructor, return_type: DirectConstructor,
}, },
), ),
@ -2247,8 +2247,8 @@ fn test() {
"fn(&self) -> Option<Foo>", "fn(&self) -> Option<Foo>",
Some( Some(
CompletionRelevanceFn { CompletionRelevanceFn {
has_args: true, has_params: true,
has_self_arg: true, has_self_param: true,
return_type: Constructor, return_type: Constructor,
}, },
), ),
@ -2257,8 +2257,8 @@ fn test() {
"fn(&self) -> Result<Foo, Bar>", "fn(&self) -> Result<Foo, Bar>",
Some( Some(
CompletionRelevanceFn { CompletionRelevanceFn {
has_args: true, has_params: true,
has_self_arg: true, has_self_param: true,
return_type: Constructor, return_type: Constructor,
}, },
), ),
@ -2267,8 +2267,8 @@ fn test() {
"fn(&self) -> Result<Bar, Foo>", "fn(&self) -> Result<Bar, Foo>",
Some( Some(
CompletionRelevanceFn { CompletionRelevanceFn {
has_args: true, has_params: true,
has_self_arg: true, has_self_param: true,
return_type: Constructor, return_type: Constructor,
}, },
), ),
@ -2277,8 +2277,8 @@ fn test() {
"fn(&self, u32) -> Option<Foo>", "fn(&self, u32) -> Option<Foo>",
Some( Some(
CompletionRelevanceFn { CompletionRelevanceFn {
has_args: true, has_params: true,
has_self_arg: true, has_self_param: true,
return_type: Constructor, return_type: Constructor,
}, },
), ),
@ -2433,8 +2433,8 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 }
is_definite: false, is_definite: false,
function: Some( function: Some(
CompletionRelevanceFn { CompletionRelevanceFn {
has_args: true, has_params: true,
has_self_arg: true, has_self_param: true,
return_type: Other, return_type: Other,
}, },
), ),
@ -2565,8 +2565,8 @@ fn main() {
is_definite: false, is_definite: false,
function: Some( function: Some(
CompletionRelevanceFn { CompletionRelevanceFn {
has_args: false, has_params: false,
has_self_arg: false, has_self_param: false,
return_type: Other, return_type: Other,
}, },
), ),
@ -2932,13 +2932,7 @@ fn main() {
is_private_editable: false, is_private_editable: false,
postfix_match: None, postfix_match: None,
is_definite: false, is_definite: false,
function: Some( function: None,
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
return_type: Other,
},
),
}, },
}, },
CompletionItem { CompletionItem {
@ -2961,13 +2955,7 @@ fn main() {
is_private_editable: false, is_private_editable: false,
postfix_match: None, postfix_match: None,
is_definite: false, is_definite: false,
function: Some( function: None,
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
return_type: Other,
},
),
}, },
}, },
] ]

View File

@ -66,9 +66,9 @@ fn render(
), ),
_ => (name.unescaped().to_smol_str(), name.to_smol_str()), _ => (name.unescaped().to_smol_str(), name.to_smol_str()),
}; };
let has_self_param = func.self_param(db).is_some();
let mut item = CompletionItem::new( let mut item = CompletionItem::new(
if func.self_param(db).is_some() { if has_self_param {
CompletionItemKind::Method CompletionItemKind::Method
} else { } else {
CompletionItemKind::SymbolKind(SymbolKind::Function) CompletionItemKind::SymbolKind(SymbolKind::Function)
@ -104,18 +104,21 @@ fn render(
.filter(|_| !has_call_parens) .filter(|_| !has_call_parens)
.and_then(|cap| Some((cap, params(ctx.completion, func, &func_kind, has_dot_receiver)?))); .and_then(|cap| Some((cap, params(ctx.completion, func, &func_kind, has_dot_receiver)?)));
let type_match = if has_call_parens || complete_call_parens.is_some() { let function = assoc_item
.and_then(|assoc_item| assoc_item.implementing_ty(db))
.map(|self_type| compute_return_type_match(db, &ctx, self_type, &ret_type))
.map(|return_type| CompletionRelevanceFn {
has_params: has_self_param || func.num_params(db) > 0,
has_self_param,
return_type,
});
item.set_relevance(CompletionRelevance {
type_match: if has_call_parens || complete_call_parens.is_some() {
compute_type_match(completion, &ret_type) compute_type_match(completion, &ret_type)
} else { } else {
compute_type_match(completion, &func.ty(db)) compute_type_match(completion, &func.ty(db))
}; },
let function = assoc_item
.and_then(|assoc_item| assoc_item.implementing_ty(db))
.and_then(|self_type| compute_function_match(db, &ctx, self_type, func, &ret_type));
item.set_relevance(CompletionRelevance {
type_match,
exact_name_match: compute_exact_name_match(completion, &call), exact_name_match: compute_exact_name_match(completion, &call),
function, function,
is_op_method, is_op_method,
@ -168,26 +171,22 @@ fn render(
item item
} }
fn compute_function_match( fn compute_return_type_match(
db: &dyn HirDatabase, db: &dyn HirDatabase,
ctx: &RenderContext<'_>, ctx: &RenderContext<'_>,
self_type: hir::Type, self_type: hir::Type,
func: hir::Function, ret_type: &hir::Type,
func_return_type: &hir::Type, ) -> CompletionRelevanceReturnType {
) -> Option<CompletionRelevanceFn> { if match_types(ctx.completion, &self_type, &ret_type).is_some() {
let has_args = func.num_params(db) > 0;
let has_self_arg = func.self_param(db).is_some();
let return_type = if match_types(ctx.completion, &self_type, &func_return_type).is_some() {
// fn([..]) -> Self // fn([..]) -> Self
CompletionRelevanceReturnType::DirectConstructor CompletionRelevanceReturnType::DirectConstructor
} else if func_return_type } else if ret_type
.type_arguments() .type_arguments()
.any(|ret_type_arg| match_types(ctx.completion, &self_type, &ret_type_arg).is_some()) .any(|ret_type_arg| match_types(ctx.completion, &self_type, &ret_type_arg).is_some())
{ {
// fn([..]) -> Result<Self, E> OR Wrapped<Foo, Self> // fn([..]) -> Result<Self, E> OR Wrapped<Foo, Self>
CompletionRelevanceReturnType::Constructor CompletionRelevanceReturnType::Constructor
} else if func_return_type } else if ret_type
.as_adt() .as_adt()
.and_then(|adt| adt.name(db).as_str().map(|name| name.ends_with("Builder"))) .and_then(|adt| adt.name(db).as_str().map(|name| name.ends_with("Builder")))
.unwrap_or(false) .unwrap_or(false)
@ -196,9 +195,7 @@ fn compute_function_match(
CompletionRelevanceReturnType::Builder CompletionRelevanceReturnType::Builder
} else { } else {
CompletionRelevanceReturnType::Other CompletionRelevanceReturnType::Other
}; }
Some(CompletionRelevanceFn { return_type, has_args, has_self_arg })
} }
pub(super) fn add_call_parens<'b>( pub(super) fn add_call_parens<'b>(