Skip to content

Commit 14777ce

Browse files
committed
fix indentation of unlinked_file quickfix
1 parent 5214a98 commit 14777ce

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

crates/ide-diagnostics/src/handlers/unlinked_file.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ide_db::{
99
RootDatabase,
1010
};
1111
use syntax::{
12-
ast::{self, HasModuleItem, HasName},
12+
ast::{self, edit::IndentLevel, HasModuleItem, HasName},
1313
AstNode, TextRange, TextSize,
1414
};
1515
use text_edit::TextEdit;
@@ -184,30 +184,36 @@ fn make_fixes(
184184
Some(last) => {
185185
cov_mark::hit!(unlinked_file_append_to_existing_mods);
186186
let offset = last.syntax().text_range().end();
187-
mod_decl_builder.insert(offset, format!("\n{mod_decl}"));
188-
pub_mod_decl_builder.insert(offset, format!("\n{pub_mod_decl}"));
187+
let indent = IndentLevel::from_node(last.syntax());
188+
mod_decl_builder.insert(offset, format!("\n{indent}{mod_decl}"));
189+
pub_mod_decl_builder.insert(offset, format!("\n{indent}{pub_mod_decl}"));
189190
}
190191
None => {
191192
// Prepend before the first item in the file.
192193
match items.next() {
193-
Some(item) => {
194+
Some(first) => {
194195
cov_mark::hit!(unlinked_file_prepend_before_first_item);
195-
let offset = item.syntax().text_range().start();
196-
mod_decl_builder.insert(offset, format!("{mod_decl}\n\n"));
197-
pub_mod_decl_builder.insert(offset, format!("{pub_mod_decl}\n\n"));
196+
let offset = first.syntax().text_range().start();
197+
let indent = IndentLevel::from_node(first.syntax());
198+
mod_decl_builder.insert(offset, format!("{mod_decl}\n\n{indent}"));
199+
pub_mod_decl_builder.insert(offset, format!("{pub_mod_decl}\n\n{indent}"));
198200
}
199201
None => {
200202
// No items in the file, so just append at the end.
201203
cov_mark::hit!(unlinked_file_empty_file);
204+
let mut indent = IndentLevel::from(0);
202205
let offset = match &source {
203206
ModuleSource::SourceFile(it) => it.syntax().text_range().end(),
204207
ModuleSource::Module(it) => {
208+
indent = IndentLevel::from_node(it.syntax()) + 1;
205209
it.item_list()?.r_curly_token()?.text_range().start()
206210
}
207-
ModuleSource::BlockExpr(_) => return None,
211+
ModuleSource::BlockExpr(it) => {
212+
it.stmt_list()?.r_curly_token()?.text_range().start()
213+
}
208214
};
209-
mod_decl_builder.insert(offset, format!("{mod_decl}\n"));
210-
pub_mod_decl_builder.insert(offset, format!("{pub_mod_decl}\n"));
215+
mod_decl_builder.insert(offset, format!("{indent}{mod_decl}\n"));
216+
pub_mod_decl_builder.insert(offset, format!("{indent}{pub_mod_decl}\n"));
211217
}
212218
}
213219
}
@@ -406,15 +412,13 @@ mod foo;
406412
mod bar;
407413
//- /bar.rs
408414
mod foo {
409-
410415
}
411416
//- /bar/foo/baz.rs
412417
$0
413418
"#,
414419
r#"
415420
mod foo {
416-
417-
mod baz;
421+
mod baz;
418422
}
419423
"#,
420424
);
@@ -428,15 +432,13 @@ mod baz;
428432
mod bar;
429433
//- /bar.rs
430434
mod baz {
431-
432435
}
433436
//- /bar/baz/foo/mod.rs
434437
$0
435438
"#,
436439
r#"
437440
mod baz {
438-
439-
mod foo;
441+
mod foo;
440442
}
441443
"#,
442444
);
@@ -448,15 +450,13 @@ mod foo;
448450
r#"
449451
//- /main.rs
450452
mod bar {
451-
452453
}
453454
//- /bar/foo/mod.rs
454455
$0
455456
"#,
456457
r#"
457458
mod bar {
458-
459-
mod foo;
459+
mod foo;
460460
}
461461
"#,
462462
);

crates/ide/src/goto_declaration.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::{
1717
// This is the same as `Go to Definition` with the following exceptions:
1818
// - outline modules will navigate to the `mod name;` item declaration
1919
// - trait assoc items will navigate to the assoc item of the trait declaration opposed to the trait impl
20+
// - fields in patterns will navigate to the field declaration of the struct, union or variant
2021
pub(crate) fn goto_declaration(
2122
db: &RootDatabase,
2223
position: FilePosition,

0 commit comments

Comments
 (0)