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.
- Assistant
The extract function feature should be in the language-server. Let me search for it.
- Assistant
The code action is in language-server/src/code_action.rs. Let me look at that.
- Assistant
Let me search more specifically for extract function.
- Assistant
Let me read the ExtractFunction implementation carefully.
- Assistant
Let me continue reading the core logic for determining comments and selection.
- Assistant
Let me continue reading the code_actions method and how statements are visited.
- Assistant
Let me look at the Visit implementation for statements, which is where the selection matching likely happens.
- Assistant
Let me continue reading how statements are handled when visiting.
- Assistant
Let me look at the
can_extract_statementandcan_extract_expressionfunctions. - Assistant
Let me read can_extract_expression and can_extract_statement.
- Assistant
Let me continue reading the can_extract_statement for use statements.
- Assistant
Let me look at related tests for use expressions and extract function.
- Assistant
There are tests specifically for use. Let me look at those.
- Assistant
There's a test
extract_use_with_some_statements_beforewhich relates closely. Let me examine it. - Assistant
Let me check how location spans work for Use and the within/overlaps helpers.
- Assistant
Let me read these helper functions.
- Assistant
Let me look at how tests and snapshots work for extract function to better understand expected behavior.
- Assistant
Let me check Use statement locations and search for relatedSnap regressions.
- Assistant
Let me look at Use location and Statement location.
- Assistant
Confirmed - Use statement location spans the entire block.
- Assistant
Let me look at recent git history related to extract function and any TODOs.
- Assistant
Let me understand the test helper and try to reproduce the bug.
- Assistant
Let me examine making a temporary reproduction of the bug by looking at how apply_code_action works.
- Assistant
I'll write a temporary reproduction test. First, let me check how to run language-server tests.
- Assistant
Let me look at how Use is extracted in code_actions and then reproduce the bug.
- Assistant
I'll add a temporary test reproducing the reported bug and run it.
- Assistant
I'll append a temporary test that reproduces the issue.
- Assistant
Let me run the temporary test to reproduce the bug.
- Assistant
Need to configure the Rust toolchain first.
- Assistant
Now let's run the temporary test.
- Assistant
My reproduction didn't trigger the bug. Let me check how selection ranges work and refine the test.
- Assistant
Selection needs to include the end of the last statement — that matches the original bug more closely.
- Assistant
I accidentally edited the wrong file. Let me fix that.
- Assistant
Good, tests.rs is restored. Now let me update the temp test properly.
- Assistant
There's an extra closing brace — let me remove it.
- Assistant
Let me clean up the old snapshot and rerun the test.
- Assistant
Let me check whether SrcSpan end is exclusive, and compare LSP ranges carefully.
- Assistant
Let me check how SrcSpans map to LSP ranges.
- Assistant
Let me read the src_span_to_lsp_range function.
- Assistant
Looking at under_last_char - it sets offset to len-1, so for "result" (6 chars), offset is 5, pointing to the last character 't'.
- Assistant
Let me look at how Use statements are visited and the structure of their calls.
- Assistant
Let me look at visit_typed_use and visit_typed_function to understand body structure.
- Assistant
The core issue may be selection ending past the Use span. Let me inspect existing Use extract snapshots and the header location.
- Assistant
Let me extend the selection past the end of the last statement to better match the report.
- Assistant
I edited the wrong file again. Let me fix that.
- Assistant
Now let me properly update the test.
- Assistant
There's an extra closing brace at the end again.
- Assistant
Good. Now run the test again.
- Assistant
Selection went to the wrong
}. Let me refine the test to end after the last statement. - Assistant
Let me fix the trailing brace.
- Assistant
The closing brace of the test function is missing. Let me fix it.
- Assistant
Good. Now run the test.
- Assistant
I reproduced the bug. Let me check the Use
locationfield and implement the fix. - Assistant
I'll implement the fix: only extract a
usewhen the selection overlaps its header line. - Assistant
I need to check if
overlapsis imported. Looking at the imports. - Assistant
overlapsis used but onlycompletely_withinandposition_withinare imported. Let me check how overlaps gets into scope. - Assistant
Now I'll apply the fix for Use statements.
- Assistant
Let me rerun the reproduction test to verify the fix.
- Assistant
The fix wasn't applied as expected. Let me investigate the extract code path further.
- Assistant
Bash quoting issue. Let me simplify.
- Assistant
The test seems to have used a cached binary. Let me force a rebuild.
- Assistant
Still no recompilation. Let me force a full rebuild of the language-server package.
- Assistant
Cargo isn't detecting the change. Let me force a clean rebuild.
- Assistant
The issue is that dig deeper then extracts the desugared
usecall as an expression! Let me check how use is desugared. - Assistant
There's
FunctionLiteralKind::Use. Let me look at how use code is structured. - Assistant
Let me check how to detect a desugared use call and fix can_extract_expression.
- Assistant
I need a more complete fix: don't extract desugared use calls/fns as expressions, and handle blocks better.
- Assistant
Let me verify the enum variant name exists and then run the test.
- Assistant
Good. Let me run the reproduction test again.
- Assistant
There's a brace mismatch. Let me inspect the area around the error.
- Assistant
There's an extra closing brace on line 11318.
- Assistant
Looks good. Let me run the test again.
- Assistant
The fix works. Now let me run all extract_function tests to check for regressions.
- Assistant
All existing extract tests passed. Let me also verify the case where selection ends exactly at the last expression (as in the issue).
- Assistant
Extra brace again.
- Assistant
Missing closing brace again.
- Assistant
Good. Let me run it.
- Assistant
Both cases work. Let me clean up the temporary test and snapshots.
- Assistant
Let me verify the final state of the changes.
- Assistant
Let me run the full language-server action test suite to be thorough.
- Assistant
All 881 action tests passed. Let me do a final review of the fix and submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.