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 nightlyInterface
No new interfaces are introduced.
ADDITIONAL 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 locate the extract-function implementation and related tests.
- Assistant
I'll search the codebase for the extract-function feature next.
- Assistant
Let me look at the extract function implementation in code_action.rs
- Assistant
There's ExtractVariable, but I need Extract Function specifically.
- Assistant
Reading the ExtractFunction implementation.
- Assistant
I'll keep reading the selection logic and surrounding code.
- Assistant
Let me continue with the visit implementation and how selections are matched.
- Assistant
Let me look at the visit implementation for ExtractFunction.
- Assistant
Looking closer at how selection ranges are checked for statements and use-expressions.
- Assistant
Let me continue reading can_extract_statement
- Assistant
Reviewing how Use is visited and checking related extract-function tests.
- Assistant
Checking how Use statement locations are defined.
- Assistant
Let me look at the Use struct and its location methods.
- Assistant
I need to check what location is used for Statement::Use
- Assistant
Hmm let me look at TypedStatement location
- Assistant
Checking how use desugaring sets locations on the callback function.
- Assistant
Let me search more specifically for how use expressions are typed.
- Assistant
Let me look at infer_use
- Assistant
So the use
calllocation spans fromusekeyword to the end of the sequence. When we extract Use:I understand the issue you're describing with theExtract functioncommand in the Gleam language server. The problem occurs when the selected range is inside auseexpression that spans multiple statements, causing the extracted function to include too much code.Let me break this down:
- Assistant
I'll scan how ExtractedValue::Expression handles different FunctionLiteralKind values, including Use.
- Assistant
Need to open the right file first.
- Assistant
Checking completely_within, within, and overlaps helpers.
- Assistant
Let me read those helper functions.
- Assistant
I'll look through extract function tests for use-related cases.
- Assistant
Let me look at extract function tests involving use.
- Assistant
There are more, let me get the rest.
- Assistant
Hmm empty. Let me try again.
- Assistant
Let me search for "use" in extract function test code specifically.
- Assistant
Let me look for more extract function tests with use, particularly later or involving selecting inside use body.
- Assistant
There's extract_use_inside_function and similar tests around 12462. Let me get those.
- Assistant
I'll check git history and any related issues around extract-function and use.
- Assistant
I'll inspect language-server test helpers and snapshots for extract-function.
- Assistant
I'll try to reproduce the bug with a focused scenario and run the relevant tests.
- Assistant
Checking how language-server tests are run and scanning related snapshots.
- Assistant
I'll try recreating the reported case with a temporary test.
- Assistant
Need to set up rust.
- Assistant
I'll add a temporary test that mirrors the reported case.
- Assistant
I'll insert a temporary regression test right after the existing extract-use cases.
- Assistant
I need to understand how find_position_of and nth_occurrence works for gleam_toml. Let me run the test.
- Assistant
There's a syntax error. Looking at my edit - I replaced only line 12533 which was blank, but maybe something went wrong. Let me check around that area.
- Assistant
The test looks fine. There's an error about unclosed delimiter at line 8110. Let me check that.
- Assistant
That looks fine - it's a long function name split across lines. The error might be something else. Let me check the end of the file and count braces.
- Assistant
There's a brace imbalance around 13707. Let me check.
- Assistant
I’m rechecking rustc’s error details and verifying the temporary test syntax.
- Assistant
Let me write a proper script file.
- Assistant
I'll stash my change and confirm whether the compile error existed beforehand.
- Assistant
So my edit introduced a syntax error. Let me pop stash and fix more carefully.
- Assistant
I’ll re-run the compiler for a clearer error message.
- Assistant
rg not found, use grep.
- Assistant
Hmm only shows first line now. Let me get full error.
- Assistant
Checking the full diff for unexpected characters or broken string delimiters.
- Assistant
The closing brace for confidential outer test may have been lost—checking the original structure.
- Assistant
I accidentally overwrote the previoustest's closing brace. Fixing the temporary test insert.
- Assistant
Good, now let's run the test.
- Assistant
The bug likely depends on selecting through the tail statement, so I'll adjust the selection to the end.
- Assistant
Let me look at how find_position_of works and under_last_char.
- Assistant
I'll try selecting through the final gleam_toml with nth_occurrence(4).
- Assistant
Also remove the .snap.new file before running.
- Assistant
The correct path may depend on starting at theコメント comment—I'll try that selection next.
- Assistant
Also installation need to check how select_until works with under_last_char - is the end inclusive of character or position?
- Assistant
I'll inspect what sequence_location covers for use bodies, especially trailing comments.
- Assistant
I'll end the selection after the last expression, for example up through the closing brace.
- Assistant
I accidentally edited expression.rs; restoring it and applying the change in the test file.
- Assistant
Good. Now edit with --file option.
- Assistant
Now run the test again.
- Assistant
Checking how CallKind is represented ontyped calls.
- Assistant
Let me look at TypedExpr::Call structure
- Assistant
I'll inspect CallArg and implicit Use origin next.
- Assistant
I'll check how partial selections are handled in existing tests.
- Assistant
Confirming the biopsy fix: treat a selection as “inside” if its start is strictly inside the node so trailing ends don’t expand extraction. Checking all uses in extract_function.
- Assistant
Implementing the fix in extract-function selection logic.
- Assistant
I'll update the three places and add a helper function, plus revise the comment.
- Assistant
I need to add the helper function and clean up the import.
- Assistant
Removing the extra closing brace and adding the helper.
- Assistant
I need to check what Range is - is it imported in code_action.rs? And completely_within import should be removed if unused.
- Assistant
Range is imported_from lsp_types. I should remove
completely_withinfrom the import. - Assistant
Now let's run the reproduction test.
- Assistant
Fixed. Running existing extract-function tests and removing the temporary test afterward.
- Assistant
Two existing tests failed — inspecting them to see what broke.
- Assistant
Narrowing the fix to Use-related nodes so we don't break partial multi-statement selection.
- Assistant
Now rewrite can_extract_expression and can_extract_statement properly.
- Assistant
I'll fix the typo and verify the surrounding structure.
- Assistant
Looks good. CallArg is already imported. Let's run the tests again.
- Assistant
All existing tests passed. Removing the temporary test next.
- Assistant
Confirming only the intended changes remain and re-running extract-function tests.
- Assistant
All extract tests passed. Reviewing the final diff.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.