From 14777ce75190d21f52ae068e3d43a7d16de84ca7 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 12 Jan 2023 11:24:44 +0100 Subject: [PATCH] fix indentation of unlinked_file quickfix --- .../src/handlers/unlinked_file.rs | 38 +++++++++---------- crates/ide/src/goto_declaration.rs | 1 + 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/crates/ide-diagnostics/src/handlers/unlinked_file.rs b/crates/ide-diagnostics/src/handlers/unlinked_file.rs index f527aa290c..3d45a75913 100644 --- a/crates/ide-diagnostics/src/handlers/unlinked_file.rs +++ b/crates/ide-diagnostics/src/handlers/unlinked_file.rs @@ -9,7 +9,7 @@ use ide_db::{ RootDatabase, }; use syntax::{ - ast::{self, HasModuleItem, HasName}, + ast::{self, edit::IndentLevel, HasModuleItem, HasName}, AstNode, TextRange, TextSize, }; use text_edit::TextEdit; @@ -184,30 +184,36 @@ fn make_fixes( Some(last) => { cov_mark::hit!(unlinked_file_append_to_existing_mods); let offset = last.syntax().text_range().end(); - mod_decl_builder.insert(offset, format!("\n{mod_decl}")); - pub_mod_decl_builder.insert(offset, format!("\n{pub_mod_decl}")); + let indent = IndentLevel::from_node(last.syntax()); + mod_decl_builder.insert(offset, format!("\n{indent}{mod_decl}")); + pub_mod_decl_builder.insert(offset, format!("\n{indent}{pub_mod_decl}")); } None => { // Prepend before the first item in the file. match items.next() { - Some(item) => { + Some(first) => { cov_mark::hit!(unlinked_file_prepend_before_first_item); - let offset = item.syntax().text_range().start(); - mod_decl_builder.insert(offset, format!("{mod_decl}\n\n")); - pub_mod_decl_builder.insert(offset, format!("{pub_mod_decl}\n\n")); + let offset = first.syntax().text_range().start(); + let indent = IndentLevel::from_node(first.syntax()); + 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 => { // No items in the file, so just append at the end. cov_mark::hit!(unlinked_file_empty_file); + let mut indent = IndentLevel::from(0); let offset = match &source { ModuleSource::SourceFile(it) => it.syntax().text_range().end(), ModuleSource::Module(it) => { + indent = IndentLevel::from_node(it.syntax()) + 1; 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")); - pub_mod_decl_builder.insert(offset, format!("{pub_mod_decl}\n")); + mod_decl_builder.insert(offset, format!("{indent}{mod_decl}\n")); + pub_mod_decl_builder.insert(offset, format!("{indent}{pub_mod_decl}\n")); } } } @@ -406,15 +412,13 @@ mod foo; mod bar; //- /bar.rs mod foo { - } //- /bar/foo/baz.rs $0 "#, r#" mod foo { - -mod baz; + mod baz; } "#, ); @@ -428,15 +432,13 @@ mod baz; mod bar; //- /bar.rs mod baz { - } //- /bar/baz/foo/mod.rs $0 "#, r#" mod baz { - -mod foo; + mod foo; } "#, ); @@ -448,15 +450,13 @@ mod foo; r#" //- /main.rs mod bar { - } //- /bar/foo/mod.rs $0 "#, r#" mod bar { - -mod foo; + mod foo; } "#, ); diff --git a/crates/ide/src/goto_declaration.rs b/crates/ide/src/goto_declaration.rs index c7130a2a4b..e70bc2ec54 100644 --- a/crates/ide/src/goto_declaration.rs +++ b/crates/ide/src/goto_declaration.rs @@ -17,6 +17,7 @@ use crate::{ // This is the same as `Go to Definition` with the following exceptions: // - 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 +// - fields in patterns will navigate to the field declaration of the struct, union or variant pub(crate) fn goto_declaration( db: &RootDatabase, position: FilePosition,