From 8c95b205a27a27bed6434644e8016caa49aeffc1 Mon Sep 17 00:00:00 2001 From: Jesse Bakker Date: Wed, 12 May 2021 15:06:54 +0200 Subject: fix: Keep doc comments and outer attrs on "Move module to file" assist --- .../src/handlers/move_module_to_file.rs | 38 +++++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) (limited to 'crates') diff --git a/crates/ide_assists/src/handlers/move_module_to_file.rs b/crates/ide_assists/src/handlers/move_module_to_file.rs index 6e685b4b2..93f702c55 100644 --- a/crates/ide_assists/src/handlers/move_module_to_file.rs +++ b/crates/ide_assists/src/handlers/move_module_to_file.rs @@ -1,4 +1,4 @@ -use ast::{edit::IndentLevel, VisibilityOwner}; +use ast::edit::IndentLevel; use ide_db::base_db::AnchoredPathBuf; use stdx::format_to; use syntax::{ @@ -60,12 +60,18 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt }; let mut buf = String::new(); - if let Some(v) = module_ast.visibility() { - format_to!(buf, "{} ", v); - } format_to!(buf, "mod {};", module_name); - builder.replace(module_ast.syntax().text_range(), buf); + let replacement_start = if let Some(mod_token) = module_ast.mod_token() { + mod_token.text_range().start() + } else { + module_ast.syntax().text_range().start() + }; + + builder.replace( + TextRange::new(replacement_start, module_ast.syntax().text_range().end()), + buf, + ); let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path }; builder.create_file(dst, contents); @@ -184,4 +190,26 @@ pub(crate) mod tests; cov_mark::check!(available_before_curly); check_assist_not_applicable(move_module_to_file, r#"mod m { $0 }"#); } + + #[test] + fn keep_outer_comments_and_attributes() { + check_assist( + move_module_to_file, + r#" +/// doc comment +#[attribute] +mod $0tests { + #[test] fn t() {} +} +"#, + r#" +//- /main.rs +/// doc comment +#[attribute] +mod tests; +//- /tests.rs +#[test] fn t() {} +"#, + ); + } } -- cgit v1.2.3