Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: LSP auto-import text indent #6699

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -134,14 +134,14 @@
#[test]
async fn test_use_first_segment() {
let src = r#"
mod foobaz {}

Check warning on line 137 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foobaz)
mod foobar {}
use foob>|<

Check warning on line 139 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
"#;

assert_completion(
src,
vec![module_completion_item("foobaz"), module_completion_item("foobar")],

Check warning on line 144 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foobaz)
)
.await;
}
Expand Down Expand Up @@ -304,7 +304,7 @@
mod bar {
mod something {}

use super::foob>|<

Check warning on line 307 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
}
"#;

Expand Down Expand Up @@ -1586,6 +1586,54 @@
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 Expand Up @@ -1668,7 +1716,7 @@
#[test]
async fn test_auto_import_suggests_modules_too() {
let src = r#"mod foo {
pub mod barbaz {

Check warning on line 1719 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (barbaz)
fn hello_world() {}
}
}
Expand All @@ -1678,10 +1726,10 @@
}
}"#;

let expected = r#"use foo::barbaz;

Check warning on line 1729 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (barbaz)

mod foo {
pub mod barbaz {

Check warning on line 1732 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (barbaz)
fn hello_world() {}
}
}
Expand All @@ -1695,12 +1743,12 @@
assert_eq!(items.len(), 1);

let item = items.remove(0);
assert_eq!(item.label, "barbaz");

Check warning on line 1746 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (barbaz)

assert_eq!(
item.label_details,
Some(CompletionItemLabelDetails {
detail: Some("(use foo::barbaz)".to_string()),

Check warning on line 1751 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (barbaz)
description: None
})
);
Expand Down Expand Up @@ -2087,7 +2135,7 @@
async fn test_completes_after_first_letter_of_path() {
let src = r#"
fn main() {
h>|<ello();

Check warning on line 2138 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (ello)
}

fn hello_world() {}
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),
}]
}
Loading