Skip to content

Commit b963893

Browse files
committed
Fix #7712 retain visibility extracting mod to file
1 parent de67469 commit b963893

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

crates/assists/src/handlers/move_module_to_file.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ast::edit::IndentLevel;
1+
use ast::{edit::IndentLevel, VisibilityOwner};
22
use ide_db::base_db::AnchoredPathBuf;
33
use syntax::{
44
ast::{self, edit::AstNodeEdit, NameOwner},
@@ -36,6 +36,8 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt
3636

3737
let module_def = ctx.sema.to_def(&module_ast)?;
3838
let parent_module = module_def.parent(ctx.db())?;
39+
let vis_str =
40+
if let Some(v) = module_ast.visibility() { v.to_string() + " " } else { "".to_string() };
3941

4042
acc.add(
4143
AssistId("move_module_to_file", AssistKind::RefactorExtract),
@@ -59,7 +61,10 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt
5961
items
6062
};
6163

62-
builder.replace(module_ast.syntax().text_range(), format!("mod {};", module_name));
64+
builder.replace(
65+
module_ast.syntax().text_range(),
66+
format!("{}mod {};", vis_str, module_name),
67+
);
6368

6469
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
6570
builder.create_file(dst, contents);
@@ -137,6 +142,42 @@ fn f() {}
137142
);
138143
}
139144

145+
#[test]
146+
fn extract_public() {
147+
check_assist(
148+
move_module_to_file,
149+
r#"
150+
pub mod $0tests {
151+
#[test] fn t() {}
152+
}
153+
"#,
154+
r#"
155+
//- /main.rs
156+
pub mod tests;
157+
//- /tests.rs
158+
#[test] fn t() {}
159+
"#,
160+
);
161+
}
162+
163+
#[test]
164+
fn extract_public_crate() {
165+
check_assist(
166+
move_module_to_file,
167+
r#"
168+
pub(crate) mod $0tests {
169+
#[test] fn t() {}
170+
}
171+
"#,
172+
r#"
173+
//- /main.rs
174+
pub(crate) mod tests;
175+
//- /tests.rs
176+
#[test] fn t() {}
177+
"#,
178+
);
179+
}
180+
140181
#[test]
141182
fn available_before_curly() {
142183
mark::check!(available_before_curly);

0 commit comments

Comments
 (0)