Skip to content

Commit d15c0c3

Browse files
giacomocavalierilpil
authored andcommitted
trigger the inline variable code action when over the let keyword as well
1 parent e23d5ef commit d15c0c3

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

compiler-core/src/language_server/code_action.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6226,6 +6226,31 @@ impl<'a> InlineVariable<'a> {
62266226
}
62276227

62286228
impl<'ast> ast::visit::Visit<'ast> for InlineVariable<'ast> {
6229+
fn visit_typed_assignment(&mut self, assignment: &'ast TypedAssignment) {
6230+
let TypedPattern::Variable { location, name, .. } = &assignment.pattern else {
6231+
ast::visit::visit_typed_assignment(self, assignment);
6232+
return;
6233+
};
6234+
6235+
// We special case assignment variables because we want to trigger the
6236+
// code action also if we're over the let keyword:
6237+
//
6238+
// ```gleam
6239+
// let wibble = 11
6240+
// // ^^^^^^^^^^ Here!
6241+
// ```
6242+
//
6243+
let assignment_range = self
6244+
.edits
6245+
.src_span_to_lsp_range(SrcSpan::new(assignment.location.start, location.end));
6246+
if !within(self.params.range, assignment_range) {
6247+
ast::visit::visit_typed_assignment(self, assignment);
6248+
return;
6249+
}
6250+
6251+
self.maybe_inline(*location, name.clone());
6252+
}
6253+
62296254
fn visit_typed_expr_var(
62306255
&mut self,
62316256
location: &'ast SrcSpan,

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8321,6 +8321,20 @@ pub fn main(x) {
83218321
);
83228322
}
83238323

8324+
#[test]
8325+
fn inline_variable_when_over_let_keyword() {
8326+
assert_code_action!(
8327+
INLINE_VARIABLE,
8328+
r#"
8329+
pub fn main() {
8330+
let x = 123
8331+
x + 1
8332+
}
8333+
"#,
8334+
find_position_of("let").to_selection()
8335+
);
8336+
}
8337+
83248338
#[test]
83258339
fn no_code_action_to_inline_variable_used_multiple_times() {
83268340
let src = r#"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "\npub fn main() {\n let x = 123\n x + 1\n}\n"
4+
---
5+
----- BEFORE ACTION
6+
7+
pub fn main() {
8+
let x = 123
9+
10+
x + 1
11+
}
12+
13+
14+
----- AFTER ACTION
15+
16+
pub fn main() {
17+
123 + 1
18+
}

0 commit comments

Comments
 (0)