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:/// 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:
fn prepare_project_structure( target_path target: TargetPath, user_id user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) { function(target, user_id) }instead of the expected:
/// 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 function(target, user_id) } fn function( target: TargetPath, user_id: uuid.Uuid, ) -> Result(Nil, SubmitError) { 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) }Using gleam
1.16.0in neovim nightlyADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use to restore the file to its original state. You can also reset all changes in the repository using command. Additionally, you can use and to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call ) using or commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted.
Assistant
I'll start by exploring the repository structure to understand the codebase and then locate the relevant code for the "Extract function" feature.
Assistant
This is the Gleam compiler repository. The issue is about "Extract function" which is a language server feature. Let me look at the language-server directory.
Assistant
The
code_action.rsfile is quite large (438KB) - this is likely where the extract function feature is implemented. Let me search for "extract" related code:Assistant
Let me look at the ExtractFunction struct and related code:
Assistant
I need to provide the line_number as an integer, not a string.
Assistant
Let me continue reading the code actions function to understand how it determines what to extract:
Assistant
Now let me look at the visitor implementation to understand how statements are being selected:
Assistant
Now let me look at the
can_extract_statementfunction:Assistant
Now let me look at the
can_extract_expressionfunction:Assistant
Now let me search for the
overlapsandcompletely_withinfunctions:Assistant
Assistant
Now let me look at the tests for extract function to understand the expected behavior better:
Assistant
Let me look at the test for partially selected extract function:
Assistant
Now let me check the snapshots directory for extract function tests:
Assistant
Let me look at the
extract_function_from_statementstest case since it seems most relevant to the issue:Assistant
Now let me understand the issue better. The problem is when selecting statements starting from a comment in the middle of a function, it extracts too much (the entire function body). Let me create a test case that reproduces the issue:
Assistant
Let me look for existing tests related to selecting statements from a comment or partially:
Assistant
Now let me understand the issue more precisely. Looking at the issue description, the user selects from a comment "// <-------------------------------------------------------------- selected from here" to another comment "// <---------------------------------------------------------------- to here". But it extracts the entire function content. Let me look at how the visitor is traversing and deciding what to extract.
The key logic seems to be in
can_extract_statement:Assistant
Now let me look at
can_extract_expressionmore closely:Assistant
Now I need to understand the issue. The problem is:
- When a user selects from a comment before a statement to a comment after another statement
- The extract function extracts too much - the entire function body
The key issue seems to be how the visitor traverses statements and decides what to extract. Let me look at the
visit_typed_statementfunction again and trace through how it processes statements:Assistant
I think I see the issue now. The problem is when
can_extract_statementreturnstruefor a statement, it's added to the extraction. The issue is that when the selection starts from a comment (which is not part of any statement's location span), the firstusestatement might be considered as "overlapping" when it shouldn't be.Let me check what
overlapsdoes with ranges and understand how the selection from a comment might be interpreted:Assistant
Let me search for tests that involve extracting from use expressions:
Assistant
Let me look for tests with multiple statements including use:
Assistant
Let me create a test case that reproduces the issue described. First, let me look at how to run the tests:
Assistant
Let me look at how to run specific tests:
Assistant
Good, Rust is installed. Now let me run the extract function tests to make sure I understand the test framework:
Assistant
The build is downloading crates. I need to wait for it to finish. Let me continue waiting for the test output.
Assistant
Good, the test passes. Now let me understand the issue better by creating a test case that reproduces the bug. The issue is about extracting from a comment, which means the selection doesn't start at the beginning of a statement. Let me look for a test that starts selection from somewhere other than the beginning:
Assistant
Assistant
Let me search for the TypedUse struct definition to understand its structure.
Assistant
Let me search for the TypedUse struct more broadly:
Assistant
The grep didn't find "pub struct TypedUse". Let me search for it differently.
Assistant
The user hasn't received my previous response yet - the environment is still showing me my last command output. I need to continue investigating the TypedUse struct.
Assistant
I see! The
TypedUsestruct has alocationfield that only covers the use line itself (fromusekeyword to the end of the RHS of<-), NOT the callback body. Let me check howStatement::location()is implemented:Assistant
Assistant
Now I understand the issue! The
Statement::location()forUsestatements mergesuse_.locationwithuse_.call.location(). Thecallin a typedUseis the desugared function call including the callback body, which spans all statements in the use block. This causes statements before the selection to be incorrectly included.Let me verify my understanding and then implement the fix:
Assistant
Now I understand the issue! The
statement.location()forUsestatements mergesuse_.locationwithuse_.call.location(). Thecallin a typedUseis the desugared function call including the callback body, which spans all statements in the use block. This causes statements before the selection to be incorrectly included.The fix is to use
use_.location(just the use line) instead ofstatement.location()(which includes the callback body) when checking overlaps forUsestatements.Let me implement the fix:
Assistant
Now I understand the issue clearly. The
statement.location()forUsestatements includes the entire callback body (viause_.location.merge(&use_.call.location())), which spans all statements in the use block. When the selection starts from within a use callback (e.g., from a comment after some statements), the overlap check incorrectly includes the precedingusestatements.The fix is to use only
use_.location(just the use line, not the callback body) when checking overlaps forUsestatements.Let me implement this fix:
Assistant
Now I need to implement the fix properly. Let me edit the
can_extract_statementfunction to handleUsestatements specially. The issue is thatstatement.location()forUsestatements includes the entire callback body, but we only want to check if the selection overlaps with the use line itself (not the callback body).Assistant
Now I understand the issue. Let me implement the fix properly by handling
Usestatements specially:Assistant
I see there are duplicate closing braces. Let me fix that:
Assistant
Now let me run the tests to verify the fix:
Assistant
I need to run the tests related to extracting use expressions to make sure my fix doesn't break anything. Let me run more tests.
Assistant
Good, the extract_use tests are passing. Now let me run all the tests related to extract to make sure I haven't broken anything.
<tool_call> <function=bash_terminal> <parameter=command> cd /gleam && cargo test --package gleam-language-server "extract" -- --nocapture 2>&1 | tail -100 </tool_call>
Assistant
I need to run the command again with proper formatting.
Assistant
All 179 extract tests pass. Now let me create a test case that specifically reproduces the issue from the bug report to verify my fix works correctly.
Assistant
Let me create a proper test case that matches the issue description to verify my fix:
Assistant
Let me look at existing tests that test selection starting after use statements:
Assistant
Now let me look at the snapshot for
extract_use_with_some_statements_beforeto understand how it handles use statements:Assistant
I need to understand the issue better. The test
extract_use_with_some_statements_beforeselects fromlettouse, which should extract both statements. This is working as expected.Now let me write a test case that reproduces the actual bug - where selecting from a comment (after use statements) should NOT include the preceding use statements:
Assistant
I need to run a more comprehensive test. Let me run all the language server tests to make sure nothing is broken:
Assistant
All 1537 tests pass. Now let me create a temporary test script to verify that my fix actually addresses the issue described in the bug report.
Assistant
Let me examine the test that selects statements after a use expression to ensure my fix handles it correctly:
Assistant
Now let me verify my understanding of the fix by checking the diff:
Assistant
The diff looks good. My fix:
-
For
Usestatements: Instead of usingstatement.location()which includes the entire callback body, I now useuse_.locationwhich is just the use line (fromusekeyword to the end of the RHS of<-). This prevents the issue where selecting from within a use callback would incorrectly mark preceding use statements for extraction. -
For
Assertstatements: Added an explicitoverlapscheck before thecompletely_withincheck, consistent with other statement types. -
For
Assignmentstatements: Added an explicitoverlapscheck before the other checks, consistent with other statement types.
The key fix is for
Usestatements - by usinguse_.locationinstead ofstatement.location(), we ensure that only selections that actually overlap with the use line itself will cause the use statement to be extracted, not selections that overlap with the callback body.Now let me verify the fix by reviewing the final code:
-
Assistant
I need to verify the tests pass by running the full test suite.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.