Extract function extracts too much When triggering Extract function with the selection:…
- User
Extract function extracts too much When triggering
Extract functionwith 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:
rsfn prepare_project_structure( target_path target: TargetPath, user_id user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) { function(target, user_id) }instead of the expected:
rs - Assistant
Now let me understand how
usedesugars — the callbackFnlocation: - Assistant
Now let me look at
register_referenced_variableand check the existing tests: - Assistant
Now I have a full picture. Let me look at
extract_code_in_tail_positionbriefly to confirm the expression path output: - Assistant
Now I'll apply the fix. First, the
can_extract_expressionchange: - Assistant
Now the
can_extract_statementchange — split theUsearm fromAssert: - Assistant
Now let me check the test position helpers to build a regression test whose selection reaches the very end of the block:
- Assistant
Now add regression tests. Let me check the end of the extract-function test section to place them nearby:
- Assistant
Let me check whether I can find the actual issue number for the reference:
- Assistant
Now build and run the extract-function tests:
- Assistant
Format matches existing snapshots. Now run the full language-server test suite:
- Assistant
All 1540 language-server tests pass. Now update the changelog:
- Assistant
Confirmed — identical behavior exists on unmodified code in plain function bodies, so that's pre-existing and out of scope. My temp tests are removed. Final verification run:
- Assistant
The first temp test came back with the stash pop. Remove it:
- Assistant
The removal cut at the wrong
}(inside the raw string). Remove the leftover fragment: - Assistant
The fix is complete. Here's a summary:
Root cause
A
usestatement's span covers the entire rest of the enclosing block, because everything after it desugars into the use's callback function. InExtractFunction::can_extract_statement(language-server/src/code_action.rs), ausewas extractable whenever the selection wasn't within its span — and requires strict inequality at both ends. So when a selection inside a use callback body extended to the end of the function (sharing the end position with the use's span), the whole outer was extracted instead of just the selected statements.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.