From b963893349c0df4c6e8786303eaa39e2bc9fa500 Mon Sep 17 00:00:00 2001 From: Matt Hall Date: Sat, 20 Feb 2021 16:51:16 +0000 Subject: Fix #7712 retain visibility extracting mod to file --- crates/assists/src/handlers/move_module_to_file.rs | 45 +++++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/assists/src/handlers/move_module_to_file.rs b/crates/assists/src/handlers/move_module_to_file.rs index 9d8579f47..1f2d8f20f 100644 --- a/crates/assists/src/handlers/move_module_to_file.rs +++ b/crates/assists/src/handlers/move_module_to_file.rs @@ -1,4 +1,4 @@ -use ast::edit::IndentLevel; +use ast::{edit::IndentLevel, VisibilityOwner}; use ide_db::base_db::AnchoredPathBuf; use syntax::{ ast::{self, edit::AstNodeEdit, NameOwner}, @@ -36,6 +36,8 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt let module_def = ctx.sema.to_def(&module_ast)?; let parent_module = module_def.parent(ctx.db())?; + let vis_str = + if let Some(v) = module_ast.visibility() { v.to_string() + " " } else { "".to_string() }; acc.add( AssistId("move_module_to_file", AssistKind::RefactorExtract), @@ -59,7 +61,10 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt items }; - builder.replace(module_ast.syntax().text_range(), format!("mod {};", module_name)); + builder.replace( + module_ast.syntax().text_range(), + format!("{}mod {};", vis_str, module_name), + ); let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path }; builder.create_file(dst, contents); @@ -137,6 +142,42 @@ fn f() {} ); } + #[test] + fn extract_public() { + check_assist( + move_module_to_file, + r#" +pub mod $0tests { + #[test] fn t() {} +} +"#, + r#" +//- /main.rs +pub mod tests; +//- /tests.rs +#[test] fn t() {} +"#, + ); + } + + #[test] + fn extract_public_crate() { + check_assist( + move_module_to_file, + r#" +pub(crate) mod $0tests { + #[test] fn t() {} +} +"#, + r#" +//- /main.rs +pub(crate) mod tests; +//- /tests.rs +#[test] fn t() {} +"#, + ); + } + #[test] fn available_before_curly() { mark::check!(available_before_curly); -- cgit v1.2.3 From 8497b5bd75159cf3e491af8dadb4b1b0da95477d Mon Sep 17 00:00:00 2001 From: Matt Hall Date: Sat, 20 Feb 2021 17:11:04 +0000 Subject: Use format_to! to make logic a bit nicer --- crates/assists/src/handlers/move_module_to_file.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'crates') diff --git a/crates/assists/src/handlers/move_module_to_file.rs b/crates/assists/src/handlers/move_module_to_file.rs index 1f2d8f20f..91c395c1b 100644 --- a/crates/assists/src/handlers/move_module_to_file.rs +++ b/crates/assists/src/handlers/move_module_to_file.rs @@ -1,5 +1,6 @@ use ast::{edit::IndentLevel, VisibilityOwner}; use ide_db::base_db::AnchoredPathBuf; +use stdx::format_to; use syntax::{ ast::{self, edit::AstNodeEdit, NameOwner}, AstNode, TextRange, @@ -36,8 +37,6 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt let module_def = ctx.sema.to_def(&module_ast)?; let parent_module = module_def.parent(ctx.db())?; - let vis_str = - if let Some(v) = module_ast.visibility() { v.to_string() + " " } else { "".to_string() }; acc.add( AssistId("move_module_to_file", AssistKind::RefactorExtract), @@ -61,10 +60,13 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt items }; - builder.replace( - module_ast.syntax().text_range(), - format!("{}mod {};", vis_str, module_name), - ); + 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 dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path }; builder.create_file(dst, contents); -- cgit v1.2.3