ISSUE DESCRIPTION Extract function extracts too much When triggering Extract function…
- User
ISSUE DESCRIPTION
Extract function extracts too much When triggering
Extract functionwith the selection:/// prepare an empty gleam project in the `target_path` /// fn prepare_project_structure( target_path target: TargetPath, user_id user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) { // get the priv dir use priv <- result.try( application.priv_directory("gleeps") |> result.replace_error(FailedToFindPrivDirectory), ) // copy the base project to the target_path use _ <- result.try( simplifile.copy_directory(filepath.join(priv, "base_project"), target.path) |> result.replace_error(FailedToCopyBaseProject), ) // replace $PROJECT_NAME placeholder in the gleam.toml // <-------------------------------------------------------------- selected from here let toml_path = target.path |> filepath.join("gleam.toml") use gleam_toml <- result.try( toml_path |> simplifile.read |> result.replace_error(FailedToReadGleamToml), ) let gleam_toml = gleam_toml |> string.replace("$PROJECT_NAME", with: user_id_to_string(user_id)) simplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml) // <---------------------------------------------------------------- to here }It will extract the entire function content into a new function leaving:
fn prepare_project_structure( target_path target: TargetPath, user_id user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) { function(target, user_id) }instead of the expected:
/// prepare an empty gleam project in the `target_path` /// fn prepare_project_structure( target_path target: TargetPath, user_id user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) { // get the priv dir use priv <- result.try( application.priv_directory("gleeps") |> result.replace_error(FailedToFindPrivDirectory), ) // copy the base project to the target_path use _ <- result.try( simplifile.copy_directory(filepath.join(priv, "base_project"), target.path) |> result.replace_error(FailedToCopyBaseProject), ) // replace $PROJECT_NAME placeholder in the gleam.toml function(target, user_id) } fn function( target: TargetPath, user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) { let toml_path = target.path |> filepath.join("gleam.toml") use gleam_toml <- result.try( toml_path |> simplifile.read |> result.replace_error(FailedToReadGleamToml), ) let gleam_toml = gleam_toml |> string.replace("$PROJECT_NAME", with: user_id_to_string(user_id)) simplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml) }Using gleam
1.16.0in neovim nightlyInterface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /gleam, current file: none) bash-$
Assistant
I'll start by exploring the repository structure to understand the codebase.
Assistant
This is the Gleam compiler repository. The issue is about the Language Server's "Extract function" feature extracting too much code. Let me explore the language server directory.
Assistant
Let me look at the ExtractFunction implementation in detail.
Assistant
Now let me look at the visit functions for the ExtractFunction to understand how it identifies the selected range.
Assistant
Let me look at the
can_extract_statementmethod more carefully:Assistant
Now I need to understand the helpers like
within,completely_within, andoverlaps. Let me search for these.Assistant
Now let me understand the issue. Let me look at the existing tests for ExtractFunction to understand expected behavior:
Assistant
Now let me understand the issue better. Let me trace through the logic:
Looking at the example:
fn prepare_project_structure(...) -> Result(Nil, SubmitError) { // get the priv dir use priv <- result.try(...) // copy the base project to the target_path use _ <- result.try(...) // replace $PROJECT_NAME placeholder in the gleam.toml // <----------- selected from here let toml_path = ... // SELECTED use gleam_toml <- result.try(...) // SELECTED let gleam_toml = ... // SELECTED simplifile.write(toml_path, gleam_toml) ... // SELECTED // <--------- to here }The expected behavior is to only extract the selected statements. But the current implementation extracts the entire function body (everything from the first
useto the last statement).Let me trace through:
visit_typed_functionis called - setsfunction_end_positionandlast_statement_locationvisit_typed_expr_blockis called - which updateslast_statement_locationto last statement- For each statement,
visit_typed_statementis called. If the statement can be extracted, it gets added.
The issue is: when iterating statements, if one is selected, we add it. But then if there's another selected later, we extend the location. The issue may be in how
visit_typed_expris called for the block, where the block expression might be marked as the whole extracted range.Let me look at the issue more carefully. Let me first check the visit flow when visiting blocks.
Assistant
Let me understand the AST visit:
Assistant
OK so I see the issue now. Let me trace through the example:
fn prepare_project_structure(...) -> Result(Nil, SubmitError) { // get the priv dir use priv <- result.try(...) // Statement 1: Use // copy the base project to the target_path use _ <- result.try(...) // Statement 2: Use // replace $PROJECT_NAME placeholder in the gleam.toml // <-- selected from here let toml_path = ... // Statement 3: Assignment (selected) use gleam_toml <- result.try(...) // Statement 4: Use (selected) let gleam_toml = ... // Statement 5: Assignment (selected) simplifile.write(toml_path, gleam_toml) // Statement 6: Expression (selected) // <-- to here }The body of the function is a block expression. The last statement's location is
simplifile.write(...). Whenvisit_typed_expr_blockis called, it updatesself.last_statement_location = statements.last().map(|last| last.location()), so it's the last statement.Then
visit_typed_statementis called for each statement. Now let's trace:Statement 1:
use priv <- result.try(...)- this overlaps with selection? NO, this is outside the selection. Statement 2:use _ <- result.try(...)- this overlaps with selection? NO, outside selection. Statement 3:let toml_path = ...- this overlaps with selection? YES.can_extract_statementreturnstruebecause of the assignment.- Sets
function= Some(Statements { location: stmt3.location, position: NotTail }) Statement 4:use gleam_toml <- result.try(...)- overlaps? YES. can_extract_statementreturns true.- Now we hit the
Some(ExtractedFunction { value: ExtractedValue::Statements { location, position: extracted_position }, .. })branch, so we merge:*location = location.merge(&statement_location);*extracted_position = position;
Wait.
positionis determined at this point based on whether the current statement is in tail position (i.e., is the last statement). But statement 4 is not in tail position because the last statement is statement 6. Sopositionwould be NotTail. Then*extracted_position = positionwhich is NotTail. OK, that's correct.But wait! Let's look at this again. Actually, looking at the visitor flow:
- The block visitor first calls
ast::visit::visit_typed_expr_block(self, location, statements);which iterates over each statement. - But before iterating, the
last_statement_locationis updated to the last statement.
Looking at this again:
fn visit_typed_expr_block( &mut self, location: &'ast SrcSpan, statements: &'ast [TypedStatement], ) { let last_statement_location = self.last_statement_location; self.last_statement_location = statements.last().map(|last| last.location()); ast::visit::visit_typed_expr_block(self, location, statements); self.last_statement_location = last_statement_location; }OK so
last_statement_locationis the location of the last statement in the block.Let me trace again more carefully. The
last_statement_locationis set to the last statement's location. Whenvisit_typed_statementis called for statement 3, theis_in_tail_positioncheck is:let is_in_tail_position = self.last_statement_location .is_some_and(|last_statement_location| { last_statement_location == statement_location });This is false because statement 3 is not the last statement. So position = NotTail. Good.
When
visit_typed_statementis called for statement 4 (use), the same check applies.is_in_tail_positionis false, so position = NotTail. Good.Then for statement 5 (let), position = NotTail.
For statement 6, position = Tail (because it's the last statement).
Let me look at the merge code again carefully:
Some(ExtractedFunction { value: ExtractedValue::Statements { location, position: extracted_position, }, .. }) => { *location = location.merge(&statement_location); *extracted_position = position; }When statement 4 (use) comes in, we update
*extracted_position = NotTail. Then when statement 6 (Tail) comes in, we update*extracted_position = Tail. So we end up with the right position.Wait. So what's the issue? Let me actually create a test case to reproduce.
Actually wait. Let me re-read the bug. The issue is that the user selects from the comment line above
let toml_path = ...to the last statement. So the selection starts at the comment, but does NOT include the comment text since it's just position.Let me check if the issue might be related to how
visit_typed_expris being called. Look at:fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { // If we have already determined what code we want to extract, we don't // want to extract this instead. if self.function.is_none() { // If this expression is fully selected, we mark it as being extracted. if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } ... }This is where the problem may be. The block expression itself has a location that is the entire block. If the user's selection covers the entire block (including the
use priv <- ...statements that are not selected), thencan_extract_expressionmight return true.Wait. Let me look at what
completely_withinreturns. The check is!completely_within(selected_range, expression_range). If the selected_range is completely within the expression range, this returns false, meaning we don't extract.In the issue:
- selected_range covers statements 3-6
- expression_range (block) covers the entire function body, statements 1-6
So
selected_rangeis NOT completely withinexpression_range(since selected_range doesn't include statement 1 and 2). Therefore!completely_within(...)returns true. And then the entire block would be marked for extraction.Yes, this is the bug! Let me verify.
Actually wait, let me re-read the issue more carefully. The selection starts with the comment
// replace $PROJECT_NAME placeholder in the gleam.tomland includeslet toml_path = ...throughsimplifile.write(toml_path, gleam_toml) ....The
visit_typed_functionis called first:fn visit_typed_function(&mut self, function: &'ast TypedFunction) { let range = self.edits.src_span_to_lsp_range(function.full_location()); if within(self.params.range, range) { self.function_end_position = Some(function.end_position); self.last_statement_location = function.body.last().map(|last| last.location()); ast::visit::visit_typed_function(self, function); } }Then
ast::visit::visit_typed_functionprobably callsvisit_typed_expr_fn(which is the body). Then we enter the function body which is a block.Then
visit_typed_expr_blockis called:fn visit_typed_expr_block( &mut self, location: &'ast SrcSpan, statements: &'ast [TypedStatement], ) { let last_statement_location = self.last_statement_location; self.last_statement_location = statements.last().map(|last| last.location()); ast::visit::visit_typed_expr_block(self, location, statements); self.last_statement_location = last_statement_location; }The
statements.last().map(|last| last.location())is the last statement.But this is the block visit! Before this is called,
visit_typed_expris called (which has the bug):The block expression is the function body, so the block's location is the entire function body. When
visit_typed_expris called for this block expression, it checks ifcan_extract_expressionreturns true. The block expression's range covers all 6 statements. The selection covers only 4 statements (3-6). Socompletely_within(selected_range, expression_range)would be false. So!completely_withinis true. Socan_extract_expressionreturns true. The block is then marked as the whole extracted function.But wait, the user actually wanted to extract just 4 statements.
Let me check the order. When we call
visit_typed_function, it callsvisit_typed_expr_fn, which visits the body (statements). Wait, actually no - the body of a function is a Vec1, so it visits each statement. Let me check this.Assistant
Let me look at how visit_typed_function traverses into the body:
Assistant
OK, so
visit_typed_functioniterates over statements directly (not as a block expression). It just visits each statement. So there's novisit_typed_exprbeing called for the body of the function itself.Now let me trace through. Each statement in
fn prepare_project_structurebody is:use priv <- result.try(...)use _ <- result.try(...)let toml_path = ...use gleam_toml <- result.try(...)let gleam_toml = ...simplifile.write(...)(Expression statement)
For each statement,
visit_typed_statementis called. Let's see:Statement 1 (use priv):
can_extract_statementreturnsfalsebecause it doesn't overlap with selection.- We fall through to
ast::visit::visit_typed_statement(self, statement).
Statement 2 (use _):
can_extract_statementreturnsfalse(no overlap).- We fall through to
ast::visit::visit_typed_statement(self, statement).
Statement 3 (let toml_path = ...):
can_extract_statementreturnstrue(overlap exists, value not within selection, not completely within).is_in_tail_positionis false (not the last statement).position= NotTail.match &mut self.function { None => ... }branch:self.function = Some(ExtractedFunction::new(ExtractedValue::Statements { location: statement_location, position })).- Then we visit_typed_statement(self, statement).
Statement 4 (use gleam_toml):
can_extract_statementreturnstrue(overlap exists, not completely within).is_in_tail_positionis false (not last statement).position= NotTail.match &mut self.function { Some(ExtractedFunction { value: ExtractedValue::Statements { location, position: extracted_position }, .. }) => { *location = location.merge(&statement_location); *extracted_position = position; } }
So now the function.value is Statements { location: stmt3.location merged with stmt4.location, position: NotTail }.
Statement 5 (let gleam_toml):
- Similar to 4. The location extends.
Statement 6 (simplifile.write(...)):
can_extract_statementreturnstrue(it's an Expression statement, so it calls can_extract_expression).- Wait, let me check this. For Expression statements:
ast::Statement::Expression(expression) => self.can_extract_expression(expression), - In
can_extract_expression, it callsoverlapswhich returns true if any part overlaps. The expression is fully within the selection. So!completely_within(selected_range, expression_range)should be false (because it IS completely within). - Hmm wait. The expression
simplifile.write(toml_path, gleam_toml) |> result.replace_error(...)is a Pipeline expression. Let me check.
Actually wait. Let me check the pipeline case:
TypedExpr::Pipeline { first_value, finally, .. } => { // We can extract a pipeline as a whole only if the selection // spans all of its steps! let first_step = self.edits.src_span_to_lsp_range(first_value.location); let last_step = self.edits.src_span_to_lsp_range(finally.location()); position_within(selected_range.start, first_step) && position_within(selected_range.end, last_step) }For a pipeline, we need the selection to span from the first step to the last step. So if the entire pipeline is selected, then we can extract it.
For statement 6, the pipeline is the entire statement. The selection ends at the end of statement 6 (or just before). So this might pass.
Then we'd hit:
Some(ExtractedFunction { value: ExtractedValue::Statements { location, position: extracted_position, }, .. }) => { *location = location.merge(&statement_location); *extracted_position = position; }So the location extends to include statement 6.
Now the extracted function should be Statements with location from stmt3 start to stmt6 end, and position = Tail (because statement 6 is the last one).
So the expected behavior should be just stmt3-stmt6. But the bug says the entire function is extracted!
Let me re-read the bug. The bug says: "It will extract the entire function content into a new function". So that means
self.functionis being set to something that includes all 6 statements, not just 3-6.Hmm. Let me look at
last_statement_locationmore carefully:fn visit_typed_function(&mut self, function: &'ast TypedFunction) { let range = self.edits.src_span_to_lsp_range(function.full_location()); if within(self.params.range, range) { self.function_end_position = Some(function.end_position); self.last_statement_location = function.body.last().map(|last| last.location()); ast::visit::visit_typed_function(self, function); } }Wait,
self.last_statement_location = function.body.last().map(|last| last.location());- this sets it to the last statement of the function (statement 6).But then for
usestatements, the last statement of theuseblock is different. Let me check ifusestatements have a special body...Actually, looking at the code in
can_extract_statement:ast::Statement::Use(_) | ast::Statement::Assert(_) => { !completely_within(selected_range, statement_range) }So a
usestatement's "location" includes the body. Let me check what location ausestatement has.Actually, I think the issue might be elsewhere. Let me look at when
visit_typed_expris called for the body. The body of a function is aVec1<TypedStatement>, not an expression. So novisit_typed_expris called for it.Wait but then there's the visit_typed_expr_block for blocks. The function body might be wrapped in a block. Let me check.
Assistant
So a
TypedFunctionhasbody: Vec<Statement<T, Expr>>. The body is a Vec of statements, not a block expression.OK, let me create a test to reproduce the bug:
Assistant
Let me look at how snapshots work:
Assistant
Let me look at the
extract_function_from_statementstest:Assistant
OK, that works correctly. Let me also check
extract_function_with_selected_pipeline_expression_in_statement:Assistant
Now let me look at the issue more closely. Let me try to write a test that reproduces the bug. First, let me run the existing tests to make sure they pass:
Assistant
Good. Let me look at the existing tests more carefully. Let me find the
extract_function_from_statementstest:Assistant
Let me see what
select_untildoes:Assistant
Now let me first try to write a reproducer. Let me create a test file with the bug:
Assistant
Now let me write a reproducer test based on the issue:
Assistant
Let me first check the current
extract_function_with_assignment_expression_and_multiple_assignmentssnapshot to understand more:Assistant
Good, that works correctly. Let me create a reproducer test based on the issue.
Assistant
Let me write a temporary test based on the issue. First let me see the test macro:
Assistant
Let me create a reproducer test. First, let me look at the structure of an existing extract_function test to understand the structure:
Assistant
Now let me write a test that reproduces the issue. The bug says when the function starts with a
usestatement (which is in tail position because of the implicit Nil), and then has more statements, the extract function grabs the entire function content.Let me make a test file:
Assistant
Let me now add this test to action.rs temporarily and run it:
Assistant
Let me write a simpler reproducer first. I want a simple test case where the bug is reproduced. Let me first write a script that can test this without modifying the existing tests.
Let me first understand the source better. Looking at this code:
Assistant
Now I need to look at
last_statement_location. Let me see what happens in the visit. The block visitor callslast_statement_location = statements.last()which sets it to the very last statement of the block.Let me look at
positionagain:let is_in_tail_position = self.last_statement_location .is_some_and(|last_statement_location| { last_statement_location == statement_location }); // A use is always eating up the entire block, if we're extracting it, // it will be in tail position there and the extracted function should // return its returned value. let position = if statement.is_use() || is_in_tail_position { StatementPosition::Tail { type_: statement.type_(), } } else { StatementPosition::NotTail };So when we encounter the very last statement (the
simplifile.write(...)call),position = Tail. So the issue is thatpositionisTailfor the last statement, even though we are extracting many statements.But that's correct - if the last selected statement is the last statement of the function, then the position is Tail.
But wait. Looking at the merge code:
Some(ExtractedFunction { value: ExtractedValue::Statements { location, position: extracted_position, }, .. }) => { *location = location.merge(&statement_location); *extracted_position = position; }For each subsequent statement, we update
extracted_position = position. So the final position is the position of the last selected statement.In the issue example, the last selected statement is the last statement of the function, so position would be
Tail. That's correct.OK so the logic seems correct for that part. Let me actually trace through the bug case more carefully.
Let me write a test reproducer and check the output:
Assistant
Let me find a good place to add the test. Let me see what's near the end of the extract function tests:
Assistant
Let me also look at
extract_function_partially_selected:Assistant
OK so that works correctly. Now let me reproduce the bug. Let me add a test case to action.rs:
Assistant
Let me create a temporary file to reproduce the bug:
Assistant
Let me add the test to the action.rs and run it to see what happens:
Assistant
Let me look at the function above extract_statements_in_tail_position:
Assistant
Let me insert my test after the
extract_statements_in_tail_positiontest (around line 12870). I'll do this temporarily, then remove it before submission.Assistant
Let me check what tests exist that look like my reproducer:
Assistant
Let me find the right place to insert:
Assistant
Let me find the next test after the closing brace:
Assistant
Let me first run the existing tests to see if they pass:
Assistant
Let me now insert the bug repro test temporarily and run it:
Assistant
Let me insert the test using sed to add my test at line 12868:
Assistant
Now let me run the test:
Assistant
Hmm, "No action with the given title". That means the EXTRACT_FUNCTION code action isn't being offered for this range. Let me check what's happening. Maybe my range is off. Let me see:
Assistant
Let me check the actual range selected by my test. Let me modify the test to print debug info first.
Actually let me check if the issue is with how
select_untilworks - maybe my range doesn't include everything I want:Assistant
OK so the start position is at "let toml_path" and the end is at the last char of "FailedToWriteGleamToml". Let me look at where the selection is in the source. Let me look at the existing test:
Assistant
Let me write a simpler reproducer that doesn't depend on so many imports:
Assistant
Let me update the test:
Assistant
Now let me run this:
Assistant
Excellent! I've reproduced the bug! Let me see the full output:
Assistant
Wait! The test is actually extracting correctly! The extraction shows
function()taking over only the lineslet toml_path,use c <- ...,let gleam_toml, andOk(gleam_toml). That's the correct behavior.But the bug says the entire function is extracted. Let me look at the bug more carefully.
Hmm, wait. Looking at the test output:
- BEFORE: selection from "let toml_path" to "Ok(gleam_toml)" (lines 7-17).
- AFTER:
- The function
mainonly containsuse a <- ...,use b <- ..., blank,function(). That looks correct! - The function
function()containslet toml_path = "gleam.toml", blank,use c <- ..., blank,let gleam_toml = "abc", blank,Ok(gleam_toml).
- The function
So my test passes correctly! Maybe the issue requires the exact case described in the bug.
Let me look at the bug more carefully. The user mentions the
// replace $PROJECT_NAME placeholder in the gleam.tomlcomment is included in the selection. Wait, actually looking at the comment more carefully:// replace $PROJECT_NAME placeholder in the gleam.toml // <-------------------------------------------------------------- selected from here let toml_path =The selection starts at the comment line
// <---. So the selection starts at the start of the comment.Wait, looking at the user's output:
// replace $PROJECT_NAME placeholder in the gleam.toml function(target, user_id) }The comment IS in the result. But the
function(target, user_id)replaces what was the selected statements. Let me re-read.Actually, looking at the BEFORE/AFTER:
BEFORE:
/// prepare an empty gleam project in the `target_path` /// fn prepare_project_structure( target_path target: TargetPath, user_id user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) { // get the priv dir use priv <- result.try( application.priv_directory("gleeps") |> result.replace_error(FailedToFindPrivDirectory), ) // copy the base project to the target_path use _ <- result.try( simplifile.copy_directory(filepath.join(priv, "base_project"), target.path) |> result.replace_error(FailedToCopyBaseProject), ) // replace $PROJECT_NAME placeholder in the gleam.toml // <-------------------------------------------------------------- selected from here let toml_path = target.path |> filepath.join("gleam.toml") use gleam_toml <- result.try( toml_path |> simplifile.read |> result.replace_error(FailedToReadGleamToml), ) let gleam_toml = gleam_toml |> string.replace("$PROJECT_NAME", with: user_id_to_string(user_id)) simplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml) // <---------------------------------------------------------------- to here }EXPECTED (after):
/// prepare an empty gleam project in the `target_path` /// fn prepare_project_structure( target_path target: TargetPath, user_id user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) { // get the priv dir use priv <- result.try( application.priv_directory("gleeps") |> result.replace_error(FailedToFindPrivDirectory), ) // copy the base project to the target_path use _ <- result.try( simplifile.copy_directory(filepath.join(priv, "base_project"), target.path) |> result.replace_error(FailedToCopyBaseProject), ) // replace $PROJECT_NAME placeholder in the gleam.toml function(target, user_id) } fn function( target: TargetPath, user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) { let toml_path = target.path |> filepath.join("gleam.toml") use gleam_toml <- result.try( toml_path |> simplifile.read |> result.replace_error(FailedToReadGleamToml), ) let gleam_toml = gleam_toml |> string.replace("$PROJECT_NAME", with: user_id_to_string(user_id)) simplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml) }ACTUAL (after - the bug):
fn prepare_project_structure( target_path target: TargetPath, user_id user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) { function(target, user_id) }So the bug is that ALL the body (including the
use priv <- ...anduse _ <- ...) is extracted, leaving justfunction(target, user_id).So my test case should be similar - the body should be entirely extracted. Let me make a test that better reproduces this:
Assistant
Let me delete the new snapshot file:
Assistant
Now let me look at the original test to understand what's happening. In my test, the result shows that
use a <- ...anduse b <- ...are kept in the original function. But the bug says ALL of the body should be extracted.Hmm, maybe my test range doesn't span the same lines. Let me check:
In the bug:
- Selection starts at the
// <--- selected from hereline (line N) - Selection ends at
// <--- to hereline (line M, where the last statement is)
So the selection spans from
// <--- selected from hereline all the way to (and including)simplifile.write(toml_path, gleam_toml) ....So the selection includes the comment line
// replace $PROJECT_NAME placeholder in the gleam.tomlas well as the actual statements.Wait. Looking at the comment:
// replace $PROJECT_NAME placeholder in the gleam.toml // <-------------------------------------------------------------- selected from hereThe selection starts at
// <--. So the selection includes the comment// replace...and the// <--line. But since the user just shows the comment for reference, I think the actual selection in the editor is from the start of the line// replace $PROJECT_NAME placeholder in the gleam.toml.So the selection starts at the beginning of the comment line
// replace $PROJECT_NAME placeholder in the gleam.tomland ends at the// <-- to hereline (or just before it).Wait, but the user shows
<---------------------------------------------------------------- to hereaftersimplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml). So the selection ends right aftersimplifile.write(...).In my test, the selection starts at
let toml_pathand ends atOk(gleam_toml). This spans the statements:let toml_path,use c <- ...,let gleam_toml,Ok(gleam_toml). So the extracted function should include these 4 statements.Looking at the snapshot output I got:
fn function() -> a { let toml_path = "gleam.toml" use c <- result.try(Ok(3)) let gleam_toml = "abc" Ok(gleam_toml) }That's exactly 4 statements extracted. So my test passes.
Hmm, so maybe the bug requires the selection to start AT the comment line above. Let me check if the comment line position matters.
Actually wait. Looking at the bug carefully:
// replace $PROJECT_NAME placeholder in the gleam.toml // <-------------------------------------------------------------- selected from hereThe selection starts AFTER the comment line
// replace.... Wait, that's just an arrow showing where the selection starts. The actual cursor position is at// <-- selected from here.Hmm, I think the user's range starts at the line beginning of
// replace $PROJECT_NAME placeholder in the gleam.toml(or perhaps at the start of// <--- selected from here). In neovim, the user's selection starts at the comment line.Let me check by trying with a different range - starting from the line before
let toml_path:Wait actually the user wrote:
// replace $PROJECT_NAME placeholder in the gleam.toml // <-------------------------------------------------------------- selected from hereI think the cursor starts at the
// replace...line. The// <--- selected from hereis just an indicator in the bug description.Hmm. But maybe the bug is that the comment line itself is included in the selection. Let me check the exact selection range. Maybe the issue is in how the visitor handles this.
Wait, let me re-read the bug output:
ACTUAL (after - the bug): fn prepare_project_structure( target_path target: TargetPath, user_id user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) { function(target, user_id) }So in the actual, the entire body is replaced with
function(target, user_id). Theuse priv <- ...anduse _ <- ...are also extracted. So the entire function body is extracted, not just the selected statements.But in my test, only the selected statements are extracted. So something is different.
Maybe the difference is the comments? Or maybe the exact start position? Let me think about this.
In the bug:
// replace $PROJECT_NAME placeholder in the gleam.toml // <-------------------------------------------------------------- selected from here let toml_path =The selection START is at the beginning of
// replace $PROJECT_NAME placeholder in the gleam.toml(or just after, but it's a comment line, so probably at the beginning).In my test, the selection START is at
let toml_path.Hmm. So the issue is that the selection might START before
let toml_path?Let me think. In neovim, when you put your cursor at the end of
// replace $PROJECT_NAME placeholder in the gleam.tomland select down, the selection starts at the END of that comment line. Or if you select withVin visual line mode, it starts at the start of that line.Actually I think the user is showing the entire selection as a visual block. The
<-- selected from hereis just an indicator. The actual selection is probably from somewhere on the// replace $PROJECT_NAME placeholder in the gleam.tomlline.Hmm, let me try another approach. Let me check what happens if the selection starts at a comment line above the
letstatement.Wait. Let me look at the original test case I created. The selection starts at
let toml_path. But what if the user uses a slightly different start point. Let me check if maybe the issue is that the selection actually starts a few chars before, like in a comment line.Actually wait, let me read the bug again carefully:
When triggering
Extract functionwith the selection:/// prepare an empty gleam project in the `target_path` /// fn prepare_project_structure( target_path target: TargetPath, user_id user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) { // get the priv dir use priv <- result.try( application.priv_directory("gleeps") |> result.replace_error(FailedToFindPrivDirectory), ) // copy the base project to the target_path use _ <- result.try( simplifile.copy_directory(filepath.join(priv, "base_project"), target.path) |> result.replace_error(FailedToCopyBaseProject), ) // replace $PROJECT_NAME placeholder in the gleam.toml // <-------------------------------------------------------------- selected from here let toml_path = target.path |> filepath.join("gleam.toml") ...The
// <-- selected from hereand// <-- to herelines indicate where the selection starts and ends. The actual code text within those markers is what's selected. So the selection is:- START: at the beginning of
let toml_path =line - END: at the end of
|> result.replace_error(FailedToWriteGleamToml)line
Wait but that wouldn't trigger the bug either. Hmm.
Actually, looking at the actual code in the issue:
- The
<-- selected from hereappears BEFORE the comment// replace $PROJECT_NAME placeholder in the gleam.toml. So the selection might start at the comment line.
Let me look at this differently. The user might have meant to indicate that the selection starts at the line of
// replace $PROJECT_NAME placeholder in the gleam.toml(the comment line) and ends at the end of the last statement.Actually, looking more carefully, I see the markers:
// <-------------------------------------------------------------- selected from hereappears AFTER
// replace $PROJECT_NAME placeholder in the gleam.toml. So the actual code start of the selection is at the// replace...comment line.Wait wait, the
<-- selected from hereis part of a comment line itself, it's just a visual indicator in the bug report. The actual code lines without the<--would be:// replace $PROJECT_NAME placeholder in the gleam.toml(this is a comment in the original code)let toml_path =
If the user's selection starts at the BEGINNING of
// replace $PROJECT_NAME placeholder in the gleam.toml, then it would span from that comment line to the end ofsimplifile.write(...) ....So the selection range would be:
- START: position 0 on the
// replace $PROJECT_NAME placeholder in the gleam.tomlline - END: end of
|> result.replace_error(FailedToWriteGleamToml)line
But the comment
// replace $PROJECT_NAME placeholder in the gleam.tomlis just a comment line. Does the extract function know about comments?Hmm. Actually, let me look at this from a different angle. The issue might be with how the
last_statement_locationis determined. Let me re-read the visitor:fn visit_typed_function(&mut self, function: &'ast TypedFunction) { let range = self.edits.src_span_to_lsp_range(function.full_location()); if within(self.params.range, range) { self.function_end_position = Some(function.end_position); self.last_statement_location = function.body.last().map(|last| last.location()); ast::visit::visit_typed_function(self, function); } }So when we enter the function,
last_statement_locationis set to the last statement. Then we visit each statement.OK let me look at the test case again. In my test:
pub fn main() { use a <- result.try(Ok(1)) // Statement 1 use b <- result.try(Ok(2)) // Statement 2 let toml_path = "gleam.toml" // Statement 3 [selected] use c <- result.try(Ok(3)) // Statement 4 [selected] let gleam_toml = "abc" // Statement 5 [selected] Ok(gleam_toml) // Statement 6 [selected] }Selection:
let toml_path(statement 3 start) to end ofOk(gleam_toml)(statement 6 end).My test result showed that only statements 3-6 were extracted. So the test works correctly!
But the bug says ALL statements are extracted. So something is different.
Let me think... What's different between my test and the bug?
In the bug:
- The function starts with 2
usestatements (statements 1 and 2) - Then selected statements (3-6)
In my test:
- Same structure
What's different? Hmm. Let me try with the exact same code as in the bug.
Actually wait, maybe the difference is in the LAST statement. Let me see:
- Bug: last statement is
simplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml)(a pipeline expression with result typeResult(Nil, SubmitError)) - My test: last statement is
Ok(gleam_toml)(a Call expression)
Hmm, in both cases, the last statement is some kind of expression.
Wait, let me think about this differently. Maybe the issue is the
last_statement_locationbeing set wrong.Let me look at the actual case. Looking at the visit logic:
fn visit_typed_function(&mut self, function: &'ast TypedFunction) { let range = self.edits.src_span_to_lsp_range(function.full_location()); if within(self.params.range, range) { self.function_end_position = Some(function.end_position); self.last_statement_location = function.body.last().map(|last| last.location()); ast::visit::visit_typed_function(self, function); } }After visiting statements, we visit_typed_function which iterates over each statement.
OK let me see what happens in the visit order for a function with multiple statements. Let me check if there's a
visit_typed_expr_blockcall somewhere.Wait, looking at the AST for TypedFunction:
pub struct Function<T, Expr> { pub location: SrcSpan, pub body_start: Option<u32>, pub end_position: u32, pub name: Option<SpannedString>, pub arguments: Vec<Arg<T>>, pub body: Vec<Statement<T, Expr>>, ... }The
bodyisVec<Statement<T, Expr>>. Looking atvisit_typed_function:pub fn visit_typed_function<'a, V>(v: &mut V, fun: &'a TypedFunction) where V: Visit<'a> + ?Sized, { for argument in fun.arguments.iter() { if let Some(annotation) = &argument.annotation { v.visit_type_ast(annotation, Some(argument.type_.clone())); } } if let Some(annotation) = &fun.return_annotation { v.visit_type_ast(annotation, Some(fun.return_type.clone())); } for statement in &fun.body { v.visit_typed_statement(statement); } }So it just iterates over statements. No block expression visit.
OK so let me actually look at the case more carefully. Maybe the bug is in the visit_typed_expr pipeline logic. Let me see:
fn visit_typed_expr_pipeline( &mut self, _location: &'ast SrcSpan, first_value: &'ast TypedPipelineAssignment, assignments: &'ast [(TypedPipelineAssignment, PipelineAssignmentKind)], finally: &'ast TypedExpr, _finally_kind: &'ast PipelineAssignmentKind, ) { self.previous_pipeline_assignment_type = None; self.visit_typed_pipeline_assignment(first_value); self.previous_pipeline_assignment_type = Some(first_value.type_()); for (assignment, _kind) in assignments { self.visit_typed_pipeline_assignment(assignment); self.previous_pipeline_assignment_type = Some(assignment.type_()); } // If we're selecting a pipeline and the selection ends on its final step // we want to include that as well into the extracted bit. let final_step_range = self.edits.src_span_to_lsp_range(finally.location()); if let Some(extracted_function) = &mut self.function && position_within(self.params.range.end, final_step_range) { extracted_function.try_add_pipeline_step(finally.type_(), finally.location()); }; self.visit_typed_expr(finally); self.previous_pipeline_assignment_type = None; }So for a pipeline, we visit each step (assignment). The
finallyis the final step. We add the final step if the selection ends within it.Hmm, let me think about this. The user's last statement is
simplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml). This is a pipeline expression.In Gleam AST, a pipeline expression with 2 steps is:
first_value = simplifile.write(toml_path, gleam_toml)assignments = [(result.replace_error(FailedToWriteGleamToml), ...)]finally = ...(which is the last expression after|>)
Actually wait, let me check how pipelines are parsed.
Looking at
visit_typed_expr_pipeline, thefinallyis an expression. The assignments are the steps after the first.Let me look at the Gleam pipeline AST:
- Selection starts at the
Assistant
OK so for
simplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml):first_valuelocation =simplifile.write(toml_path, gleam_toml)(the LHS of|>)- assignment with kind
FirstArgumentlocation =result.replace_error(FailedToWriteGleamToml)(the RHS of|>) finally= the result of the pipeline (could be the same as the assignment or a separate expression)
Actually for a 2-step pipeline
a |> b, the AST is:first_value = aassignments = [](no middle assignments)finally = b(butbis a function call whereais the first arg)
Hmm, but actually the visit function shows
visit_typed_expr_pipelinewith assignments list. Let me look at this more carefully.Actually let me check
TypedExpr::Pipeline:Assistant
OK so
Pipelinehas:location: the entire pipelinefirst_value: TypedPipelineAssignment for the first stepassignments: Vec of (assignment, kind) - these are intermediate stepsfinally: Box - the final step's expressionfinally_kind: the kind of the final step
Looking at the visit function for pipeline:
fn visit_typed_expr_pipeline( &mut self, _location: &'ast SrcSpan, first_value: &'ast TypedPipelineAssignment, assignments: &'ast [(TypedPipelineAssignment, PipelineAssignmentKind)], finally: &'ast TypedExpr, _finally_kind: &'ast PipelineAssignmentKind, ) { self.previous_pipeline_assignment_type = None; self.visit_typed_pipeline_assignment(first_value); self.previous_pipeline_assignment_type = Some(first_value.type_()); for (assignment, _kind) in assignments { self.visit_typed_pipeline_assignment(assignment); self.previous_pipeline_assignment_type = Some(assignment.type_()); } // If we're selecting a pipeline and the selection ends on its final step // we want to include that as well into the extracted bit. let final_step_range = self.edits.src_span_to_lsp_range(finally.location()); if let Some(extracted_function) = &mut self.function && position_within(self.params.range.end, final_step_range) { extracted_function.try_add_pipeline_step(finally.type_(), finally.location()); }; self.visit_typed_expr(finally); self.previous_pipeline_assignment_type = None; }So for a pipeline
a |> b:first_value = aassignments = []finally = b(or possiblyb(a)if it's a function call)
Wait, actually
finallyin a pipeline might be aCallexpression. Let me check this more carefully.Actually, looking at the visitor, it visits
first_value(which is the LHS of first|>), then visits eachassignment(which is each subsequent|>'s RHS). Then it visitsfinally(the very last step).For
simplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml):first_value.locationissimplifile.write(toml_path, gleam_toml)assignments = [(assignment forresult.replace_error(FailedToWriteGleamToml), ...)]finally = result.replace_error(FailedToWriteGleamToml)(...)(the actual call expression)
Hmm. Wait actually the assignment's
valueis the function nameresult.replace_error. And thefinallyis the call expression.Let me look at the source for how Pipeline is parsed.
Actually let me move on. Let me check the visit_typed_pipeline_assignment:
fn visit_typed_pipeline_assignment(&mut self, assignment: &'ast TypedPipelineAssignment) { // In order to be extracted, a pipeline step must be overlapping with // the cursor selection! let assignment_range = self.edits.src_span_to_lsp_range(assignment.location); if !overlaps(self.params.range, assignment_range) { return; } match &mut self.function { None => { self.function = Some(ExtractedFunction::new(ExtractedValue::PipelineSteps { location: assignment.location, before_first: self.previous_pipeline_assignment_type.clone(), return_type: assignment.type_(), })); } Some(extracted_function) => { extracted_function.try_add_pipeline_step(assignment.type_(), assignment.location) } } ast::visit::visit_typed_pipeline_assignment(self, assignment); }So this sets up PipelineSteps when a pipeline assignment overlaps. The
assignment.locationis the location of the pipeline step (just the function name?).Let me check
assignment.locationmore carefully. Looking at the comment:wibble |> wobble |> woo ^^^^^^ location of first step ^^^^^^ location of second stepSo
assignment.locationis just the function name's location, not the wholewibble |> wobble.Hmm. So for
simplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml):first_value.location= location ofsimplifile.writeassignments[0].0.location= location ofresult.replace_errorfinally.location()= location of the final call expressionresult.replace_error(FailedToWriteGleamToml)(...)(the actual call)
Now the issue. Let me trace through the bug:
fn prepare_project_structure(...) -> Result(Nil, SubmitError) { use priv <- result.try(...) // Statement 1 (Use) use _ <- result.try(...) // Statement 2 (Use) let toml_path = ... // Statement 3 (Assignment) use gleam_toml <- result.try(...) // Statement 4 (Use) let gleam_toml = ... // Statement 5 (Assignment) simplifile.write(...) // Statement 6 (Expression - Pipeline) }Selection: from start of
let toml_pathline to end ofsimplifile.write(...) ...line.Wait but actually wait. Looking at the issue text again, the
// <-- selected from hereis on its own line. So maybe the selection starts at the end of the// replace $PROJECT_NAME placeholder in the gleam.tomlline. Or maybe at the start of the// <-- selected from hereline.Let me try a different test where the selection starts a bit before the
letstatement, like including a comment line.Actually, looking at the user's report, I think the comment is just an arrow indicator showing where the selection starts/ends. The actual selection might be from the start of
let toml_pathline.But wait, in my test, the result was correct (only statements 3-6 extracted). So the bug doesn't reproduce.
Let me try with a more complex case. Maybe the issue is that the body wraps in a block expression that gets confused. Let me check.
Actually, maybe the issue is in
visit_typed_expr. Let me see if the body of the function gets wrapped in a block expression somewhere.Assistant
OK so the function body is wrapped in a
TypedExpr::Blockwhen it's parsed. Let me see when that happens.Actually wait, looking at
infer_block:fn infer_block(&mut self, statements: Vec1<UntypedStatement>, location: SrcSpan) -> TypedExpr { self.expr_in_new_scope(|typer| { let statements = typer.infer_statements(statements); TypedExpr::Block { statements, location, } }) }This creates a Block with statements. The statements are NOT the function body, but the contents of an explicit block
{ ... }in the code.So the function body is just a Vec directly, not wrapped in a Block.
Hmm. So in my trace, my test should be working correctly. Let me check the issue once more.
Let me look at the actual case in the bug. Maybe the issue is more subtle.
Wait! Let me look at the difference. In the bug, the function is defined with labels:
fn prepare_project_structure( target_path target: TargetPath, user_id user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) {These are labelled arguments (
target_path target,user_id user_id). In my test, I just haveOk(1),Ok(2), etc.Hmm, but that shouldn't affect the body. Let me look at another difference.
Actually wait! I notice now. Let me look at the body. In the bug, the function body is a sequence of statements. The function returns
Result(Nil, SubmitError).The first two statements are
use priv <- result.try(...)anduse _ <- result.try(...). These are Use statements that internally have their own bodies.A
usestatement looks like:use priv <- result.try(...)But the
usestatement desugars to:result.try(application.priv_directory("gleeps"), fn(priv) { // rest of the function body })Wait, that's a weird thing. The
usestatement's body is essentially the rest of the function block. So when we have:fn foo() -> Result(Nil, Error) { use priv <- result.try(...) use _ <- result.try(...) let toml_path = ... use gleam_toml <- result.try(...) let gleam_toml = ... simplifile.write(...) }The
use priv <- ...statement's "body" is everything after it. Theuse _ <- ...statement's "body" is everything after it. And so on.So a
Usestatement has a location that might span the rest of the function. Let me check.Assistant
EUREKA! Here's the key:
// A use statement covers the entire block: `use_.location` covers // just the use's first line and not what comes after it. Statement::Use(use_) => use_.location.merge(&use_.call.location()),So a
Usestatement's location is the use statement itself merged with theuse_.call.location(). Theuse_.callis the inner function call passed touse.Let me understand what
use_.call.location()returns. Foruse priv <- result.try(...), thecallisresult.try(application.priv_directory(...))which is a function call. Its location is just the call expression's location, NOT the rest of the function body.Wait, but the comment says "A use statement covers the entire block". That seems contradictory. Let me look at this more carefully.
Looking at the code:
Statement::Use(use_) => use_.location.merge(&use_.call.location()),So it takes
use_.location(the start of the use statement) and merges withuse_.call.location()(the end of the call inside the use).For
use priv <- result.try(application.priv_directory("gleeps") |> result.replace_error(FailedToFindPrivDirectory)):use_.location=use priv <-(or theuse privpart)use_.call.location()=result.try(application.priv_directory("gleeps") |> result.replace_error(FailedToFindPrivDirectory))
So the merged location would be the whole
useline. But the comment says "A use statement covers the entire block"...Wait, let me look at how
use_.locationis set. Maybe it actually includes the rest of the block.Assistant
So
use_.locationis just theuse a <- result.try(result)line, NOT the rest of the block.So
Usestatement's location isuse_.location.merge(&use_.call.location())= the entireuse a <- result.try(...)line.OK so that should be just the use line itself. Let me trace through more carefully.
Now I think the bug might be somewhere else. Let me look at
visit_typed_expr:fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { // If we have already determined what code we want to extract, we don't // want to extract this instead. if self.function.is_none() { // If this expression is fully selected, we mark it as being extracted. if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } ... }For each expression, we check if it can be extracted. The expression is fully selected only if the selection covers it.
Now wait. When we visit a
usestatement, we visituse_.callwhich is the inner expression. Let me check this.Assistant
OK so
visit_typed_usevisitsuse_.callwhich is a TypedExpr. So when we visit a use statement, we visit the inner expression. But the location is still the use line.Now for
visit_typed_expr:fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { if self.function.is_none() { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } ... }When we visit the
use priv <- result.try(...)statement:visit_typed_statementis called with the Use statement.can_extract_statementreturns false (since the statement is outside the selection).- So we fall through to
visit_typed_statement(self, statement). visit_typed_statementcallsvisit_typed_use(use_).visit_typed_usevisitsuse_.callwhich is the call expression.- In
visit_typed_expr, we checkcan_extract_expressionfor this call.
The call expression's location is
result.try(application.priv_directory(...)). If the selection doesn't overlap with this, we don't extract it.Hmm but the issue is the call expression. Let me trace through more carefully.
Wait, but what if
use_.callis a BLOCK expression? Let me check how the use statement desugars.Looking at the comment:
In a typed use this is the typed function call the use expression desugars to.
So
use_.callis the typed function call. Foruse priv <- result.try(result),use_.callisresult.try(result, fn(priv) { ... })(desugared).Hmm, that's a complex call expression. Let me check the location of this call.
Actually let me just trace through with the actual bug. Let me modify my test to better match the bug:
Assistant
OK so my test passes correctly! The bug doesn't reproduce with my simple test case.
Let me think about this. What might be different?
Let me look at the bug report again:
- The user starts the selection right after a comment line
// replace $PROJECT_NAME placeholder in the gleam.toml - The
<-- selected from hereis just an indicator showing where the selection starts.
Hmm. Maybe in neovim, the selection includes the newline after the comment line. Let me try starting the selection a bit earlier:
- The user starts the selection right after a comment line
Assistant
Still correct. Let me clean up the snapshot file:
Assistant
OK so my simple test case shows correct behavior. The bug must require something more specific. Let me try with the EXACT code from the bug report. Let me also try with
usebody having actual pipeline expressions:Assistant
OK so my test reproduces the source from the bug, but the extraction is CORRECT! Only the selected statements (4-9) are extracted, not all of them.
Let me clean up and try with imports:
Assistant
Hmm. So the extraction is correct. Let me actually carefully look at the user's bug case. The user wrote:
- After (extracted):
function(target, user_id)(the entire body)
So in the user's case, the entire body is extracted. But in my test, only the selected part is extracted.
Let me look at the version - maybe the user is on a different version. Let me check the git log:
- After (extracted):
Assistant
Hmm. So we're on a recent commit. Let me see if maybe there's a specific case I'm missing.
Let me look at the bug report more carefully:
- Function signature:
fn prepare_project_structure(target_path target: TargetPath, user_id user_id: uuid.Uuid) -> Result(Nil, SubmitError) - These are LABELED arguments
In Gleam, labeled arguments have special syntax. Let me check if there's something special about them.
Actually wait, looking at the bug output. The expected
functionsignature is:fn function( target: TargetPath, user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) {So the labels are stripped from the function definition.
But that's not the issue. The issue is the extraction grabbing the entire function.
Let me look at this more carefully. The bug is that the extract function grabs EVERYTHING, not just the selected statements. So
self.functionis being set to something that spans all statements.Let me think about when that could happen.
Going back to my trace, when I extract statements 3-6, the location becomes the span from stmt3 to stmt6. But in the bug, the location might somehow become the span from stmt1 to stmt6.
Hmm. Let me check if there's a way that
visit_typed_exprcould be triggered for an expression that spans the whole block.Wait! Let me re-read the visit_typed_expr:
fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { // If we have already determined what code we want to extract, we don't // want to extract this instead. if self.function.is_none() { // If this expression is fully selected, we mark it as being extracted. if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } ... }This is called for every expression. If
self.function.is_none(), then it checks if the expression can be extracted. If yes, setsself.functionto this expression.Now
can_extract_expressionfor the Block expression. Let me check:TypedExpr::Int { .. } | TypedExpr::Float { .. } | TypedExpr::String { .. } | TypedExpr::Block { .. } | TypedExpr::Var { .. } | TypedExpr::Fn { .. } | TypedExpr::List { .. } | TypedExpr::Call { .. } | TypedExpr::BinOp { .. } | TypedExpr::Case { .. } | TypedExpr::RecordAccess { .. } | TypedExpr::PositionalAccess { .. } | TypedExpr::ModuleSelect { .. } | TypedExpr::Tuple { .. } | TypedExpr::TupleIndex { .. } | TypedExpr::Todo { .. } | TypedExpr::Panic { .. } | TypedExpr::Echo { .. } | TypedExpr::BitArray { .. } | TypedExpr::RecordUpdate { .. } | TypedExpr::NegateBool { .. } | TypedExpr::NegateInt { .. } | TypedExpr::Invalid { .. } => !completely_within(selected_range, expression_range),For Block, it returns
!completely_within(selected_range, expression_range). If the selection is NOT completely within the block range, then we extract the block.For the function body, the function body's location is NOT a block expression (it's a Vec). So this shouldn't apply.
But wait, the FUNCTION body might be wrapped in a block when desugaring the
usestatements. Let me check.When we have:
fn foo() { use priv <- result.try(...) // rest of body }The
use priv <- result.try(...)desugars to:fn foo() { result.try(..., fn(priv) { // rest of body }) }So the rest of the body becomes the body of an anonymous function inside the
result.trycall. And theresult.try(...)call's location includes the entire anonymous function's body!So when we visit the use statement's call expression, the call expression's location spans from
result.try(to the end of the body. So the call expression's location would be HUGE.Then
visit_typed_exprwould be called for this huge call expression. Let me check.visit_typed_exprfor this call:self.function.is_none()(initially true)can_extract_expression(call)- check if call overlaps with selection.
If the selection overlaps with this huge call expression (which it does because the call spans the whole body), then we set
self.function = ExtractedValue::Expression(call).YES! This is the bug!
So when a function starts with
usestatements, the use's call expression spans the entire function body. Whenvisit_typed_expris called for this call, if the user's selection overlaps with the call's location (which it always does for any selection within the function), thenself.functiongets set to this call expression, which then triggers extraction of the entire body.Wait but in my test, the extraction worked correctly. So why doesn't my test trigger this bug?
Let me check the location of
use_.callmore carefully. Let me look at the code:- Function signature:
Assistant
So
Call.locationis the location stored in the Call expression. Let me check what this is when the use statement is desugared.Let me look at how use is desugared:
Assistant
AH HA! Look at this:
let call_location = SrcSpan { start: use_.location.start, end: sequence_location.end, };So the
call_locationfor ausestatement starts at theusekeyword and ends atsequence_location.endwhich is the end of the entire block (the end of the function body in this case).So the call expression's location spans from the start of the
usestatement to the END OF THE FUNCTION BODY!Then in
visit_typed_expr, we check if this call can be extracted. The selection overlaps with this call (since the call spans the whole body), so we extract it. The entire function is then extracted as an expression.This is the bug!
Let me verify. Let me look at the visit again:
fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { // If we have already determined what code we want to extract, we don't // want to extract this instead. if self.function.is_none() { // If this expression is fully selected, we mark it as being extracted. if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } // If the expression is a function, then the last statement location // needs to be updated accordingly let last_statement_location = self.last_statement_location; if let TypedExpr::Fn { body, .. } = expression { self.last_statement_location = Some(body.last().location()); } ast::visit::visit_typed_expr(self, expression); self.last_statement_location = last_statement_location; }Now, the order of visits is important. Let me think.
For a function:
visit_typed_functionis called.- It iterates statements.
- For each statement,
visit_typed_statementis called. visit_typed_statementfor aUsestatement callsvisit_typed_use.visit_typed_usecallsvisit_typed_expr(&use_.call).visit_typed_exprfor the call checks ifself.function.is_none(). If yes, it might setself.function.
Now, when is
visit_typed_exprcalled for the use's call? It's called immediately during the statement visit. So if the user selection starts at some point INSIDE the use's call (which spans the whole body), thenself.functiongets set toExtractedValue::Expression(call).But wait, the user's selection is from
let toml_pathto the end. The use's call's location spans from the start ofuse priv <- ...to the end of the body. So the selection IS within the use's call's location.Let me check
can_extract_expressionfor a Call:TypedExpr::Call { .. } => !completely_within(selected_range, expression_range),So if the selection is NOT completely within the call's location, we extract. But here, the selection IS completely within the call's location (since the call spans the entire body).
So
!completely_withinis FALSE. So we don't extract.Wait! Then why is the bug happening?
Let me think again. Maybe the use statement is iterated first, and then the use's call is visited. Then we visit subsequent statements.
Actually, the visit order is:
visit_typed_function- For each statement:
visit_typed_statement visit_typed_statement->visit_typed_use->visit_typed_expr(use_.call)
So when we're visiting the first use statement:
visit_typed_statementis called for the first use.- Inside,
can_extract_statementis checked first. - If the statement can be extracted (overlaps with selection), it's added to
self.function. - Then we visit the statement body.
So the order matters! Let me re-read
visit_typed_statement:fn visit_typed_statement(&mut self, statement: &'ast TypedStatement) { let statement_location = statement.location(); if self.can_extract_statement(statement) { // ... determines position ... match &mut self.function { None => { self.function = match statement { TypedStatement::Expression(TypedExpr::Pipeline { .. }) => None, TypedStatement::Assert(_) | TypedStatement::Assignment(_) | TypedStatement::Expression(_) => { Some(ExtractedFunction::new(ExtractedValue::Statements { location: statement_location, position, })) } TypedStatement::Use(use_) => { Some(ExtractedFunction::new(ExtractedValue::Use { location: use_.call.location(), type_: statement.type_(), })) } } } // ... other branches ... } } ast::visit::visit_typed_statement(self, statement); }OK so:
can_extract_statementis checked first.- If yes, update
self.function(if needed). - Then visit the statement body.
For the first use statement (statement 1:
use priv <- ...):can_extract_statementchecks overlap with selection.- The selection is from
let toml_pathto end. The use statement's location isuse priv <- result.try(...). Does this overlap? - The use's location is from
usetoresult.try(...). So the selection (starting atlet toml_path) does NOT overlap with this use's location. - So
can_extract_statementreturns false. - We fall through to
visit_typed_statement(self, statement). visit_typed_statement->visit_typed_use->visit_typed_expr(use_.call).visit_typed_exprfor the call. The call's location spans fromuse priv <-to end of body.- The call's location overlaps with the selection.
can_extract_expressionis called for the call.- For Call:
!completely_within(selected_range, call_range). selected_rangeis fromlet toml_pathto end.call_rangeis fromuse priv <-to end.- Is
selected_rangecompletely withincall_range? selected_range.start(which islet toml_path) >call_range.start(use priv <-) ✓selected_range.start<call_range.end✓selected_range.end>call_range.start✓selected_range.end<call_range.end? Hmm, theselected_range.endis the END of the function body. Thecall_range.endis the END of the function body. Soselected_range.end<call_range.endis FALSE.
So
completely_withinreturns FALSE. So!completely_withinreturns TRUE. Socan_extract_expressionreturns TRUE. So we extract this Call!YES! That's the bug!
So the issue is that
visit_typed_exprfor the use's call expression is triggered, and the call's location spans the entire body. When the user selects statements inside the body, the call is considered as "extractable" because it's not entirely within the selection.The fix: when we visit a use's call expression, we should NOT extract it as an expression, because the use already takes care of it.
Or alternatively, we should check if we're already inside a use statement and skip extracting the use's call.
Let me see the order. In my trace:
- Statement 1 (use priv): can_extract_statement returns false (no overlap with selection). Fall through to visit. Inside visit,
visit_typed_expr(use_.call). Here,self.function.is_none()is true. So ifcan_extract_expression(call)returns true, we setself.function = ExtractedValue::Expression(call).
But wait!
self.functionwas None initially because we haven't processed any selectable statement yet.OK so the bug is clear. Let me see how to fix it.
The simplest fix would be: in
visit_typed_expr, don't extract the call expression of a use statement. We can track that we're inside a use statement visit.But there's a simpler fix: in
visit_typed_statementfor a Use statement, setself.function = Some(ExtractedValue::Use { ... })even ifcan_extract_statementreturns false? No, that would cause extraction of use statements that aren't selected.Actually, looking at the code more carefully:
TypedStatement::Use(use_) => { Some(ExtractedFunction::new(ExtractedValue::Use { location: use_.call.location(), type_: statement.type_(), })) }This sets the function to extract the use if the use itself can be extracted. So if the use's call is selected, we extract the use.
But the issue is that the use's CALL spans the entire body. So even if the use itself isn't selected, its call expression overlaps with selections inside the body.
The fix: When we're inside
visit_typed_use, we should skip thevisit_typed_exprforuse_.call, OR we should track that we're in a use context and not extract the call.Let me think about this differently. The issue is that
visit_typed_expris called foruse_.call(which is a Call expression spanning the entire body). This Call expression's location overlaps with the user's selection (since the user's selection is within the body).The fix should be: when visiting the call expression of a use statement, we should not consider it as extractable as a whole, because the use statement handles its extraction.
Actually, looking at the code, I think the issue is that
visit_typed_exprshould NOT be called for the use's call expression. The use's call is an internal implementation detail of the use statement.Wait, but the use's call IS visited. Look at
visit_typed_use:pub fn visit_typed_use<'a, V>(v: &mut V, use_: &'a TypedUse) where V: Visit<'a> + ?Sized, { v.visit_typed_expr(&use_.call); // TODO: We should also visit the typed patterns!! }So yes, the use's call is visited. We need to handle this specially.
Looking at the original code:
fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { // If we have already determined what code we want to extract, we don't // want to extract this instead. if self.function.is_none() { // If this expression is fully selected, we mark it as being extracted. if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } ... }This is checking if
self.function.is_none()before setting. So if a previous statement already setself.function, we don't overwrite. But the FIRST time we encounter a Use statement,self.functionis still None.Actually wait, I need to re-examine. Let me think about the visit order:
visit_typed_functionis called.function_end_position = Some(...),last_statement_location = ....- Iterate statements.
- For statement 1 (use priv), call
visit_typed_statement:can_extract_statement(stmt1)- returns false (no overlap).- Fall through to
ast::visit::visit_typed_statement(self, statement). visit_typed_statementcallsvisit_typed_use(use_).visit_typed_usecallsvisit_typed_expr(use_.call).visit_typed_exprfor the call:self.function.is_none()- YES (still None).can_extract_expression(call)- check.- For Call:
!completely_within(selected_range, call_range). - selected_range = from
let toml_pathto end. - call_range = from
use priv <-to end. - Is selected_range completely within call_range?
selected_range.start > call_range.start?let toml_path>use priv? YES.selected_range.start < call_range.end?let toml_path< end? YES.selected_range.end > call_range.start? end >use priv? YES.selected_range.end < call_range.end? end < end? NO.- So
completely_withinreturns FALSE. !completely_withinreturns TRUE.- So
can_extract_expressionreturns TRUE. - So
self.function = Some(ExtractedFunction::new(ExtractedValue::Expression(call))).
This sets
self.functionto the call expression! Then subsequent statements are visited, butself.functionis already set, so they don't change it.Then in
code_actions, we haveextracted.value = ExtractedValue::Expression(call). The call's location is the entire function body. So the entire function body is extracted.This confirms the bug! Now let me fix it.
The fix: When visiting the call expression of a Use statement, we should not set
self.function. We can do this by either:- Tracking when we're inside a use statement.
- Not visiting the use's call expression.
- Setting
self.functiontoNoneif we're in a use statement and the use itself isn't selected.
Looking at the code, option 1 seems cleanest. Let me think about how to implement it.
Actually, the simplest fix is: In
visit_typed_expr, check if the expression is inside a use statement's call. We could add a flag likeself.inside_use_call = truebefore callingvisit_typed_expr(use_.call)and reset it after.Or alternatively: in
visit_typed_use, if the use statement is NOT selected, we should clearself.functionif it was set by the call expression. But this is hacky.Actually wait, looking again at the issue:
The use's call expression's location spans the entire body. When we visit this call, we might want to extract it. But we don't want to extract it unless the user explicitly selects the entire call (which would be the entire body).
In
can_extract_expression:TypedExpr::Call { .. } => !completely_within(selected_range, expression_range),For the use's call (which spans the entire body), if the selection is somewhere inside, then the selection is COMPLETELY within the call's range (because the call covers everything). So
!completely_withinis FALSE. So we don't extract.Wait, that contradicts what I said before. Let me re-check.
completely_within(a, b)is true if:a.start > b.startANDa.start < b.endANDa.end > b.startANDa.end < b.end
For:
- a = selected_range = from
let toml_path(line N) to end of body (line M end) - b = call_range = from
use priv <-(line 1 of body) to end of body (line M end)
So:
a.start > b.start?let toml_path>use priv? YES.a.start < b.end?let toml_path< end? YES.a.end > b.start? end >use priv? YES.a.end < b.end? end < end? NO (they're equal).
So
completely_within(a, b)is FALSE. So!completely_within(a, b)is TRUE. Socan_extract_expressionreturns TRUE.So we DO extract the call. But this is wrong because the user didn't select the call's entire range (which would be from
use priv <-to end of body, i.e., the entire body).So the issue is the
<vs<=incompletely_within. The checka.end < b.endmeans that the end ofamust be strictly less than the end ofb. But here they're equal. Socompletely_withinreturns FALSE.This means that if
aends at the same position asb,ais NOT considered completely withinb. This is by design (see the comment incompletely_within):The range cannot have any extreme in common to be considered completely within another.
OK so the fix is that when we're inside a use statement and visit the use's call expression, we should not consider it as extractable.
Let me look at the simplest fix: in
visit_typed_use, we should set a flag or skip the visit. Or we should skip the entire extraction logic for the use's call.Actually, I think the cleanest fix is to add a method to override
visit_typed_usein our extractor and skip the call visit. Let me look at whatvisit_typed_usedoes:pub fn visit_typed_use<'a, V>(v: &mut V, use_: &'a TypedUse) where V: Visit<'a> + ?Sized, { v.visit_typed_expr(&use_.call); // TODO: We should also visit the typed patterns!! }It only visits
use_.call. So if we override this method in our extractor, we can choose to NOT visit the call expression.Let me check how the extractor handles the use statement currently:
TypedStatement::Use(use_) => { Some(ExtractedFunction::new(ExtractedValue::Use { location: use_.call.location(), type_: statement.type_(), })) }This is in
visit_typed_statement. So when a use statement is selected, we create anExtractedValue::Usewith the use's call location.Then later in
code_actions:ExtractedValue::Use { location, type_ } | ExtractedValue::Statements { location, position: StatementPosition::Tail { type_ }, } => self.extract_code_in_tail_position( location, location, type_, extracted.parameters, end, ),So we extract the use as a function in tail position. The location is
use_.call.location()which is the entire body (when the use is the first one).So the use's call location is intentional - it's used to capture the entire body that the use is responsible for.
OK so we DO need to visit the use's call expression to capture variables used in the body. The issue is just that we shouldn't extract it as the whole expression.
Let me think about the fix:
Option A: Override
visit_typed_useto skip the call extraction logic.Option B: Add a flag
inside_use_calland check it invisit_typed_expr.Option C: Don't visit the call expression in
visit_typed_usedirectly. Instead, visit its parts but not the whole.Option B seems easiest. Let me implement it.
Actually wait, let me think about option C. The use's call is a call expression like
result.try(application.priv_directory("gleeps"), fn(priv) { ... }). We need to visit the args of this call (to capture variables), but we should NOT consider the whole call as extractable.Hmm. Looking at
visit_typed_expr:fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { if self.function.is_none() { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } let last_statement_location = self.last_statement_location; if let TypedExpr::Fn { body, .. } = expression { self.last_statement_location = Some(body.last().location()); } ast::visit::visit_typed_expr(self, expression); self.last_statement_location = last_statement_location; }The function visits the children expressions via
ast::visit::visit_typed_expr. So when it visits the call, it visits its arguments. The arguments include the anonymous function (the body of the use). When visiting the anonymous function, it visits the body statements.So actually, the visit of the use's call ALSO visits the body statements! So the statements inside the body are visited via the call.
Let me trace again:
visit_typed_function->visit_typed_statement(stmt1)for use priv.visit_typed_statementcallsvisit_typed_usebecause stmt1 is Use.visit_typed_usecallsvisit_typed_expr(use_.call)where use_.call is the Call.visit_typed_exprcheckscan_extract_expression(call). The call is extracted asExtractedValue::Expression(call).- Then visits the call's children. The call's args include the anonymous function.
- The anonymous function's body is visited, which includes the subsequent statements.
So when we extract the call expression as a function, we extract the entire call (which includes the anonymous function and all its body).
This is the bug! When we extract the call, we extract everything inside it.
Now I understand. Let me fix this.
The simplest fix: in
visit_typed_use, set a flaginside_use_call = truebefore visiting the call, and unset after. Then invisit_typed_expr, check this flag and don't try to extract the call.Actually wait, let me re-read visit_typed_expr. We don't want to extract the call when we're inside a use. So:
fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { if self.function.is_none() && !self.inside_use_call { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } ... }And add:
fn visit_typed_use(&mut self, use_: &'ast TypedUse) { let was_inside = self.inside_use_call; self.inside_use_call = true; ast::visit::visit_typed_use(self, use_); self.inside_use_call = was_inside; }Hmm wait. The default
visit_typed_useis:pub fn visit_typed_use<'a, V>(v: &mut V, use_: &'a TypedUse) where V: Visit<'a> + ?Sized, { v.visit_typed_expr(&use_.call); }So it visits the call. The call contains an anonymous function with the body. The body contains all the subsequent statements.
When we visit the use's call, we want to extract the call's parts (for variable capture etc.) but not the call itself.
Hmm actually let me think about this more. The
visit_typed_exprfor the call:- Sets
self.functionif it's the entire call. - Then visits the call's arguments (via
ast::visit::visit_typed_expr). - The call's args include the anonymous function.
- The anonymous function's body is visited.
For variable capture, we need to visit the call's arguments. But we don't want to extract the call itself.
So the fix should be: when entering the use's call, set a flag to prevent the call from being extracted, but still allow visiting its parts.
Let me implement this:
Assistant
Let me check the structure of the
ExtractFunctionstruct:Assistant
Now let me think about the cleanest fix.
Actually, the simplest fix is: in
visit_typed_expr, when we are visiting the use's call expression (i.e., a Call whose parent is a Use), don't extract it.But the visitor pattern doesn't easily give us parent information. So let me add a flag.
Actually, looking again, I realize there's another approach. Let me check
visit_typed_use:We can override
visit_typed_usein our extractor and:- Set a flag
self.inside_use = true. - Visit
use_.callwith the flag set, so thatvisit_typed_exprdoesn't extract the call. - Restore the flag.
Let me implement this:
- Set a flag
Assistant
So there are multiple visit_typed_use implementations. Let me find the one in ExtractFunction:
Assistant
Good. Let me look at the visit_typed_expr in ExtractFunction:
Assistant
Now I understand the issue. The fix is to prevent the use's call expression from being treated as extractable. Let me look at the fix approach.
The cleanest fix would be to add a flag
inside_use_callthat is set when visiting the use's call expression, and check it invisit_typed_exprto skip the extraction logic.Let me implement this:
Assistant
Now let me think about the fix.
Actually, I think a simpler approach would be to check whether the expression is the
use_.callexpression itself. We could do this by checking if the expression's location matches a use's call location. But that's fragile.Let me think about this differently. The bug is that:
- A use statement's call expression has a location that spans the entire body.
- When the call expression is visited (via
visit_typed_use->visit_typed_expr),can_extract_expressionreturns true (because the selection is not completely within the call - it ends at the same position as the call). - So
self.functionis set to the entire call expression.
The fix: We should not extract the use's call expression as a whole, because the use statement itself handles that.
Let me look at how to skip the call visit. We could override
visit_typed_usein our extractor:fn visit_typed_use(&mut self, use_: &'ast TypedUse) { // The location of the use's call expression spans from the use keyword to // the end of the body. We don't want to consider this call expression as // extractable as a whole, because the use statement itself handles the // extraction of its body. let was_function = self.function.take(); ast::visit::visit_typed_use(self, use_); // If visiting the use's call caused us to set `self.function` to the call // expression (which spans the entire body), we need to restore the previous // value. if let Some(ExtractedFunction { value: ExtractedValue::Expression(_), .. }) = &self.function { self.function = was_function; } }Hmm, but this is hacky. A cleaner approach would be to use a flag.
Actually wait, let me re-read the visit_typed_expr:
fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { if self.function.is_none() { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } ... }So it only sets
self.functionif it's None. So if a statement is visited first (and setsself.function), the call expression won't override it.But in our bug case, the FIRST statement (use priv) is NOT selected. So
can_extract_statementreturns false, and we fall through tovisit_typed_statementwhich callsvisit_typed_use->visit_typed_expr(use_.call). Here,self.function.is_none()is true, so the call expression gets extracted.So the bug is specifically when the FIRST statement is a use statement that's NOT selected, but the use's call spans the entire body, and the selection is inside the body.
OK let me think about the cleanest fix.
Option 1: Add a flag
inside_use_call.pub struct ExtractFunction<'a> { // ... existing fields ... /// When visiting the call expression of a use statement, we don't want to /// consider it as extractable as a whole, because the use statement itself /// handles the extraction. inside_use_call: bool, } // In visit_typed_expr: fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { if self.function.is_none() && !self.inside_use_call { if self.can_extract_expression(expression) { ... } } ... } // Add visit_typed_use: fn visit_typed_use(&mut self, use_: &'ast TypedUse) { let was_inside = self.inside_use_call; self.inside_use_call = true; ast::visit::visit_typed_use(self, use_); self.inside_use_call = was_inside; }Option 2: Override
visit_typed_useto skip the extraction logic.Hmm, option 1 is cleaner. But wait, the default
visit_typed_usecallsvisit_typed_expr(&use_.call). So if we set the flag before, the call won't be extracted. Then we restore the flag. That works.But wait, I realized there's still a problem. The use's call includes an anonymous function with the body. When we visit that anonymous function via
visit_typed_expr, we need to visit the body's statements. But the body's statements might be the ones we want to extract (they're inside the selection).Actually, that's fine. The visit still happens, so the body's statements are visited. And
visit_typed_statementfor each statement will checkcan_extract_statement. So if a statement overlaps with the selection, it's added toself.function.But wait, the visit order: use -> use_.call (Call) -> call's args (which include the anonymous function) -> anonymous function's body statements.
When we visit the anonymous function's body statements, each statement is visited via
visit_typed_statement. Socan_extract_statementis checked. If yes,self.functionis updated.But wait, we said
self.functionwas already set to the call expression. So the subsequent visits don't updateself.function(because theif self.function.is_none()check fails).Wait that's a problem too! If the use's call is extracted first, then the subsequent statements (which are inside the call's body) don't update
self.function. So the call (which spans the whole body) is extracted.But in the BUG case, we don't want to extract the call. We want to extract only the selected statements. So the fix is: don't extract the call in the first place.
OK let me look at this fix more carefully. With option 1 (flag), the order would be:
- Statement 1 (use priv): not selected. Fall through to visit. Set
inside_use_call = true. Visit the call. Call is not extracted. Visit call's args (anonymous function). Visit anonymous function's body statements. - Each statement in the body is visited. For selected ones,
self.functionis updated.
Hmm but wait, the visit of the use's call already visits the body statements. So when we get to the actual top-level statement visit for statement 3 (let toml_path), we've already visited statements 3-6 via the use's call.
So the order is:
- Visit statement 1 (use priv). Set
inside_use_call = true. Visit use's call. Visit call's args. Visit anonymous function. Visit body statements.- Statement 3 (let toml_path):
can_extract_statementis true. Setself.functionto statement 3. - Statement 4 (use gleam_toml):
can_extract_statementis true. Setself.functionto merged. - Statement 5 (let gleam_toml):
can_extract_statementis true. Setself.functionto merged. - Statement 6 (simplifile.write ...):
can_extract_statementis true. Setself.functionto merged.
- Statement 3 (let toml_path):
- Then we continue with top-level statement 2 (use _). Set
inside_use_call = true. Visit use's call. Butself.functionis already set, so no changes.
OK with option 1, the fix should work.
Actually wait, I need to also handle the case where the use statement is the FIRST thing selected. In that case, we want to extract the use statement. Let me think...
Looking at the existing logic in
visit_typed_statement:TypedStatement::Use(use_) => { Some(ExtractedFunction::new(ExtractedValue::Use { location: use_.call.location(), type_: statement.type_(), })) }If the use statement is selected,
self.functionis set toExtractedValue::Use { location: use_.call.location(), type_: ... }. Then incode_actions:ExtractedValue::Use { location, type_ } | ExtractedValue::Statements { location, position: StatementPosition::Tail { type_ }, } => self.extract_code_in_tail_position( location, location, type_, extracted.parameters, end, ),So it extracts the use's call's location. This is the entire body (for the first use). So the entire body is extracted as the use function.
OK so this is intentional - if the user selects a use statement, the entire body is extracted.
Hmm, but what about the
visit_typed_exprfor the use's call? When we visit the use's call expression, we'd hit:if self.function.is_none() { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression(expression))); } }But
self.functionis already set (to the Use). So this check fails. Good.OK so the fix is just to prevent the use's call from being extracted when the use itself is not selected.
Let me implement option 1.
Actually, wait. Looking at the code more carefully:
fn visit_typed_statement(&mut self, statement: &'ast TypedStatement) { let statement_location = statement.location(); if self.can_extract_statement(statement) { // ... determines position, sets self.function ... } ast::visit::visit_typed_statement(self, statement); }So
can_extract_statementis checked first. If yes, setself.function. Then visit the statement.For a Use statement that is NOT selected:
can_extract_statementreturns false.- Fall through to
ast::visit::visit_typed_statement(self, statement). visit_typed_statementcallsvisit_typed_use.visit_typed_usecallsvisit_typed_expr(use_.call).visit_typed_exprfor the call:self.function.is_none()is true.can_extract_expression(call)returns true (because the call spans the whole body). Soself.functionis set to the call.
OK so the bug is clear. Let me fix it.
Let me think about a cleaner approach. The issue is that the use's call expression's location is the entire body. We don't want to extract this call.
Approach A: Add a flag
inside_use_callthat's set invisit_typed_useand checked invisit_typed_expr.Approach B: Override
visit_typed_useand skip the call extraction by clearingself.functionif it was set by the call.Approach C: In
visit_typed_expr, check if the expression is a Call expression whose parent is a Use. (This requires tracking parent info, which is hard.)Approach D: When visiting a use statement, always set
self.functionto either the Use (if selected) or None (if not selected), but don't visit the use's call expression as a potential extractable.Hmm. Approach A seems cleanest. Let me also think about whether there could be other cases where
visit_typed_exprfor a child expression could mistakenly extract a big thing.Actually, let me think. The
Fn(anonymous function) expression has a body. When visiting it, we visit the body. The body has statements. For each statement,visit_typed_statementis called.For a Fn expression that's inside the use's call: the Fn's location is just the function expression, not the body. So no issue there.
OK let me implement Approach A.
Wait actually, looking at this again, I realize the issue might be that the use's call's location is the entire body. But for a use statement in the middle of a function, the use's call's location might not span the entire body. Let me think.
For:
fn foo() { use priv <- result.try(...) let toml_path = ... use gleam_toml <- result.try(...) let gleam_toml = ... simplifile.write(...) }The first use (
use priv <- result.try(...)) has call location fromuse priv <-to the END of the body. So it's the entire body.The second use (
use gleam_toml <- result.try(...)) has call location fromuse gleam_toml <-to the END of the body. So it's from statement 4 to the end.So the use's call always extends to the end of the body.
OK so the fix should handle this case. Let me implement it.
Actually, I realize a cleaner approach is to simply NOT visit the use's call expression when the use is NOT selected. Because if the use is selected, we handle it specially via
ExtractedValue::Use.But we still need to visit the call's arguments for variable capture. Hmm.
Wait, looking at the code:
fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { if self.function.is_none() { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression(expression))); } } // Visit children ast::visit::visit_typed_expr(self, expression); }So the visit_typed_expr first checks if the expression itself is extractable. Then visits children. If we DON'T want to extract the use's call, we can override
visit_typed_useto skip the call's extraction check but still visit children.Hmm. Let me try a different approach. Let me override
visit_typed_use:fn visit_typed_use(&mut self, use_: &'ast TypedUse) { // The location of the use's call expression spans from the use keyword // to the end of the body, because the use takes the rest of the body as // its callback. We don't want to extract this call expression as a whole // because the use statement handles the extraction of its body. // So we set a flag to skip the extraction logic for this expression. let was_inside = self.inside_use_call; self.inside_use_call = true; ast::visit::visit_typed_use(self, use_); self.inside_use_call = was_inside; }And in
visit_typed_expr:fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { if self.function.is_none() && !self.inside_use_call { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression(expression))); } } ... }OK let me try this fix. But first, let me remove my temporary test file. Let me also clean up the snapshots:
Assistant
OK no leftover snapshots. Now let me first re-think the test case. The issue is:
- 2 use statements at the beginning of a function.
- Selected statements (which include more use statements).
Let me make sure my test case triggers the bug. Let me first revert the fix temporarily and write a test that triggers the bug.
Actually, the bug case I have is the test from the issue. Let me write it as a proper test:
Assistant
WAIT! Look at this output! The test is passing correctly!
----- AFTER ACTION pub fn main() { use priv <- result.try( application.priv_directory("gleeps") |> result.replace_error(FailedToFindPrivDirectory), ) use _ <- result.try( simplifile.copy_directory(filepath.join(priv, "base_project"), target.path) |> result.replace_error(FailedToCopyBaseProject), ) function() } fn function() -> a { let toml_path = target.path |> filepath.join("gleam.toml") use gleam_toml <- result.try( toml_path |> simplifile.read |> result.replace_error(FailedToReadGleamToml), ) let gleam_toml = gleam_toml |> string.replace("$PROJECT_NAME", with: user_id_to_string(user_id)) simplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml) }The test only extracts the selected statements (3-6)! It correctly leaves the
use priv <- ...anduse _ <- ...in the original function.Hmm so this case is also working correctly. So my hypothesis about the bug is wrong? Let me check what's different about the user's case.
Wait, maybe I need to remove the
|in front oftarget.path, etc. Let me see.Actually wait, let me re-read the original bug. The actual code in the bug uses:
// replace $PROJECT_NAME placeholder in the gleam.toml // <-------------------------------------------------------------- selected from here let toml_path = target.path |> filepath.join("gleam.toml")The user's selection includes a comment line. Maybe the issue is the comment line.
Let me try with a comment above
let toml_path:Actually wait. The
<--comment is just an indicator in the bug report. It's not actual code. So the user's selection starts at the// replace $PROJECT_NAME placeholder in the gleam.tomlline, NOT at the<--line.Hmm. Let me think about this.
Actually wait, the snapshot shows the test passes. Maybe my test is actually working correctly but the test framework is comparing against a non-existent snapshot file. Let me check.
Assistant
OK so my test produces the CORRECT output. The first
use priv <- ...anduse _ <- ...statements are correctly NOT extracted. Only the selected statements (3-6) are extracted.So the bug doesn't reproduce in my test. Let me think about why.
Maybe the user's selection actually includes the comment line
// replace $PROJECT_NAME placeholder in the gleam.toml. Let me try that:Assistant
Still working correctly! The first two use statements are kept in the original function. Only the selected statements are extracted.
Let me clean up:
Assistant
OK my test passes correctly. But the bug still claims the wrong behavior. Let me think again.
Maybe the issue is the version. Let me check if there's something different in the neovim nightly vs the current code. Or maybe there's a specific scenario I'm missing.
Let me look at the visit more carefully. Let me add some debug prints:
Actually, wait. Let me check the
last_statement_locationlogic for the body. When the visit starts,last_statement_locationis set to the last statement of the body (statement 6 in my test). Then we iterate statements.For statement 1 (use priv):
can_extract_statementreturns false. We fall through tovisit_typed_statement(self, statement). This callsvisit_typed_use->visit_typed_expr(use_.call).visit_typed_exprfor the call. The call has location fromuse priv <-to the end of the body. The selection starts atlet toml_path(statement 3) and ends at the end of the body.can_extract_expressionfor the call:- For Call:
!completely_within(selected_range, call_range). - selected_range = (stmt3.start, end_of_body).
- call_range = (start_of_body, end_of_body).
- Is selected_range completely within call_range?
- selected_range.start > call_range.start? stmt3.start > start_of_body? YES.
- selected_range.start < call_range.end? stmt3.start < end_of_body? YES.
- selected_range.end > call_range.start? end_of_body > start_of_body? YES.
- selected_range.end < call_range.end? end_of_body < end_of_body? NO.
- So
completely_withinreturns FALSE. - So
!completely_withinreturns TRUE. - So
can_extract_expressionreturns TRUE.
So we extract the call! But my test shows this is NOT happening. Why?
Wait, let me re-check. Maybe the visit order is different. Let me look at the visit_typed_function:
pub fn visit_typed_function<'a, V>(v: &mut V, fun: &'a TypedFunction) where V: Visit<'a> + ?Sized, { for argument in fun.arguments.iter() { if let Some(annotation) = &argument.annotation { v.visit_type_ast(annotation, Some(argument.type_.clone())); } } if let Some(annotation) = &fun.return_annotation { v.visit_type_ast(annotation, Some(fun.return_type.clone())); } for statement in &fun.body { v.visit_typed_statement(statement); } }So it iterates over each statement and calls
visit_typed_statement. Good.Now let me check
visit_typed_statement:fn visit_typed_statement(&mut self, statement: &'ast TypedStatement) { let statement_location = statement.location(); if self.can_extract_statement(statement) { // ... position and self.function ... } ast::visit::visit_typed_statement(self, statement); }For a Use statement,
can_extract_statement:ast::Statement::Use(_) | ast::Statement::Assert(_) => { !completely_within(selected_range, statement_range) }The use statement's range is
use_.location.merge(&use_.call.location())which is the entire use line.For statement 1 (use priv):
- statement_range = (use_keyword_start, end_of_use_line).
- selected_range = (let_toml_path_start, end_of_body).
- Are these overlapping?
Looking at
overlaps:pub fn overlaps(a: Range, b: Range) -> bool { position_within(a.start, b) || position_within(a.end, b) || position_within(b.start, a) || position_within(b.end, a) }position_within(position, range):pub fn position_within(position: Position, range: Range) -> bool { position >= range.start && position <= range.end }So for statement_range = (use_keyword_start, end_of_use_line) and selected_range = (let_toml_path_start, end_of_body):
- position_within(use_keyword_start, selected_range)? use_keyword_start >= let_toml_path_start? NO.
- position_within(end_of_use_line, selected_range)? end_of_use_line >= let_toml_path_start? Maybe yes if end_of_use_line > let_toml_path_start.
- position_within(let_toml_path_start, statement_range)? let_toml_path_start >= use_keyword_start? YES. let_toml_path_start <= end_of_use_line? NO (statement 2 starts after statement 1 ends).
Hmm. So
position_within(let_toml_path_start, statement_range)istrue && false= FALSE.- position_within(end_of_body, statement_range)? end_of_body >= use_keyword_start? YES. end_of_body <= end_of_use_line? NO.
So
overlapsreturns FALSE. Socan_extract_statementreturns FALSE for the use statement.OK so the use statement is not selected. Then we fall through to
visit_typed_statementwhich visitsuse_.call.The call's location is (use_keyword_start, end_of_body). For selected_range = (let_toml_path_start, end_of_body):
- position_within(let_toml_path_start, call_range)? YES (let_toml_path_start > use_keyword_start and let_toml_path_start < end_of_body).
- position_within(end_of_body, call_range)? YES (end_of_body >= use_keyword_start and end_of_body <= end_of_body).
So
overlapsreturns TRUE. Socan_extract_expressionreturns TRUE for the call.Wait, but
can_extract_expressionfor Call returns!completely_within(selected_range, expression_range). Let me re-check.completely_within(selected_range, call_range):- selected_range.start > call_range.start? YES.
- selected_range.start < call_range.end? YES.
- selected_range.end > call_range.start? YES.
- selected_range.end < call_range.end? NO (they're equal).
So
completely_withinreturns FALSE. So!completely_withinreturns TRUE. Socan_extract_expressionreturns TRUE.So
visit_typed_exprwould extract the call. But my test shows it doesn't. So there must be something else preventing it.Wait! Let me re-read visit_typed_expr:
fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { if self.function.is_none() { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } ... }OK so if
self.function.is_none()andcan_extract_expressionis true, we extract.In my test, when we visit statement 1 (use priv),
self.functionis None. So we should extract the call.But my test shows we DON'T extract the call. So something is different.
Let me check the actual selection. The selection is
find_position_of("let toml_path")tofind_position_of("FailedToWriteGleamToml").under_last_char(). Theunder_last_charmeans the last character of "FailedToWriteGleamToml", which isl.So selected_range.end is at the position of the last
linFailedToWriteGleamToml. That's NOT at the end of the body. The end of the body is at}(the closing brace of the function).So
selected_range.end < call_range.endIS true (because selected_range.end is before the}).Wait, but the call's end position is
sequence_location.end. What'ssequence_location.end?Looking at
infer_use:fn infer_use( &mut self, use_: UntypedUse, sequence_location: SrcSpan, mut following_expressions: Vec<UntypedStatement>, ) -> TypedStatement { // ... let call_location = SrcSpan { start: use_.location.start, end: sequence_location.end, };And
sequence_locationis passed from whereinfer_useis called. Let me check.- For Call:
Assistant
Let me look at where
infer_iter_statementsis called to understandlocation:Assistant
OK so
locationis from the first statement to the last statement of the function body. Sosequence_location.endis the end of the LAST statement.So
call_location.endis the end of the last statement, NOT the end of the function body (i.e., not including the}).So when I select from
let toml_pathtoFailedToWriteGleamToml.under_last_char() (which isl), my selected_range.end is BEFORE call_location.end.So
completely_within(selected_range, call_range):- selected_range.start > call_range.start? YES.
- selected_range.start < call_range.end? YES.
- selected_range.end > call_range.start? YES.
- selected_range.end < call_range.end? YES (because under_last_char is before the end of
FailedToWriteGleamToml).
So
completely_withinreturns TRUE. So!completely_withinreturns FALSE. Socan_extract_expressionreturns FALSE.So the call is NOT extracted!
OK so my test passes correctly because the selection doesn't extend to the end of the body.
In the bug, the user's selection likely extends to the end of the body. Let me test that.
Actually wait. Let me re-read the bug:
simplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml) // <---------------------------------------------------------------- to here }So the user's selection ends at
// <--- to here. The// <--- to hereis on its own line, BEFORE the closing}. So the selection ends at the end of that comment line, which is BEFORE the closing}.But the closing
}is AFTER the last statement (|> result.replace_error(FailedToWriteGleamToml)). So the selection.end is on the comment line, which is AFTER the last statement.Hmm. Let me look at the actual line:
|> result.replace_error(FailedToWriteGleamToml) // <---------------------------------------------------------------- to here }So the
// <--comment is on its own line. The selection extends to the end of this line.The last statement's end position is at the end of
FailedToWriteGleamToml). The comment// <-- to herestarts AFTER the last statement ends.So selection.end > last_statement.end.
So call_location.end = last_statement.end = end of
FailedToWriteGleamToml). And selection.end = end of the// <-- to herecomment line.So selection.end > call_location.end. So
selected_range.end < call_range.endis FALSE. Socompletely_withinreturns FALSE. So!completely_withinreturns TRUE. Socan_extract_expressionreturns TRUE. So we extract the call!THAT'S the bug! In my test, the selection ended inside the last statement, so it was completely within. But in the user's case, the selection extends to the comment line AFTER the last statement, so it's not completely within.
Let me update my test:
Assistant
EXCELLENT! NOW I've reproduced the bug! Look at the output:
----- AFTER ACTION pub fn main() { function() } fn function() -> a { use priv <- result.try( ... ) ... }The entire body is extracted! The first
use priv <- ...anduse _ <- ...are also extracted.So the bug is reproduced when the selection extends BEYOND the last statement (e.g., to the closing
}).Now let me fix it.
Assistant
Good, the snapshot file is gone. Now let me apply the fix.
Let me look at the structure of the fix more carefully:
Assistant
Now let me add the fix. I'll add a flag
inside_use_calland use it to prevent the use's call from being extracted:Assistant
Now let me apply the fix. First, add the flag to the struct:]<]minimax[>[<tool_call> ]<]minimax[>[<invoke name="edit --file language-server/src/code_action.rs 10395:10399 << 'EOF' /// Since the
visit_typed_statementvisitor function doesn't tell us when /// a statement is the last in a block or function, we need to track that /// manually. last_statement_location: Option, /// When visiting a pipeline step, this will hold the type of the value /// returned by the previous step (if any!) previous_pipeline_assignment_type: Option<Arc>, /// Ausestatement's call expression has a location that spans from the ///usekeyword all the way to the end of the block that follows it, /// because the use statement consumes the rest of the block as its /// callback. When we visit such a call expression, we don't want to /// consider it as extractable as a whole, because the use statement /// already handles the extraction of its body. inside_use_call: bool, }]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>Assistant
Now let me update the
newmethod to initialize the flag:Assistant
Now let me update the
visit_typed_exprmethod to check the flag:Assistant
Now let me add the
visit_typed_useoverride. Let me look at where to add it:Assistant
Now let me add the visit_typed_use override. I'll add it after visit_typed_expr:
Assistant
Now let me run the test:
Assistant
Hmm wait, still showing the buggy output. Let me check my changes:
Assistant
Wait the fix should work. Let me check the snapshot file to see what's actually happening:
Assistant
Hmm, still buggy. My fix didn't work. Let me think again.
Wait. Let me look at the visit order more carefully. Looking at the
ast::visit::visit_typed_function:for statement in &fun.body { v.visit_typed_statement(statement); }So we iterate statements. For each statement, we call
visit_typed_statement. The extractor overrides this method.For statement 1 (use priv):
can_extract_statement(stmt1)returns false (no overlap).- Fall through to
ast::visit::visit_typed_statement(self, statement). - This calls
visit_typed_use(use_). - The extractor overrides
visit_typed_useand setsinside_use_call = true. - Then calls
ast::visit::visit_typed_use(self, use_). - This calls
visit_typed_expr(&use_.call). - The extractor overrides
visit_typed_expr. Sinceinside_use_callis true, we don't extract the call. - Then
ast::visit::visit_typed_expr(self, expression)visits the call's children. - The call's children include the anonymous function. We visit it.
- The anonymous function's body is visited via
visit_typed_expr_block. - For each body statement,
visit_typed_statementis called.
For statement 3 (let toml_path):
can_extract_statement(stmt3)returns true.self.functionis updated to include stmt3.- Then
ast::visit::visit_typed_statement(self, statement)visits stmt3.
Then we return from the visit of use priv's call. The visit_typed_use restores
inside_use_call.Then statement 2 (use _) is visited similarly.
But wait, there's still an issue. The
visit_typed_exprfor the anonymous function's body... let me look at that.Actually, I think the issue might be that the use's call expression is being visited multiple times. Let me check more carefully.
Let me add some debug prints:
Assistant
OK so visit_typed_use is called for 3 use statements. The last one is for
use gleam_toml <- result.try(...)which goes from 379 to 702 (end of body).Hmm. So my fix should be working. Let me check the visit_typed_expr for the call:
Assistant
Hmm, all calls have
inside_use_call=trueso they shouldn't be extracted. But the test is still failing. Let me check whatfunction.is_none()is when each Call is visited:Looking at the output, all calls have
inside_use_call=trueandfunction.is_none()=false. So they should NOT be extracted.But the test is still showing the bug. Let me look more carefully. The test output shows the entire body is extracted as a function. So something else is happening.
Let me look at what could be happening. Maybe there's another place where the call is being visited outside of
visit_typed_use?Assistant
Hmm, only the one in ExtractFunction is the relevant one. Let me check the snapshots directory:
Assistant
Wait, the test output still shows the bug. But my debug print showed
function.is_none()=falsefor all calls. Sofunctionwas set before the calls were visited.Wait, this is weird. Let me check the actual order. Looking at the output:
visit_typed_use: location=19 to 702(first use)visit_typed_expr: Call at location=19 to 702, inside_use_call=true, function.is_none()=false
So
function.is_none()=falsealready when we visit the first use's call. That meansfunctionwas set BEFORE the first use statement was visited.Hmm. So something sets
functionbefore. Let me check.Actually, maybe the bug is that
functionis being set invisit_typed_functionor somewhere else before statements are visited. Let me check.Assistant
Hmm. So
visit_typed_functiononly setsfunction_end_positionandlast_statement_location, then iterates statements.Then
visit_typed_expr_block(which is for blocks inside expressions) setslast_statement_locationand iterates statements.Wait. But the function body itself doesn't go through
visit_typed_expr_block. The function body is a Vec, not a block.But the visit DOES call visit_typed_expr_block for the use's callback. Let me trace again.
Actually wait. The use's call has an anonymous function as its last argument. The anonymous function has a body of statements. The body of the anonymous function is a Vec1, NOT a TypedExpr::Block.
So when we visit the anonymous function, we visit each statement directly (no block).
Hmm. So where is the bug?
Let me add more debug:
Assistant
Hmm no visit_typed_stmt output. Let me check the source.
Assistant
OK so visit_typed_statement does check can_extract_statement first. Let me run the test and capture all output:
Assistant
AH HA! Now I can see the bug clearly!
Look at the first line:
visit_typed_statement: location=19 to 702, can_extract=true, function.is_none()=true, inside_use_call=falseSo the FIRST use statement (location 19 to 702) has
can_extract=true! That means the use statement IS considered selectable.But wait, I said earlier that the use statement's location is from
use privtoresult.try(...). Let me check.Actually, the location for a Use statement is
use_.location.merge(&use_.call.location()). So for the first use, it's fromuse priv(start of use) to the end of the use's call, which spans the entire body (because the call's location is fromuse privto end of body).So the first use's location is from 19 to 702 (entire body).
And
can_extract_statementfor Use returns!completely_within(selected_range, statement_range). The statement_range is from 19 to 702. The selected_range is fromlet toml_pathto}.under_last_char().Is selected_range completely within statement_range?
- selected_range.start > statement_range.start? YES.
- selected_range.start < statement_range.end? YES.
- selected_range.end > statement_range.start? YES.
- selected_range.end < statement_range.end? Hmm, selected_range.end is at
}.under_last_char() which is BEFORE the closing}. And statement_range.end is at end of last statement (before the closing}).
Hmm wait. Looking at the output, location is 19 to 702. But I had said the use's call_location.end is sequence_location.end which is end of last statement. So the use's location would be from
use priv(19) to end of last statement.But the selected range ends at
}.under_last_char() which is BEFORE}. Let me check the source code again:pub fn main() { use priv <- result.try(...) use _ <- result.try(...) let toml_path = ... ... }The
}is on its own line at the end. Theunder_last_charof}is the}character itself. So selected_range.end is the position of}.But the last statement is
|> result.replace_error(FailedToWriteGleamToml). This statement ends before the}. So statement_range.end (702) < selected_range.end (position of}).So selected_range.end > statement_range.end. So
selected_range.end < statement_range.endis FALSE. Socompletely_withinreturns FALSE. So!completely_withinreturns TRUE. Socan_extract_statementreturns TRUE.So the use statement is selected. Then in
visit_typed_statement:TypedStatement::Use(use_) => { Some(ExtractedFunction::new(ExtractedValue::Use { location: use_.call.location(), type_: statement.type_(), })) }Sets
self.functionto ExtractedValue::Use with the use's call location. The use's call location is the entire body (19 to 702).Then
code_actions:ExtractedValue::Use { location, type_ } | ... => self.extract_code_in_tail_position( location, location, type_, extracted.parameters, end, ),So the entire body is extracted. That's the bug!
Wait but in my earlier analysis I thought the bug was that the visit_typed_expr for the call was extracting it. But actually the bug is that the Use statement itself is being detected as selectable because the use's location spans the entire body.
So my fix needs to be different. The issue is that when computing the use statement's location, it includes the entire body. So
can_extract_statementfor a use returns true if any selection overlaps with the body.Actually wait, let me re-think. Looking at:
Statement::Use(use_) => use_.location.merge(&use_.call.location()),The use's location is from
use privto end ofuse_.call.location(). Anduse_.call.location()is fromuse privto end of body (sequence_location.end = end of last statement).So the use's location is from
use privto end of body. That's the entire body.For can_extract_statement, this overlaps with any selection within the body. So it's always selected if the selection is within the body.
That's the issue! The use's statement's location is too large.
Hmm. Let me look at how this is supposed to work. Let me look at the comment in
TypedStatement::location:// A use statement covers the entire block: `use_.location` covers // just the use's first line and not what comes after it. Statement::Use(use_) => use_.location.merge(&use_.call.location()),The comment says the use's location covers the entire block. That's by design!
So this is intentional behavior. The use statement's location includes the rest of the block. So when we ask "is this use selected?", we ask if the selection overlaps with the use's location (which spans the entire block).
But that's problematic because if the user selects a statement INSIDE the block (not the use itself), we still consider the use as "selected".
OK so this is a tricky case. The use's location is intentionally large. We need to handle this carefully.
Let me think about the design. When the user selects statements 3-6 (inside the use's body), the use statement is NOT selected. Only statements 3-6 are selected.
So the fix should be: for a Use statement, the "can be extracted" check should only return true if the selection actually overlaps with the USE PART of the use statement (not its body).
Let me look at how to fix this.
Option 1: Change the location of a Use statement to just
use_.location(the use part), not the entire body.Option 2: Add a special case in
can_extract_statementfor Use statements to use justuse_.location.Let me see what happens if we change the location. Other parts of the code use
statement.location()for various things. Changing it might have side effects.Looking at the comment:
// A use statement covers the entire block: `use_.location` covers // just the use's first line and not what comes after it. Statement::Use(use_) => use_.location.merge(&use_.call.location()),The intent is that the use statement's location covers the entire block. This makes sense for things like "where does this use start" (e.g., for error messages). But for extract function, we want the location to be just the use part.
Let me look at the can_extract_statement:
ast::Statement::Use(_) | ast::Statement::Assert(_) => { !completely_within(selected_range, statement_range) }For Use and Assert, the rule is "don't extract if the selection is completely within the statement".
For a Use statement, if the selection is completely within the use's body (i.e., the body of the use statement), we don't want to extract the use. We want to extract the body statements.
Hmm. So we want the use's statement range to be just the use part, not the entire body.
But changing the location has side effects. Let me think of another approach.
Approach: In
can_extract_statement, for Use statements, use a different range (just the use part).Let me check the
use_.locationwhich is the use part:/// This is the location of the whole use line, starting from the `use` /// keyword and ending with the function call on the right hand side of /// `<-`. /// /// ```gleam /// use a <- result.try(result) /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ /// ``` /// pub location: SrcSpan,So
use_.locationis theuse a <- result.try(result)part. That's what we want to consider as the "use statement's location".For
can_extract_statement, if the selection overlaps with the use part (but not the body), we extract the use. If the selection is only within the body, we don't extract the use.Hmm wait, actually,
use_.call.location()is just the call expression, not the body. Let me check.Looking at infer_use:
let use_call_location = use_.call.location(); let mut call = get_use_expression_call(*use_.call);So
use_.call.location()is the call's location. But this is the UNTYPED call before desugaring.For
use priv <- result.try(application.priv_directory(...)):- The untyped call is
result.try(application.priv_directory(...)). - Its location is just the
result.try(application.priv_directory(...))part.
Then after desugaring:
let call_location = SrcSpan { start: use_.location.start, end: sequence_location.end, };The TYPED call's location is from the use keyword to the end of the body. This is what's stored in
use_.call.location()AFTER typing.So
use_.call.location()(typed) = entire body. Anduse_.location= just the use part.So the use's typed statement location is
use_.location.merge(&use_.call.location())= entire body.OK so the issue is clear. Let me think about the fix.
Approach 1: In
can_extract_statementfor Use, useuse_.location(just the use part, not the entire body).Let me check:
ast::Statement::Use(_) | ast::Statement::Assert(_) => { !completely_within(selected_range, statement_range) }For Use, if we use
use_.location:- selected_range = from
let toml_pathto}. - use_part_range = from
use privto end ofresult.try(...). - These don't overlap (selected_range.start > use_part_range.end).
- So
overlapsreturns false. Socan_extract_statementreturns false.
OK that's what we want. But what if the user selects the use statement itself?
- selected_range = from
use privtoresult.try(...). - use_part_range = from
use privto end ofresult.try(...). - These overlap.
can_extract_statementreturns true.
Let me implement this fix. But wait, I need to figure out how to access
use_.locationfromcan_extract_statement. Let me look at the function signature:Assistant
So I need to modify
can_extract_statementto handle Use statements specially. For Use, we should useuse_.locationinstead of the entire statement location.Let me implement this fix:
Assistant
Wait, but the
overlapscheck at the beginning ofcan_extract_statementis already done. Let me re-read:fn can_extract_statement(&self, statement: &TypedStatement) -> bool { let statement_range = self.edits.src_span_to_lsp_range(statement.location()); let selected_range = self.params.range; // If the selected range doesn't touch the statement at all, then there // is no reason to extract it. if !overlaps(statement_range, selected_range) { return false; } ...So
overlaps(statement_range, selected_range)is checked. For a Use, statement_range = entire body (which always overlaps with selection inside the body).So we need to check overlap with
use_range(just the use part) too.Hmm. Let me refine the fix. Let me revert the previous addition and do it more cleanly:
Assistant
Wait. Looking at the existing
overlapscheck at the top ofcan_extract_statement:if !overlaps(statement_range, selected_range) { return false; }This returns false if there's no overlap with the use statement (which is the entire body). If the user's selection is inside the body, this returns true.
But for our fix, we want to check overlap with
use_.location(just the use line). If the user's selection doesn't overlap with the use line, we don't extract the use.So we need to do an additional check inside the Use branch.
Let me update the fix:
Assistant
Let me run the test now:
Assistant
GREAT! Now the test is showing correct behavior:
pub fn main() { use priv <- result.try(...) use _ <- result.try(...) function() } fn function() -> Nil { let toml_path = ... use gleam_toml <- result.try(...) let gleam_toml = ... simplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml) Nil }The first two
usestatements are kept in the original function. Only the selected statements (3-6) are extracted.The test failure is just because the snapshot doesn't exist yet (the test hasn't been accepted). Let me check the snapshot file:
Assistant
The fix works correctly. Now let me remove the debug prints and verify the fix doesn't break existing tests:
Assistant
Wait, I realize I added the
visit_typed_useandinside_use_callfield, but they may not be needed if the fix tocan_extract_statementis sufficient. Let me check if theinside_use_callflag is still needed.Actually, let me think about this. With the fix to
can_extract_statement, the use statement is no longer considered "selected" when the selection is only inside its body. So the bug should be fixed.But wait, the visit_typed_expr for the use's call could still be triggered. Let me trace:
For statement 1 (use priv):
can_extract_statement(stmt1)now returns false (because the use_line_range doesn't overlap with selection).- Fall through to
ast::visit::visit_typed_statement(self, statement). visit_typed_use->visit_typed_expr(use_.call).visit_typed_exprfor the call:self.function.is_none()is true.can_extract_expression(call)returns true (because the call spans the entire body, and the selection ends after the call).- So
self.functionis set to ExtractedValue::Expression(call)!
UH OH. So the bug is back! Even with the fix to
can_extract_statement, the visit_typed_expr for the use's call still extracts the call.So I need BOTH fixes:
- Fix
can_extract_statementto not consider use statements as selected when the selection is only in their body. - Fix
visit_typed_exprto not extract use's call expressions (viainside_use_callflag).
OK so the inside_use_call flag is still needed. Let me verify:
Assistant
The fix still works! But the test is failing because the snapshot doesn't exist. Let me check what's there:
Assistant
The output is correct! Now let me run all the existing tests to make sure I haven't broken anything:
Assistant
1537 passed, only my temporary test failed (because it doesn't have a snapshot).
Let me also check what tests pass. Let me also run the compiler-core tests to make sure nothing's broken:
Assistant
Now let me remove my temporary test and clean up:
Assistant
Now let me remove my test from action.rs since I added it just for debugging:
Assistant
Good, the test is removed. Now let me run all tests to verify nothing is broken:
Assistant
All 1537 tests pass! Let me also verify my fix by writing a temporary test:
Assistant
The fix is working correctly. The test output shows:
use priv <- ...is kept in the original functionuse _ <- ...is kept in the original function- Only statements 3-6 are extracted into the
functionfunction
Let me clean up my test:
Assistant
Let me run the tests one more time to make sure everything's clean:
Assistant
All 1537 tests pass. Let me verify my fix is still in place:
Assistant
Good. Now let me think about edge cases.
Let me think about what edge cases I should consider:
-
Single use statement at the beginning, with no other statements selected - the entire body should be extracted (since the use spans everything). This should work with my fix because
can_extract_statementreturns true if the selection overlaps with the use line. -
Use statement at the END of a function - the use's call location extends to the end of the body, so the use's call location is just the use line itself (no body after it). The use's statement location is from use line to end of use line. So
can_extract_statementshould work correctly. -
Use statement selected exactly - the use line is selected, should extract the use.
-
Use statement selected with some inner statements - this is the bug case, should extract only the inner statements.
-
Multiple use statements - should work correctly.
Let me think about the test case where the user selects the use statement itself. Let me see:
-
Assistant
These tests already exist. Let me verify they still pass:
Assistant
Both pass! Let me check the existing extract_use_in_tail_position snapshots:
Assistant
Good, the existing test for extracting a use statement still works. Let me also check edge cases.
Let me think about a case where the use statement is selected and there's only the use line (no body):
Actually wait,
use <- wibbleis the use line, then123is the body. So the body has one statement.When the user selects the use line, we should extract the entire body. Let me trace through:
For
use <- wibble:use_.location= the use line (fromusetowibble).use_.call.location()= the entire body (fromuse <-to123).- statement_location =
use_.location.merge(&use_.call.location())= entire body. - selected_range = use line (from
usetowibble). - For
can_extract_statement:use_line_range= use line range.selected_rangeoverlaps withuse_line_range? YES (selected_range = use_line_range).!completely_within(selected_range, use_line_range)? selected_range == use_line_range, so they're not strictly within. Returns true.
- So
can_extract_statementreturns true.
Then
visit_typed_statement:match statement { TypedStatement::Use(use_) => ... }- setsself.functiontoExtractedValue::Use { location: use_.call.location(), ... }.- This is the entire body. Then
extract_code_in_tail_positionextracts it.
Now let me also think about when the use is at the END of the function. Like:
fn foo() { let x = 1 use priv <- result.try(...) }The use's body is empty. So
use_.call.location()should just be the call (no body).Let me check this scenario. Actually for the existing code, the use's call location is from
use privtosequence_location.endwhich is end of body. If there's no body after the use, then it's just the use line itself.Hmm but actually, looking at the desugaring:
if following_expressions.is_empty() { let todo = Statement::Expression(UntypedExpr::Todo { location: use_.location, message: None, kind: TodoKind::IncompleteUse, }); statements.push(todo); }If there's no body after the use, a
todois added. So the use always has a body.OK so the use's call always extends to the end of the body.
Now let me think about another edge case. What if the use statement is the LAST statement of a function?
fn foo() { use priv <- result.try(...) }The use is the only statement. Its body would have just the todo. The use's call location extends from
use privtotodo. The use's statement location extends fromuse privtotodo.So when the user selects the use line,
can_extract_statementreturns true. The use is extracted.What if the user selects just the use line and nothing else?
can_extract_statementchecks overlap withuse_line_range. If selection is within use_line_range, returns true. So the use is extracted.OK let me think about another case. What if the user selects statements WITHIN the use's body, but the use is NOT the first statement?
fn foo() { let x = 1 use priv <- result.try(...) let y = 2 }Here:
- Statement 1:
let x = 1 - Statement 2:
use priv <- result.try(...)(with body containinglet y = 2) - Statement 3: doesn't exist (the
let y = 2is inside the use's body)
For statement 2 (use priv), the body contains
let y = 2. The use's call location is fromuse privto end of body (which islet y = 2). The use's statement location is the same.If the user selects
let y = 2:- selected_range =
let y = 2line. - For statement 1 (let x): no overlap, skip.
- For statement 2 (use priv): use_line_range =
use priv <- ...line. selected_range doesn't overlap with use_line_range. Socan_extract_statementreturns false. - Visit use's call. The call's location is from
use privto end of body. selected_range overlaps with call.can_extract_expression(call)for Call:!completely_within(selected_range, call_range). selected_range is completely within call_range? selected_range.end < call_range.end? YES (sincelet y = 2ends before call's end). Socompletely_withinreturns TRUE. So!completely_withinreturns FALSE. Socan_extract_expressionreturns FALSE. - So no extraction.
But wait, the visit visits the call's arguments, which include the anonymous function. The anonymous function's body has
let y = 2. When we visit that, we visit it as a statement.For the
let y = 2statement:can_extract_statementreturns true (overlap with selection).self.functionis updated to include it.
So
let y = 2gets extracted.OK so the fix should work for this case.
Now let me think about a case where the user selects from inside a use's body to outside:
fn foo() { use priv <- result.try(...) let y = 2 // part of use's body let z = 3 // not part of use's body (after use) }Hmm wait, the use's body includes everything after it until the end of the block. So
let y = 2andlet z = 3are both inside the use's body.So the use's call location is from
use privto end of body (which islet z = 3).If user selects
let y = 2:- For statement 1 (use priv): use_line_range =
use priv <- ...line. selection doesn't overlap.can_extract_statementreturns false. - Visit use's call. call_range = entire body. selected_range =
let y = 2.can_extract_expression(call)returns false (selected_range is completely within call_range). - Visit call's args. Anonymous function's body has
let y = 2andlet z = 3. - For
let y = 2:can_extract_statementreturns true. Extracted.
Good.
Now let me think about another edge case. What if there's a use in the middle of a function:
fn foo() { let x = 1 use priv <- result.try(...) let y = 2 let z = 3 }Statements in the original code:
let x = 1(Statement 1, outside use's body)use priv <- result.try(...)(Statement 2, use statement)let y = 2andlet z = 3are inside use's body
If user selects
let y = 2:- Statement 1 (let x): no overlap, skip.
- Statement 2 (use priv): use_line_range doesn't overlap with selection.
can_extract_statementreturns false. - Visit use's call. call_range = entire body. selection doesn't overlap with... wait, selection is
let y = 2, which IS inside the body. Socan_extract_expression(call)- call_range = fromuse privto end of body. selected_range =let y = 2. selected_range completely within call_range? YES. So!completely_withinis FALSE. Socan_extract_expressionreturns FALSE. So we don't extract the call. - Visit call's args (anonymous function with body
let y = 2; let z = 3). - For
let y = 2:can_extract_statementreturns true. Extracted.
Good.
If user selects both
let y = 2andlet z = 3:- Statement 1 (let x): no overlap, skip.
- Statement 2 (use priv): use_line_range doesn't overlap.
can_extract_statementreturns false. - Visit use's call. selected_range =
let y = 2tolet z = 3. selected_range completely within call_range? YES. Socan_extract_expressionreturns false. - Visit call's args.
- For
let y = 2: extract. - For
let z = 3: extract. Merge with previous.
Now let me think about what happens if the user selects the use line and the first statement of its body:
fn foo() { use priv <- result.try(...) let y = 2 let z = 3 }Selection:
use priv <- ...tolet y = 2.- Statement 1 (use priv): use_line_range =
use priv <- ...line. selected_range overlaps with use_line_range.can_extract_statementreturns true. self.function = ExtractedValue::Use { location: use_.call.location() }. This is the entire body.- Extract the entire body.
Hmm, but the user only selected the use line and the first statement of its body. So maybe they wanted to extract only those two? Let me think.
Actually if the user selected the use line and the first statement, the use's call location includes both. So extracting the entire body makes sense.
But wait, the user's selection is
use priv <- ...tolet y = 2. The end is atlet y = 2. So they're selecting the use and the first statement.The use's call location is from
use priv <- ...to end of body. The selection is fromuse priv <- ...tolet y = 2. The selection is partially within the call (overlaps with it but doesn't extend to the end).Hmm. So the user might want to extract just the use + first statement, not the entire body.
But the current behavior extracts the entire body. Hmm.
Actually, looking at the existing test
extract_use_in_tail_position:pub fn main() { use <- wibble 123 }Selection:
usetowibble. The use line is selected.Output:
pub fn main() { function() } fn function() -> Int { use <- wibble 123 }The entire body (use + 123) is extracted. So this is the expected behavior.
OK so my fix preserves this behavior.
Now let me think about another edge case. What about the
inside_use_callflag - does it break anything?The flag is set when visiting a use's call expression. It prevents the call from being extracted. But it doesn't prevent children of the call from being extracted. So children (like the anonymous function's body) can still be visited normally.
OK let me also think about cases where the use statement is INSIDE a block:
fn foo() { let x = { use priv <- result.try(...) let y = 2 } let z = 3 }Here, the use is inside a block. The block is the value of
let x = {...}.The use's call location is from
use priv <- ...to end of block body (which islet y = 2). The block itself has a different location.If user selects
let y = 2:- Statement 1 (let x = {...}): value_range = block location. selected_range might overlap with block but not completely within block? Let me check.
- The block contains
use priv <- ...andlet y = 2. selected_range islet y = 2. - The block's location is from
{to}. selected_range is completely within the block. - So
can_extract_statementfor let x returns false (becausewithin(selected_range, value_range)is true, meaning the selection is within the value range).
Wait, looking at the code:
ast::Statement::Assignment(assignment) => { let value_range = self .edits .src_span_to_lsp_range(assignment.value.location()); !within(selected_range, value_range) && !completely_within(selected_range, statement_range) }!within(selected_range, value_range)- means the selection is NOT within the value range. For our case, the selection is within the value range (the block). So this returns false. So we don't extract the assignment.OK so we fall through to visiting the assignment, which visits the block.
The block has the use and
let y = 2. Visit block.In visit_typed_expr_block:
- update last_statement_location.
- iterate statements.
For statement inside block (use priv): similar trace as before.
can_extract_statementreturns false. Visit use's call. ...OK this is getting complex. Let me just run all the tests to make sure nothing's broken.
Let me check the visit_typed_expr_block override I have:
Assistant
Let me also look at the visit_typed_expr_block to see how it interacts:
Assistant
Good. Now let me think about whether my fix has any unintended consequences.
The
inside_use_callflag prevents the use's call from being extracted. But the use's call's children (the anonymous function and its body) are still visited normally. So variables used in the use's call's arguments are still captured.Hmm wait, the
inside_use_callflag is set invisit_typed_use. But what if the use is visited via a different path? Let me check.Looking at the visit module,
visit_typed_useis only called when iterating statements (viavisit_typed_statement). So the flag is correctly set/restored.But wait, what about nested uses? Like:
fn foo() { use a <- result.try(fn(x) { use b <- result.try(...) ... }) }For the outer use, we set inside_use_call = true, then visit the call. The call's arguments include an anonymous function. The anonymous function's body has
use b <- .... We visit that use. We set inside_use_call = true again. Then visit the inner use's call. Then restore inside_use_call = false (back to true from outer).OK the flag is properly handled for nested cases.
Let me also think about what happens if there's a use inside an existing extracted function (like the use's call inside an extracted expression):
Actually, the flag is set ONLY when we're in
visit_typed_use. Once we leave the use, the flag is restored. So nested cases should work.OK let me also verify my fix works with the existing tests by running them:
Assistant
All 179 extract tests pass.
Now let me think more about the fix. There might be an issue with how
can_extract_statementis now handling use statements. Let me re-read my changes:Assistant
Hmm, the
overlapscheck at the beginning ofcan_extract_statementis forstatement_range(the full use statement location). Since the use statement covers the entire body, this is always true. Then I checkoverlaps(use_line_range, selected_range)to ensure the selection is actually on the use line.So:
- If selection is on the use line:
overlaps(use_line_range, selected_range)= true. And!completely_within(selected_range, use_line_range)= true (because selected_range might extend beyond use_line_range, e.g., includes some body too). Socan_extract_statementreturns true. - If selection is entirely inside the use's body (not on use line):
overlaps(use_line_range, selected_range)= false. Socan_extract_statementreturns false.
Wait, actually
!completely_within(selected_range, use_line_range)- let me think.For the case where selection is on the use line:
- selected_range = use_line_range. So
completely_within(selected_range, use_line_range)is false (because they have the same extremes). So!completely_withinis true.
For the case where selection extends beyond the use line into the body:
- selected_range includes use_line_range and some body.
completely_within(selected_range, use_line_range)is false (because selected_range extends beyond use_line_range). So!completely_withinis true. Combined withoverlaps, returns true.
For the case where selection is entirely within the body:
overlaps(use_line_range, selected_range)is false. So we return false (because of short-circuit).
So my fix should work for these cases. But wait, the case where the use statement is selected might need a different behavior. Let me check the existing test:
extract_use_in_tail_position:pub fn main() { use <- wibble 123 }Selection:
usetowibble.For statement 1 (use <- wibble):
- use_line_range = from
usetowibble. - selected_range = from
usetowibble. overlaps(use_line_range, selected_range)= true.!completely_within(selected_range, use_line_range)- selected_range.start == use_line_range.start. selected_range.end == use_line_range.end. Socompletely_withinis false (because start and end are equal). So!completely_withinis true.can_extract_statementreturns true.
Let me think about edge cases where the use is at the end of a function with no body:
Actually, use always has a body (even if it's just
todo). So this shouldn't be an issue.Let me think about when the user selects the use line and the FIRST statement of its body, but not the rest:
fn foo() { use priv <- result.try(...) let y = 2 let z = 3 }Selection:
use privtolet y = 2.For statement 1 (use priv):
- use_line_range = from
use privto end ofresult.try(...). - selected_range = from
use privtolet y = 2. overlaps(use_line_range, selected_range)= true.!completely_within(selected_range, use_line_range)- selected_range.end > use_line_range.end. Socompletely_withinis false. So!completely_withinis true.can_extract_statementreturns true.
So we extract the use. The use's call location is from
use privto end of body. So the entire body is extracted.This is the same as the existing behavior for
extract_use_in_tail_position. So my fix is consistent.OK I think the fix is correct. Let me also clean up any extra debug prints:
- If selection is on the use line:
Assistant
Good, no debug prints.
Let me also look at the final state of my changes:
Assistant
The changes are clean. Let me also check the changelog to see if I should add an entry:
Assistant
OK so the changelog does include bug fixes. Let me look at the most recent unreleased section. Actually let me check what the next version is going to be:
Assistant
Hmm, this is 1.17.0 which is released. Let me check what the next unreleased version is:
Assistant
Let me add a changelog entry for my fix:
Assistant
Now let me run all tests again to make sure my fix is stable:
Assistant
All 1537 tests pass.
Now let me think about another edge case. What if there's a use with a more complex body that includes nested use:
fn foo() { use a <- result.try(...) use b <- result.try(...) let x = 1 }For the outer use:
- use_line_range =
use a <- ...line. - use_.call.location() = entire body (from
use a <- ...to end of body).
If user selects just
let x = 1:- For outer use: can_extract_statement returns false (use_line_range doesn't overlap).
- Visit outer use's call. selected_range is
let x = 1. call_range is entire body.completely_within(selected_range, call_range)= true. Socan_extract_expressionreturns false. - Visit outer use's call's args (anonymous function with body).
- Visit anonymous function's body.
- For inner use: use_line_range =
use b <- ...line. selected_range islet x = 1.overlaps(use_line_range, selected_range)= false. So can_extract_statement returns false. - Visit inner use's call. call_range = from
use b <- ...to end of body. selected_range =let x = 1.completely_within= true. So no extraction. - Visit inner use's call's args. Anonymous function with body. Has
let x = 1. - For
let x = 1: can_extract_statement returns true. Extracted.
Now let me think about a case where the use is selected and we want to extract it:
fn foo() { use a <- result.try(...) let x = 1 }User selects the use line
use a <- result.try(...):- For outer use: use_line_range =
use a <- ...line. selected_range = use line.overlaps= true.!completely_within(selected_range, use_line_range)= !false = true. So can_extract_statement returns true. - Visit outer use. self.function = ExtractedValue::Use { location: use_.call.location() }.
- Visit outer use's call. self.function is already set. visit_typed_expr skips the extraction check.
- Visit outer use's call's args. Anonymous function. Visit body.
- For
let x = 1: can_extract_statement returns true. But self.function is already set (to Use). Check the match:
Some(ExtractedFunction { value: ExtractedValue::Use { location, .. }, .. }) if location.contains_span(statement_location) => {}So if the statement's location is contained within the use's location, we don't update. Since the use's location is the entire body, the inner statement is contained. So we skip.
Then extract_code_in_tail_position is called for the Use. The location is the entire body. So the entire body (use +
let x = 1) is extracted.Good, this is the expected behavior.
Let me also think about another case. What if the user selects statements across multiple uses:
fn foo() { use a <- result.try(...) let x = 1 use b <- result.try(...) let y = 2 }User selects
let x = 1anduse b <- result.try(...):- For outer use: can_extract_statement returns false (use_line_range doesn't overlap with selected_range).
- Visit outer use's call. selected_range = [let x = 1, use b <- ..., let y = 2 line). Hmm wait,
use b <- ...is selected butlet y = 2is not (assuming selection ends beforelet y = 2). - Actually wait, the selection starts at
let x = 1and ends at... let me say the end ofuse b <- ...line. So selected_range = [let x = 1, end of use b line). - For outer use: can_extract_statement returns false.
- Visit outer use's call. call_range = entire body. selected_range is within call_range but doesn't cover it.
completely_within(selected_range, call_range)= false (because selected_range.start > call_range.start and selected_range.end < call_range.end - oh wait, selected_range.end < call_range.end). Socompletely_within= true. So!completely_within= false. Socan_extract_expressionreturns false. No extraction of call. - Visit outer use's call's args. Anonymous function. Visit body.
- For
let x = 1: can_extract_statement returns true. self.function = Statements { location: let x = 1, position: NotTail }. - For inner use
use b <- ...: can_extract_statement: use_line_range =use b <- ...line. selected_range starts atlet x = 1and ends at end of use b line. overlaps? selected_range.end >= use_line_range.start. selected_range.start <= use_line_range.end. YES. So overlaps.!completely_within(selected_range, use_line_range)= !false = true. can_extract_statement returns true. - The match:
Some(ExtractedFunction { value: ExtractedValue::Statements { location, position: extracted_position, }, .. }) => { *location = location.merge(&statement_location); *extracted_position = position; }- So self.function.value is now Statements { location: from let x = 1 start to inner use b end, position: NotTail (since inner use is not the last statement,
let y = 2is) }. - For
let y = 2: can_extract_statement:let y = 2is not selected (selection ends before it). So can_extract_statement returns false.
So self.function is Statements { location: [let x = 1 start, use b end], position: NotTail }.
The extracted function would include
let x = 1anduse b <- .... Then it would be replaced withfunction()in the original.Good. This is the expected behavior.
Let me also test my fix by running a quick sanity check:
- use_line_range =
Assistant
Both edge cases work correctly:
-
temp_test_edge_1: User selects the
usestatement. The entire body is extracted (as expected). -
temp_test_edge_2: User selects just
let y = 2. Only that statement is extracted (as expected).
Let me clean up my temporary tests:
-
Assistant
Let me also check that my fix doesn't have any issues with the
inside_use_callflag interaction with other tests.Let me look at the test for
extract_use_inside_block:Assistant
Good, this test still works correctly. The use statement is extracted.
Now let me think about what happens with
extract_use_with_some_statements_before:Assistant
Good. This test still works correctly. The user selects
let a = ...anduse a <- ..., and both are extracted together.Let me look at this case more carefully:
- Selection:
let a = wibble(todo)line anduse a <- wibble()line. - Statement 1 (let a = wibble(todo)): can_extract_statement returns true. self.function = Statements { location: let a, position: NotTail }.
- Statement 2 (use a <- wibble()): can_extract_statement: use_line_range =
use a <- wibble()line. selected_range = fromlet a = wibble(todo)start to end ofuse a <- wibble()line.- selected_range overlaps with use_line_range? YES (selected_range.end is at end of use line, which is at the end of use_line_range).
!completely_within(selected_range, use_line_range): selected_range.start < use_line_range.start. So selected_range is not strictly within use_line_range. Socompletely_within= false. So!completely_within= true.- So can_extract_statement returns true.
- The match:
Some(ExtractedFunction { value: ExtractedValue::Statements { location, position: extracted_position, }, .. }) => { *location = location.merge(&statement_location); *extracted_position = position; }- self.function.value = Statements { location: from let a start to use a end, position: Tail (use a is not the last statement... wait, the last statement is
Ok(Nil). So use a is NOT the last. So position = NotTail).
Hmm wait. Let me check
is_in_tail_position:let is_in_tail_position = self.last_statement_location .is_some_and(|last_statement_location| { last_statement_location == statement_location });last_statement_locationis set to the last statement's location (which isOk(Nil)). The current statement isuse a <- wibble(). Solast_statement_location != statement_location. Sois_in_tail_positionis false.But for use, the position is:
let position = if statement.is_use() || is_in_tail_position { StatementPosition::Tail { type_: statement.type_(), } } else { StatementPosition::NotTail };So for use statements, position is always Tail.
So self.function.value = Statements { location: from let a start to use a end, position: Tail }.
Then statement 3 (Ok(Nil)): can_extract_statement:
Ok(Nil)not selected. Returns false.Then in code_actions:
ExtractedValue::Statements { location, position: StatementPosition::Tail { type_ }, } => self.extract_code_in_tail_position( location, location, type_, extracted.parameters, end, ),So we extract the code in tail position. The location is from let a start to use a end. The type is the type of use a (which is
a- the function's return type).Hmm but the actual extracted function is:
fn function() -> a { let a = wibble(todo) use a <- wibble() Ok(Nil) }Wait that includes
Ok(Nil)which is NOT in the selection. Let me check.Oh I see, the
functionextracts the entire use (which includes its body). SoOk(Nil)is part of the use's body. The use is atuse a <- wibble(). Its call location extends toOk(Nil). So the extracted function includes the use + body.But wait, self.function.value is Statements { location: from let a start to use a end, position: Tail }. So the location doesn't include
Ok(Nil).Let me look at extract_code_in_tail_position:
- Selection:
Assistant
Hmm.
extract_code_in_tail_positionusescode_location(notlocation) for the expression code. Let me check where this is called from.Looking at code_actions:
ExtractedValue::Statements { location, position: StatementPosition::Tail { type_ }, } => self.extract_code_in_tail_position( location, location, type_, extracted.parameters, end, ),So
locationis passed for bothlocationandcode_location. So the code_location is the same as location.So for
extract_use_with_some_statements_before:- location = from let a start to use a end.
- code_location = same.
So the extracted function should include only
let a = wibble(todo)anduse a <- wibble(). But the snapshot showsOk(Nil)is also included.Wait. Maybe the snapshot is using the typed use's call location, not just the use line. Let me check.
Actually, looking at the visit_typed_statement more carefully:
Some(ExtractedFunction { value: ExtractedValue::Statements { location, position: extracted_position, }, .. }) => { *location = location.merge(&statement_location); *extracted_position = position; }So we merge the location with the statement_location. For use, statement_location is from use line to end of body. So the location extends to the end of the body (including
Ok(Nil)).So the location becomes from
let a = wibble(todo)start to end of body (which isOk(Nil)end). Then we extract the code from that location, which includesOk(Nil).OK so my understanding is correct. The use statement's location extends to the end of the body, so when we merge, we include the body.
But wait, that's the behavior I want to preserve. When the user selects the use + something before it, we should extract the use + body.
OK so my fix doesn't break this case.
But wait, my fix only kicks in for the use statement. Let me re-trace.
For statement 2 (use a <- wibble()):
- statement_location = use_.location.merge(&use_.call.location()) = from use line to end of body (including Ok(Nil)).
- can_extract_statement (with my fix):
- use_line_range = from
use a <- wibble()line to end ofwibble()line (just the use line, not the body). - selected_range = from
let a = wibble(todo)start to end of use line. overlaps(use_line_range, selected_range)= YES (selected_range.end >= use_line_range.end).!completely_within(selected_range, use_line_range)= !false = true (selected_range is wider than use_line_range).- So can_extract_statement returns true.
- use_line_range = from
Then we hit the Statements match arm and merge location. The location becomes from
let astart to end of body.Then in code_actions, we extract code from this location, which includes
let a,use a <- ..., andOk(Nil).Good, this is the expected behavior.
Let me run all tests once more:
Assistant
All 1537 tests pass.
Let me also verify the fix by re-applying my test case manually:
Assistant
The fix works correctly! The output shows:
use priv <- ...is kept in the original functionuse _ <- ...is kept in the original function- Only statements 3-6 are extracted into the
functionfunction
Let me clean up my temporary test:
Assistant
Good, all temporary tests are cleaned up. Let me run all tests one final time to confirm everything is good:
Assistant
All tests pass. Let me also run the compiler-core tests:
Assistant
Let me also build the whole project to make sure nothing is broken:
Assistant
The build succeeds with no errors related to my changes.
Let me also check the diff of my changes to confirm:
Assistant
The changes look clean. Let me verify by looking at git status:
Assistant
Let me now also clean up my temporary files:
Assistant
Good, all temp files are cleaned up.
Let me also verify that no test snapshots were accidentally created or modified:
Assistant
Good, no snapshot files were modified.
Let me look at the diff one more time and think about whether the fix is complete and correct:
Assistant
The fix consists of:
-
Adding
inside_use_callfield toExtractFunctionstruct: This field tracks when we're inside a use statement's call expression. -
Initializing
inside_use_calltofalsein thenewmethod. -
Special handling for Use statements in
can_extract_statement: For Use statements, we check the selection againstuse_.location(just the use line) rather than the full statement location (which spans the entire body). -
Updating
visit_typed_exprto skip the extraction check wheninside_use_callis true: This prevents the use's call expression from being extracted as a whole when we're inside a use statement. -
Adding
visit_typed_useoverride: This sets theinside_use_callflag when entering a use statement and restores it when leaving.
Let me also check if I should remove the
inside_use_callfield since the fix tocan_extract_statementmight be sufficient on its own.Actually, looking at this again, I think the
inside_use_callfield is still needed because:- The fix to
can_extract_statementprevents the use statement itself from being extracted when only the body is selected. - But the use's call expression is still visited (via
visit_typed_use->visit_typed_expr(use_.call)). If the selection extends to the end of the body (or beyond),can_extract_expression(call)would return true, and the call would be extracted.
So both fixes are needed:
- Fix 1 (can_extract_statement): For when the use statement is visited via visit_typed_statement.
- Fix 2 (inside_use_call): For when the use's call expression is visited via visit_typed_expr.
Let me verify by removing the inside_use_call fix and seeing if the bug returns:
Actually, let me not do that. The fix is working, and both changes are needed for completeness.
Let me look one more time at the code to make sure I haven't missed anything:
-
Assistant
Everything looks good. Let me also run the tests one final time to confirm:
Assistant
All 1537 tests pass.
Let me also write a test that explicitly tests the bug fix to verify it works:
Assistant
The fix works correctly. The output shows:
function()(the extracted function is called)fn function() -> Nil(the extracted function definition)
Now let me clean up my temporary test:
Assistant
All 1537 tests pass.
Let me check the final git status:
Assistant
My changes are:
language-server/src/code_action.rs- The fixCHANGELOG.md- The changelog entry
Let me review the checklist:
Checklist:
-
If an error was reported in the issue description, I have successfully reproduced it.
- Yes, I successfully reproduced the bug. The user's bug case (extracting from a function with multiple
usestatements at the beginning) was reproduced with a test case.
- Yes, I successfully reproduced the bug. The user's bug case (extracting from a function with multiple
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- Yes, my fix resolves the error. The test case shows that only the selected statements are extracted, not the entire function body.
-
I have thought about edge cases and made sure my fix handles them as well.
-
Yes, I considered:
- Single use statement at the beginning
- Multiple use statements at the beginning
- Use statement at the end of a function
- Use statement selected exactly
- Use statement selected with some inner statements
- Multiple use statements
- Selection entirely within use body
- Selection extending beyond use body
- Nested use statements
All edge cases are handled correctly.
-
-
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass.
- Yes, all 1537 existing tests pass.
Let me do one more check by examining if my fix is robust to potential issues.
Assistant
All references to
inside_use_callare correct.Let me also check the code style. The existing comments are well-written, so I should match that style:
Assistant
The comments are clear and explanatory.
Let me also check the other comment:
Assistant
Looks good.
Let me also check the can_extract_statement comment:
Assistant
All comments are clear and explanatory.
Let me also verify by running the test once more:
Assistant
All extract_function tests pass.
Let me also check the changelog entry is well-formatted:
Assistant
Good.
Now let me look at the diff once more and see if there's anything I can simplify or improve:
Assistant
The diff is clean. Let me check the test count one more time:
Assistant
All 1537 tests pass. The fix is complete.
Now let me think about whether I should also handle the case where the use statement's call is being visited directly (not as part of the use statement visit). Let me check the visitor chain:
Looking at the visitor:
visit_typed_function->visit_typed_statement->visit_typed_use->visit_typed_expr(use_.call)
So the use's call is visited via
visit_typed_usewhich we override. The override setsinside_use_call = true. So the call'svisit_typed_exprcorrectly skips the extraction check.But what if
use_.callis visited from a different path? Like if there's some expression that visits the use's call directly?Looking at the visitor pattern, the use's call is only visited via
visit_typed_use. There's no other path.Actually wait, let me check
visit_typed_statementfor Use statements:pub fn visit_typed_statement<'a, V>(v: &mut V, statement: &'a TypedStatement) where V: Visit<'a> + ?Sized, { match statement { Statement::Expression(expression) => v.visit_typed_expr(expression), Statement::Assignment(assignment) => v.visit_typed_assignment(assignment), Statement::Use(use_) => v.visit_typed_use(use_), Statement::Assert(assert) => v.visit_typed_assert(assert), } }So Use statements are visited via
visit_typed_use. Good.But wait, the default
visit_typed_useis:pub fn visit_typed_use<'a, V>(v: &mut V, use_: &'a TypedUse) where V: Visit<'a> + ?Sized, { v.visit_typed_expr(&use_.call); // TODO: We should also visit the typed patterns!! }So the default visits
use_.call. My override wraps this with the flag.But wait, the
visit_typed_expr(&use_.call)calls our override ofvisit_typed_expr. This checksinside_use_call(which is true since we set it). So it skips the extraction check.But after
visit_typed_exprreturns, the children of the expression are visited. These children include the anonymous function and its body. The body's statements are visited viavisit_typed_statement, which callscan_extract_statement. This is fine because we're insideinside_use_call, butinside_use_callis only checked invisit_typed_expr, notvisit_typed_statement.Hmm wait, that's fine because
visit_typed_statementcheckscan_extract_statementfor each statement. For each statement inside the use's body, if it overlaps with the selection, it's added toself.function.So this works correctly. The
inside_use_callflag is only needed to prevent the use's CALL from being extracted as a whole. The individual statements inside the use's body are still extractable.OK the fix is complete and correct.
Let me also verify one more time that my fix doesn't break the
extract_use_inside_blocktest:Assistant
All 7 extract_use tests pass.
Let me also verify the build works:
Assistant
Build succeeds. No warnings related to my changes.
Let me submit the fix:
Assistant
The fix is minimal and focused:
- 41 insertions, 2 deletions
- 2 files modified: CHANGELOG.md (bug fix entry) and language-server/src/code_action.rs (the actual fix)
Let me check that I have not modified any tests:
Assistant
No test files were modified.
Let me also check no snapshots were modified:
Assistant
No snapshots were modified.
Let me also check that no other test files were changed:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →