Extract function extracts too much
When triggering Extract function with the selection:
rs
/// 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:
Now let me look at how Use statements report their location and check existing tests:
Assistant
Let me look at the TypedUse structure:
Assistant
Now let me check how the visitor handles Use statements:
Assistant
Let me write a reproduction test first to confirm the behavior:
Assistant
Let me add a reproduction test to confirm the bug:
Assistant
Now let me add the reproduction test after extract_use_including_some_statements_after (line 12533):
Assistant
The simple test produced correct output — the selection end must not have reached the very end of the block. Let me check the helpers:
Assistant
The bug is reproduced — the whole use body gets extracted. Now let me check helper methods on CallArg for detecting use desugar:
Assistant
Now I'll make the three fixes:
Assistant
The output is now correct — the snapshot just needs accepting. Let me accept it and run the full test suite:
Assistant
All three outputs are correct. Accepting the snapshots and running the full suite:
Assistant
All tests pass. Now let me add a changelog entry:
Assistant
Let me verify the issue number I referenced actually matches this bug report:
Assistant
I can't verify the issue number, so let me remove the unverified links from the test comments:
Assistant
All tests pass and clippy is clean. Let me review the final diff:
Assistant
The fix is complete. Here's a summary of what was done:
Root cause
In the typed AST, a use expression desugars into a call whose callback wraps all statements coming after it, so three AST nodes end up with source spans extending to the very end of the enclosing block:
the Statement::Use itself,
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
/// 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.0 in neovim nightly
Interface
No new interfaces are introduced.
its desugared TypedExpr::Call,
the callback TypedExpr::Fn { kind: FunctionLiteralKind::Use }.
The "Extract function" code action decided whether to extract a node with !completely_within(selection, node_range), which is false when the ranges share an endpoint. When the user's selection reached the end of the function body (as in the report, where the selection ended at the last statement), it shared its end with the enclosing use's artificial span — so the whole use (i.e. the entire function body) was extracted instead of just the selected statements.
Fix (language-server/src/code_action.rs)
can_extract_statement, Statement::Use arm: the whole use is now only extracted if the selection also touches the use ... <- line itself (use_.location); otherwise traversal continues into the callback body.
can_extract_expression: the desugared use call (recognised by its implicit callback argument) is never extracted as a plain expression — statement-level handling covers extracting a use.
can_extract_expression: a use callback Fn is only extracted as a whole when the selection covers its entire body (within), since it has no delimiters of its own — otherwise the individually selected statements are extracted instead.
Tests
Added four snapshot tests in language-server/src/tests/action.rs covering:
statements after a use selected to the end of the function (the reported case),
the same with multiple chained uses (matching the report's shape — extracts function(n) in tail position with outer parameters passed through),
a partial selection (let y → end) which now extracts only those statements with x as a parameter,
selecting just the first statement after a use, which previously also swallowed the whole rest of the body.
All 1541 language-server tests pass, clippy is clean, and a changelog entry was added under Unreleased → Bug fixes (I left off the author attribution since I don't know who'll be credited on the PR).