Skip to content

Commit

Permalink
fix: LSP auto-import text indent (#6699)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Dec 4, 2024
1 parent c400543 commit bbe7564
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
48 changes: 48 additions & 0 deletions tooling/lsp/src/requests/completion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,54 @@ fn main() {
assert_eq!(changed, expected);
}

#[test]
async fn test_auto_import_inserts_after_last_use_in_nested_module() {
let src = r#"mod foo {
pub mod bar {
pub fn hello_world() {}
}
}
mod baz {
fn qux() {}
}
mod other {
use baz::qux;
fn main() {
hel>|<
}
}"#;

let expected = r#"mod foo {
pub mod bar {
pub fn hello_world() {}
}
}
mod baz {
fn qux() {}
}
mod other {
use baz::qux;
use super::foo::bar::hello_world;
fn main() {
hel
}
}"#;
let mut items = get_completions(src).await;
assert_eq!(items.len(), 1);

let item = items.remove(0);

let changed =
apply_text_edits(&src.replace(">|<", ""), &item.additional_text_edits.unwrap());
assert_eq!(changed, expected);
}

#[test]
async fn test_does_not_auto_import_test_functions() {
let src = r#"
Expand Down
4 changes: 2 additions & 2 deletions tooling/lsp/src/use_segment_positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ fn new_use_completion_item_additional_text_edits(
request: UseCompletionItemAdditionTextEditsRequest,
) -> Vec<TextEdit> {
let line = request.auto_import_line as u32;
let character = (request.nesting * 4) as u32;
let character = 0;
let indent = " ".repeat(request.nesting * 4);
let mut newlines = "\n";

Expand All @@ -331,6 +331,6 @@ fn new_use_completion_item_additional_text_edits(

vec![TextEdit {
range: Range { start: Position { line, character }, end: Position { line, character } },
new_text: format!("use {};{}{}", request.full_path, newlines, indent),
new_text: format!("{}use {};{}", indent, request.full_path, newlines),
}]
}

0 comments on commit bbe7564

Please sign in to comment.