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: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
I'll start by exploring the repository structure and locating the extract-function implementation so we can reproduce the reported bug.
- Assistant
I'll search the language-server and compiler for extract-function related code and tests.
- Assistant
Seems like extract is in language-server. Let me look at code_action.rs
- Assistant
rgisn't available here, so I'll switch togrepfor the rest of the search. - Assistant
Let me find the Extract function code specifically.
- Assistant
I'll open the ExtractFunction implementation and the related tests next.
- Assistant
Let me continue reading the ExtractFunction implementation, especially the visit methods and how selection works.
- Assistant
Let me keep reading the code_actions method and how it determines what's selected.
- Assistant
Let me look at the visit implementation - that's probably where the bug is with selecting statements.
- Assistant
Let me continue reading the visit_typed_statement method and the can_extract methods.
- Assistant
Looking at how use-statement selection is handled—those earlier
usebindings may be skewing the extract range. - Assistant
I'll check how
Usestatements record their spans — that might explain why earlieruses get pulled into the extract. - Assistant
Found it! Look at:
rust - Assistant
Next I’ll inspect existing extract-function use tests and how typed function bodies structure nested
usestatements. - Assistant
I'll pull the extract-use snapshots and search for a test that selects only trailing statements after earlier
uses. - Assistant
I'll inspect the test helpers and try reproducing the bug with a focused temporary test.
- Assistant
I’ll check how the visitor walks
usecallback bodies—that’s a likely source of the over-extraction. - Assistant
Let me look at visit_typed_use and how function bodies with use are structured.
- Assistant
I'll check the range helper functions — perhaps the selection comparison is off.
- Assistant
Let me read those functions.
- Assistant
I'll trace how the extracted span is turned into edits, and confirm whether outer
uses match when the selection ends at the body end. - Assistant
I need to open the right file first.
- Assistant
I'll also check whether Assert spans behave like Use before choosing the fix.
- Assistant
I'llinspect how a typed
usecall and callback function record their source spans. - Assistant
I'll check whether typed
Callnodes keep a marker that they came fromuse. - Assistant
Let me look at the TypedExpr::Call structure
- Assistant
I'll inspect
ImplicitCallArgOriginand how use-calls are identified elsewhere. - Assistant
There's already something at language-server featuring Use. Let me check that.
- Assistant
I'll re-read that Use Fn expression special-case so we don't break intended extract behavior.
- Assistant
I'll check what
sequence_locationis when typechecking a function body that starts withuse. - Assistant
I'll check
is_use_implicit_callbackand existing use-extract tests next. - Assistant
I'll implement the fix, then reproduce with a temporary test to confirm it.
- Assistant
Implementing the fix in
can_extract_expressionandcan_extract_statement. - Assistant
Next I'll update
can_extract_statementso auseis only extracted when its header is selected. - Assistant
Now update Use handling separately from Assert.
- Assistant
I'll check how tests are structured and write a temporary reproduction for the issue.
- Assistant
I'll add a temporary test matching the reported case, run it, then remove it before submitting.
- Assistant
I'll add a temporary test after extract_block_tail_position_4.
- Assistant
Checking the package name, then running the temporary reproduction test.
- Assistant
Let me run the test. First I might need to check if things are already built.
- Assistant
Need to set up rustup.
- Assistant
Now run the test. This might take a while for first build.
- Assistant
The position selector failed — I'll simplify it and rerun.
- Assistant
There's "Ok(Nil)" only once at the end - and also Result(Nil,m Nil). under_last_charn on Ok(Nil) should work. "Ok(a)" is different. Let me run again.
- Assistant
The fix matches the expected behavior. Next I'll run the existing extract-function and use-related tests.
- Assistant
All related existing tests passed. I'll remove the temporary test and snapshot artifact next.
- Assistant
I'll stash the fix briefly and re-run the reproduction to confirm the old over-extraction behavior.
- Assistant
Good, stashed the fix. Now run the test with old code.
- Assistant
Stash may not have redefined the bug — checking whether the fix is still in the working tree.
- Assistant
I'll check whether cargo rebuilt without the fix, and inspect the extract edit logic more carefully.
- Assistant
Cargo didn't rebuild after the stash — forcing a clean rebuild of the language server.
- Assistant
It still didn't recompile! Let me check if code_action is part of a different crate - gleam-core vs gleam-language-server.
- Assistant
Rebuilding from a clean slate so we can observe the original broken behavior.
- Assistant
I'll add debug prints to inspect why the old code doesn't over-extract on this case.
- Assistant
I'll check whether LSP ranges treat ends as exclusive — that may explain the endpoint comparison.
- Assistant
Let me open src_sspant_to_lsp_range
- Assistant
The selection ends past the final expression (trailing comments), so I'll update the reproduction to match that.
- Assistant
Bug reproduced. Restoring the fix and verifying it.
- Assistant
Fix confirmed. Cleaning up the temporary test and re-running extract-function tests.
- Assistant
All 29 tests passed. Reviewing the final diff before submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.