Skip to content

Commit ff80553

Browse files
giacomocavalierilpil
authored andcommitted
fix another bug
1 parent 50cd622 commit ff80553

5 files changed

+185
-5
lines changed

compiler-core/src/language_server/code_action.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8623,10 +8623,16 @@ impl<'a> ExtractFunction<'a> {
86238623
statements,
86248624
location: full_location,
86258625
}) => {
8626-
let location = SrcSpan::new(
8627-
statements.first().location().start,
8628-
statements.last().location().end,
8629-
);
8626+
let location = statements
8627+
.first()
8628+
.location()
8629+
.merge(&match &statements.last() {
8630+
ast::Statement::Expression(expression) => expression.location(),
8631+
ast::Statement::Assignment(assignment) => assignment.location,
8632+
ast::Statement::Assert(assert) => assert.location,
8633+
ast::Statement::Use(use_) => use_.call.location(),
8634+
});
8635+
86308636
self.extract_code_in_tail_position(
86318637
*full_location,
86328638
location,
@@ -8966,7 +8972,12 @@ impl<'ast> ast::visit::Visit<'ast> for ExtractFunction<'ast> {
89668972
statements: &'ast [TypedStatement],
89678973
) {
89688974
let last_statement_location = self.last_statement_location;
8969-
self.last_statement_location = statements.last().map(|last| last.location());
8975+
self.last_statement_location = statements.last().map(|last| match last {
8976+
ast::Statement::Expression(_)
8977+
| ast::Statement::Assignment(_)
8978+
| ast::Statement::Assert(_) => last.location(),
8979+
ast::Statement::Use(use_) => last.location().merge(&use_.call.location()),
8980+
});
89708981

89718982
ast::visit::visit_typed_expr_block(self, location, statements);
89728983

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10590,3 +10590,50 @@ fn wobble(f: fn() -> Int) -> Float { 1.1 }
1059010590
.select_until(find_position_of("wobble"))
1059110591
);
1059210592
}
10593+
10594+
#[test]
10595+
fn extract_block_tail_position_3() {
10596+
assert_code_action!(
10597+
EXTRACT_FUNCTION,
10598+
r#"
10599+
pub fn main() {
10600+
case 1 {
10601+
_ -> {
10602+
use <- wibble
10603+
123
10604+
}
10605+
_ -> todo
10606+
}
10607+
}
10608+
10609+
fn wibble(f: fn() -> Float) -> Float { f() }
10610+
"#,
10611+
find_position_of("{")
10612+
.nth_occurrence(3)
10613+
.select_until(find_position_of("}"))
10614+
);
10615+
}
10616+
10617+
#[test]
10618+
fn extract_block_tail_position_4() {
10619+
assert_code_action!(
10620+
EXTRACT_FUNCTION,
10621+
r#"
10622+
pub fn main() {
10623+
case 1 {
10624+
_ -> {
10625+
use <- wibble
10626+
use <- wibble
10627+
123
10628+
}
10629+
_ -> todo
10630+
}
10631+
}
10632+
10633+
fn wibble(f: fn() -> Float) -> Float { f() }
10634+
"#,
10635+
find_position_of("{")
10636+
.nth_occurrence(3)
10637+
.select_until(find_position_of("}"))
10638+
);
10639+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
assertion_line: 10596
4+
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"
5+
snapshot_kind: text
6+
---
7+
----- BEFORE ACTION
8+
9+
pub fn main() {
10+
case 1 {
11+
_ -> {
12+
13+
use <- wibble
14+
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
15+
123
16+
▔▔▔▔▔▔▔▔▔
17+
}
18+
▔▔▔▔↑
19+
_ -> todo
20+
}
21+
}
22+
23+
fn wibble(f: fn() -> Float) -> Float { f() }
24+
25+
26+
----- AFTER ACTION
27+
28+
pub fn main() {
29+
case 1 {
30+
_ -> function()
31+
_ -> todo
32+
}
33+
}
34+
35+
fn function() -> Float {
36+
use <- wibble
37+
123
38+
}
39+
40+
fn wibble(f: fn() -> Float) -> Float { f() }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
assertion_line: 10619
4+
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"
5+
snapshot_kind: text
6+
---
7+
----- BEFORE ACTION
8+
9+
pub fn main() {
10+
case 1 {
11+
_ -> {
12+
13+
use <- wibble
14+
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
15+
use <- wibble
16+
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
17+
123
18+
▔▔▔▔▔▔▔▔▔
19+
}
20+
▔▔▔▔↑
21+
_ -> todo
22+
}
23+
}
24+
25+
fn wibble(f: fn() -> Float) -> Float { f() }
26+
27+
28+
----- AFTER ACTION
29+
30+
pub fn main() {
31+
case 1 {
32+
_ -> function()
33+
_ -> todo
34+
}
35+
}
36+
37+
fn function() -> Float {
38+
use <- wibble
39+
use <- wibble
40+
123
41+
}
42+
43+
fn wibble(f: fn() -> Float) -> Float { f() }
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
assertion_line: 10596
4+
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"
5+
snapshot_kind: text
6+
---
7+
----- BEFORE ACTION
8+
9+
pub fn main() {
10+
case 1 {
11+
_ -> {
12+
13+
use <- wibble
14+
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
15+
123
16+
▔▔▔▔▔▔▔▔▔
17+
}
18+
▔▔▔▔↑
19+
_ -> todo
20+
}
21+
}
22+
23+
fn wibble(f: fn() -> Float) -> Float { f() }
24+
25+
26+
----- AFTER ACTION
27+
28+
pub fn main() {
29+
case 1 {
30+
_ -> function()
31+
_ -> todo
32+
}
33+
}
34+
35+
fn function() -> Float {
36+
use <- wibble
37+
}
38+
39+
fn wibble(f: fn() -> Float) -> Float { f() }

0 commit comments

Comments
 (0)