fix indentation of unlinked_file quickfix

This commit is contained in:
Lukas Wirth 2023-01-12 11:24:44 +01:00
parent 5214a98d9c
commit 14777ce751
2 changed files with 20 additions and 19 deletions

View File

@ -9,7 +9,7 @@
RootDatabase, RootDatabase,
}; };
use syntax::{ use syntax::{
ast::{self, HasModuleItem, HasName}, ast::{self, edit::IndentLevel, HasModuleItem, HasName},
AstNode, TextRange, TextSize, AstNode, TextRange, TextSize,
}; };
use text_edit::TextEdit; use text_edit::TextEdit;
@ -184,30 +184,36 @@ fn is_outline_mod(item: &ast::Item) -> bool {
Some(last) => { Some(last) => {
cov_mark::hit!(unlinked_file_append_to_existing_mods); cov_mark::hit!(unlinked_file_append_to_existing_mods);
let offset = last.syntax().text_range().end(); let offset = last.syntax().text_range().end();
mod_decl_builder.insert(offset, format!("\n{mod_decl}")); let indent = IndentLevel::from_node(last.syntax());
pub_mod_decl_builder.insert(offset, format!("\n{pub_mod_decl}")); mod_decl_builder.insert(offset, format!("\n{indent}{mod_decl}"));
pub_mod_decl_builder.insert(offset, format!("\n{indent}{pub_mod_decl}"));
} }
None => { None => {
// Prepend before the first item in the file. // Prepend before the first item in the file.
match items.next() { match items.next() {
Some(item) => { Some(first) => {
cov_mark::hit!(unlinked_file_prepend_before_first_item); cov_mark::hit!(unlinked_file_prepend_before_first_item);
let offset = item.syntax().text_range().start(); let offset = first.syntax().text_range().start();
mod_decl_builder.insert(offset, format!("{mod_decl}\n\n")); let indent = IndentLevel::from_node(first.syntax());
pub_mod_decl_builder.insert(offset, format!("{pub_mod_decl}\n\n")); mod_decl_builder.insert(offset, format!("{mod_decl}\n\n{indent}"));
pub_mod_decl_builder.insert(offset, format!("{pub_mod_decl}\n\n{indent}"));
} }
None => { None => {
// No items in the file, so just append at the end. // No items in the file, so just append at the end.
cov_mark::hit!(unlinked_file_empty_file); cov_mark::hit!(unlinked_file_empty_file);
let mut indent = IndentLevel::from(0);
let offset = match &source { let offset = match &source {
ModuleSource::SourceFile(it) => it.syntax().text_range().end(), ModuleSource::SourceFile(it) => it.syntax().text_range().end(),
ModuleSource::Module(it) => { ModuleSource::Module(it) => {
indent = IndentLevel::from_node(it.syntax()) + 1;
it.item_list()?.r_curly_token()?.text_range().start() it.item_list()?.r_curly_token()?.text_range().start()
} }
ModuleSource::BlockExpr(_) => return None, ModuleSource::BlockExpr(it) => {
it.stmt_list()?.r_curly_token()?.text_range().start()
}
}; };
mod_decl_builder.insert(offset, format!("{mod_decl}\n")); mod_decl_builder.insert(offset, format!("{indent}{mod_decl}\n"));
pub_mod_decl_builder.insert(offset, format!("{pub_mod_decl}\n")); pub_mod_decl_builder.insert(offset, format!("{indent}{pub_mod_decl}\n"));
} }
} }
} }
@ -406,15 +412,13 @@ fn unlinked_file_insert_into_inline_simple() {
mod bar; mod bar;
//- /bar.rs //- /bar.rs
mod foo { mod foo {
} }
//- /bar/foo/baz.rs //- /bar/foo/baz.rs
$0 $0
"#, "#,
r#" r#"
mod foo { mod foo {
mod baz;
mod baz;
} }
"#, "#,
); );
@ -428,15 +432,13 @@ fn unlinked_file_insert_into_inline_simple_modrs() {
mod bar; mod bar;
//- /bar.rs //- /bar.rs
mod baz { mod baz {
} }
//- /bar/baz/foo/mod.rs //- /bar/baz/foo/mod.rs
$0 $0
"#, "#,
r#" r#"
mod baz { mod baz {
mod foo;
mod foo;
} }
"#, "#,
); );
@ -448,15 +450,13 @@ fn unlinked_file_insert_into_inline_simple_modrs_main() {
r#" r#"
//- /main.rs //- /main.rs
mod bar { mod bar {
} }
//- /bar/foo/mod.rs //- /bar/foo/mod.rs
$0 $0
"#, "#,
r#" r#"
mod bar { mod bar {
mod foo;
mod foo;
} }
"#, "#,
); );

View File

@ -17,6 +17,7 @@
// This is the same as `Go to Definition` with the following exceptions: // This is the same as `Go to Definition` with the following exceptions:
// - outline modules will navigate to the `mod name;` item declaration // - outline modules will navigate to the `mod name;` item declaration
// - trait assoc items will navigate to the assoc item of the trait declaration opposed to the trait impl // - trait assoc items will navigate to the assoc item of the trait declaration opposed to the trait impl
// - fields in patterns will navigate to the field declaration of the struct, union or variant
pub(crate) fn goto_declaration( pub(crate) fn goto_declaration(
db: &RootDatabase, db: &RootDatabase,
position: FilePosition, position: FilePosition,