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.
Assistant
I'll start by exploring the repository structure.
Assistant
This is the Gleam compiler. The issue is about the "Extract function" feature in the language server. Let me find the relevant code.
Assistant
This file seems to deal with "Extract variable". The issue is about "Extract function". Let me search for that specifically.
Assistant
Let me look at the ExtractFunction implementation in detail.
Assistant
Now let me look at the visitor implementation and the statement extraction logic.
Assistant
Let me understand how the
usestatement's location is defined.Assistant
Now let me look at the
Usetyped statement and how its location is defined.Assistant
Now let me look at the
Statementlocation implementation.Assistant
Let me look at the helper functions
overlaps,completely_within,within,position_within.Assistant
Now let me look at the existing extract function tests to understand the test format.
Assistant
Let me look for tests involving
usestatements with extract function.Assistant
Let me look at the test around extracting statements that include a
usestatement, and tests with statements after a use.Assistant
Let me look at the test helpers to understand the test infrastructure.
Assistant
Let me create a reproduction test based on the issue. Let me write a temporary test file.
Assistant
Now let me create a temporary test to reproduce the issue. First, let me check the test helpers for position selection.
Assistant
Let me create a temporary reproduction test. I'll add it to the end of the action.rs tests file temporarily.
Assistant
edition = "2024" requires a recent rust version. The stable 1.97.1 should support edition 2024? Actually edition 2024 was stabilized in Rust 1.85. 1.97.1 is way past that. Let me run the test.
Assistant
The tests are inline (src/tests.rs is the test entry point). Let me check the lib.rs / main.rs.
Assistant
Interesting - the simple case actually works correctly. Let me look at how
useis typed to understand the call location.Assistant
Let me look at the visit functions to understand the traversal order.
Assistant
Let me check
visit_typed_statementdefault andvisit_typed_use.Assistant
Let me check how
infer_callsets the Call location.Assistant
Let me look at how the function body statements are typed with use consumption.
Assistant
Let me find how the body is typed with use consumption - look for
infer_usecalls and how statements are split.Assistant
Let me check
src_span_to_lsp_rangeto understand span end semantics.Assistant
Let me look at the snapshot for the
extract_use_including_some_statements_aftertest.Assistant
Now let me update my reproduction test to match the issue scenario - selection ending past/at the last statement (after the
usestatements).Assistant
I've reproduced the bug. The use statement is incorrectly selected. Let me check the expected snapshots for the existing use tests before making changes.
Assistant
Let me check the
extract_use_in_tail_position_2andextract_use_inside_blocksnapshots.Assistant
I've reproduced the bug. The
usestatement's full location (which spans the entire block due to desugaring) causes it to be selected even when the selection starts after theuseline. Let me look at the exact code to fix.Assistant
Now let me apply the fix:
Assistant
The bug persists. Let me verify my patch was applied correctly.
Assistant
The use's desugared call expression is also being selected via
can_extract_expression. Let me check theTypedExpr::Callstructure for akindfield.Assistant
The
TypedExpr::Calldoesn't store theCallKind. Let me check theCallArgfor theimplicitfield.Assistant
Let me look at the
ImplicitCallArgOriginenum andis_use_implicit_callback.Assistant
Let me check
get_use_expression_callto understand the use call structure for pipelines.Assistant
I need a second fix: the use's desugared call expression is also selected via
can_extract_expression. Let me look at it and add a guard for use calls.Assistant
Now let me add a guard for use calls. I'll add a special case before the match: if the expression is a
TypedExpr::Callwhose last argument is a use implicit callback, return false.Let me modify can_extract_expression to add this check after the overlaps check.
Assistant
The output is now the expected result. The
useis kept and only the selected statements are extracted. The test "fails" only because there's no snapshot yet. Let me now run all the existing extract tests to make sure nothing broke.Assistant
All 179 existing extract tests pass; only my new temp test "fails" because it has no snapshot yet. Let me now create a more comprehensive reproduction that mirrors the exact issue (two
usestatements before the selection, plus auseinside it).Assistant
The result is close but
use _ <- wobble(before the selection) got extracted too. The callback fn's location spans from the first callback statement. Let me look atextract_code_in_tail_position.Assistant
The use's callback
Fn(withFunctionLiteralKind::Use) also gets selected as a whole, including statements before the selection. Let me checkFunctionLiteralKind.Assistant
Let me look at the full
extract_code_in_tail_positionfunction.Assistant
I need a third guard: the use's callback
Fn(withFunctionLiteralKind::Use) also gets selected as a whole. Let me extend the guard incan_extract_expression.Assistant
Let me check what's imported in code_action.rs for
FunctionLiteralKind.Assistant
FunctionLiteralKindis already imported. Now let me add the guard for the use callback fn:Assistant
Excellent! Now the comprehensive test produces the correct result matching the issue's expected behavior. The two
usestatements before the selection are kept. Let me run all existing extract tests to ensure nothing broke.Assistant
All 179 existing extract tests pass. Let me run the broader test suite (skipping my temp test) to check for any other regressions.
Assistant
All 1537 tests pass. Let me now test additional edge cases with a standalone test (not using insta snapshots) to verify robustness. Let me look at the test helpers available.
Assistant
Let me replace my temp test with standalone assertion-based tests (no snapshots) to verify multiple edge cases.
Assistant
All 3 edge case tests pass. Let me check the changelog convention used by this repo.
Assistant
Let me look at the latest changelog to add an entry.
Assistant
The v1.17.md is the released version (v1.17.0 - 2026-06-02). The current version in Cargo.toml is 1.17.0. So the next version would be unreleased. Let me check the CHANGELOG.md at the root and the git log to understand where unreleased changes go.
Assistant
The CHANGELOG.md has an "Unreleased" section. Let me look at it more to find the Language Server section.
Assistant
Let me add a changelog entry under the Language server section in CHANGELOG.md.
Assistant
Now let me remove my temporary tests (since I must not add tests to the repo) and run the full suite to confirm everything is clean.
Assistant
The temp tests are removed. Let me run the full language-server test suite to confirm everything passes.
Assistant
All 1537 tests pass. Let me do a final verification by temporarily re-adding a quick test, running it, then removing it.
Assistant
The fix produces exactly the expected output. Now let me remove the temp verification test.
Assistant
Now let me review the final diff of my source code changes to make sure everything is clean and correct.
Assistant
The diff looks clean. Let me verify the build has no warnings related to my changes and run clippy on the language-server package.
Assistant
The build succeeds cleanly. Let me review the CHANGELOG diff and do a final full test run.
Assistant
All tests pass. Let me verify the final git status to ensure only intended changes are present (no leftover temp files).
Assistant
There's a leftover snapshot file from the insta test runs. Let me remove it.
Assistant
Now only the intended changes remain. Let me do a final review of the complete diff.
Assistant
The changes are clean and complete. Let me do one final full test run to be absolutely certain everything passes before submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.