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 and locating the extract-function implementation so we can reproduce the over-extraction bug.
- Assistant
I'll look at the extract function implementation and the related partial-selection test.
- Assistant
Let me look at the extract function related code in code_action.rs
- Assistant
Let me read the ExtractFunction implementation carefully.
- Assistant
Let me continue reading the ExtractFunction implementation.
- Assistant
Let me continue reading.
- Assistant
Let me continue and also look at the visitor implementation.
- Assistant
Let me read the visitor methods that find what to extract - especially how statements are selected.
- Assistant
There's something to note about uses - let me continue reading can_extract_statement and the use visit logic.
- Assistant
Let me look at can_extract_expression and can_extract_statement more carefully.
- Assistant
I need to understand how "use" statements span works. Looking at the issue, the selected code includes:
use gleam_toml <- result.try(...) ... simplifile.write(...)There's a comment that says "a use is always eating up the entire block". So when you have
usestatements, they might span the entire rest of the function. - Assistant
Let me look at how Use statements are structured in the AST and existing related tests.
- Assistant
Let me read the full visit_typed_statement for ExtractFunction
- Assistant
Let me check what visit does for Use statements and how use locations work.
- Assistant
Important: for Statement::Use location:
- Assistant
Let me look at how use is visited and how FunctionLiteralKind::Use works.
- Assistant
Let me read visit_typed_use and completely_within functions.
- Assistant
Let me check completely_within:
- Assistant
Looking at extract_use tests and partial extraction when uses are involved.
- Assistant
There are specific tests for extract_use! Let me read those tests.
- Assistant
Let me look at these extract_use tests carefully.
- Assistant
Let me look at extractention macros and partially selected test, and the snapshots for use with statements before.
- Assistant
Let me understand locations more carefully for nested uses and look at convert_from_use for related patterns.
- Assistant
Let me read how Use extraction generates the code, and continue the visitor.
- Assistant
I opened the wrong file - need to open code_action.rs again.
- Assistant
Let me look at how the typed use-call's location is set.
- Assistant
Let me read that section.
- Assistant
Let me check TypedExpr::Call fields.
- Assistant
So CallArg has is_use_implicit_callback. Let me also check Fn kind location and how visit works for nested Fn of Use when we DON'T extract the Call. I think the core issue is that
can_extract_expressionis too eager to match parent containers when the selection is partial. The current logic!completely_within(selected_range, expression_range)combined with the top-down walk means it sometimes grabs a larger expression even when the user selected a smaller inner part.The suggestion to switch to
!withinwould break exact matches, so we need a better predicate. The pipeline example shows the right pattern: check if the selection fully covers the expression (expression_within(selected, expr)). That would let us extract the smallest expression that the selection actually covers.For the
Usecase specifically, we should also skip extracting the desugaredFnwhenkind: FunctionLiteralKind::Use. Theusestatement itself should be extracted as a statement, and if the user only selected the body, we should descend into the statements instead.I’ll implement that by:
- Updating
can_extract expressionto use a coverage check instead of strict interior. - Adding an explicit guard for
UseFns in the expression visitor. - Ensuring we still descend into the body statements when the selection is partial.
- Updating
- Assistant
Let me read extract_use_in_tail sandwiches and similar snapshots.
- Assistant
Let me check how tests work so I can reproduce the bug.
- Assistant
I've identified the bug: nested
usespans the whole remaining body, so a selection that ends at the function end matches the outer use via!completely_within. Fixingcan_extract_statement/can_extract_expressionso we only extract ausewhen the selection touches the actualuseline, and we dig into the callback body otherwise. - Assistant
I need to check FunctionLiteralKind import and CallArg is_use_implicit_callback availability.
- Assistant
Applying the fix to both can_extract_expression and can_extract_statement.
- Assistant
Now update can_extract_statement for Use.
- Assistant
Need to go to that line first.
- Assistant
Now I can apply the replace.
- Assistant
I need to check if
overlapsis imported in code_action.rs. - Assistant
There was use of overlaps but import only has completely_within, position_within. /// Let me check how overlapses is available.
- Assistant
Let me look at the assert_code_action macro and add a temporary test.
- Assistant
I'll add a temporary test based on the issue, then run existing extract_use tests and the new one.
- Assistant
I'll add a temporary test after extract_use_in_tail_position_2 that reproduces the bug.
- Assistant
There's issue with find_position_of("gleam_toml)") - it may match multiple. Let me use write(...) end instead.
- Assistant
Let me fix the selection to use write call.
- Assistant
Need to goto first.
- Assistant
Now replace.
- Assistant
Now let's run the temporary test and the existing extract_use tests.
- Assistant
Need to set up rust toolchain.
- Assistant
Now run the tests. This might take a while to compile.
- Assistant
Need hyphen not underscore.
- Assistant
The fix works. Running broader extract_function tests and then removing the temporary test.
- Assistant
32 other tests passed. Removing the temporary test and any snapshot it created.
- Assistant
I'll remove the temporary test.
- Assistant
Removing the leftover snapshot and verifying the diff is clean.
- Assistant
Good - only code_action.rs is modified. Re-running extract tests to confirm all pass.
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 →