Extract function extracts too much When triggering Extract function with the selection:…
- User
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.
- Assistant
Let me look at the existing extract-function tests involving statements inside use bodies:
- Assistant
Now let me write a reproduction test to confirm the bug:
- Assistant
Let me check the existing snapshot for
extract_use_in_tail_position_2to understand current expected behavior with nested uses: - Assistant
Now let me implement the fix. First, in
can_extract_expression— handle use callback fns and use desugared calls: - Assistant
Next, the
can_extract_statementUse arm: - Assistant
Now the third piece — override
visit_typed_useso the desugared call isn't extracted as a whole expression when the selection is only inside the callback body: - Assistant
All 1539 tests pass. Let me add one more test to lock in the improved behavior when only part of a use body is selected starting from its first statement (previously the shared-start also caused over-extraction):
- Assistant
The output is correct: only the two selected statements are extracted, with
creturned (the trailingcin the new function is the generated return value). Accept the snapshot and run the whole suite again: - Assistant
Let me also add a test that mirrors the reporter's exact shape — nested uses, a
usein the middle of the selection, and a pipeline in tail position: - Assistant
This matches the report's expected output exactly. Accept and run the full suite:
- Assistant
All tests pass. Now add a changelog entry:
- Assistant
Now let's run a final verification — clippy plus the full language server test suite:
- Assistant
Fixed. Clippy is clean and all 1541 language-server tests pass.
Root cause
When Gleam desugars
use a <- wibbleintowibble(fn(a) { ...rest of block... }), three AST nodes — theusestatement, the desugared call, and the callback fn — all have locations that end at the end of the enclosing block, with no closing delimiter after the last statement.The extract-function code action decided "did the user select this whole node?" with
!completely_within(selection, node_range), which treats a shared extreme as "whole node selected". That's correct for blocks/fns that end in}, but for use-desugared nodes any selection of trailing statements inside the use body shares its end with the whole chain of use nodes — so the outermostusegrabbed the extraction and the entire function body was extracted.Changes (
language-server/src/code_action.rs)can_extract_statement— ausestatement is now only extracted as a whole if the selection touches theuse ... <-line itself (use_.location); otherwise traversal continues into the callback body.visit_typed_useoverride — when the selection doesn't touch theuse ... <-line, the desugared call is skipped as an extraction candidate (its children are still traversed), preventing the call expression from being extracted whole.can_extract_expression— a use callbackFn(whose location starts at its first body statement, also with no delimiters) is now only extracted when the selection spans its entire body, otherwise the individual selected statements are extracted.
Tests
Four new snapshot tests in
language-server/src/tests/action.rs:- selection of trailing statements inside a use body → extracts just those, leaving
use a <- wibble+function(a) - same inside nested uses (the reported scenario)
- selection starting at the first body statement but covering only part of the body → extracts only the selected statements (previously also over-extracted via the shared start)
- selection containing a mid-body
useand a tail pipeline, mirroring the reporter's code shape — output matches the report's expected result
Also added a changelog entry under
Unreleased → Bug fixesinCHANGELOG.md(I left off the attribution line since I don't know whose name to credit — you may want to add it).
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
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- fable-5Compare side by side →
- fable-5Compare side by side →