this repo has no description

Extract code within blocks without braces

authored by gearsco.de and committed by

Louis Pilfold 2d3ef4b3 b8e634cb

+53 -10
+50 -1
compiler-core/src/language_server/code_action.rs
··· 8524 8524 parameters: Vec<(EcoString, Arc<Type>)>, 8525 8525 function_end: u32, 8526 8526 ) { 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); 8528 8577 8529 8578 let name = self.function_name(); 8530 8579 let arguments = parameters.iter().map(|(name, _)| name).join(", ");
+1 -3
compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__action__extract_function.snap
··· 27 27 } 28 28 29 29 fn function(a: Int, b: Int) -> Int { 30 - { 31 - let a = 10 + a 30 + let a = 10 + a 32 31 let b = 10 + b 33 32 a * b 34 - } 35 33 }
+1 -3
compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__action__extract_function_when_multiple_names_already_in_scope.snap
··· 37 37 } 38 38 39 39 fn function_5(a: Int, b: Int) -> Int { 40 - { 41 - let a = 10 + a 40 + let a = 10 + a 42 41 let b = 10 + b 43 42 a * b 44 - } 45 43 }
+1 -3
compiler-core/src/language_server/tests/snapshots/gleam_core__language_server__tests__action__extract_function_when_name_already_in_scope.snap
··· 31 31 } 32 32 33 33 fn function_2(a: Int, b: Int) -> Int { 34 - { 35 - let a = 10 + a 34 + let a = 10 + a 36 35 let b = 10 + b 37 36 a * b 38 - } 39 37 }