Skip to content

Commit d7374ab

Browse files
bors[bot]Veykril
andauthored
Merge #9896
9896: internal: Only complete type annotations for patterns in function params r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 5a60e91 + bf91804 commit d7374ab

File tree

5 files changed

+67
-19
lines changed

5 files changed

+67
-19
lines changed

crates/ide_completion/src/completions/fn_param.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ use syntax::{
66
match_ast, AstNode,
77
};
88

9-
use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions};
9+
use crate::{
10+
context::ParamKind, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind,
11+
Completions,
12+
};
1013

1114
/// Complete repeated parameters, both name and type. For example, if all
1215
/// functions in a file have a `spam: &mut Spam` parameter, a completion with
1316
/// `spam: &mut Spam` insert text/label and `spam` lookup string will be
1417
/// suggested.
1518
pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
16-
if !ctx.is_param {
19+
if ctx.is_param != Some(ParamKind::Function) {
1720
return None;
1821
}
1922

crates/ide_completion/src/context.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ pub(crate) enum CallKind {
6060
Mac,
6161
Expr,
6262
}
63+
64+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
65+
pub(crate) enum ParamKind {
66+
Function,
67+
Closure,
68+
}
6369
/// `CompletionContext` is created early during completion to figure out, where
6470
/// exactly is the cursor, syntax-wise.
6571
#[derive(Debug)]
@@ -91,7 +97,7 @@ pub(crate) struct CompletionContext<'a> {
9197

9298
// potentially set if we are completing a name
9399
pub(super) is_pat_or_const: Option<PatternRefutability>,
94-
pub(super) is_param: bool,
100+
pub(super) is_param: Option<ParamKind>,
95101

96102
pub(super) completion_location: Option<ImmediateLocation>,
97103
pub(super) prev_sibling: Option<ImmediatePrevSibling>,
@@ -158,7 +164,7 @@ impl<'a> CompletionContext<'a> {
158164
lifetime_allowed: false,
159165
is_label_ref: false,
160166
is_pat_or_const: None,
161-
is_param: false,
167+
is_param: None,
162168
completion_location: None,
163169
prev_sibling: None,
164170
attribute_under_caret: None,
@@ -670,7 +676,17 @@ impl<'a> CompletionContext<'a> {
670676
self.fill_impl_def();
671677
}
672678

673-
self.is_param |= is_node::<ast::Param>(name.syntax());
679+
if let Some(param) = name
680+
.syntax()
681+
.ancestors()
682+
.find_map(ast::Param::cast)
683+
.filter(|it| it.syntax().text_range() == name.syntax().text_range())
684+
{
685+
let is_closure_param =
686+
param.syntax().ancestors().nth(2).and_then(ast::ClosureExpr::cast).is_some();
687+
self.is_param =
688+
Some(if is_closure_param { ParamKind::Closure } else { ParamKind::Function });
689+
}
674690
}
675691

676692
fn classify_name_ref(&mut self, original_file: &SyntaxNode, name_ref: ast::NameRef) {
@@ -774,13 +790,6 @@ fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Op
774790
syntax.covering_element(range).ancestors().find_map(N::cast)
775791
}
776792

777-
fn is_node<N: AstNode>(node: &SyntaxNode) -> bool {
778-
match node.ancestors().find_map(N::cast) {
779-
None => false,
780-
Some(n) => n.syntax().text_range() == node.text_range(),
781-
}
782-
}
783-
784793
fn path_or_use_tree_qualifier(path: &ast::Path) -> Option<(ast::Path, bool)> {
785794
if let Some(qual) = path.qualifier() {
786795
return Some((qual, false));

crates/ide_completion/src/render/pattern.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use hir::{db::HirDatabase, HasAttrs, HasVisibility, Name, StructKind};
44
use ide_db::helpers::SnippetCap;
55
use itertools::Itertools;
66

7-
use crate::{item::CompletionKind, render::RenderContext, CompletionItem, CompletionItemKind};
7+
use crate::{
8+
context::ParamKind, item::CompletionKind, render::RenderContext, CompletionItem,
9+
CompletionItemKind,
10+
};
811

912
pub(crate) fn render_struct_pat(
1013
ctx: RenderContext<'_>,
@@ -83,7 +86,7 @@ fn render_pat(
8386
_ => return None,
8487
};
8588

86-
if ctx.completion.is_param {
89+
if ctx.completion.is_param == Some(ParamKind::Function) {
8790
pat.push(':');
8891
pat.push(' ');
8992
pat.push_str(name);

crates/ide_completion/src/render/struct_literal.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ fn render_literal(
5858
_ => return None,
5959
};
6060

61-
if ctx.completion.is_param {
62-
literal.push(':');
63-
literal.push(' ');
64-
literal.push_str(name);
65-
}
6661
if ctx.snippet_cap().is_some() {
6762
literal.push_str("$0");
6863
}

crates/ide_completion/src/tests/pattern.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,41 @@ fn outer(Foo { bar$0 }: Foo) {}
309309
expect![[r#""#]],
310310
)
311311
}
312+
313+
#[test]
314+
fn completes_in_fn_param() {
315+
check_empty(
316+
r#"
317+
struct Foo { bar: Bar }
318+
struct Bar(u32);
319+
fn foo($0) {}
320+
"#,
321+
expect![[r#"
322+
kw mut
323+
bn Foo Foo { bar$1 }: Foo$0
324+
st Foo
325+
bn Bar Bar($1): Bar$0
326+
st Bar
327+
"#]],
328+
)
329+
}
330+
331+
#[test]
332+
fn completes_in_closure_param() {
333+
check_empty(
334+
r#"
335+
struct Foo { bar: Bar }
336+
struct Bar(u32);
337+
fn foo() {
338+
|$0| {};
339+
}
340+
"#,
341+
expect![[r#"
342+
kw mut
343+
bn Foo Foo { bar$1 }$0
344+
st Foo
345+
bn Bar Bar($1)$0
346+
st Bar
347+
"#]],
348+
)
349+
}

0 commit comments

Comments
 (0)