Skip to content

Commit c2e4a9a

Browse files
authored
fix: use module name as line after which we'll insert auto-import (#6025)
# Description ## Problem While working with attributes I found that auto-import didn't work well if a module has an attribute on it. ## Summary Before: ![lsp-auto-import-before](https://github.com/user-attachments/assets/750c89cb-98a6-4d23-bd43-1ed1156aa2a5) After: ![lsp-auto-import-after](https://github.com/user-attachments/assets/ab3418b5-e7ec-4eec-8625-e657163f9a0d) ## Additional Context ## Documentation Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
1 parent a09646b commit c2e4a9a

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

tooling/lsp/src/requests/completion.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ impl<'a> Visitor for NodeFinder<'a> {
882882
false
883883
}
884884

885-
fn visit_parsed_submodule(&mut self, parsed_sub_module: &ParsedSubModule, span: Span) -> bool {
885+
fn visit_parsed_submodule(&mut self, parsed_sub_module: &ParsedSubModule, _span: Span) -> bool {
886886
// Switch `self.module_id` to the submodule
887887
let previous_module_id = self.module_id;
888888

@@ -897,7 +897,9 @@ impl<'a> Visitor for NodeFinder<'a> {
897897
let old_auto_import_line = self.auto_import_line;
898898
self.nesting += 1;
899899

900-
if let Some(lsp_location) = to_lsp_location(self.files, self.file, span) {
900+
if let Some(lsp_location) =
901+
to_lsp_location(self.files, self.file, parsed_sub_module.name.span())
902+
{
901903
self.auto_import_line = (lsp_location.range.start.line + 1) as usize;
902904
}
903905

tooling/lsp/src/requests/completion/tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,7 @@ mod completion_tests {
13951395
#[test]
13961396
async fn test_auto_imports_when_in_nested_module_and_item_is_further_nested() {
13971397
let src = r#"
1398+
#[something]
13981399
mod foo {
13991400
mod bar {
14001401
pub fn hello_world() {}
@@ -1422,8 +1423,8 @@ mod completion_tests {
14221423
item.additional_text_edits,
14231424
Some(vec![TextEdit {
14241425
range: Range {
1425-
start: Position { line: 2, character: 4 },
1426-
end: Position { line: 2, character: 4 },
1426+
start: Position { line: 3, character: 4 },
1427+
end: Position { line: 3, character: 4 },
14271428
},
14281429
new_text: "use bar::hello_world;\n\n ".to_string(),
14291430
}])

0 commit comments

Comments
 (0)