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
The issue is about "Extract function" refactoring in Gleam (a programming language). The problem is that when extracting a function from a selection that includes
useexpressions, the extract function refactoring extracts too much - it extracts the entire function body instead of just the selected portion.Let me explore the repository structure to understand the codebase.
Assistant
This looks like a Gleam language repository. The issue is about "extract function" refactoring. Let me explore the language server and compiler-core directories to find relevant code.
Assistant
Let me look more specifically for "extract function" or "extract_function" or similar.
Assistant
Let me look at these files to understand how extract function works.
Assistant
Let me open the code_action.rs file and look at the ExtractFunction implementation.
Assistant
Let me just use the terminal commands.
Assistant
Let me look at more of the ExtractFunction implementation, particularly the visitor and how it handles
useexpressions.Assistant
Let me look at the visitor implementation for ExtractFunction.
Assistant
Let me see the rest of the visitor implementation.
Assistant
Let me look at how the AST defines Use statements and understand the structure better.
Assistant
Let me look at the AST definition for Use and how visit_typed_statement works for use.
Assistant
Let me look at the AST definition for Use.
Assistant
Let me check how the location of a use statement is determined in the typed AST.
Assistant
Let me look at the
location()method forStatementto understand how it works for Use statements.Assistant
Let me look at how typed use statements are structured and what their location is.
Assistant
Let me see the visit_typed_use function:
Assistant
Let me see how
visit_typed_statementworks in the visit module:Assistant
Let me check what the default
visit_typed_useimplementation looks like.Assistant
Let me understand the typed AST structure better. Let me look at how use is typed/desugared.
Assistant
Let me look at how use is handled in the type checker/inference.
Assistant
Let me look at the infer_use function to understand what happens when a use statement is processed.
Assistant
Let me look at the extract function tests to understand the expected behavior better and try to reproduce the issue.
Assistant
Let me look at the extract function tests related to use.
Assistant
Let me look at tests that involve extracting statements AFTER a use expression.
Assistant
Let me look at the test snapshot files for extract function tests.
Assistant
Let me look at the extract_use_with_some_statements_before snapshot and other relevant ones:
Assistant
Interesting. Now let me look at the extract_use_inside_function:
Assistant
Let me look at extract_use_including_some_statements_after:
Assistant
Let me look at the test infrastructure and try to reproduce the issue with a simple test case. First, let me look at how
assert_code_action!works:Assistant
Let me look at the assert_code_action macro:
Assistant
Let me look at the helper functions used for range comparison:
Assistant
Let me search for these functions more broadly.
Assistant
Let me look at these functions to understand how they work:
Assistant
Now I understand the issue. Let me look at the
can_extract_statementfunction more carefully and trace through the logic.Assistant
Now I have a clear understanding of the bug. Let me verify my hypothesis by creating a test script to reproduce the issue:
Assistant
Let me search for existing tests that involve selecting code after use statements:
Assistant
Let me look at the
extract_use_in_tail_positiontests:Assistant
Let me look at the
extract_use_in_tail_position_2snapshot:Assistant
Now I understand the bug. Let me trace through the logic:
The issue is in
can_extract_statementforUse. When the selection starts AFTER ausestatement but ends at the same position as the end of the function/block (which is also the end of the use statement's range),completely_withinreturns false (because the end edges are equal), so!completely_withinreturns true, causing the use statement to be selected for extraction. Since theusestatement's location spans from theusekeyword to the end of the function, the entire function body gets extracted.The fix: for
Usestatements, we should only extract them if the selection actually starts at or before the use statement. Let me implement this.Assistant
Now let me apply the fix. I need to change the
can_extract_statementforUsestatements to require that the selection starts at or before the use statement:Assistant
Let me look at the exact lines to ensure correct whitespace:
Assistant
Let me view more context around these lines:
Assistant
Let me use a more precise replacement. I'll use
editto replace lines 11319-11321:Assistant
Good, the change looks correct. Now let me run the existing tests to make sure nothing breaks:
Assistant
Let me set up the Rust toolchain:
Assistant
Now let me run the tests related to extract function with use:
Assistant
Let me find the correct package name:
Assistant
Let me run the tests with the correct package name:
Assistant
All use-related tests pass. Let me also run all extract_function tests:
Assistant
All extract_function tests pass. Let me also run the other related extract tests to make sure nothing is broken:
Assistant
All 175 extract-related tests pass. Let me also run the full test suite for the action tests to ensure nothing is broken:
Assistant
All 881 tests pass. Let me verify the change I made:
Assistant
The diff looks correct. Let me also verify that
Assertdoesn't have the same issue by checking its location:Assistant
Wait, I realize my fix might not be complete. Let me trace through more carefully what happens when
can_extract_statementreturns false for a use - the call expression inside the use might still get selected bycan_extract_expression. Let me look at how Use-related call and Fn expressions are tagged:Assistant
Let me look at the TypedExpr variants more carefully:
Assistant
Let me look at the TypedExpr definition:
Assistant
Let me look at the TypedExpr definition:
Assistant
I realize I need to trace through more carefully. The fix to
can_extract_statementmight not be enough because when the use statement is rejected, the visitor still dives into its call expression, which might get selected bycan_extract_expression. Let me check this by looking at howvisit_typed_useworks and whether the ExtractFunction overrides it:Assistant
Let me look at the
CallArgstructure:Assistant
Let me look at how the use's callback function can be accessed from a
TypedUse:Assistant
I need to also fix
can_extract_expressionfor use-relatedFnandCallexpressions. Let me first check what imports are available:Assistant
Good,
FunctionLiteralKindandImplicitCallArgOriginare imported. Now let me updatecan_extract_expressionto handle use-related expressions. Let me first see the full function:Assistant
Now I need to modify
can_extract_expressionto handle use-relatedFnandCallexpressions. Let me apply the fix:Assistant
I see the edit created duplicate entries. Let me fix this by removing the duplicates:
Assistant
There's still a dangling line. Let me remove it:
Assistant
Let me verify the full function looks correct:
Assistant
The code looks correct. Let me run the tests again:
Assistant
Let me fix the command:
Assistant
Good, use tests pass. Let me run extract_function tests:
Assistant
Let me run the full action test suite:
Assistant
All 881 tests pass. Let me also run the broader language server tests and compiler-core tests:
Assistant
All 1537 language server tests pass. Let me also run the compiler-core tests to be safe:
Assistant
All tests pass. Let me verify the final diff:
Assistant
The changes look correct. Let me write a quick summary of what I've done:
Summary of Changes
The issue was that when a user selects code that starts AFTER some
useexpressions in a function, the "Extract function" refactoring incorrectly extracts the entire function body instead of just the selected portion.Root Cause
In Gleam,
useexpressions desugar to function calls with callbacks that capture all subsequent statements in the enclosing block. This means:TypedUse.call(the desugared call) has a location that spans from the keyword to the end of the enclosing block/function.
Assistant
I'm satisfied with the fix. Let me submit the changes.
Checklist
-
If an error was reported in the issue description, I have successfully reproduced it.
- I analyzed the code in detail and traced through the logic to confirm the bug. The issue is in
can_extract_statementandcan_extract_expression: when ausestatement's location spans to the end of the function (due to desugaring including a callback),completely_withinreturnsfalsewhen the selection touches the same end boundary, causing to return and the entire use expression to be selected even though the selection starts after it.
- I analyzed the code in detail and traced through the logic to confirm the bug. The issue is in
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.