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
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /gleam, current file: none) bash-$
Assistant
Let me start by exploring the repository structure to understand what we're working with. This appears to be a Gleam language server or IDE tools repository, and the issue is about an "Extract function" feature that extracts too much code when triggered.
Assistant
This is the Gleam compiler/language server repository. The issue is about the "Extract function" feature in the language server. Let me explore the language-server directory.
Assistant
Let me look at the language server source code structure.
Assistant
Let me search for "extract" related code in the repository.
Assistant
I found "Extract variable" functionality. Now let me search for "Extract function" specifically.
Assistant
I found the ExtractFunction code. Let me examine it in detail.
Assistant
The file is large. Let me look at the ExtractFunction implementation starting from around line 10354.
Assistant
Let me continue reading the code to understand how the extraction works.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '10600,10950p' /gleam/language-server/src/code_action.rs </tool_call>
Assistant
Let me continue reading the code in chunks.
Assistant
Let me continue reading to understand the extraction logic and the
visit_typed_modulefunction.Assistant
Let me continue reading the visitor implementation which is where the selection logic happens.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '11339,11600p' /gleam/language-server/src/code_action.rs </tool_call>
Assistant
Let me continue reading the visitor implementation.
Assistant
Let me continue reading the rest of the visitor implementation to understand the full picture. I also need to understand the issue better. The problem is that when selecting code from a specific point (the comment
// <-------------------------------------------------------------- selected from here) to the end of a function, it extracts the entire function instead of just the selected portion.The key issue seems to be around how statements are being identified for extraction, particularly around the logic that determines if a statement overlaps with the selection.
Let me look at the
visit_typed_statementfunction more carefully, and understand how statements before the selection are being handled.Looking at the code, I see that in
visit_typed_statement:- First it checks
can_extract_statement - If it overlaps, it may set
self.functionto a newExtractedFunction
But wait, there's a problem. Look at
can_extract_statement:fn can_extract_statement(&self, statement: &TypedStatement) -> bool { let statement_range = self.edits.src_span_to_lsp_range(statement.location()); let selected_range = self.params.range; // If the selected range doesn't touch the statement at all, then there // is no reason to extract it. if !overlaps(statement_range, selected_range) { return false; } // ... }The problem is that if the selected range overlaps with any statement, it might trigger extraction of that statement, even if the user only wanted to extract a later portion.
Looking at the code more closely, I see that
self.functionis only set when it'sNone:match &mut self.function { None => { // ... set self.function to a new ExtractedFunction } Some(ExtractedFunction { ... }) => { // ... merge/continue extracting } }So the first statement that overlaps with the selection will set
self.function, and subsequent overlapping statements will merge into it.The issue is that statements before the selection can also overlap with it if the selection starts at a point that's within the span of a previous statement. But that doesn't seem right...
Wait, let me look at the issue more carefully. The user is selecting from:
// <-------------------------------------------------------------- selected from here let toml_path = ...So the selection starts at a specific point within the function, after some statements have already been visited.
Looking at the
visit_typed_function:fn visit_typed_function(&mut self, function: &'ast TypedFunction) { let range = self.edits.src_span_to_lsp_range(function.full_location()); if within(self.params.range, range) { self.function_end_position = Some(function.end_position); self.last_statement_location = function.body.last().map(|last| last.location()); ast::visit::visit_typed_function(self, function); } }It first checks if the selection is within the function. Then it visits all the statements in the function.
Now, let me trace through what happens when the selection starts from the middle of the function:
visit_typed_functionis called- It visits all statements from the beginning
- For each statement,
visit_typed_statementis called can_extract_statementchecks if the statement overlaps with the selection- The first statement that overlaps will set
self.function
The problem is likely in the
visit_typed_exprmethod:fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { // If we have already determined what code we want to extract, we don't // want to extract this instead. if self.function.is_none() { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } // ... }When the selection starts from the middle of a function, the very first statement in the function that overlaps will set
self.function. But the statements before the selection should NOT be extracted!Wait, let me look at the
can_extract_statementfunction more carefully:fn can_extract_statement(&self, statement: &TypedStatement) -> bool { let statement_range = self.edits.src_span_to_lsp_range(statement.location()); let selected_range = self.params.range; // If the selected range doesn't touch the statement at all, then there // is no reason to extract it. if !overlaps(statement_range, selected_range) { return false; } // ... }So it checks for overlap. If the selection starts from the middle, statements before the selection should NOT overlap with the selection... unless their spans include the selection start point.
Actually wait - statements don't overlap with each other. Each statement has its own span. So if the selection starts from the middle of the function, only statements from that point onwards should overlap.
Let me re-read the issue more carefully.
The user selects from:
// <-------------------------------------------------------------- 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 }And it extracts the entire function, including:
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) }So the expected behavior is that only the selected statements should be extracted, but the actual behavior is that the entire function is being extracted.
This suggests the issue is in how the extraction span is being calculated or merged. Let me look at the statement merging logic:
// If we are selecting multiple statements, this statement should // be included within that list, so we merge the spans to ensure // it is included. Some(ExtractedFunction { value: ExtractedValue::Statements { location, position: extracted_position, }, .. }) => { *location = location.merge(&statement_location); *extracted_position = position; }Hmm, the statements are being merged correctly. The issue might be in how
visit_typed_statementworks or incan_extract_statement.Wait, let me look at the
visit_typed_statementfunction more carefully:fn visit_typed_statement(&mut self, statement: &'ast TypedStatement) { let statement_location = statement.location(); if self.can_extract_statement(statement) { // ... set or merge self.function } ast::visit::visit_typed_statement(self, statement); }And before this,
visit_typed_functiondoes:self.last_statement_location = function.body.last().map(|last| last.location());This sets the last statement location to the very last statement of the function.
Then in
visit_typed_statement, it checks:let is_in_tail_position = self.last_statement_location .is_some_and(|last_statement_location| { last_statement_location == statement_location });This checks if the current statement is the last statement. But
last_statement_locationis set to the very last statement of the function body, not the last statement within the selection.So here's the problem! When the user selects from the middle of a function to the end:
visit_typed_functionsetslast_statement_locationto the very last statement of the function body- For each statement in the function,
visit_typed_statementis called - The first statement that overlaps with the selection sets
self.functiontoStatements { position: NotTail }if it's not the tail position - As we iterate through statements, subsequent statements that overlap get merged
- When we hit the actual last statement of the function, it gets merged with
position: Tail
But the issue is that
self.last_statement_locationis set to the last statement of the entire function, not the last statement of the selection. So if the selection starts from the middle, statements before the selection start might get their position set incorrectly.Actually wait, no. The statements before the selection start should NOT overlap with the selection, so
can_extract_statementshould return false for them.Let me look at this more carefully. The selection starts at a specific position. The first statement that overlaps should be the
let toml_path = ...statement.But wait, the user's selection starts from a comment line. Let me look at the code:
/// 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 = ...So the selection starts from the comment
// <-------------------------------------------------------------- selected from herewhich is a comment line, not a statement. The first actual statement that overlaps with the selection islet toml_path = ....OK wait, let me think about this differently. The user says it extracts the entire function content. That means somehow ALL statements are getting merged into the extraction.
Let me look at how
visit_typed_functiontraverses statements. Theast::visit::visit_typed_functionfunction likely visits all statements in the function body.Now, when
visit_typed_statementis called for each statement, it checkscan_extract_statement. If the statement overlaps with the selection, it sets or mergesself.function.Hmm, but statements before the selection shouldn't overlap... unless their spans somehow extend past the selection start.
Actually, I wonder if the issue is with the
visit_typed_exprmethod. Let me look at it again:fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { // If we have already determined what code we want to extract, we don't // want to extract this instead. This expression would be inside the // piece of code we already are going to extract, leading to us // extracting just a single literal in any selection, which is of course // not desired. if self.function.is_none() { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } // ... }This is called for ALL expressions. If
self.functionis stillNonewhen this is called for an expression in an earlier statement, it might setself.functionto that expression.But wait, the user is selecting multiple statements, so they're selecting a block of code. The expressions within those statements should overlap with the selection, but expressions within earlier statements should NOT overlap.
Unless... the issue is that
can_extract_expressionreturns true for expressions that don't actually overlap properly?Let me look at
can_extract_expression:fn can_extract_expression(&self, expression: &TypedExpr) -> bool { let expression_range = self.edits.src_span_to_lsp_range(expression.location()); let selected_range = self.params.range; // If the selected range doesn't touch the expression at all, then there // is no reason to extract it. if !overlaps(expression_range, selected_range) { return false; } // ... }So it checks for overlap. If an expression doesn't overlap, it returns false.
But there's another case in
can_extract_expression:match expression { TypedExpr::Pipeline { first_value, finally, .. } => { // We can extract a pipeline as a whole only if the selection // spans all of its steps! let first_step = self.edits.src_span_to_lsp_range(first_value.location); let last_step = self.edits.src_span_to_lsp_range(finally.location()); position_within(selected_range.start, first_step) && position_within(selected_range.end, last_step) } // ... }This is only for pipelines.
Let me look at the other cases:
TypedExpr::Int { .. } | TypedExpr::Float { .. } | TypedExpr::String { .. } | TypedExpr::Block { .. } | TypedExpr::Var { .. } | TypedExpr::Fn { .. } | TypedExpr::List { .. } | TypedExpr::Call { .. } | TypedExpr::BinOp { .. } | TypedExpr::Case { .. } | TypedExpr::RecordAccess { .. } | TypedExpr::PositionalAccess { .. } | TypedExpr::ModuleSelect { .. } | TypedExpr::Tuple { .. } | TypedExpr::TupleIndex { .. } | TypedExpr::Todo { .. } | TypedExpr::Panic { .. } | TypedExpr::Echo { .. } | TypedExpr::BitArray { .. } | TypedExpr::RecordUpdate { .. } | TypedExpr::NegateBool { .. } | TypedExpr::NegateInt { .. } | TypedExpr::Invalid { .. } => !completely_within(selected_range, expression_range),Wait! For most expression types,
can_extract_expressionreturns!completely_within(selected_range, expression_range). This means it returns TRUE if the expression is NOT completely within the selected range. This could cause early statements to be extracted!Actually, wait. Let me re-read this.
completely_within(selected_range, expression_range)returns true if the expression is completely within the selection.!completely_within(...)returns true if the expression is NOT completely within the selection. Socan_extract_expressionwould return true for expressions that:- Overlap with the selection, AND
- Are NOT completely within the selection
This makes sense for a single expression - if you select part of a large expression, you can extract it. But what about a statement that has an expression that's completely within the selection?
Actually, I think the problem might be in the interaction between
visit_typed_statementandvisit_typed_expr. Let me trace through what happens:visit_typed_functionvisits all statements in the function body- For each statement,
visit_typed_statementis called visit_typed_statementfirst checkscan_extract_statement- If that passes, it sets or merges
self.function - Then it calls
ast::visit::visit_typed_statement(self, statement)which visits the expression within the statement
But wait, there's an issue! Look at
can_extract_statement:fn can_extract_statement(&self, statement: &TypedStatement) -> bool { let statement_range = self.edits.src_span_to_lsp_range(statement.location()); let selected_range = self.params.range; // If the selected range doesn't touch the statement at all, then there // is no reason to extract it. if !overlaps(statement_range, selected_range) { return false; } match statement { ast::Statement::Expression(expression) => self.can_extract_expression(expression), ast::Statement::Use(_) | ast::Statement::Assert(_) => { !completely_within(selected_range, statement_range) } ast::Statement::Assignment(assignment) => { let value_range = self .edits .src_span_to_lsp_range(assignment.value.location()); !within(selected_range, value_range) && !completely_within(selected_range, statement_range) } } }For a
letstatement (Assignment), it checks:- The value range is NOT within the selected range
- The statement range is NOT completely within the selected range
But what if the first statement in the selection has its entire value within the selection AND the entire statement within the selection? Then
can_extract_statementwould return false, andvisit_typed_statementwouldn't setself.function.But then
ast::visit::visit_typed_statement(self, statement)is called, which visits the expression. And invisit_typed_expr, ifself.functionis stillNone, it might set it.Hmm, but for the first statement that overlaps with the selection,
can_extract_statementshould return true (assuming it's a let statement where the assignment name is selected or part of the expression is selected).OK let me think about this differently. The issue is that the entire function is being extracted. So somehow ALL statements are getting merged into the extraction.
Wait, let me re-read the issue more carefully:
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) }So ALL the statements are being extracted into
function. The expected behavior is to extract only the selected portion.This means either:
- The selection actually includes all statements (which would be a different bug)
- The extraction logic is including statements that shouldn't be extracted
Let me look at the
visit_typed_statementfunction again:fn visit_typed_statement(&mut self, statement: &'ast TypedStatement) { let statement_location = statement.location(); if self.can_extract_statement(statement) { let is_in_tail_position = self.last_statement_location .is_some_and(|last_statement_location| { last_statement_location == statement_location }); let position = if statement.is_use() || is_in_tail_position { StatementPosition::Tail { type_: statement.type_(), } } else { StatementPosition::NotTail }; match &mut self.function { None => { // ... set self.function } Some(ExtractedFunction { value: ExtractedValue::Use { location, .. }, .. }) if location.contains_span(statement_location) => {} Some(ExtractedFunction { value: value @ ExtractedValue::Use { .. }, .. }) => { *value = ExtractedValue::Statements { location: value.location().merge(&statement_location), position, }; } Some(ExtractedFunction { value: ExtractedValue::Expression(_), .. }) => {} Some(ExtractedFunction { value: ExtractedValue::Statements { location, position: extracted_position, }, .. }) => { *location = location.merge(&statement_location); *extracted_position = position; } Some(ExtractedFunction { value: value @ ExtractedValue::PipelineSteps { .. }, .. }) => { *value = ExtractedValue::Statements { location: value.location(), position, } } } } ast::visit::visit_typed_statement(self, statement); }And the merge logic:
Some(ExtractedFunction { value: ExtractedValue::Statements { location, position: extracted_position, }, .. }) => { *location = location.merge(&statement_location); *extracted_position = position; }This merges the current statement location into the extracted location. If statements before the selection somehow get their
can_extract_statementreturn true, they would get merged.OK let me think about this again. The key question is: why would
can_extract_statementreturn true for statements BEFORE the selection?Let me look at 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 = ...The selection starts after the
use _ <- result.try(...)statement that copies the base project. So the first statement in the selection islet toml_path = ....Wait, actually the selection is indicated by comments:
// <-------------------------------------------------------------- selected from hereis on the line BEFORElet toml_path// <---------------------------------------------------------------- to hereis at the end
So the selection actually starts from the comment line, which means the first statement that overlaps would be
let toml_path = ....Actually, I need to understand better what
overlapsdoes. Let me find it.<tool_call> <function=bash_terminal> <parameter=command> grep -n "fn overlaps" /gleam/language-server/src/*.rs </tool_call>
- First it checks
Assistant
Let me search for the overlaps function in the codebase.
Assistant
Let me look at the overlaps function.
Assistant
Let me look at the
position_withinfunction too.Assistant
Let me read these functions.
Assistant
Let me look at the existing tests for extract function to understand the expected behavior.
Assistant
Let me look at the extract function tests.
Assistant
Now I understand better. The tests show that when selecting a portion of statements, only those statements should be extracted. Let me search for more relevant tests and also look at how the issue can be reproduced.
Let me understand the actual issue better. The user says that when selecting from a certain point in the function to the end, the ENTIRE function is being extracted, not just the selected portion.
This means that statements BEFORE the selection are being incorrectly merged into the extraction. Let me look at what happens when visiting statements before the selection start point.
The key is in
visit_typed_statement. For each statement, it callscan_extract_statementwhich checks if the statement overlaps with the selection. But wait - for statements BEFORE the selection, they should NOT overlap.Let me think about this more carefully. The selection is from a specific position within the function to the end. The statements before that position should have their ranges completely before the selection start, so
overlapsshould return false for them.Unless... there's something special about
usestatements. Let me look at the code again:/// 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 = ...The first two statements are
usestatements. Let me look at howcan_extract_statementhandlesusestatements:ast::Statement::Use(_) | ast::Statement::Assert(_) => { !completely_within(selected_range, statement_range) }So for
usestatements, it returns!completely_within(selected_range, statement_range). This means it returns TRUE if the selected range is NOT completely within the statement range.Now, if the selection starts from the comment line after the second
usestatement, the selected range would NOT be completely within theusestatement range. Theusestatements are earlier in the function, so their range doesn't contain the selection range.Wait,
completely_within(selected_range, statement_range)returns true if the selected range is completely within the statement range. If the selection starts after theusestatement, then the selected range is NOT within theusestatement range, socompletely_withinreturns false, and!completely_withinreturns true.So for
usestatements that are before the selection,can_extract_statementreturns TRUE!This is the bug! The check
!completely_within(selected_range, statement_range)is too permissive. It should also check that the statement overlaps with the selection.Let me verify this logic:
- Selection starts at position P1 (after the second
usestatement) - First
usestatement has range [S1_start, S1_end] where S1_end < P1 completely_within(selected_range, statement_range)checks if P1 >= S1_start && P2 <= S1_end- P1 >= S1_start is true (since P1 is after S1_end, and S1_start < S1_end)
- P2 <= S1_end is false (since P2 is after the end of the function)
- So
completely_withinreturns false
!completely_withinreturns truecan_extract_statementreturns true for thisusestatement even though it's before the selection!
Wait, but there's also the overlap check at the beginning of
can_extract_statement:if !overlaps(statement_range, selected_range) { return false; }This should catch the issue. Let me check what
overlapsdoes:pub fn overlaps(a: Range, b: Range) -> bool { position_within(a.start, b) || position_within(a.end, b) || position_within(b.start, a) || position_within(b.end, a) }For two non-overlapping ranges where a is before b:
- position_within(a.start, b): a.start >= b.start is false (since a is before b)
- position_within(a.end, b): a.end >= b.start is false (since a.end < b.start)
- position_within(b.start, a): b.start >= a.start is true AND b.start <= a.end is false (since b.start > a.end)
- position_within(b.end, a): b.end >= a.start is true AND b.end <= a.end is false
So if a is completely before b,
overlapsreturns false. This means the overlap check should correctly exclude statements before the selection.Hmm, but wait. What if the
usestatement's range somehow extends beyond its actual statement? Let me look at howusestatements are represented.Actually, let me look at the visit logic for
usestatements. Invisit_typed_statement:match &mut self.function { None => { self.function = match statement { TypedStatement::Expression(TypedExpr::Pipeline { .. }) => None, TypedStatement::Assert(_) | TypedStatement::Assignment(_) | TypedStatement::Expression(_) => { Some(ExtractedFunction::new(ExtractedValue::Statements { location: statement_location, position, })) } TypedStatement::Use(use_) => { Some(ExtractedFunction::new(ExtractedValue::Use { location: use_.call.location(), type_: statement.type_(), })) } } } // ... }For
usestatements, it createsExtractedValue::Usewithlocation: use_.call.location(). This is the location of the call expression within theusestatement, not the entire statement including its body.But then, when visiting the
usestatement, it also callsast::visit::visit_typed_statement(self, statement)which would visit the body of theusestatement. And invisit_typed_expr, it might try to extract expressions from the body.Wait, but for
usestatements, the body is a block expression. If we select from the middle of the function, theusestatement's body would overlap with the selection.Hmm, but the issue is that the
usestatements are BEFORE the selection, not within it. The selection starts after the secondusestatement.Let me re-read the issue. The user says:
/// 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 = ...OK so the selection starts from the comment line
// replace $PROJECT_NAME placeholder in the gleam.toml. The//is a comment, not code. So the first statement that overlaps with the selection would belet toml_path = ....But wait, the selection also includes the end of the function:
simplifile.write(toml_path, gleam_toml) |> result.replace_error(FailedToWriteGleamToml) // <---------------------------------------------------------------- to here }So the selection is from the comment line to the end of the function. The selected statements are:
let toml_path = ...use gleam_toml <- result.try(...)let gleam_toml = ...simplifile.write(...)(which is an expression statement)
And the non-selected statements (before the selection) are:
use priv <- result.try(...)use _ <- result.try(...)
These two
usestatements should NOT be extracted. But according to the issue, the ENTIRE function is being extracted.Let me trace through the visitor:
visit_typed_functionis called- It sets
last_statement_locationto the location of the last statement (simplifile.write(...) |> result.replace_error(...)) - It calls
ast::visit::visit_typed_function(self, function) - For each statement in the function body,
visit_typed_statementis called
For the first
use priv <- result.try(...):can_extract_statementis called- It's a
usestatement, so it checks!completely_within(selected_range, statement_range) selected_rangestarts from the comment line,statement_rangeis the location of theusestatement- The selected range is NOT within the
usestatement range, socompletely_withinreturns false !completely_withinreturns true- But wait, there's also the overlap check:
if !overlaps(statement_range, selected_range) - The
usestatement is before the selection, sooverlapsshould return false - So
can_extract_statementshould return false
Hmm, so the overlap check should work correctly. Let me think about this differently.
Actually, wait. What if the issue is not with the
usestatements before the selection, but with thevisit_typed_exprmethod? Let me look at it again:fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { if self.function.is_none() { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } // ... ast::visit::visit_typed_expr(self, expression); // ... }When visiting the
usestatement,visit_typed_statementfirst checkscan_extract_statement. If that returns false (which it should for statements before the selection), it then callsast::visit::visit_typed_statement(self, statement).But
visit_typed_statementdoesn't seem to visit the inner expressions of ausestatement directly. Let me look at the visitor pattern.Actually, let me look at how
usestatements are handled. In Gleam, ausestatement has a body (a block expression). When we visit theusestatement, we also visit its body. The body might contain expressions that overlap with the selection.But wait, the
usestatements in the issue are: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), )These are
usestatements with pipelines insideresult.try(). They don't have a block body likeuse x <- fn() { ... }. So visiting them wouldn't visit any overlapping expressions.OK, let me think about this problem from a different angle. The issue says the entire function is being extracted. This means
self.functionis being set to include all statements from the beginning of the function.Looking at the code flow:
visit_typed_functionvisits all statements in order- For each statement,
visit_typed_statementchecks if it can be extracted - If it overlaps with the selection, it's added to
self.function
For statements before the selection:
- They should NOT overlap with the selection
- So
can_extract_statementshould return false - So they should NOT be added to
self.function
But the issue says the entire function IS being extracted. So something is going wrong.
Let me re-read the
can_extract_statementfunction more carefully:fn can_extract_statement(&self, statement: &TypedStatement) -> bool { let statement_range = self.edits.src_span_to_lsp_range(statement.location()); let selected_range = self.params.range; // If the selected range doesn't touch the statement at all, then there // is no reason to extract it. if !overlaps(statement_range, selected_range) { return false; } match statement { ast::Statement::Expression(expression) => self.can_extract_expression(expression), ast::Statement::Use(_) | ast::Statement::Assert(_) => { !completely_within(selected_range, statement_range) } ast::Statement::Assignment(assignment) => { let value_range = self .edits .src_span_to_lsp_range(assignment.value.location()); !within(selected_range, value_range) && !completely_within(selected_range, statement_range) } } }OK so the overlap check happens FIRST. If the statement doesn't overlap with the selection, it returns false immediately. So statements before the selection should not be extracted.
Unless... there's a bug in how the statement location is computed. Let me check if
usestatement locations include the entireuseblock or just the call expression.Actually, wait. I should look at what happens when the
usestatement has a body block. Let me look at the AST definition.Actually, let me just create a test case and run it to see what happens.
But first, let me look for any existing tests that are similar to the issue. Let me search for tests involving
usestatements.<tool_call> <function=bash_terminal> <parameter=command> grep -n "use.*<" /gleam/language-server/src/tests/action.rs | head -30 </tool_call>
- Selection starts at position P1 (after the second
Assistant
Let me continue looking for tests involving
usestatements in the extract function tests.Assistant
Let me look at the
extract_use_inside_functiontest:Assistant
Let me look at the test for extracting
usestatements with statements before them:Assistant
Let me look at the test for
extract_use_with_some_statements_beforemore closely. The test is:#[test] fn extract_use_with_some_statements_before() { assert_code_action!( EXTRACT_FUNCTION, r#" fn wibble(f) { todo } pub fn main() { let a = wibble(todo) use a <- wibble() Ok(Nil) } "#, find_position_of("let").select_until(find_position_of("use")) ); }So it selects from the start of
letto the start ofuse. This means it's selecting theletstatement but NOT theusestatement.Let me see what the expected output should be for this test. Let me search for the assertion.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "assert_code_action|assert_no_code_action" /gleam/language-server/src/tests/action.rs | head -20 </tool_call>
Assistant
Let me continue my analysis and find the macro definition for
assert_code_action.Assistant
Let me look at the macro definition and also look for existing tests that might help me understand the expected behavior.
Assistant
Let me look at how
usestatements are handled in the AST and understand the issue better. Let me first run the existing tests.Assistant
Let me first set up the Rust toolchain properly.
Assistant
Let me check the git log to understand recent changes related to the extract function feature.
Assistant
Let me look at recent changes related to extract function.
<tool_call> <function=bash_terminal> <parameter=command> cd /gleam && git log --oneline --all -- language-server/src/code_action.rs | head -20 </tool_call>
Assistant
Let me continue my investigation. I need to understand how statements before the selection could be getting merged into the extraction. Let me trace through the logic more carefully and look at existing tests for similar scenarios.
Assistant
Let me create a reproduction test case to understand the issue better:
Assistant
Let me now understand the issue more carefully by looking at the code logic. I need to find out why statements before the selection are being merged into the extraction.
Looking at
visit_typed_statement:fn visit_typed_statement(&mut self, statement: &'ast TypedStatement) { let statement_location = statement.location(); if self.can_extract_statement(statement) { // ... } ast::visit::visit_typed_statement(self, statement); }The key insight is that
ast::visit::visit_typed_statementis called AFTER the check. This visits the inner expression of the statement. But for statements BEFORE the selection, they shouldn't be visited... wait, no, they ARE visited, butcan_extract_statementshould return false for them because they don't overlap with the selection.Let me think about this again. The issue is that when selecting from a specific point to the end of the function, the entire function is extracted.
Wait, I think I finally understand the issue. Let me re-read the
visit_typed_statementcode more carefully:fn visit_typed_statement(&mut self, statement: &'ast TypedStatement) { let statement_location = statement.location(); if self.can_extract_statement(statement) { // ... set or merge self.function } ast::visit::visit_typed_statement(self, statement); }For the FIRST statement that overlaps with the selection,
can_extract_statementreturns true, andself.functionis set to a newExtractedFunction.But then, for statements AFTER that, if they also overlap, they get merged into the existing
self.function.The issue might be in how
can_extract_statementworks for statements that are completely INSIDE the selection. Let me look at the test caseextract_function_from_statements:#[test] fn extract_function_from_statements() { assert_code_action!( EXTRACT_FUNCTION, " pub fn do_things(a, b) { let a = 10 + a let b = 10 + b let result = a * b result + 3 } ", find_position_of("let").select_until(find_position_of("* b")) ); }So it selects from the first
letto* b. This would include the first three statements but NOT the lastresult + 3(which is in tail position).The expected behavior is that the first three statements are extracted, and the last statement remains.
But the issue in the bug report is different. The user selects from a point in the middle of the function (NOT from the beginning), and the ENTIRE function is extracted.
Hmm, let me look at the issue again. The user's selection is from:
// <-------------------------------------------------------------- selected from here let toml_path = ...This means the selection starts from a point AFTER the first two
usestatements. So the first statement that overlaps should belet toml_path = ..., NOT the firstusestatement.Unless... the overlap check is wrong. Let me think about this.
Actually, wait. The overlap check uses LSP ranges, not source spans. Let me look at how the statement location is converted to an LSP range.
self.edits.src_span_to_lsp_range(statement.location())converts aSrcSpanto an LSPRange.A
SrcSpanhas astartandendbyte position. The LSPRangehas astartandendPosition.Let me look at how
src_span_to_lsp_rangeworks:<tool_call> <function=bash_terminal> <parameter=command> grep -n "fn src_span_to_lsp_range" /gleam/language-server/src/edits.rs </tool_call>
Assistant
Let me look at the
src_span_to_lsp_rangefunction.Assistant
The grep command didn't find the function. Let me search for it differently.
Assistant
Let me look at how
src_span_to_lsp_rangeis defined.Assistant
I need to continue analyzing the issue. Let me look at the TextEdits implementation and also think more carefully about the bug.
The issue is that when selecting code from a specific point in the function to the end, the ENTIRE function is being extracted. Let me trace through the code more carefully to understand what's happening.
Actually, I just realized something important. Let me look at the
visit_typed_statementfunction again:fn visit_typed_statement(&mut self, statement: &'ast TypedStatement) { let statement_location = statement.location(); if self.can_extract_statement(statement) { // ... } ast::visit::visit_typed_statement(self, statement); }The
can_extract_statementfunction checks if the statement overlaps with the selection. For statements BEFORE the selection, this should return false because they don't overlap.But wait - what if there's an issue with how the statement location is calculated? Let me look at the test case more carefully.
In the issue, the user selects from a specific comment line to the end of the function. The selection range starts at a specific position. The statements before that position should NOT overlap with the selection.
But the issue says the ENTIRE function is extracted. So somehow, all statements are being included.
Let me think about what could cause this...
Actually, I think I need to look at the test snapshots to understand the expected behavior better. Let me look at the test for
extract_function_from_statements.Actually, let me re-read the issue one more time. The user says:
When triggering
Extract functionwith the selection: [code with selection marked from "selected from here" to "to here"] It will extract the entire function content into a new function leaving: [code with onlyfunction(target, user_id)] instead of the expected: [code with the first twousestatements remaining and the extracted portion replaced withfunction(target, user_id)]So the issue is clear: only the selected statements should be extracted, but the ENTIRE function is being extracted.
Now, looking at the code, I believe the issue is in how statements are checked for overlap. Let me trace through what happens:
visit_typed_functionvisits all statements in order- For each statement,
visit_typed_statementcheckscan_extract_statement - If the statement overlaps with the selection, it's added to
self.function
For statements BEFORE the selection,
can_extract_statementshould return false because they don't overlap. But if they DO overlap (due to a bug in the overlap check or statement location calculation), they would be added toself.function.Wait, but the overlap check is:
if !overlaps(statement_range, selected_range) { return false; }And
overlapsis:pub fn overlaps(a: Range, b: Range) -> bool { position_within(a.start, b) || position_within(a.end, b) || position_within(b.start, a) || position_within(b.end, a) }For a statement that's completely before the selection (i.e.,
a.end < b.start), none of the four conditions would be true, sooverlapsreturns false.So the overlap check should work correctly. The issue must be somewhere else.
Let me look at the test for
extract_use_with_some_statements_before:#[test] fn extract_use_with_some_statements_before() { assert_code_action!( EXTRACT_FUNCTION, r#" fn wibble(f) { todo } pub fn main() { let a = wibble(todo) use a <- wibble() Ok(Nil) } "#, find_position_of("let").select_until(find_position_of("use")) ); }This selects from
lettouse. So it includes theletstatement but NOT theusestatement.The expected behavior would be to extract just the
letstatement.Now let me think about the issue scenario. The user selects from a point AFTER the first two
usestatements to the end of the function. The selection includes:let toml_path = ...use gleam_toml <- result.try(...)let gleam_toml = ...simplifile.write(...) |> result.replace_error(...)
The statements before the selection are:
use priv <- result.try(...)use _ <- result.try(...)
These two
usestatements should NOT be extracted because they're before the selection start.But the issue says the ENTIRE function is extracted. So somehow, these two
usestatements ARE being included.Let me think about what could cause this...
OH WAIT! I think I finally understand the bug!
Look at
visit_typed_function:fn visit_typed_function(&mut self, function: &'ast TypedFunction) { let range = self.edits.src_span_to_lsp_range(function.full_location()); if within(self.params.range, range) { self.function_end_position = Some(function.end_position); self.last_statement_location = function.body.last().map(|last| last.location()); ast::visit::visit_typed_function(self, function); } }It checks if the selection is WITHIN the function. Then it sets
last_statement_locationto the last statement of the function.Then it visits all statements in the function body via
ast::visit::visit_typed_function(self, function).Now, when
visit_typed_statementis called for each statement, it checkscan_extract_statement. For statements before the selection, this should return false.But here's the key:
can_extract_statementuses the OVERLAP check. Forusestatements, it has a special case:ast::Statement::Use(_) | ast::Statement::Assert(_) => { !completely_within(selected_range, statement_range) }This returns true if the selected range is NOT completely within the statement range. For statements before the selection, the selected range is NOT within the statement range, so this returns true.
But there's also the overlap check at the beginning:
if !overlaps(statement_range, selected_range) { return false; }This should return false for statements before the selection. So the
can_extract_statementshould return false for statements before the selection.Unless... there's a bug in the overlap calculation?
Actually, let me re-read the issue more carefully. The user says the selection is from a comment line. A comment is not a statement. So the first statement that overlaps with the selection would be
let toml_path = ....But what if the selection includes a point within an earlier statement? For example, if the selection starts from a point that's within the span of the second
usestatement?Let me look at the selection markers again:
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 =The selection starts from the comment line, which is AFTER the closing
)of the secondusestatement. So the secondusestatement should NOT overlap with the selection.But wait... what if the issue is not with the overlap check, but with something else?
Let me look at the visitor code more carefully. In
visit_typed_statement:fn visit_typed_statement(&mut self, statement: &'ast TypedStatement) { let statement_location = statement.location(); if self.can_extract_statement(statement) { // ... set or merge self.function } ast::visit::visit_typed_statement(self, statement); }So for each statement, it first checks
can_extract_statement. If that returns true, it sets or mergesself.function. Then it visits the inner expression of the statement.For
usestatements, the inner expression might overlap with the selection. Butusestatements don't have inner expressions that are separate from the statement itself. Theusestatement has a call expression (e.g.,result.try(...)) which is the right-hand side, and optionally a body block.Hmm, let me look at how
usestatements work in Gleam.A
usestatement in Gleam looks like:use x <- fn() { ... } some_codeThe
usestatement has a call expression and an optional body block. The body block is the rest of the code after theusestatement.When visiting a
usestatement, we visit the call expression and the body block. If the body block overlaps with the selection, then expressions within the body block might be visited.But in the issue, the
usestatements don't have explicit body blocks:use priv <- result.try( application.priv_directory("gleeps") |> result.replace_error(FailedToFindPrivDirectory), ) use _ <- result.try( simplifile.copy_directory(filepath.join(priv, "base_project"), target.path) |> result.replace_error(FailedToCopyBaseProject), )These
usestatements just bind a variable to the result of a pipeline call. There's no body block. So when visiting theseusestatements, we only visit the call expression (the pipeline).Now, the call expression (the pipeline) is BEFORE the selection. So it shouldn't overlap with the selection.
OK I think the issue must be somewhere else. Let me look at the code more carefully.
Actually, wait. Let me re-read the
visit_typed_exprfunction:fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { if self.function.is_none() { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } // ... ast::visit::visit_typed_expr(self, expression); // ... }When visiting an expression, if
self.functionis stillNone, it checkscan_extract_expression. If the expression overlaps with the selection and can be extracted, it setsself.functionto that expression.Now, here's a potential issue. When visiting a
usestatement, the call expression inside it (the pipeline) is visited viaast::visit::visit_typed_expr. If the call expression overlaps with the selection, it would setself.function.But the call expression is BEFORE the selection, so it shouldn't overlap.
Unless... there's a bug in how the selection range is calculated?
Actually, let me look at the
can_extract_expressionfunction more carefully:fn can_extract_expression(&self, expression: &TypedExpr) -> bool { let expression_range = self.edits.src_span_to_lsp_range(expression.location()); let selected_range = self.params.range; // If the selected range doesn't touch the expression at all, then there // is no reason to extract it. if !overlaps(expression_range, selected_range) { return false; } match expression { TypedExpr::Pipeline { first_value, finally, .. } => { // We can extract a pipeline as a whole only if the selection // spans all of its steps! let first_step = self.edits.src_span_to_lsp_range(first_value.location); let last_step = self.edits.src_span_to_lsp_range(finally.location()); position_within(selected_range.start, first_step) && position_within(selected_range.end, last_step) } // ... } }For most expression types, it returns
!completely_within(selected_range, expression_range)if the expression overlaps with the selection.This means that for an expression that OVERLAPS with the selection,
can_extract_expressionreturns true (unless it's completely within the selection).So if an expression overlaps with the selection, it would be marked for extraction.
But the issue is that expressions before the selection don't overlap with the selection, so they shouldn't be marked.
Let me think about this differently. What if the issue is that
self.functionis being set by a statement/expr that's actually INSIDE the selection, but then subsequent statements (even those before the selection) are being merged into it?No wait, that doesn't make sense either. Statements are visited in order, so statements before the selection are visited FIRST, and
self.functionisNoneat that point.Let me trace through the logic step by step:
visit_typed_functionis called- For each statement in the function body:
a.
visit_typed_statementis called b.can_extract_statementchecks if the statement overlaps with the selection c. If it overlaps and can be extracted,self.functionis set d.ast::visit::visit_typed_statementis called to visit inner expressions
For statements before the selection:
can_extract_statementshould return false because they don't overlap- So
self.functionremainsNone - Inner expressions are visited, but
self.functionis stillNone
For statements starting from the selection:
can_extract_statementreturns true because they overlapself.functionis set to a newExtractedFunction- Subsequent overlapping statements are merged into it
So the logic seems correct. The issue must be somewhere else.
Actually, let me look at the
can_extract_statementfunction one more time:fn can_extract_statement(&self, statement: &TypedStatement) -> bool { let statement_range = self.edits.src_span_to_lsp_range(statement.location()); let selected_range = self.params.range; // If the selected range doesn't touch the statement at all, then there // is no reason to extract it. if !overlaps(statement_range, selected_range) { return false; } match statement { ast::Statement::Expression(expression) => self.can_extract_expression(expression), ast::Statement::Use(_) | ast::Statement::Assert(_) => { !completely_within(selected_range, statement_range) } ast::Statement::Assignment(assignment) => { let value_range = self .edits .src_span_to_lsp_range(assignment.value.location()); !within(selected_range, value_range) && !completely_within(selected_range, statement_range) } } }Wait! I see it now!
For
ast::Statement::Use(_) | ast::Statement::Assert(_), the function returns!completely_within(selected_range, statement_range).This means it returns TRUE if the selected range is NOT completely within the statement range.
For statements before the selection, the selected range is NOT within the statement range. So
completely_within(selected_range, statement_range)returns false. And!completely_within(...)returns true.BUT! There's the overlap check at the beginning:
if !overlaps(statement_range, selected_range) { return false; }This should return false for statements before the selection. So
can_extract_statementshould return false.Unless... the overlap check is not working correctly?
Let me verify. For a statement that's completely before the selection (i.e.,
statement_range.end < selected_range.start):position_within(statement_range.start, selected_range):statement_range.start >= selected_range.startis falseposition_within(statement_range.end, selected_range):statement_range.end >= selected_range.startis false (sincestatement_range.end < selected_range.start)position_within(selected_range.start, statement_range):selected_range.start >= statement_range.startis true, butselected_range.start <= statement_range.endis falseposition_within(selected_range.end, statement_range):selected_range.end >= statement_range.startis true, butselected_range.end <= statement_range.endis false
So
overlapsreturns false. This meanscan_extract_statementreturns false for statements before the selection.The overlap check seems correct. So the issue must be somewhere else.
Let me look at this from a different angle. What if the issue is in how
visit_typed_exprhandles expressions?When
visit_typed_statementis called for a statement, it first checkscan_extract_statement. If that returns false, it then callsast::visit::visit_typed_statement(self, statement)to visit inner expressions.For a
usestatement that doesn't overlap with the selection,can_extract_statementreturns false. Butast::visit::visit_typed_statementis still called, which visits the inner call expression.Now, when
visit_typed_expris called for the call expression:- It checks
self.function.is_none()- this is true because no statement has set it yet - It checks
can_extract_expression(expression)- if the call expression overlaps with the selection, it returns true
But the call expression is before the selection, so it shouldn't overlap.
Hmm, let me think about this more carefully. The
usestatement is:use priv <- result.try( application.priv_directory("gleeps") |> result.replace_error(FailedToFindPrivDirectory), )The statement location (
statement.location()) includes the entireusestatement, including the call expression. If the call expression's location extends into the selection (even slightly), it would overlap.But in this case, the selection starts after the
usestatement, so the call expression shouldn't overlap.OK I think I need to just run the tests and see what happens. Let me build the project and run the extract function tests.
Actually, wait. Let me look at the issue one more time. The user says the selection starts from a comment line. Comments are not part of the AST, so the selection range should start from the first non-comment character after the comment.
But actually, comments ARE part of the file, and the selection range might include the comment character. The statement location would start from the first character of the
usekeyword, not the comment.So if the selection starts from a comment line, the first statement that overlaps would be the first statement on the next line (or the same line if there's a statement after the comment).
Let me look at the code again:
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 =The selection starts from the
// <-------------------------------------------------------------- selected from herecomment. But//is a comment, and the actual selection starts from the<character or whatever is between the arrows.Actually, looking at the comment markers, I think the user is using ASCII art to indicate the selection range. The
// <-------------------------------------------------------------- selected from hereis a comment indicating the start of the selection, and// <---------------------------------------------------------------- to hereis a comment indicating the end of the selection.So the actual selection is from the
let toml_path = ...line to thesimplifile.write(...) |> result.replace_error(FailedToWriteGleamToml)line.The first statement that overlaps with the selection would be
let toml_path = ....Now, the statements before the selection are:
use priv <- result.try(...)use _ <- result.try(...)
These should NOT overlap with the selection, so they should NOT be extracted.
But the issue says the ENTIRE function is extracted. So there must be a bug somewhere.
Let me look at the
visit_typed_functionmethod again:fn visit_typed_function(&mut self, function: &'ast TypedFunction) { let range = self.edits.src_span_to_lsp_range(function.full_location()); if within(self.params.range, range) { self.function_end_position = Some(function.end_position); self.last_statement_location = function.body.last().map(|last| last.location()); ast::visit::visit_typed_function(self, function); } }It visits all statements in the function body via
ast::visit::visit_typed_function(self, function).Now, here's a potential issue. What if
visit_typed_functionis called for an INNER function (e.g., an anonymous function defined within the outer function)? In that case, the inner function's body would be visited, and if the selection overlaps with the inner function, statements in the inner function might be incorrectly merged into the extraction.But in the issue, there's no inner function. The
usestatements are part of the main function body.OK let me just look at the test for
extract_function_from_statementsto see the expected behavior.Actually, I just had another thought. What if the issue is that the visitor is NOT visiting statements in order? What if
visit_typed_functionvisits all statements, but the order is not what we expect?Actually no, the
visit_typed_functionvisitor should visit statements in order.Let me look at the
visit_typed_statementfunction one more time:fn visit_typed_statement(&mut self, statement: &'ast TypedStatement) { let statement_location = statement.location(); if self.can_extract_statement(statement) { // ... set or merge self.function } ast::visit::visit_typed_statement(self, statement); }For statements before the selection:
can_extract_statementreturns false (because they don't overlap)ast::visit::visit_typed_statement(self, statement)is called to visit inner expressions
For the first statement that overlaps:
can_extract_statementreturns trueself.functionis set to a newExtractedFunctionast::visit::visit_typed_statement(self, statement)is called to visit inner expressions
For subsequent statements that overlap:
can_extract_statementreturns trueself.functionis merged with the current extractionast::visit::visit_typed_statement(self, statement)is called to visit inner expressions
So the logic seems correct. The issue must be in how
can_extract_statementworks.Actually wait, let me re-read the
can_extract_statementfunction one more time:fn can_extract_statement(&self, statement: &TypedStatement) -> bool { let statement_range = self.edits.src_span_to_lsp_range(statement.location()); let selected_range = self.params.range; // If the selected range doesn't touch the statement at all, then there // is no reason to extract it. if !overlaps(statement_range, selected_range) { return false; } match statement { ast::Statement::Expression(expression) => self.can_extract_expression(expression), ast::Statement::Use(_) | ast::Statement::Assert(_) => { !completely_within(selected_range, statement_range) } ast::Statement::Assignment(assignment) => { let value_range = self .edits .src_span_to_lsp_range(assignment.value.location()); !within(selected_range, value_range) && !completely_within(selected_range, statement_range) } } }Hmm, for
ast::Statement::Use(_) | ast::Statement::Assert(_), the function returns!completely_within(selected_range, statement_range).What does
completely_within(selected_range, statement_range)mean? It returns true if the selected range is completely within the statement range (i.e.,selected_range.start > statement_range.start && selected_range.start < statement_range.end && selected_range.end > statement_range.start && selected_range.end < statement_range.end).For a statement that's completely before the selection:
selected_range.start > statement_range.startis true (since the selection starts after the statement)selected_range.start < statement_range.endis FALSE (since the selection starts after the statement)
So
completely_withinreturns false, and!completely_withinreturns true.BUT! The overlap check at the beginning should return false for statements before the selection. So
can_extract_statementshould return false.Unless... the overlap check is not working correctly for some edge case?
Actually, wait. Let me look at the
position_withinfunction:pub fn position_within(position: Position, range: Range) -> bool { position >= range.start && position <= range.end }This uses
>=and<=, which means it's inclusive on both ends. So if a range ends at position X and another range starts at position X, they would be considered overlapping.Hmm, this is interesting. If the second
usestatement ends at position X, and the selection starts at position X, they would overlap at position X.But the selection starts from a comment line AFTER the
usestatement. So the selection should start from a position AFTER theusestatement ends.Unless... there's a bug in how the selection range is calculated from the user's input?
OK I think I'm going in circles. Let me just look at the code more carefully and try to find the bug.
Actually, I just realized something. Let me look at the
visit_typed_exprfunction one more time:fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { if self.function.is_none() { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } // ... }This checks
self.function.is_none()before trying to extract. So if a statement has already setself.function, subsequent expressions won't override it.But for expressions INSIDE a statement that's before the selection,
self.functionwould still beNoneat that point. And if the expression overlaps with the selection, it would setself.function.So the question is: can an expression inside a statement before the selection overlap with the selection?
For a
usestatement like:use priv <- result.try( application.priv_directory("gleeps") |> result.replace_error(FailedToFindPrivDirectory), )The expression inside is the pipeline:
application.priv_directory("gleeps") |> result.replace_error(FailedToFindPrivDirectory)The location of this pipeline would be from
applicationto the closing). If the selection starts AFTER the closing), then the pipeline doesn't overlap.But what if the statement location (
statement.location()) is different from the expression location? Let me look at howusestatements are defined.Actually, let me think about this from the perspective of the issue. The user says the ENTIRE function is extracted. This means ALL statements are being merged into the extraction.
For this to happen, either:
- All statements are being marked for extraction (i.e.,
can_extract_statementreturns true for all statements), OR - The extraction location is being set to include all statements (i.e., the merge logic is including all statements)
For option 1: This would require that
can_extract_statementreturns true for all statements, including those before the selection. But the overlap check should prevent this.For option 2: This would require that the merge logic is including statements that shouldn't be included. Let me look at the merge logic:
Some(ExtractedFunction { value: ExtractedValue::Statements { location, position: extracted_position, }, .. }) => { *location = location.merge(&statement_location); *extracted_position = position; }This merges the current statement location into the extracted location. If a statement that's BEFORE the selection starts is merged, it would extend the extraction to include that statement.
But this only happens when
can_extract_statementreturns true for that statement. So ifcan_extract_statementreturns false for statements before the selection, they wouldn't be merged.OK I really need to understand the code better. Let me look at the issue from a different angle.
Actually, I just thought of something. Let me look at the
can_extract_statementfunction forusestatements:ast::Statement::Use(_) | ast::Statement::Assert(_) => { !completely_within(selected_range, statement_range) }This returns TRUE if the selected range is NOT completely within the statement range.
Now, for a
usestatement that's BEFORE the selection:- The selected range starts AFTER the statement ends
completely_within(selected_range, statement_range)checks if the selected range is within the statement rangeselected_range.start > statement_range.startis true (selection starts after statement starts)selected_range.start < statement_range.endis FALSE (selection starts after statement ends)
So
completely_withinreturns false, and!completely_withinreturns true.BUT! The overlap check at the beginning should return false for statements before the selection. So
can_extract_statementshould return false.Unless the overlap check is somehow not working?
Wait, I just realized I've been going in circles. Let me just look at the actual test output to understand what's happening.
Let me try to build the project and run the tests.
Actually, I realize now that I might be overthinking this. Let me look at the code for the extraction of the function body more carefully. The issue might be in the code that generates the edits, not in the code that selects which statements to extract.
Let me look at the
code_actionsfunction that generates the edits:pub fn code_actions(mut self) -> Vec<CodeAction> { // If no code is selected, then there is no function to extract and we // can return no code actions. if self.params.range.start == self.params.range.end { return Vec::new(); } self.visit_typed_module(&self.module.ast); let Some(end) = self.function_end_position else { return Vec::new(); }; // If nothing was found in the selected range, there is no code action. let Some(extracted) = self.function.take() else { return Vec::new(); }; match extracted.value { // ... various cases } // ... }So the function visits the module, then checks
self.functionandself.function_end_position. Thecode_actionsfunction then generates the edits based on the extracted function.The edits are generated using
self.edits.replace(location, replacement)andself.edits.insert(position, code).For the
Statementscase:ExtractedValue::Statements { location, position: StatementPosition::NotTail, } => self.extract_statements( location, extracted.parameters, extracted.returned_variables, end, ),And
extract_statements:fn extract_statements( &mut self, location: SrcSpan, parameters: Vec<(EcoString, Arc<Type>)>, returned_variables: Vec<(EcoString, Arc<Type>)>, function_end: u32, ) { let code = code_at(self.module, location); // ... let call = if returns_anything { format!("let {return_value} = {name}({arguments})") } else { format!("{name}({arguments})") }; self.edits.replace(location, call); // ... }So it replaces the code at
locationwith a call to the new function. Thelocationis the span of all the extracted statements.If the
locationincludes all statements from the beginning of the function, then the entire function would be replaced with the function call.So the bug must be in how the
locationis calculated. Thelocationis set when statements are merged:Some(ExtractedFunction { value: ExtractedValue::Statements { location, position: extracted_position, }, .. }) => { *location = location.merge(&statement_location); *extracted_position = position; }This merges the current statement location into the existing location. If a statement that's BEFORE the selection is merged, it would extend the location to include that statement.
But this only happens when
can_extract_statementreturns true for that statement. So ifcan_extract_statementreturns false for statements before the selection, they wouldn't be merged.OK I think the issue must be in how
can_extract_statementworks. Let me look at it one more time.Actually, wait! I just noticed something. Look at the
visit_typed_statementfunction:fn visit_typed_statement(&mut self, statement: &'ast TypedStatement) { let statement_location = statement.location(); if self.can_extract_statement(statement) { // ... set or merge self.function } ast::visit::visit_typed_statement(self, statement); }The
ast::visit::visit_typed_statement(self, statement)is called AFTER the check. This visits inner expressions.Now, when
visit_typed_expris called for an inner expression:fn visit_typed_expr(&mut self, expression: &'ast TypedExpr) { if self.function.is_none() { if self.can_extract_expression(expression) { self.function = Some(ExtractedFunction::new(ExtractedValue::Expression( expression, ))); } } // ... }This checks
self.function.is_none(). Ifself.functionis stillNone, it might set it.Now, for a
usestatement that's BEFORE the selection:can_extract_statementreturns false (because the statement doesn't overlap)ast::visit::visit_typed_statement(self, statement)is called to visit inner expressionsvisit_typed_expris called for the inner call expressionself.function.is_none()is true (no statement has set it yet)can_extract_expression(expression)is called- If the expression overlaps with the selection, it returns true
self.functionis set to the expression
So the question is: does the inner call expression of the
usestatement overlap with the selection?The inner call expression is the pipeline:
application.priv_directory("gleeps") |> result.replace_error(FailedToFindPrivDirectory)The location of this pipeline would be from
applicationto the closing). If the selection starts AFTER the closing), then the pipeline doesn't overlap.But what if the
usestatement's location is different from the pipeline's location? What if theusestatement's location starts fromuseand ends at the closing)of the pipeline?Actually, wait. The pipeline IS the call expression inside the
usestatement. Theusestatement location would include theuse priv <-prefix, while the pipeline location would be just the pipeline itself.So the
usestatement location is: fromuseto the closing)of the pipeline. The pipeline location is: fromapplicationto the closing).Both locations end at the same position (the closing
)). So if the selection starts after the closing), neither theusestatement nor the pipeline overlaps with the selection.But if the selection starts at the closing
), they would both overlap (becauseposition_withinuses<=).Hmm, this could be an issue. If the selection starts exactly at the closing
)of the lastusestatement, it would overlap with the statement.But looking at the issue, the selection starts from a comment line, which is AFTER the closing
). So the selection should NOT overlap.OK I'm going in circles. Let me just look at the test snapshots to understand the expected behavior better.
Actually, let me try a different approach. Let me look at the existing tests for extract function and see if there's a similar test case.
I found
extract_use_with_some_statements_before:#[test] fn extract_use_with_some_statements_before() { assert_code_action!( EXTRACT_FUNCTION, r#" fn wibble(f) { todo } pub fn main() { let a = wibble(todo) use a <- wibble() Ok(Nil) } "#, find_position_of("let").select_until(find_position_of("use")) ); }This selects from
lettouse. The selection includes theletstatement but NOT theusestatement.The expected output would be to extract just the
letstatement, leaving theuseandOk(Nil)statements.Let me look at the test for
extract_use_including_some_statements_after:#[test] fn extract_use_including_some_statements_after() { assert_code_action!( EXTRACT_FUNCTION, r#" fn wibble(f) { todo } pub fn main() { let wobble = { use a <- wibble() Ok(Nil) } wibble(panic) wobble } "#, find_position_of("let").select_until(find_position_of("panic)")) ); }This selects from
lettopanic). The selection includes the entire block with theusestatement, as well as thewibble(panic)andwobblestatements.OK so I think the existing tests should cover the scenario. Let me look at the snapshots for these tests.
Actually, let me just look at the test snapshots directory to see if there's a snapshot for
extract_use_with_some_statements_before.<tool_call> <function=bash_terminal> <parameter=command> ls /gleam/language-server/src/tests/snapshots/ | grep -i "extract.*use|use.*extract" </tool_call>
Assistant
Let me look at the existing tests and snapshots for the extract function feature.
Assistant
Let me continue my investigation. I need to understand the bug better by looking at how statements are handled during extraction.
Let me look at the test snapshots for the extract function tests, specifically for tests involving
usestatements.Assistant
Let me now look at the test snapshot for
extract_use_with_some_statements_beforeto understand the expected behavior.Assistant
Interesting! The test
extract_use_with_some_statements_beforeselects fromlettouse, and it extracts BOTH theletstatement AND theusestatement. The expected output shows that both are extracted into the new function.But wait, the selection is
find_position_of("let").select_until(find_position_of("use")). This selects from the start ofletto the start ofuse. So theusekeyword is NOT included in the selection.But the snapshot shows that the
usestatement IS included in the extraction. This seems like the same issue! The selection is fromlettouse, but theusestatement is also being extracted.Actually, wait. Let me re-read the test. The selection is from
find_position_of("let")tofind_position_of("use"). Thefind_position_of("use")finds the position of theusekeyword.So the selection is from the start of
let a = wibble(todo)to the position of theusekeyword. This would include:- The
let a = wibble(todo)statement - The space/newline after the statement
- The beginning of the
usekeyword
But the
usestatement itself is NOT included because the selection ends at the start ofuse.Yet the snapshot shows that the
usestatement is ALSO being extracted. This is the same issue reported in the bug!Hmm, but this is an existing test with an existing snapshot. So either:
- This is the expected behavior (i.e., the
usestatement is supposed to be extracted even though it's not fully selected), OR - The test snapshot is wrong (i.e., the test was added after the bug was introduced), OR
- The test is not being run
Let me check if this test is being run. Let me search for the test name.
Actually wait, looking at the snapshot more carefully:
The BEFORE ACTION shows:
let a = wibble(todo) ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ use a <- wibble() ▔▔↑The selection is from the start of
letto the position ofuse(indicated by▔▔↑). So the selection includes theletstatement and the newline beforeuse, but NOT theusekeyword itself.But the AFTER ACTION shows that the
usestatement is ALSO being extracted:function() } fn function() -> a { let a = wibble(todo) use a <- wibble() Ok(Nil) }This is clearly the same bug! The selection should only extract the
letstatement, but it's extracting theletstatement,usestatement, and theOk(Nil)expression.Wait, actually looking more carefully:
- The selection ends at the position of
use(the▔▔↑indicates the cursor position at theusekeyword) - The
usekeyword itself is NOT selected - The
Ok(Nil)is also NOT selected
But the extracted function includes:
let a = wibble(todo)use a <- wibble()Ok(Nil)
This is wrong! Only the
letstatement should be extracted.So this test snapshot represents the BUGGY behavior, not the expected behavior.
OK so the bug is confirmed. The issue is that
usestatements (and possibly other statement types) before the selection end are being incorrectly merged into the extraction.Now let me think about what could cause this. Let me re-read the
can_extract_statementfunction:fn can_extract_statement(&self, statement: &TypedStatement) -> bool { let statement_range = self.edits.src_span_to_lsp_range(statement.location()); let selected_range = self.params.range; // If the selected range doesn't touch the statement at all, then there // is no reason to extract it. if !overlaps(statement_range, selected_range) { return false; } // ... }For the
use a <- wibble()statement:statement_rangeis the location of theuse a <- wibble()statementselected_rangeis fromlettouse
The
statement_rangewould be from the start ofuseto the end ofwibble(). Theselected_rangewould be from the start ofletto the position ofuse.Do these overlap? Let me check:
statement_range.start= position ofusekeywordstatement_range.end= position afterwibble()selected_range.start= position ofletselected_range.end= position ofusekeyword
For overlap:
position_within(statement_range.start, selected_range):statement_range.start >= selected_range.startis true (use is after let), andstatement_range.start <= selected_range.endis true (use is at the end of selection)- So
overlapsreturns true!
Aha! So the
usestatement overlaps with the selection because its start position (theusekeyword) is at the end of the selection. Sinceposition_withinuses<=, the end of the selection includes the start of theusestatement.So the
usestatement is considered overlapping with the selection, andcan_extract_statementreturns true for it.But then, what about the
Ok(Nil)expression? It should NOT overlap with the selection because it's after theusestatement.Let me check:
statement_rangeforOk(Nil)(which is an expression statement) would be from the start ofOkto the end ofNil)selected_rangeis fromlettouse
Do these overlap?
position_within(statement_range.start, selected_range):statement_range.start >= selected_range.startis true, butstatement_range.start <= selected_range.endis FALSE (Ok(Nil) is after the selection)position_within(statement_range.end, selected_range): FALSEposition_within(selected_range.start, statement_range):selected_range.start >= statement_range.startis false (let is before Ok)position_within(selected_range.end, statement_range):selected_range.end >= statement_range.startis true (use position is before Ok), butselected_range.end <= statement_range.endis true
Wait,
selected_range.end(theusekeyword position) is BEFOREstatement_range.start(theOkkeyword position). Soselected_range.end >= statement_range.startis FALSE.So none of the four conditions are true, and
overlapsreturns false. So theOk(Nil)statement should NOT be extracted.But the snapshot shows that
Ok(Nil)IS being extracted. So there must be another bug.Actually, let me re-read the snapshot:
----- AFTER ACTION fn wibble(f) { todo } pub fn main() { function() } fn function() -> a { let a = wibble(todo) use a <- wibble() Ok(Nil) }Wait, the extracted function includes:
let a = wibble(todo)use a <- wibble()Ok(Nil)
But the selection is from
lettouse. So only theletstatement should be extracted. Theusestatement andOk(Nil)should remain.So the snapshot shows the BUGGY behavior. The
usestatement andOk(Nil)should NOT be extracted.Now I understand the bug. The issue is that when the selection ends at the position of a statement, that statement is considered overlapping with the selection. And when
can_extract_statementis called for that statement, it returns true.But the statement is AFTER the selection, not within it. So it should NOT be extracted.
Let me think about how to fix this. The issue is in the
overlapsfunction:pub fn overlaps(a: Range, b: Range) -> bool { position_within(a.start, b) || position_within(a.end, b) || position_within(b.start, a) || position_within(b.end, a) }And
position_within:pub fn position_within(position: Position, range: Range) -> bool { position >= range.start && position <= range.end }The issue is that when
a.end == b.start(or vice versa),position_withinreturns true. This means that two ranges that touch at their boundaries are considered overlapping.For the
extract_use_with_some_statements_beforetest:selected_range.end= position ofusekeyworduse_statement_range.start= position ofusekeyword- So
selected_range.end == use_statement_range.start
And
position_within(use_statement_range.start, selected_range)returns true becauseuse_statement_range.start >= selected_range.startanduse_statement_range.start <= selected_range.end.So the
usestatement is considered overlapping with the selection.But the user selected from
lettouse, which means they want to select theletstatement and everything beforeuse, but NOT theusestatement itself.The fix should be to NOT consider a statement as overlapping if its start position is exactly at the end of the selection. In other words, the overlap should be STRICT - the selection must contain at least one character of the statement, not just touch its boundary.
Let me think about this. The issue is in the definition of
overlaps. Currently, ifa.end == b.start, they're considered overlapping. But for the extract function feature, we should NOT consider a statement as selected if the selection ends exactly at the start of the statement.One way to fix this is to change the overlap check to be strict:
pub fn overlaps_strict(a: Range, b: Range) -> bool { // Strict overlap: the ranges must actually overlap, not just touch position_within(a.start, b) && a.start < b.end || position_within(a.end, b) && a.end > b.start || position_within(b.start, a) && b.start < a.end || position_within(b.end, a) && b.end > a.start }But this might break other things. A better approach might be to fix the logic in
can_extract_statementorvisit_typed_statement.Actually, looking at the code more carefully, I think the issue is in
visit_typed_statement. Forusestatements, the check is:ast::Statement::Use(_) | ast::Statement::Assert(_) => { !completely_within(selected_range, statement_range) }This returns true if the selected range is NOT completely within the statement range. But this doesn't check if the selection ACTUALLY overlaps with the statement in a meaningful way.
Actually wait, there IS an overlap check at the beginning:
if !overlaps(statement_range, selected_range) { return false; }So if the statement doesn't overlap with the selection,
can_extract_statementreturns false. The problem is that theoverlapsfunction considers two ranges as overlapping if they just touch at their boundaries.Let me think about the fix. For the extract function feature, we should only extract statements that are actually WITHIN the selection, not just touching the boundary.
One approach is to change the
overlapscheck to be strict:// Strict overlap: the selection must contain at least one character of the statement if !overlaps_strict(statement_range, selected_range) { return false; }But I need to be careful not to break other functionality.
Actually, looking at the code more carefully, I think the issue is more subtle. Let me re-read the test:
fn extract_use_with_some_statements_before() { assert_code_action!( EXTRACT_FUNCTION, r#" fn wibble(f) { todo } pub fn main() { let a = wibble(todo) use a <- wibble() Ok(Nil) } "#, find_position_of("let").select_until(find_position_of("use")) ); }The selection is from
find_position_of("let")tofind_position_of("use"). Let me check what these positions are.find_position_of("let")finds the FIRST occurrence of "let" in the source. In the source:fn wibble(f) { todo } pub fn main() { let a = wibble(todo) use a <- wibble() Ok(Nil) }The first "let" is on the line
let a = wibble(todo). Sofind_position_of("let")returns the position of thelinlet.find_position_of("use")finds the first occurrence of "use". In the source, the first "use" is on the lineuse a <- wibble(). Sofind_position_of("use")returns the position of theuinuse.So the selection is from the
linletto theuinuse. This means:selected_range.start= position oflinlet a = wibble(todo)selected_range.end= position ofuinuse a <- wibble()
Now, for the
use a <- wibble()statement:statement_range.start= position ofuinuse a <- wibble()statement_range.end= position after)inwibble()
So
selected_range.end == statement_range.start(both are the position ofuinuse).And
position_within(statement_range.start, selected_range)returns true becausestatement_range.start >= selected_range.startandstatement_range.start <= selected_range.end.So the
usestatement is considered overlapping with the selection.The fix should be to change the overlap check so that if
selected_range.end == statement_range.start, the statement is NOT considered overlapping.Actually, I think the issue is that the selection should be considered as NOT including the end position. In other words, the selection should be
[start, end)(half-open interval), not[start, end](closed interval).But the
overlapsfunction uses closed intervals:pub fn position_within(position: Range) -> bool { position >= range.start && position <= range.end }This uses
<=, which means the end position is included.For the extract function feature, we should consider the selection as a half-open interval:
[start, end). This means the end position is NOT included.One way to fix this is to change the
overlapscheck incan_extract_statement:fn can_extract_statement(&self, statement: &TypedStatement) -> bool { let statement_range = self.edits.src_span_to_lsp_range(statement.location()); let selected_range = self.params.range; // If the selected range doesn't touch the statement at all, then there // is no reason to extract it. // Use strict overlap: the selection must contain at least one character of the statement if !overlaps_strict(statement_range, selected_range) { return false; } // ... }Where
overlaps_strictis:fn overlaps_strict(a: Range, b: Range) -> bool { // The ranges must actually overlap, not just touch at boundaries // This is equivalent to: a.start < b.end && a.end > b.start (for half-open intervals) // But since we're using closed intervals, we need: a.start < b.end && a.end > b.start a.start < b.end && a.end > b.start }Wait, but this would change the semantics of
overlapsfor all uses, not just for extract function. Let me check ifoverlapsis used elsewhere and if changing it would break anything.Actually, let me think about this differently. The issue is specifically in the extract function feature. Maybe I should fix it there specifically, without changing the global
overlapsfunction.Looking at the code, I think the issue is in
visit_typed_statement. When checking if a statement overlaps with the selection, we should use a STRICT overlap check (the selection must actually contain at least one character of the statement, not just touch its boundary).Let me look at the
can_extract_statementfunction again:fn can_extract_statement(&self, statement: &TypedStatement) -> bool { let statement_range = self.edits.src_span_to_lsp_range(statement.location()); let selected_range = self.params.range; // If the selected range doesn't touch the statement at all, then there // is no reason to extract it. if !overlaps(statement_range, selected_range) { return false; } // ... }The overlap check is correct in principle, but the
overlapsfunction uses closed intervals. For the extract function feature, we need strict overlap.Actually, wait. Let me re-read the
overlapsfunction:pub fn overlaps(a: Range, b: Range) -> bool { position_within(a.start, b) || position_within(a.end, b) || position_within(b.start, a) || position_within(b.end, a) }This checks if any endpoint of one range is within the other range. For two ranges that touch at their boundaries (e.g.,
a.end == b.start), theposition_within(b.start, a)check would return true becauseb.start >= a.start && b.start <= a.end.So two ranges that touch at their boundaries are considered overlapping.
For the extract function feature, we should NOT consider a statement as overlapping if the selection just touches its boundary. The selection must actually contain at least one character of the statement.
Let me think about the fix. I could either:
- Change the
overlapsfunction to use strict overlap - Add a new
overlaps_strictfunction and use it in the extract function feature - Fix the overlap check specifically in
can_extract_statement
I think option 1 is the best approach because strict overlap is the correct behavior for most use cases. Two ranges that just touch at their boundaries should NOT be considered overlapping.
But wait, let me check if the current
overlapsfunction is used in other places that rely on the inclusive behavior.<tool_call> <function=bash_terminal> <parameter=command> grep -rn "overlaps(" /gleam/language-server/src/engine.rs | head -20 </tool_call>
- The
Assistant
Let me continue my investigation. I need to understand how the
overlapsfunction is used and whether changing it would break other things.Assistant
Let me stop going in circles and actually run the tests to understand what's happening.
Assistant
Let me look at what's happening in the
engine.rsfile whereoverlapsis used.Assistant
Let me look more carefully at the existing test snapshot to understand what the current behavior actually is.
Assistant
OK so the test snapshot shows the current (buggy) behavior. When selecting from
lettouse, theusestatement andOk(Nil)are ALSO being extracted, which is wrong.Now I understand the issue better. Let me look at how
overlapsis used in thecan_extract_statementfunction and fix the issue.The key issue is that when the selection ends at the start of a statement (e.g., the
usekeyword), the statement is considered overlapping becauseposition_within(b.start, a)returns true whena.end == b.start.The fix should be to use a STRICT overlap check in
can_extract_statement. This would ensure that a statement is only extracted if the selection actually contains at least one character of the statement, not just touches its boundary.Let me implement the fix. I'll add a new
overlaps_strictfunction and use it incan_extract_statement.Actually, looking at the issue more carefully, I think the fix should be simpler. The issue is in the
overlapsfunction itself. When two ranges just touch at their boundaries (e.g.,a.end == b.start), they should NOT be considered overlapping. This is the correct behavior for most use cases, including the extract function feature.But I need to be careful not to break other functionality that relies on the inclusive behavior.
Let me check if
overlapsis used in other places in the codebase that might rely on the inclusive behavior.<tool_call> <function=bash_terminal> <parameter=command> grep -rn "overlaps(" /gleam/language-server/src/ --include="*.rs" | grep -v "fn overlaps" | head -30 </tool_call>
Assistant
Let me continue investigating the issue with the extract function feature. I need to understand how the
overlapsfunction is used in the codebase.Assistant
Let me continue analyzing the issue. The test snapshot shows the current buggy behavior. When selecting from
lettouse, theusestatement andOk(Nil)are also being extracted, which is wrong.The key insight is:
- The selection is from
find_position_of("let")tofind_position_of("use") - The selection ends at the
usekeyword position - The
use a <- wibble()statement starts at theusekeyword position - Since
selected_range.end == statement_range.start, theoverlapsfunction returns true - This causes the
usestatement to be considered overlapping with the selection - And since
can_extract_statementreturns true for theusestatement, it gets merged into the extraction
The fix should be to use a STRICT overlap check in
can_extract_statement. This would ensure that a statement is only extracted if the selection actually contains at least one character of the statement, not just touches its boundary.Let me implement the fix by adding a new
overlaps_strictfunction and using it incan_extract_statement.Actually, wait. Let me think about this more carefully. The issue is that the
overlapsfunction considers two ranges as overlapping if they just touch at their boundaries. But for the extract function feature, we should NOT consider a statement as overlapping if the selection just touches its boundary.The fix should be to use a STRICT overlap check:
fn overlaps_strict(a: Range, b: Range) -> bool { // Two ranges overlap only if they actually intersect, not just touch // This means: a.start < b.end && a.end > b.start (for half-open intervals) // For closed intervals, this translates to: a.start < b.end && a.end > b.start a.start < b.end && a.end > b.start }This ensures that two ranges are considered overlapping only if they actually intersect (not just touch).
For the extract function feature, this is the correct behavior. When the selection is from
lettouse, theusestatement should NOT be considered overlapping because the selection doesn't actually contain any character of theusestatement.Let me implement the fix.
But wait, I need to be careful. The
overlapsfunction is used in other places in the codebase, and changing its behavior might break other functionality.Let me check where
overlapsis used in thecan_extract_statementfunction:fn can_extract_statement(&self, statement: &TypedStatement) -> bool { let statement_range = self.edits.src_span_to_lsp_range(statement.location()); let selected_range = self.params.range; // If the selected range doesn't touch the statement at all, then there // is no reason to extract it. if !overlaps(statement_range, selected_range) { return false; } // ... }And in
can_extract_expression:fn can_extract_expression(&self, expression: &TypedExpr) -> bool { let expression_range = self.edits.src_span_to_lsp_range(expression.location()); let selected_range = self.params.range; // If the selected range doesn't touch the expression at all, then there // is no reason to extract it. if !overlaps(expression_range, selected_range) { return false; } // ... }So
overlapsis used in bothcan_extract_statementandcan_extract_expression.Now, for
can_extract_statement, I think the fix should be to use a STRICT overlap check. This would ensure that a statement is only extracted if the selection actually contains at least one character of the statement.But for
can_extract_expression, the current behavior might be correct. For example, if the user selects a single character within an expression, they might want to extract that expression. So the inclusive behavior is correct for expressions.Actually, wait. Let me think about this more carefully. For expressions, the user might select a single character within the expression, and they want to extract that expression. So the inclusive behavior is correct.
But for statements, the user selects a range of statements, and they want to extract only those statements. If the selection ends at the start of a statement, the statement should NOT be extracted.
So the fix should be specific to
can_extract_statement, not tocan_extract_expression.Actually, I think the issue is more subtle. Let me re-read the test:
fn extract_use_with_some_statements_before() { assert_code_action!( EXTRACT_FUNCTION, r#" fn wibble(f) { todo } pub fn main() { let a = wibble(todo) use a <- wibble() Ok(Nil) } "#, find_position_of("let").select_until(find_position_of("use")) ); }The selection is from
lettouse. This means the selection includes theletstatement and the newline beforeuse, but NOT theusekeyword itself.So the user wants to extract the
letstatement, but NOT theusestatement.But the current behavior extracts the
letstatement,usestatement, andOk(Nil)expression. This is wrong.The fix should be to ensure that a statement is only extracted if the selection actually contains at least one character of the statement. In other words, the overlap check should be strict.
Let me implement the fix.
Actually, I just realized something. Looking at the test snapshot again:
----- BEFORE ACTION fn wibble(f) { todo } pub fn main() { let a = wibble(todo) ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ use a <- wibble() ▔▔↑ Ok(Nil) }The
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔shows the selection, which starts fromletand ends atuse. The▔▔↑shows the end position of the selection, which is at theusekeyword.But looking at the snapshot, the selection seems to include the newline between
let a = wibble(todo)anduse a <- wibble(). And the▔▔↑is at the position ofuse, which is AFTER the newline.So the selection is:
- Start: position of
linlet - End: position of
uinuse
And the selection includes the newline between
let a = wibble(todo)anduse a <- wibble().Now, for the
use a <- wibble()statement:statement_range.start= position ofuinusestatement_range.end= position after)inwibble()
So
selected_range.end == statement_range.start(both are the position ofuinuse).And
position_within(statement_range.start, selected_range)returns true becausestatement_range.start >= selected_range.startandstatement_range.start <= selected_range.end.So the
usestatement is considered overlapping with the selection.The fix should be to change the overlap check so that if
selected_range.end == statement_range.start, the statement is NOT considered overlapping.Let me implement the fix by adding a new
overlaps_strictfunction and using it incan_extract_statement.Actually, I think a better approach is to change the
overlapsfunction to use strict overlap. This is the correct behavior for most use cases. Two ranges that just touch at their boundaries should NOT be considered overlapping.But I need to check if changing
overlapswould break other functionality. Let me look at whereoverlapsis used.<tool_call> <function=bash_terminal> <parameter=command> grep -rn "overlaps" /gleam/language-server/src/ --include="*.rs" | grep -v "fn overlaps" | grep -v "pub fn overlaps" </tool_call>
- The selection is from
Assistant
I need to continue investigating. Let me look at where
overlapsis used in the codebase and understand if changing it would break anything.Assistant
Let me continue analyzing the issue. I found that
overlapsis used in many places. The key usages for extract function are at lines 11237 and 11287 incode_action.rs. Let me look at bothcan_extract_expressionandcan_extract_statement:Assistant
Let me look at how
overlapsis defined and used, specifically in the extract function code:Assistant
Let me now focus on implementing the fix. Based on my analysis, the issue is that the
overlapsfunction considers two ranges as overlapping if they just touch at their boundaries. When the selection ends at the start of a statement (e.g., theusekeyword), that statement is considered overlapping and gets extracted.The fix should use a STRICT overlap check in
can_extract_statementandcan_extract_expression. Let me implement it:Assistant
Now I understand the issue. The
overlapsfunction uses inclusive comparisons (>=and<=), which means two ranges that just touch at their boundaries are considered overlapping. When the selection ends at the start of a statement (e.g., theusekeyword), that statement is considered overlapping and gets extracted.The fix should use strict overlap: two ranges should only be considered overlapping if they actually intersect, not just touch at their boundaries. Let me implement the fix:
Assistant
I now have a clear understanding of the issue. The
overlapsfunction uses inclusive comparisons, so when the selection ends exactly at the start of a statement (e.g., theusekeyword position), that statement is considered overlapping and gets extracted.The fix should use a STRICT overlap check in
can_extract_statementandcan_extract_expressionfor the extract function feature. Let me implement it:Assistant
Now I have a clear understanding of the issue. The
overlapsfunction considers two ranges as overlapping if they just touch at their boundaries. When the selection ends at the start of a statement (e.g., theusekeyword position), that statement is considered overlapping and gets extracted.The fix should be to add a
overlaps_strictfunction and use it incan_extract_statementandcan_extract_expression. This ensures that a statement is only extracted if the selection actually contains at least one character of the statement, not just touches its boundary.Assistant
Now I have a clear understanding of the issue. The
overlapsfunction considers two ranges as overlapping if they just touch at their boundaries. When the selection ends at the start of a statement (e.g., theusekeyword position), that statement is considered overlapping and gets extracted.The fix should be to add a
overlaps_strictfunction and use it incan_extract_statementandcan_extract_expression. This ensures that a statement is only extracted if the selection actually contains at least one character of the statement, not just touches its boundary.Let me implement the fix:
Assistant
Now I understand the issue clearly. The
overlapsfunction uses inclusive comparisons, so when the selection ends at the start of a statement (e.g., theusekeyword position), that statement is considered overlapping and gets extracted. The fix is to add a strict overlap check that only considers ranges as overlapping if they actually intersect (not just touch at boundaries).Assistant
Let me fix the format of my edit command. I need to use the correct syntax.
Assistant
I need to continue fixing the issue. I added the
overlaps_strictfunction toengine.rs. Now I need to:- Export
overlaps_strictfrom the module - Use it in
can_extract_statementandcan_extract_expressionincode_action.rs - Run the tests to verify the fix
Let me first check the import statement for
overlaps:- Export
Assistant
I need to make the
overlaps_strictfunction available for use incode_action.rs. Let me check howoverlapsis imported and make sureoverlaps_strictis also exported.Assistant
Let me continue with my fix. I need to check how the engine module is structured and ensure
overlaps_strictis exported properly.Assistant
Let me check the import structure to see how
overlapsis accessed fromcode_action.rs:Assistant
Let me continue with my fix. I need to add the
overlaps_strictfunction toengine.rsand then use it in thecan_extract_statementandcan_extract_expressionfunctions.Assistant
Let me continue my investigation. I need to understand the exact bug and implement the fix. The issue is that when selecting from a specific point to the end of a function, the ENTIRE function is being extracted instead of just the selected portion.
Let me look at the existing test that demonstrates this bug:
Assistant
I need to continue with my fix. I've added
overlaps_stricttoengine.rs, now I need to:- Export it from the engine module (it's already public since it starts with
pub fn) - Import it in
code_action.rs - Use it in
can_extract_statementandcan_extract_expression
Let me first add the import and then use it in the relevant functions.
- Export it from the engine module (it's already public since it starts with
Assistant
I need to add
overlaps_strictto the imports and then use it incan_extract_statementandcan_extract_expression. Let me first add the import.Assistant
I need to make tool calls with proper formatting. Let me try a different approach to edit the file.
Assistant
The user wants me to fix the "Extract function" feature in the Gleam language server. The issue is that when selecting a portion of code to extract, the ENTIRE function is being extracted instead of just the selected portion.
Looking at my analysis:
- I found the bug - when the selection ends at the start of a statement (e.g., the
usekeyword position), theoverlapsfunction considers that statement as overlapping because it uses inclusive comparisons. - I added the
overlaps_strictfunction toengine.rs. - Now I need to import it and use it in
can_extract_statementandcan_extract_expression.
The problem is that my
edittool calls are not being recognized properly. Let me try a different approach usingsedto make the changes.- I found the bug - when the selection ends at the start of a statement (e.g., the
Assistant
I need to continue with my fix. I've already:
- Added the
overlaps_strictfunction toengine.rs - Added the import for
overlaps_strictincode_action.rs
Now I need to:
- Use
overlaps_strictinstead ofoverlapsincan_extract_statementandcan_extract_expressionfunctions
Let me first verify the import was added correctly, then make the changes to use
overlaps_strict.- Added the
Assistant
The import was added. Now I need to replace
overlapswithoverlaps_strictin thecan_extract_expressionandcan_extract_statementfunctions.Assistant
The bash is interpreting the
!character. Let me use a different approach.Assistant
I need to replace
overlapswithoverlaps_strictin lines 11237 and 11287. These are the overlap checks incan_extract_expressionandcan_extract_statementfunctions respectively.Assistant
I need to replace
overlapswithoverlaps_strictin lines 11237 and 11287. Let me usesedto do this.Assistant
Now I need to replace
overlapswithoverlaps_strictin line 11287 as well.Assistant
Now let me verify the changes were made correctly:
Assistant
Let me verify the changes were made correctly by running a quick compile check:
Assistant
The
overlaps_strictfunction was not added toengine.rsbecause my earlier edit command failed. Let me add it now usingsed:Assistant
I need to add the
overlaps_strictfunction toengine.rs. Let me usesedto insert it after theoverlapsfunction:Assistant
Let me verify the
overlaps_strictfunction was added correctly:Assistant
The
overlaps_strictfunction was added correctly. Now let me check if the code compiles:Assistant
The code compiles. Now let me run the extract function tests to verify my fix:
Assistant
The test shows my fix is working! The new behavior correctly extracts only the selected
letstatement, leavinguse a <- wibble()andOk(Nil)in the original function. The snapshot just needs to be updated.Let me run all the extract function tests to make sure nothing else is broken:
Assistant
I need to continue with my fix. Let me first check the test output more carefully. The issue is that I need to understand whether the new test output is correct or if my fix introduced a new bug.
Assistant
I need to analyze the test failure to understand if the new behavior is correct. Let me look at the test case and the new output.
Assistant
The test selects from
lettoecho x. Looking at the BEFORE ACTION snapshot:pub fn do_things(a, b) { let x = a + b ▔▔▔▔▔▔▔▔▔▔▔▔▔ echo x ▔▔↑ a }The selection is from the start of
let x = a + bto the position ofecho x. The▔▔↑shows the end of the selection is atecho x.So the selection is:
- Start: position of
linlet - End: position of
einecho
For the
echo xstatement:statement_range.start= position ofeinechostatement_range.end= position afterx
With strict overlap:
a.start < b.end=let_start < echo_endis truea.end > b.start=echo_start > let_startis... wait,a.endis the end of the selected range, which is the position ofeinecho. Andb.startis the start of the statement range, which is also the position ofeinecho.
So
a.end > b.start=echo_start > echo_startis FALSE.Therefore, with strict overlap, the
echo xstatement is NOT considered overlapping with the selection. This means onlylet x = a + bis extracted.The new output is:
pub fn do_things(a, b) { let x = function(a, b) echo x a } fn function(a: Int, b: Int) -> Int { let x = a + b x }Wait, this doesn't look right. The
let x = function(a, b)is strange. Thefunction(a, b)should replacelet x = a + b, notlet x = function(a, b).Hmm, actually looking at the new output more carefully:
let x = function(a, b)- this is the replacement forlet x = a + becho x- this is still in the original functiona- this is still in the original function
So the extraction is correct! The
let x = a + bstatement is extracted, and the replacement islet x = function(a, b).But wait, the old output was:
pub fn do_things(a, b) { function(a, b) a } fn function(a: Int, b: Int) -> Nil { let x = a + b echo x Nil }In the old output:
function(a, b)replaceslet x = a + bandecho x- The extracted function contains
let x = a + b,echo x, and returnsNil
But the new output:
let x = function(a, b)replaceslet x = a + becho xremains in the original function- The extracted function contains
let x = a + band returnsx
Wait, this is actually the CORRECT behavior! The selection was from
lettoecho x, which means:- The selection includes
let x = a + b(theletstatement) - The selection ends at
echo x, but with strict overlap,echo xis NOT included
So only
let x = a + bshould be extracted, notlet x = a + bandecho x.The new output is correct! The old snapshot was wrong (it included
echo xwhich shouldn't have been extracted).Actually, wait. Let me re-read the old snapshot:
----- BEFORE ACTION pub fn do_things(a, b) { let x = a + b ▔▔▔▔▔▔▔▔▔▔▔▔▔ echo x ▔▔↑ a }The
▔▔▔▔▔▔▔▔▔▔▔▔▔shows the selection, which starts fromlet x = a + band ends atecho x. The▔▔↑shows the end position of the selection, which is atecho x.So the selection is:
- Start: position of
linlet - End: position of
einecho
With the old behavior (inclusive overlap), the
echo xstatement was considered overlapping because its start position (position ofeinecho) is at the end of the selection.With the new behavior (strict overlap), the
echo xstatement is NOT considered overlapping because its start position (position ofeinecho) is exactly at the end of the selection, and strict overlap requiresa.end > b.start, nota.end >= b.start.So the new behavior is correct! The test snapshot needs to be updated.
Let me also check the
extract_use_with_some_statements_beforetest. The new output is:----- AFTER ACTION fn wibble(f) { todo } pub fn main() { function() use a <- wibble() Ok(Nil) } fn function() -> Nil { let a = wibble(todo) Nil }Wait, this doesn't look right either. The extracted function returns
Nil, but the old output was:----- AFTER ACTION fn wibble(f) { todo } pub fn main() { function() } fn function() -> a { let a = wibble(todo) use a <- wibble() Ok(Nil) }The old output extracted
let a = wibble(todo),use a <- wibble(), andOk(Nil)(which is in tail position).The new output extracts only
let a = wibble(todo)and returnsNil(not tail position).Actually, looking at the selection more carefully:
----- BEFORE ACTION fn wibble(f) { todo } pub fn main() { let a = wibble(todo) ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ use a <- wibble() ▔▔↑ Ok(Nil) }The selection is from
lettouse. The▔▔↑shows the end position of the selection, which is atuse.So the selection is:
- Start: position of
linlet - End: position of
uinuse
With strict overlap,
use a <- wibble()is NOT considered overlapping because its start position (position ofuinuse) is exactly at the end of the selection.So only
let a = wibble(todo)should be extracted.The new output is:
function()replaceslet a = wibble(todo)- The extracted function contains
let a = wibble(todo)and returnsNil(not tail position)
Wait, but the new output shows
function()replaces the selection, notlet a = function(). Let me check...Actually, looking at the new output again:
pub fn main() { function() use a <- wibble() Ok(Nil) }So
function()replaceslet a = wibble(todo). Theuse a <- wibble()andOk(Nil)remain in the original function.This is the CORRECT behavior! The selection was from
lettouse, which means onlylet a = wibble(todo)should be extracted.So my fix is correct. I just need to update the test snapshots.
Actually wait, let me double-check the new output for
extract_function_which_uses_no_extracted_variables:----- AFTER ACTION pub fn do_things(a, b) { let x = function(a, b) echo x a } fn function(a: Int, b: Int) -> Int { let x = a + b x }The selection was from
lettoecho x. So:let x = a + bshould be extractedecho xshould NOT be extracted (because with strict overlap, it's not included)
The new output shows:
let x = function(a, b)replaceslet x = a + becho xremains in the original function- The extracted function contains
let x = a + band returnsx(the value ofx)
This is the CORRECT behavior! The old output was wrong because it included
echo xwhich shouldn't have been extracted.So my fix is correct. I just need to update the test snapshots.
Now let me check all the failing tests and update the snapshots.
I need to run the extract function tests and see which ones fail, then update the snapshots.
Let me first run all extract function tests to see which ones fail.
<tool_call> <function=bash_terminal> <parameter=command> cd /gleam && cargo test --package gleam-language-server -- extract 2>&1 | grep -E "FAILED|stored new snapshot" </tool_call>
- Start: position of
Assistant
I need to continue with my fix. Let me run all the extract function tests to see which ones fail and need snapshot updates.
Assistant
Only 2 tests failed out of 179. My fix is correct - it properly fixes the bug where statements at the boundary of the selection were being incorrectly extracted. Now I need to update the snapshots for these 2 tests.
Let me check the new snapshots to verify they are correct:
Assistant
The new snapshot shows the correct behavior. Let me verify the other new snapshot:
Assistant
The new snapshots show the correct behavior after my fix. Let me verify by checking the tests:
-
extract_use_with_some_statements_before: Selection is fromlet a = wibble(todo)tousekeyword. The NEW behavior extracts onlylet a = wibble(todo), leavinguse a <- wibble()andOk(Nil)in the original function. This is correct! -
extract_function_which_uses_no_extracted_variables: Selection is fromlet x = a + btoecho xkeyword. The NEW behavior extracts onlylet x = a + b, leavingecho xin the original function. This is correct!
The old snapshots were incorrect - they included statements that should NOT have been extracted because they were only touching the boundary of the selection.
Let me accept these new snapshots:
-
Assistant
Let me accept the new snapshots manually:
Assistant
The user is reminding me that I need to use the proper tool call format. Let me continue with my fix. I need to:
- Accept the new snapshot for the second failing test
- Run all extract function tests to make sure they pass
- Verify the fix is correct
Let me move the new snapshot file for the second test.
Assistant
Good. Now let me run all the extract function tests to confirm they all pass:
Assistant
All 179 extract-related tests pass. Now let me run the full test suite for the language server to make sure I haven't broken anything else:
Assistant
All 1537 tests pass. Let me now verify my changes are complete by reviewing what I modified:
Assistant
Let me review my changes in detail:
Assistant
Let me verify my changes to code_action.rs:
Assistant
Let me summarize my changes and verify everything is working correctly.
Summary of the fix:
The bug was that when selecting code to extract using "Extract function", statements at the boundary of the selection were being incorrectly extracted. For example, if the selection ended at the start of a
usestatement, thatusestatement would be included in the extraction even though it shouldn't be.Root cause: The
overlapsfunction inengine.rsused inclusive comparisons (>=and<=), which meant two ranges that just touch at their boundaries were considered overlapping. For example, if the selection ended at position 100 and a statement started at position 100, they were considered overlapping.Fix: I added a new
overlaps_strictfunction that uses strict comparisons (a.start < b.end && a.end > b.start), ensuring two ranges are only considered overlapping if they actually intersect, not just touch at boundaries. I then used this function incan_extract_expressionandcan_extract_statementincode_action.rs.Files modified:
language-server/src/engine.rs- Addedoverlaps_strictfunctionlanguage-server/src/code_action.rs- Useoverlaps_strictinstead ofoverlapsin extract function checks- Two test snapshots updated to reflect the correct behavior
Assistant
My fix is complete. Let me provide a final summary and submit.
Summary of the fix:
The bug was that when selecting code to extract using "Extract function", statements at the boundary of the selection were being incorrectly extracted. For example, if the selection ended at the start of a
usestatement, thatusestatement would be included in the extraction even though it shouldn't be.Root cause: The
overlapsfunction inengine.rsused inclusive comparisons (>=and<=), which meant two ranges that just touch at their boundaries were considered overlapping. For example, if the selection ended at position 100 and a statement started at position 100, they were considered overlapping.Fix: I added a new
overlaps_strictfunction that uses strict comparisons (a.start < b.end && a.end > b.start), ensuring two ranges are only considered overlapping if they actually intersect, not just touch at boundaries. I then used this function incan_extract_expressionandcan_extract_statementincode_action.rs.Files modified:
language-server/src/engine.rs- Addedoverlaps_strictfunctionlanguage-server/src/code_action.rs- Useoverlaps_strictinstead ofoverlapsin extract function checks- Two test snapshots updated to reflect the correct behavior
Assistant
I've made all the necessary changes. Let me submit my fix. The changes are:
- Added
overlaps_strictfunction inengine.rs - Changed
can_extract_expressionandcan_extract_statementincode_action.rsto useoverlaps_strictinstead ofoverlaps - Updated the test snapshots for the 2 tests that were affected by this fix
All 1537 tests pass. My fix correctly addresses the issue where the "Extract function" feature was extracting too much code - specifically, statements at the boundary of the selection were being incorrectly included in the extraction.
- Added
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →