Skip to content

Commit 0414bb4

Browse files
allow "add omitted labels" in function calls with some labels
1 parent 53c0283 commit 0414bb4

5 files changed

+127
-6
lines changed

compiler-core/src/language_server/code_action.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8562,10 +8562,18 @@ impl<'ast> ast::visit::Visit<'ast> for AddOmittedLabels<'ast> {
85628562

85638563
let mut omitted_labels = Vec::with_capacity(arguments.len());
85648564
for (index, argument) in arguments.iter().enumerate() {
8565-
// We can't apply this code action to calls where any of the
8566-
// arguments have a label explicitly provided.
8567-
if argument.label.is_some() {
8568-
return;
8565+
// If the argument already has a label we don't want to add a label
8566+
// for it, so we skip it.
8567+
if let Some(label) = &argument.label {
8568+
// Though, before skipping, we want to make sure that the label
8569+
// is actually right for the function call. If it's not then we
8570+
// give up on adding labels because there wouldn't be no way of
8571+
// knowing which label to add.
8572+
if !field_map.fields.contains_key(label) {
8573+
return;
8574+
} else {
8575+
continue;
8576+
}
85698577
}
85708578

85718579
let label = argument_index_to_label

compiler-core/src/language_server/tests/action.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10321,8 +10321,23 @@ pub type Labelled {
1032110321
}
1032210322

1032310323
#[test]
10324-
fn add_omitted_labels_does_not_pop_up_if_function_already_has_labels() {
10325-
assert_no_code_actions!(
10324+
fn add_omitted_labels_works_with_constructors_calls_with_some_labels_1() {
10325+
assert_code_action!(
10326+
ADD_OMITTED_LABELS,
10327+
"
10328+
pub fn main() {
10329+
labelled(3, 1, b: 2)
10330+
}
10331+
10332+
pub fn labelled(a a, b b, c c) { todo }
10333+
",
10334+
find_position_of("labelled").to_selection(),
10335+
);
10336+
}
10337+
10338+
#[test]
10339+
fn add_omitted_labels_works_with_constructors_calls_with_some_labels() {
10340+
assert_code_action!(
1032610341
ADD_OMITTED_LABELS,
1032710342
"
1032810343
pub fn main() {
@@ -10336,6 +10351,36 @@ pub fn labelled(a a, b b) { todo }
1033610351
);
1033710352
}
1033810353

10354+
#[test]
10355+
fn add_omitted_labels_works_on_call_with_wrongly_placed_labels() {
10356+
assert_code_action!(
10357+
ADD_OMITTED_LABELS,
10358+
"
10359+
pub fn main() {
10360+
labelled(3, b: 2, 1)
10361+
}
10362+
10363+
pub fn labelled(a a, b b, c c) { todo }
10364+
",
10365+
find_position_of("labelled").to_selection(),
10366+
);
10367+
}
10368+
10369+
#[test]
10370+
fn add_omitted_labels_does_not_work_on_call_with_wrong_labels_2() {
10371+
assert_no_code_actions!(
10372+
ADD_OMITTED_LABELS,
10373+
"
10374+
pub fn main() {
10375+
labelled(3, 1, d: 2)
10376+
}
10377+
10378+
pub fn labelled(a a, b b, c c) { todo }
10379+
",
10380+
find_position_of("labelled").to_selection(),
10381+
);
10382+
}
10383+
1033910384
#[test]
1034010385
fn add_omitted_labels_does_not_pop_up_if_called_function_has_no_labels() {
1034110386
assert_no_code_actions!(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "\npub fn main() {\n labelled(3, b: 2, 1)\n}\n\npub fn labelled(a a, b b, c c) { todo }\n "
4+
---
5+
----- BEFORE ACTION
6+
7+
pub fn main() {
8+
labelled(3, b: 2, 1)
9+
10+
}
11+
12+
pub fn labelled(a a, b b, c c) { todo }
13+
14+
15+
16+
----- AFTER ACTION
17+
18+
pub fn main() {
19+
labelled(a: 3, b: 2, c: 1)
20+
}
21+
22+
pub fn labelled(a a, b b, c c) { todo }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "\npub fn main() {\n let a = 1\n labelled(a, b: 2)\n}\n\npub fn labelled(a a, b b) { todo }\n "
4+
---
5+
----- BEFORE ACTION
6+
7+
pub fn main() {
8+
let a = 1
9+
labelled(a, b: 2)
10+
11+
}
12+
13+
pub fn labelled(a a, b b) { todo }
14+
15+
16+
17+
----- AFTER ACTION
18+
19+
pub fn main() {
20+
let a = 1
21+
labelled(a:, b: 2)
22+
}
23+
24+
pub fn labelled(a a, b b) { todo }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "\npub fn main() {\n labelled(3, 1, b: 2)\n}\n\npub fn labelled(a a, b b, c c) { todo }\n "
4+
---
5+
----- BEFORE ACTION
6+
7+
pub fn main() {
8+
labelled(3, 1, b: 2)
9+
10+
}
11+
12+
pub fn labelled(a a, b b, c c) { todo }
13+
14+
15+
16+
----- AFTER ACTION
17+
18+
pub fn main() {
19+
labelled(a: 3, c: 1, b: 2)
20+
}
21+
22+
pub fn labelled(a a, b b, c c) { todo }

0 commit comments

Comments
 (0)