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 to understand the codebase and find the relevant code for the "Extract function" feature.
Assistant
Let me start over and make single tool calls.
Assistant
Let me search for the extract function code in the language-server directory.
Assistant
Let me look at the extract function code action code starting around line 10354.
Assistant
Let me look at the visitor functions that process statements and determine what gets extracted.
Assistant
Let me look at the
can_extract_statementandcan_extract_expressionfunctions, and thewithinhelper.Assistant
Let me look at the helper functions
within,overlaps,completely_within, andposition_within.Assistant
Let me look at the existing tests for extract function to understand the test format.
Assistant
Let me look at the
extract_function_partially_selectedandextract_statements_in_tail_positiontests.Assistant
Let me look at the test harness to understand how to write a reproduction test.
Assistant
Let me look at the test helpers for finding positions and selections.
Assistant
Let me check the structure of the test module and how tests are organized.
Assistant
Let me create a reproduction test. I'll add a temporary test to the action.rs file (and remove it before submitting). Let me first understand exactly how the test reproduces the issue. The key is the
usestatements before the selection.Let me write a test that matches the issue's scenario:
- A function with
usestatements, then someletstatements, then a final pipeline statement. - Select from the
letstatement (representing the comment before it) to the end of the final statement.
Let me create a temporary test. I'll add it at the end of action.rs temporarily.
Actually, let me first reproduce with a simpler version to make sure I understand. Let me create a standalone reproduction that uses the test infrastructure. The simplest is to add a
#[test]function to action.rs, run it withcargo test, and look at the output (the snapshot will be created, or I can use a non-snapshot assertion).Let me write a test that prints the result instead of using snapshots. Let me look at
apply_code_action.- A function with
Assistant
Let me check how the test module is structured.
Assistant
Let me look at how
usestatements are represented in the typed AST.Assistant
Let me look at how the statement location is determined for
Usestatements.Assistant
I found the root cause. Let me look at the key code. For a
Usestatement, the location spans the entire block (becauseuse_.call.location()includes the callback which contains the rest of the block). Let me confirm this by looking at the relevant lines again and creating a reproduction test.Assistant
Now let me create a reproduction test. Let me add a temporary test to the action.rs file.
Assistant
I've reproduced a related issue. The extract function extracts the entire callback of the use statement. Let me look at how the
Useextraction handles the location to understand the full picture.Assistant
Let me look at
extract_code_in_tail_position.Assistant
Let me look at the snapshots for the use extraction tests to understand the expected behavior.
Assistant
My simple reproduction actually produces correct behavior. The issue must involve the selection reaching the end of the block. Let me examine how
usestatements are typed to understand their actual spans.Assistant
Let me look at where
sequence_locationis set to understand the span.Assistant
Let me find where
infer_useis called and howsequence_locationis computed.Assistant
So
locationis passed toinfer_useassequence_location. Thislocationis the location of the whole block/sequence. Let me find whereinfer_iter_statementsis called.Assistant
The key finding: the use's call location ends exactly at
sequence_location.end(end of last statement). So when the selection reaches the end of the last statement,completely_withinfails for use statements. Let me look at the related use extraction tests.Assistant
Let me look at the snapshot for
extract_use_including_some_statements_after.Assistant
Let me check how
src_span_to_lsp_rangemaps SrcSpan end to LSP position.Assistant
So
src_span_to_lsp_rangeusesline_and_column_number(location.end). Thelocation.endis a byte index.line_and_column_numberreturns 1-based line and column. Then it subtracts 1 to make it 0-based.Let me check
line_and_column_number. Thelocation.endbyte index — for a SrcSpan, end is typically the byte index AFTER the last character. Soline_and_column_number(end)would give the column of the position after the last char (1-based), then -1 gives 0-based position after the last char.Hmm, let me verify by checking
line_and_column_number.Assistant
Let me check the
byte_index_to_positionfunction used by the test helpers.Assistant
Now I understand the span mapping. The selection needs to end at the position AFTER the last character of the last statement to reproduce the bug (matching the use's call location end). Let me update my reproduction test.
Assistant
Let me replace the test with one that selects to the position after the last character of the last statement.
Assistant
Now there's "No action with the given title" — a different manifestation. Let me check
code_atand print available actions.Assistant
Let me modify the test to print the available actions.
Assistant
Interesting — with the selection end at the exact use's call location end, the action isn't offered. Let me check the default
visit_typed_functionto understand the visiting flow.Assistant
Let me check what
full_location()returns for a function.Assistant
Let me search for where
self.functionmight be reset or cleared.Assistant
Line 11620 sets
self.function = None. Let me look at that context.Assistant
Let me add temporary debug prints to
code_actionsto understand the state.Assistant
I made an occurrence-counting error — "wobble" occurrence 2 is the function definition. Let me fix the test to use occurrence 1 (
a |> wobble).Assistant
RangeSelector isn't Clone. Let me simplify the test to only call apply_code_action.
Assistant
I've reproduced the bug. The entire body is extracted. Now let me look at the snapshot for
extract_statements_in_tail_positionto understand how multi-statement extraction including a tail expression works.Assistant
This reveals the issue is more nuanced. Let me check whether extracting multiple statements ending in a tail pipeline works without a use statement. Let me look for related tests and check the pipeline handling.
Assistant
Let me test the tail pipeline case without a use statement to see if it's a separate issue. Let me update my reproduction test.
Assistant
Both cases show issues. The no-use case leaves the tail pipeline behind, and the use case extracts too much. Let me check the CHANGELOG and recent git history to understand the state and the intended fix.
Assistant
The first reproduction (with
under_last_char) actually produced correct output including the tail pipeline, butwith_char_offset(6)produces the bug. This is subtle. Let me re-run theunder_last_charcase with debug prints to understand the mechanism.Assistant
The results are confusing — with the use, the tail pipeline is included (location 35..70), but without it, it's not (19..40). Let me add debug prints to
visit_typed_statementto trace each statement.Assistant
I now understand the mechanism. Let me check where
completely_withinis used to understand the impact of changing it.Assistant
Let me look at the changelog directory to understand the convention and check for related extract function changes.
Assistant
Let me check if
completely_withinis used in tests or elsewhere.Assistant
completely_withinis only used incode_action.rs. My analysis shows the root cause is the boundary check: when the selection reaches the end of the last statement (tail position),completely_within's strict<on the end fails, causing containers (use statements and their callback fns) to be extracted instead of descended into.Let me first remove my debug prints, then apply the fix to
completely_within.Assistant
Now let me look at the
completely_withinfunction and apply the fix.Assistant
Now let me apply the fix to
completely_within. The issue is that when the selection reaches the end of the last statement (tail position),completely_within's strict<on the end fails, causing containers (use statements and their callback functions) to be extracted instead of descended into. Let me relax the end check to be non-strict (<=).Assistant
Now let me run my reproduction tests to verify the fix.
Assistant
Both cases now produce correct output. Let me create a two-use reproduction matching the issue more closely to verify the nested case.
Assistant
My script didn't cleanly remove the old tests. Let me check the end of the file.
Assistant
Let me find where the tmp test starts and rewrite the end cleanly.
Assistant
Excellent! The two-use case now produces the correct output — both use statements are kept and only the selected portion is extracted. This matches the issue's expected output. Now let me run ALL the extract function tests to ensure no regressions.
Assistant
All extract_function tests pass. Let me run all tests with "extract" in the name to check extract variable/constant/use/statements tests too.
Assistant
All 181 extract tests pass. Let me run the entire action test suite to ensure nothing else broke.
Assistant
All 883 action tests pass. Let me run the entire language-server test suite to be thorough.
Assistant
All 1539 language-server tests pass. Now let me verify the no-use case is unchanged (no regression) by re-checking it isn't affected, and also reconsider the scope. The reported issue (extracts too much with use statements) is fixed. Let me look at the CHANGELOG to add an entry following repo conventions.
Assistant
I found the "Bug fixes" section. Let me add a changelog entry for this fix, grouping it with the other extract function bug fix.
Assistant
Let me verify my changelog example is accurate by testing the single-use case with a BinOp tail (matching the changelog).
Assistant
The replacement left leftover. Let me truncate at the tmp test and append cleanly.
Assistant
Both cases produce correct output matching the changelog example. Let me verify this was actually broken before my fix by temporarily reverting.
Assistant
Confirmed: without my fix, the boundary case (selection ending at the tail) extracts the whole body; with my fix, it's correct. The
under_last_charcase was already working. Now let me remove the temporary test.Assistant
Let me verify the temporary test is fully removed and the file is clean.
Assistant
All temporary code is removed. Let me run the full language-server test suite once more to confirm everything passes.
Assistant
All 1537 tests pass. Let me review the complete diff of my changes.
Assistant
The action.rs change is just a trailing newline from my edits. Let me remove it to keep the diff minimal.
Assistant
Good, the action.rs diff is now empty. Let me verify with git status.
Assistant
Only CHANGELOG.md and engine.rs are modified. Let me do one final faithful test with a comment as the selection start (matching the issue exactly), to be 100% sure, then clean up.
Assistant
The comment-based selection works perfectly, matching the issue's expected output. Now let me remove the temporary test.
Assistant
That sed command deleted almost the entire file! Let me revert action.rs immediately.
Assistant
action.rs is restored. Let me verify the temp test is gone and run the full test suite once more.
Assistant
All 1537 tests pass with no temporary test remaining. Let me do a final review of the complete diff.
Assistant
The diff is clean and minimal. Let me check the
no_code_action_to_extract_function_when_expression_is_not_fully_selectedtest to ensure my change doesn't affect the "no action" cases.Assistant
The fix is complete and minimal. Let me do a final verification that the full test suite passes and review the final state.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.