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 need to understand the issue. This is about the "Extract function" feature in Gleam's language server (or editor extension) extracting too much content. The issue is that when selecting a block of code within a function, the extract function operation extracts the entire function content instead of just the selected lines.
First, let's explore the repository to understand what we're working with.
- Assistant
This is the Gleam compiler repository. Let me find the extract function implementation.
- Assistant
This is a Gleam compiler repository written in Rust. Let me find the extract function implementation.
- Assistant
Let me look at the language-server extract function implementation.
- Assistant
Let me search for "extract function" specifically in the code_action.rs file.
- Assistant
Let me look at the ExtractFunction struct and its implementation.
- Assistant
Let me look at the ExtractFunction implementation to understand the issue.
- Assistant
Let me see more of the ExtractFunction implementation.
- Assistant
Let me look at the visitor implementation to understand how the selection range is being used.
- Assistant
Let me look at the
can_extract_statementandcan_extract_expressionfunctions. - Assistant
Let me look at
can_extract_statement: - Assistant
Let me look at the helper functions like
within,overlaps,position_within, andcompletely_within: - Assistant
Let me search more broadly.
- Assistant
Let me look at these functions.
- Assistant
Let me look at
full_location()for TypedFunction: - Assistant
Let me look at both
full_location()implementations: - Assistant
Let me look at the
Functiontype'sfull_location: - Assistant
Let me understand the visitor pattern. Let me look at how the AST is traversed:
- Assistant
Let me look at the visit_typed_function implementation:
- Assistant
Let me look at the
statement.location()method: - Assistant
Let me search for the TypedStatement type:
- Assistant
Let me look at the Statement type and its location method:
- Assistant
I need to understand how
usestatements work in Gleam's AST. The key is that ausestatement desugars into a callback. Let me look at how the AST visitor handles use statements: - Assistant
Let me find the default implementation of visit_typed_statement:
- Assistant
Let me look at both the trait definition and the implementation:
- Assistant
Now let me look at the default implementation:
- Assistant
Let me look at the tests for the extract function feature:
- Assistant
Let me look at the extract function tests, especially ones that might involve use statements:
- Assistant
Let me look at the extract_function_from_statements test:
- Assistant
Let me look at the visit_typed_function and visit_typed_statement more carefully. The issue might be in how the selection range is checked for use statements.
- Assistant
Let me look at how the use statement is desugared during type checking:
- Assistant
Let me look at the full
infer_usemethod to understand the desugaring: - Assistant
This is very revealing. Let me understand how the function body is processed during type checking. Let me look at how statements in a function body are collected:
- Assistant
Let me look at how the body of a function is processed, specifically how use consumes following expressions:
- Assistant
Now I see the key issue. When
infer_iter_statementsencounters aUsestatement, it collects ALL remaining statements and passes them toinfer_use, then breaks. So the typed function body only has ONE statement (the first Use), which contains the rest as callback arguments. Let me trace through what happens: - Assistant
Let me look at where it's called from:
- Assistant
Now I understand the issue. The key insight is in
can_extract_statementforUsestatements. Let me trace through the logic more carefully. - Assistant
Now I understand the root cause. The issue is in
can_extract_statementforUsestatements. In Gleam, ausestatement's location spans the entire rest of the block (because itscall.location()includes the callback body containing all subsequent statements). When the user selects a subset that ends at the same position as the Use statement's end,completely_within(selected_range, statement_range)returnsfalse(due to strict inequalitya.end < b.end), so!completely_withinreturnstrue, incorrectly marking the Use as extractable.The fix is to use
within(non-strict inequality) instead ofcompletely_withinforUsestatements. Let me also write a test to reproduce the issue first. - Assistant
Let me look at how the
EXTRACT_FUNCTIONtest macro works: - Assistant
Let me look at the
assert_code_action!macro: - Assistant
Let me look at the macro definition:
- Assistant
Let me look at how
find_position_ofandselect_untilwork: - Assistant
Let me search for these functions:
- Assistant
Let me search more broadly:
- Assistant
Let me look at these functions:
- Assistant
Let me look at the PositionFinder to understand how it selects ranges:
- Assistant
Now I understand the test framework. Let me first try to build the project to see if tests pass before making changes:
- Assistant
Let me check the Rust toolchain setup:
- Assistant
Let me check if there's a rust-toolchain.toml file:
- Assistant
Let me check what Rust versions are available:
- Assistant
Let me install Rust:
- Assistant
Now let me run the extract function tests:
- Assistant
Let me check the workspace structure:
- Assistant
Let me see the full workspace members:
- Assistant
Let me look at the language-server Cargo.toml:
- Assistant
Let me run the tests with the correct package name:
- Assistant
All 22 existing extract function tests pass. Now let me understand the issue better by looking at how
visit_typed_functionworks: - Assistant
Let me look at existing tests that involve
useexpressions: - Assistant
Let me check if there are existing tests for extract function that involve
use: - Assistant
Now let me write a test that reproduces the issue. Let me first find a good place to add the test:
- Assistant
Let me look at this test and find a good place to add the new test:
- Assistant
Let me write a test that reproduces the issue. Let me first create a simple reproduction script:
- Assistant
Let me find the end of the file:
- Assistant
Now let me add a test case that reproduces the issue. I'll add it near the other extract function tests:
- Assistant
Let me add a test right after the
extract_function_partially_selectedtest: - Assistant
Let me look at the end of extract_function_partially_selected test:
- Assistant
Let me add a test that reproduces the issue. I'll add it after the
extract_function_partially_selectedtest: - Assistant
Actually, I need to re-read the instructions. It says I should NOT modify existing tests or add new tests. Instead, I should:
- Reproduce the issue (if possible)
- Fix the code
- Verify the fix by running existing tests
Let me first understand the issue more deeply and then implement the fix. The root cause is clear from my analysis: the
can_extract_statementfor Use statements uses!completely_withinwhich has strict inequality, causing Use statements that span to the end of the block to be incorrectly marked as extractable when the selection ends at the same position.The fix is to change
!completely_withinto!withinfor Use statements.Let me implement the fix now.
Now I understand the issue clearly. Let me implement the fix. The root cause is in
can_extract_statementforUsestatements. In Gleam, ausestatement'slocation()spans the entire block (becauseuse_.call.location()includes the callback body with all subsequent statements). When the user's selection ends at the same position as the Use statement's end, returns due to strict inequality ( when ), making return and incorrectly marking the Use as extractable. - Assistant
Let me use the open command correctly:
- Assistant
Now I can see the exact line. The issue is on lines 11319-11321. For
Usestatements, thestatement_rangespans the entire rest of the block (because theusestatement's callback wraps all subsequent statements). When the user's selection ends at the same position as the Use statement's end,completely_withinreturnsfalsedue to strict inequality, making the Use incorrectly extractable.The fix is to separate the
Usecase and usewithininstead ofcompletely_within: - Assistant
Let me use the edit command correctly:
- Assistant
Good, the edit was applied. Now let me verify the change looks correct:
- Assistant
Good, the change looks correct. Now let me run the existing tests to make sure nothing is broken:
- Assistant
All 22 existing extract function tests pass. Let me also run the full test suite for the language server to make sure nothing else is broken:
- Assistant
All 1537 tests pass. Let me also think more carefully about edge cases. Let me reconsider whether
!withinis the right fix or if there's a more precise approach: - Assistant
Let me look at how the parser creates Use nodes to understand
use_.location: - Assistant
Let me look at the parse code:
- Assistant
I see that
use_.locationspans from theusekeyword to the end of the right-hand-side call expression (not the entire block). This is a much better reference point for checking if the selection includes the use statement itself. Let me refine my fix: - Assistant
Let me open the file first to see the current state:
- Assistant
Now let me update the Use case to use a more precise check:
- Assistant
Let me try using sed to make the edit:
- Assistant
Let me use sed to replace those lines:
- Assistant
Let me verify the edit:
- Assistant
Good, the edit looks correct. Now let me run the tests to verify:
- Assistant
All 22 extract function tests still pass. Let me run the full test suite to make sure nothing else is broken:
- Assistant
All 1537 tests pass. Let me also run the compiler-core tests:
- Assistant
Let me also check for other relevant tests:
- Assistant
All tests pass. Now let me create a temporary script to verify the fix works for the specific scenario described in the issue:
- Assistant
Let me also verify the Assert case is still correct by checking its location structure:
- Assistant
Let me look at the Assert structure:
- Assistant
Good. Assert statements have their own
locationfield that doesn't extend to subsequent statements, socompletely_withinis correct for them.Now let me also run the compiler-core tests to make sure nothing is broken:
- Assistant
All 3890 compiler-core tests pass too. Let me verify the code compiles cleanly:
- Assistant
No warnings from our code. Let me now verify the complete diff:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.