Skip to content

Commit 364d572

Browse files
bors[bot]mattyhall
andauthored
Merge #7730
7730: Fix #7712 retain visibility extracting mod to file r=lnicola a=mattyhall Co-authored-by: Matt Hall <matthew@quickbeam.me.uk>
2 parents de67469 + 8497b5b commit 364d572

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

crates/assists/src/handlers/move_module_to_file.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use ast::edit::IndentLevel;
1+
use ast::{edit::IndentLevel, VisibilityOwner};
22
use ide_db::base_db::AnchoredPathBuf;
3+
use stdx::format_to;
34
use syntax::{
45
ast::{self, edit::AstNodeEdit, NameOwner},
56
AstNode, TextRange,
@@ -59,7 +60,13 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt
5960
items
6061
};
6162

62-
builder.replace(module_ast.syntax().text_range(), format!("mod {};", module_name));
63+
let mut buf = String::new();
64+
if let Some(v) = module_ast.visibility() {
65+
format_to!(buf, "{} ", v);
66+
}
67+
format_to!(buf, "mod {};", module_name);
68+
69+
builder.replace(module_ast.syntax().text_range(), buf);
6370

6471
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
6572
builder.create_file(dst, contents);
@@ -137,6 +144,42 @@ fn f() {}
137144
);
138145
}
139146

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

0 commit comments

Comments
 (0)