Skip to content

Commit 190239b

Browse files
giacomocavalierilpil
authored andcommitted
Fix bug extracting module selects
1 parent b37a216 commit 190239b

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

compiler-core/src/language_server/code_action.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -2711,8 +2711,16 @@ impl<'ast> ast::visit::Visit<'ast> for ExtractVariable<'ast> {
27112711
if self.position != Some(ExtractVariablePosition::TopLevelStatement)
27122712
&& within(self.params.range, expr_range)
27132713
{
2714-
self.selected_expression = Some(expr_location);
2715-
self.statement_before_selected_expression = self.latest_statement;
2714+
match expr {
2715+
// We don't extract variables, they're already good.
2716+
// And we don't extract module selects by themselves but always
2717+
// want to consider those as part of a function call.
2718+
TypedExpr::Var { .. } | TypedExpr::ModuleSelect { .. } => (),
2719+
_ => {
2720+
self.selected_expression = Some(expr_location);
2721+
self.statement_before_selected_expression = self.latest_statement;
2722+
}
2723+
}
27162724
}
27172725

27182726
let previous_position = self.position;
@@ -2744,20 +2752,6 @@ impl<'ast> ast::visit::Visit<'ast> for ExtractVariable<'ast> {
27442752
self.position = previous_position;
27452753
}
27462754

2747-
// We don't want to offer the action if the cursor is over a variable
2748-
// already!
2749-
fn visit_typed_expr_var(
2750-
&mut self,
2751-
location: &'ast SrcSpan,
2752-
_constructor: &'ast type_::ValueConstructor,
2753-
_name: &'ast EcoString,
2754-
) {
2755-
let var_range = self.edits.src_span_to_lsp_range(*location);
2756-
if within(self.params.range, var_range) {
2757-
self.selected_expression = None;
2758-
}
2759-
}
2760-
27612755
// We don't want to offer the action if the cursor is over some invalid
27622756
// piece of code.
27632757
fn visit_typed_expr_invalid(&mut self, location: &'ast SrcSpan, _type_: &'ast Arc<Type>) {

compiler-core/src/language_server/tests/action.rs

+15
Original file line numberDiff line numberDiff line change
@@ -3985,6 +3985,21 @@ fn do_not_extract_top_level_expression_in_let_statement() {
39853985
);
39863986
}
39873987

3988+
#[test]
3989+
fn do_not_extract_top_level_module_call() {
3990+
let src = r#"
3991+
import list
3992+
pub fn main() {
3993+
list.map([1, 2, 3], todo)
3994+
}"#;
3995+
3996+
assert_no_code_actions!(
3997+
EXTRACT_VARIABLE,
3998+
TestProject::for_source(src).add_module("list", "pub fn map(l, f) { todo }"),
3999+
find_position_of("map").to_selection()
4000+
);
4001+
}
4002+
39884003
#[test]
39894004
fn expand_function_capture() {
39904005
assert_code_action!(

0 commit comments

Comments
 (0)