Skip to content

Commit

Permalink
optimise
Browse files Browse the repository at this point in the history
  • Loading branch information
mustakimali committed Feb 12, 2024
1 parent 24a3c42 commit 6c5403e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 54 deletions.
8 changes: 4 additions & 4 deletions crates/ide-completion/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ pub enum CompletionRelevancePostfixMatch {

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct CompletionRelevanceFn {
pub has_args: bool,
pub has_self_arg: bool,
pub has_params: bool,
pub has_self_param: bool,
pub return_type: CompletionRelevanceReturnType,
}

Expand Down Expand Up @@ -310,10 +310,10 @@ impl CompletionRelevance {
// Bump Constructor or Builder methods with no arguments,
// over them tha with self arguments
if fn_score > 0 {
if !asf.has_args {
if !asf.has_params {
// bump associated functions
fn_score += 1;
} else if asf.has_self_arg {
} else if asf.has_self_param {
// downgrade methods (below Constructor)
fn_score = 1;
}
Expand Down
64 changes: 26 additions & 38 deletions crates/ide-completion/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,8 +1420,8 @@ impl S {
is_definite: false,
function: Some(
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
has_params: true,
has_self_param: true,
return_type: Other,
},
),
Expand Down Expand Up @@ -1545,8 +1545,8 @@ fn foo(s: S) { s.$0 }
is_definite: false,
function: Some(
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
has_params: true,
has_self_param: true,
return_type: Other,
},
),
Expand Down Expand Up @@ -2207,8 +2207,8 @@ fn test() {
"fn(&self, u32) -> Bar",
Some(
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
has_params: true,
has_self_param: true,
return_type: Other,
},
),
Expand All @@ -2217,8 +2217,8 @@ fn test() {
"fn(&self)",
Some(
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
has_params: true,
has_self_param: true,
return_type: Other,
},
),
Expand All @@ -2227,8 +2227,8 @@ fn test() {
"fn(&self) -> Foo",
Some(
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
has_params: true,
has_self_param: true,
return_type: DirectConstructor,
},
),
Expand All @@ -2237,8 +2237,8 @@ fn test() {
"fn(&self, u32) -> Foo",
Some(
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
has_params: true,
has_self_param: true,
return_type: DirectConstructor,
},
),
Expand All @@ -2247,8 +2247,8 @@ fn test() {
"fn(&self) -> Option<Foo>",
Some(
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
has_params: true,
has_self_param: true,
return_type: Constructor,
},
),
Expand All @@ -2257,8 +2257,8 @@ fn test() {
"fn(&self) -> Result<Foo, Bar>",
Some(
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
has_params: true,
has_self_param: true,
return_type: Constructor,
},
),
Expand All @@ -2267,8 +2267,8 @@ fn test() {
"fn(&self) -> Result<Bar, Foo>",
Some(
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
has_params: true,
has_self_param: true,
return_type: Constructor,
},
),
Expand All @@ -2277,8 +2277,8 @@ fn test() {
"fn(&self, u32) -> Option<Foo>",
Some(
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
has_params: true,
has_self_param: true,
return_type: Constructor,
},
),
Expand Down Expand Up @@ -2433,8 +2433,8 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 }
is_definite: false,
function: Some(
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
has_params: true,
has_self_param: true,
return_type: Other,
},
),
Expand Down Expand Up @@ -2565,8 +2565,8 @@ fn main() {
is_definite: false,
function: Some(
CompletionRelevanceFn {
has_args: false,
has_self_arg: false,
has_params: false,
has_self_param: false,
return_type: Other,
},
),
Expand Down Expand Up @@ -2932,13 +2932,7 @@ fn main() {
is_private_editable: false,
postfix_match: None,
is_definite: false,
function: Some(
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
return_type: Other,
},
),
function: None,
},
},
CompletionItem {
Expand All @@ -2961,13 +2955,7 @@ fn main() {
is_private_editable: false,
postfix_match: None,
is_definite: false,
function: Some(
CompletionRelevanceFn {
has_args: true,
has_self_arg: true,
return_type: Other,
},
),
function: None,
},
},
]
Expand Down
25 changes: 13 additions & 12 deletions crates/ide-completion/src/render/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ fn render(
),
_ => (name.unescaped().to_smol_str(), name.to_smol_str()),
};

let has_self_param = func.self_param(db).is_some();
let mut item = CompletionItem::new(
if func.self_param(db).is_some() {
if has_self_param {
CompletionItemKind::Method
} else {
CompletionItemKind::SymbolKind(SymbolKind::Function)
Expand Down Expand Up @@ -110,9 +110,10 @@ fn render(
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));
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, has_self_param, &ret_type)
});

item.set_relevance(CompletionRelevance {
type_match,
Expand Down Expand Up @@ -173,21 +174,21 @@ fn compute_function_match(
ctx: &RenderContext<'_>,
self_type: hir::Type,
func: hir::Function,
func_return_type: &hir::Type,
has_self_param: bool,
ret_type: &hir::Type,
) -> Option<CompletionRelevanceFn> {
let has_args = func.num_params(db) > 0;
let has_self_arg = func.self_param(db).is_some();
let has_params = func.num_params(db) > 0;

let return_type = if match_types(ctx.completion, &self_type, &func_return_type).is_some() {
let return_type = if match_types(ctx.completion, &self_type, &ret_type).is_some() {
// fn([..]) -> Self
CompletionRelevanceReturnType::DirectConstructor
} else if func_return_type
} else if ret_type
.type_arguments()
.any(|ret_type_arg| match_types(ctx.completion, &self_type, &ret_type_arg).is_some())
{
// fn([..]) -> Result<Self, E> OR Wrapped<Foo, Self>
CompletionRelevanceReturnType::Constructor
} else if func_return_type
} else if ret_type
.as_adt()
.and_then(|adt| adt.name(db).as_str().map(|name| name.ends_with("Builder")))
.unwrap_or(false)
Expand All @@ -198,7 +199,7 @@ fn compute_function_match(
CompletionRelevanceReturnType::Other
};

Some(CompletionRelevanceFn { return_type, has_args, has_self_arg })
Some(CompletionRelevanceFn { return_type, has_params, has_self_param })
}

pub(super) fn add_call_parens<'b>(
Expand Down

0 comments on commit 6c5403e

Please sign in to comment.