Skip to content

Commit 6c5403e

Browse files
committed
optimise
1 parent 24a3c42 commit 6c5403e

File tree

3 files changed

+43
-54
lines changed

3 files changed

+43
-54
lines changed

crates/ide-completion/src/item.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ pub enum CompletionRelevancePostfixMatch {
211211

212212
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
213213
pub struct CompletionRelevanceFn {
214-
pub has_args: bool,
215-
pub has_self_arg: bool,
214+
pub has_params: bool,
215+
pub has_self_param: bool,
216216
pub return_type: CompletionRelevanceReturnType,
217217
}
218218

@@ -310,10 +310,10 @@ impl CompletionRelevance {
310310
// Bump Constructor or Builder methods with no arguments,
311311
// over them tha with self arguments
312312
if fn_score > 0 {
313-
if !asf.has_args {
313+
if !asf.has_params {
314314
// bump associated functions
315315
fn_score += 1;
316-
} else if asf.has_self_arg {
316+
} else if asf.has_self_param {
317317
// downgrade methods (below Constructor)
318318
fn_score = 1;
319319
}

crates/ide-completion/src/render.rs

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,8 +1420,8 @@ impl S {
14201420
is_definite: false,
14211421
function: Some(
14221422
CompletionRelevanceFn {
1423-
has_args: true,
1424-
has_self_arg: true,
1423+
has_params: true,
1424+
has_self_param: true,
14251425
return_type: Other,
14261426
},
14271427
),
@@ -1545,8 +1545,8 @@ fn foo(s: S) { s.$0 }
15451545
is_definite: false,
15461546
function: Some(
15471547
CompletionRelevanceFn {
1548-
has_args: true,
1549-
has_self_arg: true,
1548+
has_params: true,
1549+
has_self_param: true,
15501550
return_type: Other,
15511551
},
15521552
),
@@ -2207,8 +2207,8 @@ fn test() {
22072207
"fn(&self, u32) -> Bar",
22082208
Some(
22092209
CompletionRelevanceFn {
2210-
has_args: true,
2211-
has_self_arg: true,
2210+
has_params: true,
2211+
has_self_param: true,
22122212
return_type: Other,
22132213
},
22142214
),
@@ -2217,8 +2217,8 @@ fn test() {
22172217
"fn(&self)",
22182218
Some(
22192219
CompletionRelevanceFn {
2220-
has_args: true,
2221-
has_self_arg: true,
2220+
has_params: true,
2221+
has_self_param: true,
22222222
return_type: Other,
22232223
},
22242224
),
@@ -2227,8 +2227,8 @@ fn test() {
22272227
"fn(&self) -> Foo",
22282228
Some(
22292229
CompletionRelevanceFn {
2230-
has_args: true,
2231-
has_self_arg: true,
2230+
has_params: true,
2231+
has_self_param: true,
22322232
return_type: DirectConstructor,
22332233
},
22342234
),
@@ -2237,8 +2237,8 @@ fn test() {
22372237
"fn(&self, u32) -> Foo",
22382238
Some(
22392239
CompletionRelevanceFn {
2240-
has_args: true,
2241-
has_self_arg: true,
2240+
has_params: true,
2241+
has_self_param: true,
22422242
return_type: DirectConstructor,
22432243
},
22442244
),
@@ -2247,8 +2247,8 @@ fn test() {
22472247
"fn(&self) -> Option<Foo>",
22482248
Some(
22492249
CompletionRelevanceFn {
2250-
has_args: true,
2251-
has_self_arg: true,
2250+
has_params: true,
2251+
has_self_param: true,
22522252
return_type: Constructor,
22532253
},
22542254
),
@@ -2257,8 +2257,8 @@ fn test() {
22572257
"fn(&self) -> Result<Foo, Bar>",
22582258
Some(
22592259
CompletionRelevanceFn {
2260-
has_args: true,
2261-
has_self_arg: true,
2260+
has_params: true,
2261+
has_self_param: true,
22622262
return_type: Constructor,
22632263
},
22642264
),
@@ -2267,8 +2267,8 @@ fn test() {
22672267
"fn(&self) -> Result<Bar, Foo>",
22682268
Some(
22692269
CompletionRelevanceFn {
2270-
has_args: true,
2271-
has_self_arg: true,
2270+
has_params: true,
2271+
has_self_param: true,
22722272
return_type: Constructor,
22732273
},
22742274
),
@@ -2277,8 +2277,8 @@ fn test() {
22772277
"fn(&self, u32) -> Option<Foo>",
22782278
Some(
22792279
CompletionRelevanceFn {
2280-
has_args: true,
2281-
has_self_arg: true,
2280+
has_params: true,
2281+
has_self_param: true,
22822282
return_type: Constructor,
22832283
},
22842284
),
@@ -2433,8 +2433,8 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 }
24332433
is_definite: false,
24342434
function: Some(
24352435
CompletionRelevanceFn {
2436-
has_args: true,
2437-
has_self_arg: true,
2436+
has_params: true,
2437+
has_self_param: true,
24382438
return_type: Other,
24392439
},
24402440
),
@@ -2565,8 +2565,8 @@ fn main() {
25652565
is_definite: false,
25662566
function: Some(
25672567
CompletionRelevanceFn {
2568-
has_args: false,
2569-
has_self_arg: false,
2568+
has_params: false,
2569+
has_self_param: false,
25702570
return_type: Other,
25712571
},
25722572
),
@@ -2932,13 +2932,7 @@ fn main() {
29322932
is_private_editable: false,
29332933
postfix_match: None,
29342934
is_definite: false,
2935-
function: Some(
2936-
CompletionRelevanceFn {
2937-
has_args: true,
2938-
has_self_arg: true,
2939-
return_type: Other,
2940-
},
2941-
),
2935+
function: None,
29422936
},
29432937
},
29442938
CompletionItem {
@@ -2961,13 +2955,7 @@ fn main() {
29612955
is_private_editable: false,
29622956
postfix_match: None,
29632957
is_definite: false,
2964-
function: Some(
2965-
CompletionRelevanceFn {
2966-
has_args: true,
2967-
has_self_arg: true,
2968-
return_type: Other,
2969-
},
2970-
),
2958+
function: None,
29712959
},
29722960
},
29732961
]

crates/ide-completion/src/render/function.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ fn render(
6666
),
6767
_ => (name.unescaped().to_smol_str(), name.to_smol_str()),
6868
};
69-
69+
let has_self_param = func.self_param(db).is_some();
7070
let mut item = CompletionItem::new(
71-
if func.self_param(db).is_some() {
71+
if has_self_param {
7272
CompletionItemKind::Method
7373
} else {
7474
CompletionItemKind::SymbolKind(SymbolKind::Function)
@@ -110,9 +110,10 @@ fn render(
110110
compute_type_match(completion, &func.ty(db))
111111
};
112112

113-
let function = assoc_item
114-
.and_then(|assoc_item| assoc_item.implementing_ty(db))
115-
.and_then(|self_type| compute_function_match(db, &ctx, self_type, func, &ret_type));
113+
let function =
114+
assoc_item.and_then(|assoc_item| assoc_item.implementing_ty(db)).and_then(|self_type| {
115+
compute_function_match(db, &ctx, self_type, func, has_self_param, &ret_type)
116+
});
116117

117118
item.set_relevance(CompletionRelevance {
118119
type_match,
@@ -173,21 +174,21 @@ fn compute_function_match(
173174
ctx: &RenderContext<'_>,
174175
self_type: hir::Type,
175176
func: hir::Function,
176-
func_return_type: &hir::Type,
177+
has_self_param: bool,
178+
ret_type: &hir::Type,
177179
) -> Option<CompletionRelevanceFn> {
178-
let has_args = func.num_params(db) > 0;
179-
let has_self_arg = func.self_param(db).is_some();
180+
let has_params = func.num_params(db) > 0;
180181

181-
let return_type = if match_types(ctx.completion, &self_type, &func_return_type).is_some() {
182+
let return_type = if match_types(ctx.completion, &self_type, &ret_type).is_some() {
182183
// fn([..]) -> Self
183184
CompletionRelevanceReturnType::DirectConstructor
184-
} else if func_return_type
185+
} else if ret_type
185186
.type_arguments()
186187
.any(|ret_type_arg| match_types(ctx.completion, &self_type, &ret_type_arg).is_some())
187188
{
188189
// fn([..]) -> Result<Self, E> OR Wrapped<Foo, Self>
189190
CompletionRelevanceReturnType::Constructor
190-
} else if func_return_type
191+
} else if ret_type
191192
.as_adt()
192193
.and_then(|adt| adt.name(db).as_str().map(|name| name.ends_with("Builder")))
193194
.unwrap_or(false)
@@ -198,7 +199,7 @@ fn compute_function_match(
198199
CompletionRelevanceReturnType::Other
199200
};
200201

201-
Some(CompletionRelevanceFn { return_type, has_args, has_self_arg })
202+
Some(CompletionRelevanceFn { return_type, has_params, has_self_param })
202203
}
203204

204205
pub(super) fn add_call_parens<'b>(

0 commit comments

Comments
 (0)