Skip to content

Commit

Permalink
fix small indentation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri committed Nov 22, 2024
1 parent f102da2 commit d49ba4a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 32 deletions.
30 changes: 13 additions & 17 deletions compiler-core/src/language_server/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2272,7 +2272,7 @@ impl<'a> TurnIntoUse<'a> {
// want to move the entire body of the anonymous function to this level.
let use_nesting_level = self
.line_numbers
.src_span_to_lsp_range(called_function_span)
.src_span_to_lsp_range(call_span)
.start
.character;
let indentation = " ".repeat(use_nesting_level as usize);
Expand Down Expand Up @@ -2340,7 +2340,7 @@ impl<'a> TurnIntoUse<'a> {
);

let mut action = Vec::with_capacity(1);
CodeActionBuilder::new("Turn into use expression")
CodeActionBuilder::new("Convert to `use`")
.kind(CodeActionKind::REFACTOR_REWRITE)
.changes(self.params.text_document.uri.clone(), edits)
.preferred(false)
Expand Down Expand Up @@ -2374,21 +2374,17 @@ impl<'ast> ast::visit::Visit<'ast> for TurnIntoUse<'ast> {
body: &'ast [ast::TypedStatement],
return_annotation: &'ast Option<ast::TypeAst>,
) {
// We can only offer this code action for anonymous functions and not
// the ones implicitly added by the compiler.
if let FunctionLiteralKind::Anonymous { .. } = kind {
// The cursor has to be inside the last statement of the body to
// offer the code action.
let Some(last_statement) = body.last() else {
return;
};
let last_statement_range = self
.line_numbers
.src_span_to_lsp_range(last_statement.location());
if within(self.params.range, last_statement_range) {
if let Some(call_data) = turn_statement_into_use(last_statement) {
self.selected_call = Some(call_data);
}
// The cursor has to be inside the last statement of the body to
// offer the code action.
let Some(last_statement) = body.last() else {
return;
};
let last_statement_range = self
.line_numbers
.src_span_to_lsp_range(last_statement.location());
if within(self.params.range, last_statement_range) {
if let Some(call_data) = turn_statement_into_use(last_statement) {
self.selected_call = Some(call_data);
}
}

Expand Down
44 changes: 32 additions & 12 deletions compiler-core/src/language_server/tests/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const ADD_MISSING_PATTERNS: &str = "Add missing patterns";
const ADD_ANNOTATION: &str = "Add type annotation";
const ADD_ANNOTATIONS: &str = "Add type annotations";
const DESUGAR_USE_EXPRESSION: &str = "Desugar use expression";
const TURN_INTO_USE_EXPRESSION: &str = "Turn into use expression";
const CONVERT_TO_USE: &str = "Convert to `use`";

macro_rules! assert_code_action {
($title:expr, $code:literal, $range:expr $(,)?) => {
Expand Down Expand Up @@ -3415,7 +3415,7 @@ fn wibble(f) {
}
"#;
assert_code_action!(
TURN_INTO_USE_EXPRESSION,
CONVERT_TO_USE,
TestProject::for_source(src),
find_position_of("wibble").to_selection(),
);
Expand All @@ -3433,7 +3433,7 @@ fn wibble(f) {
}
"#;
assert_code_action!(
TURN_INTO_USE_EXPRESSION,
CONVERT_TO_USE,
TestProject::for_source(src),
find_position_of("wibble").to_selection(),
);
Expand All @@ -3451,7 +3451,7 @@ fn wibble(m, n, f) {
}
"#;
assert_code_action!(
TURN_INTO_USE_EXPRESSION,
CONVERT_TO_USE,
TestProject::for_source(src),
find_position_of("todo").to_selection(),
);
Expand All @@ -3474,7 +3474,7 @@ fn wibble(m, n, f) {
}
"#;
assert_code_action!(
TURN_INTO_USE_EXPRESSION,
CONVERT_TO_USE,
TestProject::for_source(src),
find_position_of("1, 2").select_until(find_position_of("fn(a)")),
);
Expand All @@ -3494,7 +3494,7 @@ fn wibble(m, n, f) {
}
"#;
assert_code_action!(
TURN_INTO_USE_EXPRESSION,
CONVERT_TO_USE,
TestProject::for_source(src),
find_position_of("wibble").select_until(find_position_of("a: ")),
);
Expand All @@ -3513,7 +3513,7 @@ fn wibble(m, n, f) {
}
"#;
assert_no_code_actions!(
TURN_INTO_USE_EXPRESSION,
CONVERT_TO_USE,
TestProject::for_source(src),
find_position_of("10").to_selection(),
);
Expand All @@ -3535,7 +3535,7 @@ fn wibble(m, n, f) {
}
"#;
assert_no_code_actions!(
TURN_INTO_USE_EXPRESSION,
CONVERT_TO_USE,
TestProject::for_source(src),
find_position_of("10").to_selection(),
);
Expand All @@ -3557,7 +3557,7 @@ fn wibble(m, n, f) {
}
"#;
assert_code_action!(
TURN_INTO_USE_EXPRESSION,
CONVERT_TO_USE,
TestProject::for_source(src),
find_position_of("wibble(1,").select_until(find_position_of("11")),
);
Expand All @@ -3568,7 +3568,9 @@ fn turn_call_into_use_starts_from_innermost_function() {
let src = r#"
pub fn main() {
wibble(10, 20, fn(a) {
wibble(30, 40, fn(b) { a + b })
wibble(30, 40, fn(b) {
a + b
})
})
}
Expand All @@ -3577,7 +3579,7 @@ fn wibble(m, n, f) {
}
"#;
assert_code_action!(
TURN_INTO_USE_EXPRESSION,
CONVERT_TO_USE,
TestProject::for_source(src),
find_position_of("30").select_until(find_position_of("40")),
);
Expand All @@ -3598,8 +3600,26 @@ fn wibble(m, n, f) {
}
"#;
assert_code_action!(
TURN_INTO_USE_EXPRESSION,
CONVERT_TO_USE,
TestProject::for_source(src),
find_position_of("use").to_selection(),
);
}

#[test]
fn turn_call_into_use_with_module_function() {
let src = r#"
import other
pub fn main() {
other.wibble(10, 20, fn(a) {
todo
a + b
})
}
"#;
assert_code_action!(
CONVERT_TO_USE,
TestProject::for_source(src).add_module("other", "pub fn wibble(n, m, f) { todo }"),
find_position_of("wibble").to_selection(),
);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
---
source: compiler-core/src/language_server/tests/action.rs
expression: "\npub fn main() {\n wibble(10, 20, fn(a) {\n wibble(30, 40, fn(b) { a + b })\n })\n}\n\nfn wibble(m, n, f) {\n f(1)\n}\n"
expression: "\npub fn main() {\n wibble(10, 20, fn(a) {\n wibble(30, 40, fn(b) {\n a + b\n })\n })\n}\n\nfn wibble(m, n, f) {\n f(1)\n}\n"
---
----- BEFORE ACTION

pub fn main() {
wibble(10, 20, fn(a) {
wibble(30, 40, fn(b) { a + b })
▔▔▔▔↑
wibble(30, 40, fn(b) {
▔▔▔▔↑
a + b
})
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: compiler-core/src/language_server/tests/action.rs
expression: "\nimport other\npub fn main() {\n other.wibble(10, 20, fn(a) {\n todo\n a + b\n })\n}\n"
---
----- BEFORE ACTION

import other
pub fn main() {
other.wibble(10, 20, fn(a) {
todo
a + b
})
}


----- AFTER ACTION

import other
pub fn main() {
use a <- other.wibble(10, 20)
todo
a + b
}

0 comments on commit d49ba4a

Please sign in to comment.