Skip to content

Commit 2d3ef4b

Browse files
GearsDatapackslpil
authored andcommitted
Extract code within blocks without braces
1 parent b8e634c commit 2d3ef4b

4 files changed

+53
-10
lines changed

compiler-core/src/language_server/code_action.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8524,7 +8524,56 @@ impl<'a> ExtractFunction<'a> {
85248524
parameters: Vec<(EcoString, Arc<Type>)>,
85258525
function_end: u32,
85268526
) {
8527-
let expression_code = code_at(self.module, expression.location());
8527+
// If we extract a block, it isn't very helpful to have the body of the
8528+
// extracted function just be a single block expression, so instead we
8529+
// extract the statements inside the block. For example, the following
8530+
// code:
8531+
//
8532+
// ```gleam
8533+
// pub fn main() {
8534+
// let x = {
8535+
// // ^ Select from here
8536+
// let a = 1
8537+
// let b = 2
8538+
// a + b
8539+
// }
8540+
// //^ Until here
8541+
// x
8542+
// }
8543+
// ```
8544+
//
8545+
// Would produce the following extracted function:
8546+
//
8547+
// ```gleam
8548+
// fn function() {
8549+
// let a = 1
8550+
// let b = 2
8551+
// a + b
8552+
// }
8553+
// ```
8554+
//
8555+
// Rather than:
8556+
//
8557+
// ```gleam
8558+
// fn function() {
8559+
// {
8560+
// let a = 1
8561+
// let b = 2
8562+
// a + b
8563+
// }
8564+
// }
8565+
// ```
8566+
//
8567+
let extracted_code_location = if let TypedExpr::Block { statements, .. } = expression {
8568+
SrcSpan::new(
8569+
statements.first().location().start,
8570+
statements.last().location().end,
8571+
)
8572+
} else {
8573+
expression.location()
8574+
};
8575+
8576+
let expression_code = code_at(self.module, extracted_code_location);
85288577

85298578
let name = self.function_name();
85308579
let arguments = parameters.iter().map(|(name, _)| name).join(", ");

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__action__extract_function.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ pub fn do_things(a, b) {
2727
}
2828

2929
fn function(a: Int, b: Int) -> Int {
30-
{
31-
let a = 10 + a
30+
let a = 10 + a
3231
let b = 10 + b
3332
a * b
34-
}
3533
}

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__action__extract_function_when_multiple_names_already_in_scope.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ pub fn do_things(a, b) {
3737
}
3838

3939
fn function_5(a: Int, b: Int) -> Int {
40-
{
41-
let a = 10 + a
40+
let a = 10 + a
4241
let b = 10 + b
4342
a * b
44-
}
4543
}

compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__action__extract_function_when_name_already_in_scope.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ pub fn do_things(a, b) {
3131
}
3232

3333
fn function_2(a: Int, b: Int) -> Int {
34-
{
35-
let a = 10 + a
34+
let a = 10 + a
3635
let b = 10 + b
3736
a * b
38-
}
3937
}

0 commit comments

Comments
 (0)