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
Let me start by exploring the repository structure to understand what we're working with.
Assistant
This is the Gleam compiler repository. Let me explore the language server code since the issue is related to code extraction in the language server.
Assistant
Let me look at the ExtractFunction implementation:
Assistant
Now let me look at the helpers that decide whether we can extract:
Assistant
Let me look at the test cases for
extract_functionto understand how it currently works:Assistant
Let me continue looking at more tests and look at extract_function_partially_selected:
Assistant
Let me look at the
withinandcompletely_withinhelper functions:Assistant
Let me look at the engine setup and understand how to set up tests:
Assistant
Let me look at
find_position_ofandselect_until:Assistant
Let me look for the RangeSelector's
find_rangemethod:Assistant
Let me see the test setup to understand how to write a reproducer:
Assistant
Let me try running the existing tests to confirm baseline:
Assistant
Now let me create a temporary reproducer test to confirm the issue:
Assistant
Let me create a temporary test that I can run to confirm the issue:
Assistant
Let me look at the action more carefully and reproduce the exact issue:
Assistant
Let me try with a different end position:
Assistant
Let me check how the test framework handles raw strings:
Assistant
Let me first look at what happens with this specific code, and trace the logic:
Assistant
Let me first add some debug logging to trace through this:
Assistant
Let me modify the test to print all actions to debug:
Assistant
Now let me run the actual repro test that should show the issue:
Assistant
Let me try with selection ending after the function's closing brace:
Assistant
Let me add some print statements to trace through the issue:
Assistant
Let me add print statements to the source code temporarily:
Assistant
Let me add debug logging to understand what's happening:
Assistant
Let me add debug print statements:
Assistant
Let me investigate the AST locations for these statements:
Assistant
Let me look at what location is for a Use statement:
Assistant
Let me look at how
use_.call.location()is computed for the typed call:Assistant
OK so for
TypedExpr::Call, thelocationfield is the location of the whole call expression. This includes the function and ALL its arguments.For a
use priv <- result.try(application.priv_directory("gleeps") |> result.replace_error(...), fn(priv) { ... })statement, thecallfield (typed) is aTypedExpr::Call. The location of this call covers EVERYTHING fromresult.try(...)through the END of the callback function (which is the END of the block).So when the use statement's location is computed as
use_.location.merge(&use_.call.location()), it includes the entire block.This explains why all statements after the FIRST use statement have
end: 1054(the end of the function body).Now, in
can_extract_statement, we useoverlaps(statement_range, selected_range). So when the user's selection is fromlet toml_pathto end of function, evenuse priv <- result.try(...)has its END at end of function, so it OVERLAPS with the selection (since the start of selection is at line 27, but use priv ends at line 42).But wait,
overlapschecks if any corner is in the other range: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) }So for Statement 1 (line 16-42) and selection (line 27-42):
- statement.start (16) within selection? No.
- statement.end (42) within selection? Yes (27-42 includes 42).
- selection.start (27) within statement? Yes (16-42 includes 27).
- selection.end (42) within statement? Yes.
So overlaps = true. That's the BUG.
So the fix needs to address that the
Usestatement's location extends to the end of the block, which is wrong for determining the actual statement's range. We need a way to get the statement's REAL end position (not the position that extends to end of block due to the callback).Looking at
last_location:pub fn last_location(&self) -> SrcSpan { match self { Statement::Expression(expression) => expression.last_location(), Statement::Assignment(assignment) => assignment.value.last_location(), Statement::Use(use_) => use_.call.last_location(), Statement::Assert(assert) => assert.value.last_location(), } }For a Use,
last_locationreturnsuse_.call.last_location(). The call's last_location is the location of the LAST argument, which would be the callback function. The callback function'slast_locationwould be the location of the last statement in its body. So this would still cover the entire rest of the block.Hmm. Let me look at this differently. Maybe the issue is that for extract_statement, we should use the START of the next statement to determine the END of the current one, not the END of the use statement.
Actually wait, let me re-read the comment in the AST:
Statement::Use(use_) => use_.location.merge(&use_.call.location()),And the comment says:
/// 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 just the use line. Butuse_.call.location()for a TypedExpr::Call covers the whole function call INCLUDING the callback function.So merging these gives the full block coverage. This is the BUG.
Actually wait, let me look at this in code_action.rs's
can_extract_statement: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;So we use
statement.location(). For a Use, this isuse_.location.merge(&use_.call.location())which spans the whole block.I think the fix should be: for a Use statement, we should use the location of just the
useline, NOT the full extent of the callback. Because the user-visible "use statement" is just one line.But wait, there are tests that already test that a Use statement covering the entire block is correctly NOT extracted when only the inner block is selected:
// If the selected range falls completely within the expression, we don't // want to extract it. ast::Statement::Use(_) | ast::Statement::Assert(_) => { !completely_within(selected_range, statement_range) }So this is checking that if the selection is completely within the Use statement, don't extract. This makes sense because if the selection is INSIDE the Use's callback, that's a different scenario.
But this check uses
statement_range, which incorrectly extends to the end of the block.OK so the fix needs to be more careful. Let me look at what the
last_locationwould tell us.Actually, the right approach might be: for the purpose of extracting a function, we should determine the STATEMENT's location more carefully.
Looking at
last_location:- For Use, returns
use_.call.last_location(). call.last_location()forTypedExpr::Callreturns the location of the LAST element. Let me look:
pub fn last_location(&self) -> SrcSpan { match self { TypedExpr::Int { location, .. } | TypedExpr::Float { location, .. } | TypedExpr::String { location, .. } | TypedExpr::Var { location, .. } | TypedExpr::List { location, .. } | TypedExpr::Call { location, .. } ... } }For
TypedExpr::Call { location, .. }, it returns the call's location field. And forTypedExpr::Fn:Let me check:
Assistant
Let me look at how
right_hand_side_locationis set:Assistant
Let me check how the typing phase constructs the TypedUse:
Assistant
Let me see what call_location is in infer_call:
Assistant
Let me check what other places use
TypedStatement::location():Assistant
Let me check tests that depend on the current behavior:
Assistant
Now let me revert my debug print statement first:
Assistant
Now I understand the bug. Let me revert all the debug stuff:
Assistant
Let me verify by looking at the typing code:
Assistant
Let me look at additional use statement extraction tests:
Assistant
Let me code up the fix. I need to handle this in multiple places:
Assistant
Let me look at the actual construction of use's callback:
Assistant
OK so the sequence_location is the location of the use line + the rest of the statements. Then
call_location = use_.location.start to sequence_location.end. This call_location is what becomes the typed call's location.Now this is the SEQUENCE of statements, not just the immediate next one. So the typed call extends to the end of the block (containing all subsequent statements).
Hmm wait, the sequence_location is from the un-typed use's parser. Let me find where this is computed:
Assistant
Now I understand the issue. Let me implement the fix:
The issue is that
TypedStatement::Use(use_).location()returns a span that extends to the end of the block (because the typed call's location includes the callback that extends to the end of block). This is intentional in most cases - the use conceptually "owns" the rest of the block via its callback.But for
can_extract_statement, we want to determine if the selection overlaps with the use's USER-VISIBLE source code (just the use line), not the callback.Let me first verify the reproducer more carefully:
Assistant
Now let me apply the fix:
Assistant
Let me look at Assert's location:
Assistant
Now let me apply the fix:
Assistant
Let me use the replace command:
Assistant
Good. The fix is applied. Let me also remove the now-redundant comment block above the new code (the old comment was about "if the selected range falls completely within the expression"). Let me check what's there now:
Assistant
Let me clean up the redundant comment:
Assistant
Let me build and run the tests:
Assistant
All extract tests pass. Let me also run a broader set of tests:
Assistant
All 1537 language-server tests pass! Let me also run the compiler-core tests to ensure nothing else is broken:
Assistant
Now let me create a temporary reproducer to verify the fix works for the exact issue:
Assistant
The output matches the expected behavior exactly! Let me remove the temporary test and verify the snapshot:
Assistant
Let me also look at more edge case tests:
Assistant
Let me verify the snapshot tests pass:
Assistant
Let me also look at more edge cases. Let me make sure all edge cases are tested:
Assistant
Let me re-add the test to verify the behavior:
Assistant
OK so the test is the same as the original. Since I have a temp test at the END, it should be a separate test. Let me run both:
Assistant
Let me now run a comprehensive test:
Assistant
Let me check the failures:
Assistant
These failures are unrelated to my fix - they're due to
escriptnot being installed. Let me verify these tests were failing before my changes:Assistant
These failures are pre-existing and unrelated. Let me clean up the leftover snapshot files:
Assistant
Let me view the diff:
Assistant
Let me run all language server tests once more:
Assistant
Let me add an end-to-end reproducer test that matches the exact issue scenario:
Assistant
The fix produces exactly the expected output from the issue. Let me clean up:
Assistant
Now let me view the final diff:
Assistant
Let me run all tests in the language server once more:
Assistant
All 1537 tests pass. Let me also clean up the snapshot files for tests I may have created:
Assistant
Let me also clean up the temp files:
Assistant
Now let me do a final review of my changes:
Assistant
Let me go through the checklist explicitly:
Checklist Review
1. If an error was reported in the issue description, I have successfully reproduced it.
YES. I created a temporary test that reproduces the issue using the exact code and selection range from the issue description. Without my fix, the action incorrectly extracted the entire function body, leaving only
function(target, user_id)in the original function. This matches the bug described in the issue.2. If an error was reported in the issue description, I have confirmed that my fix resolves the error.
YES. After applying the fix, my temporary test produces the exact expected output from the issue - the original function keeps the
use priv <- result.try(...)anduse _ <- result.try(...)statements, then hasfunction(target, user_id)replacing the selected code. The new function contains exactly the code that was selected.3. I have thought about edge cases and made sure my fix handles them as well.
YES. The fix changes
can_extract_statementforStatement::Useto useuse_.location(which is just the use line span from theusekeyword to the end of the right-hand side expression) instead of the full statement location (which includes the desugared callback extending to the end of the block). This handles:- Selection entirely before use line:
overlapsreturns false, returns false. ✓ - Selection entirely after use line:
overlapsreturns false, returns false. ✓ - Selection completely within use line:
overlapsreturns true,completely_withinreturns true, returns false (descend further). ✓ - Selection contains use line:
overlapsreturns true,completely_withinreturns false, returns true (extract as Use). ✓ - Selection partially overlaps use line (extends past): extract as Use, the use still consumes the rest of the block via the existing merge logic. ✓
- Selection is exactly the use line boundary:
overlapsreturns true (position_within allows equality), extracted. ✓ - Two use statements before selection: only the ones whose use lines actually overlap the selection are considered. ✓
4. 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 tests in the language-server package pass, including:
extract_function_*tests (22 tests)extract_use_*tests (7 tests includingextract_use_inside_function,extract_use_inside_block,extract_use_with_some_statements_before,extract_use_including_some_statements_after,extract_use_in_tail_position,extract_use_in_tail_position_2)- All other extract tests (179 tests for "extract" prefix)
- All other action tests (1537 total)
All 3890 compiler-core tests also pass.
The pre-existing
escript_*failures intest-commandsare unrelated infrastructure issues (missingescriptcommand in the test environment) and were failing before my changes as well.- Selection entirely before use line:
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 →