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
Now let me look at how a use-desugared call can be identified:
- Assistant
Let me write a reproduction test first. Let me look at how the extract function tests are structured:
- Assistant
Now I understand the code. Let me write a reproduction test to confirm the bug:
- Assistant
Now let me add the reproduction tests after
extract_use_including_some_statements_after: - Assistant
Now the statement-level fix:
- Assistant
Both new tests now produce exactly the expected behavior. Let me accept the snapshots and run the full test suite to check for regressions:
- Assistant
cargo instaisn't installed — I'll accept the two new snapshots manually. All 1537 other tests pass, so no regressions: - Assistant
All 1539 tests pass. Now let me update the changelog, following the repo convention:
- Assistant
Now let me verify the build is clean and run clippy:
- Assistant
Clippy is clean. Let me verify the second snapshot matches the issue's expected behavior:
- Assistant
The fix is complete. All 1539 language-server tests pass, clippy is clean, and formatting is correct.
Root cause
When a
useexpression is desugared, three AST nodes all span from their start to the very end of the enclosing block (since auseconsumes every statement after it as its callback body):- the
Usestatement itself, - the
Callit desugars to, - the callback
Fnpassed as its implicit last argument.
The "extract function" code action decides whether to recurse deeper using
completely_within, which requires inequality at both extremes. A selection of trailing statements inside a use callback naturally shares its position with all three nodes, so returned false and the whole was extracted instead of just the selected statements. - the
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.