Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
keyword of a variable to inline.
([Giacomo Cavalieri](https://github.com/giacomocavalieri))

- The "add omitted labels" code action can now be used in function calls where
some of the labels have been provided already.
([Giacomo Cavalieri](https://github.com/giacomocavalieri))

- The "pattern match on variable" code action can now pick better names when
used on tuples.
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
Expand Down
16 changes: 12 additions & 4 deletions compiler-core/src/language_server/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8562,10 +8562,18 @@ impl<'ast> ast::visit::Visit<'ast> for AddOmittedLabels<'ast> {

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

let label = argument_index_to_label
Expand Down
49 changes: 47 additions & 2 deletions compiler-core/src/language_server/tests/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10321,8 +10321,23 @@ pub type Labelled {
}

#[test]
fn add_omitted_labels_does_not_pop_up_if_function_already_has_labels() {
assert_no_code_actions!(
fn add_omitted_labels_works_with_constructors_calls_with_some_labels_1() {
assert_code_action!(
ADD_OMITTED_LABELS,
"
pub fn main() {
labelled(3, 1, b: 2)
}

pub fn labelled(a a, b b, c c) { todo }
",
find_position_of("labelled").to_selection(),
);
}

#[test]
fn add_omitted_labels_works_with_constructors_calls_with_some_labels() {
assert_code_action!(
ADD_OMITTED_LABELS,
"
pub fn main() {
Expand All @@ -10336,6 +10351,36 @@ pub fn labelled(a a, b b) { todo }
);
}

#[test]
fn add_omitted_labels_works_on_call_with_wrongly_placed_labels() {
assert_code_action!(
ADD_OMITTED_LABELS,
"
pub fn main() {
labelled(3, b: 2, 1)
}

pub fn labelled(a a, b b, c c) { todo }
",
find_position_of("labelled").to_selection(),
);
}

#[test]
fn add_omitted_labels_does_not_work_on_call_with_wrong_labels_2() {
assert_no_code_actions!(
ADD_OMITTED_LABELS,
"
pub fn main() {
labelled(3, 1, d: 2)
}

pub fn labelled(a a, b b, c c) { todo }
",
find_position_of("labelled").to_selection(),
);
}

#[test]
fn add_omitted_labels_does_not_pop_up_if_called_function_has_no_labels() {
assert_no_code_actions!(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: compiler-core/src/language_server/tests/action.rs
expression: "\npub fn main() {\n labelled(3, b: 2, 1)\n}\n\npub fn labelled(a a, b b, c c) { todo }\n "
---
----- BEFORE ACTION

pub fn main() {
labelled(3, b: 2, 1)
}

pub fn labelled(a a, b b, c c) { todo }



----- AFTER ACTION

pub fn main() {
labelled(a: 3, b: 2, c: 1)
}

pub fn labelled(a a, b b, c c) { todo }
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: compiler-core/src/language_server/tests/action.rs
expression: "\npub fn main() {\n let a = 1\n labelled(a, b: 2)\n}\n\npub fn labelled(a a, b b) { todo }\n "
---
----- BEFORE ACTION

pub fn main() {
let a = 1
labelled(a, b: 2)
}

pub fn labelled(a a, b b) { todo }



----- AFTER ACTION

pub fn main() {
let a = 1
labelled(a:, b: 2)
}

pub fn labelled(a a, b b) { todo }
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: compiler-core/src/language_server/tests/action.rs
expression: "\npub fn main() {\n labelled(3, 1, b: 2)\n}\n\npub fn labelled(a a, b b, c c) { todo }\n "
---
----- BEFORE ACTION

pub fn main() {
labelled(3, 1, b: 2)
}

pub fn labelled(a a, b b, c c) { todo }



----- AFTER ACTION

pub fn main() {
labelled(a: 3, c: 1, b: 2)
}

pub fn labelled(a a, b b, c c) { todo }

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
source: compiler-core/src/language_server/tests/action.rs
assertion_line: 10596
expression: "\npub fn main() {\n case 1 {\n _ -> {\n use <- wibble\n 123\n }\n _ -> todo\n }\n}\n\nfn wibble(f: fn() -> Float) -> Float { f() }\n"
snapshot_kind: text
---
----- BEFORE ACTION

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
source: compiler-core/src/language_server/tests/action.rs
assertion_line: 10619
expression: "\npub fn main() {\n case 1 {\n _ -> {\n use <- wibble\n use <- wibble\n 123\n }\n _ -> todo\n }\n}\n\nfn wibble(f: fn() -> Float) -> Float { f() }\n"
snapshot_kind: text
---
----- BEFORE ACTION

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
source: compiler-core/src/language_server/tests/action.rs
assertion_line: 2004
expression: "\npub fn main() {\n let assert Ok(Wibble ()) = Wibble(1, \"2\")\n}\n\npub type Wibble { Wibble(arg1: Int, arg2: String) }\n "
snapshot_kind: text
---
----- BEFORE ACTION

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
source: compiler-core/src/language_server/tests/action.rs
assertion_line: 10007
expression: "pub fn main(x) {\n case x {\n Ok(n) -> 1\n Ok(_) -> 2\n Error(_) -> todo\n Ok(1) -> 3\n }\n}\n"
snapshot_kind: text
---
----- BEFORE ACTION
pub fn main(x) {
Expand Down
Loading