Skip to content

Commit 6c6aaee

Browse files
committed
Auto merge of rust-lang#12089 - lowr:fix/move-raw-ident-module-to-file, r=jonas-schievink
fix: handle raw identifiers in move_module_to_file Fixes rust-lang#12045 Note that I special case'd mod named `r#mod` as commented in the code. Although it's very unlikely that one would use such name, I included it in this fix for the sake of completeness.
2 parents 9f69d02 + bc7cb00 commit 6c6aaee

File tree

1 file changed

+85
-2
lines changed

1 file changed

+85
-2
lines changed

Diff for: crates/ide_assists/src/handlers/move_module_to_file.rs

+85-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use itertools::Itertools;
66
use stdx::format_to;
77
use syntax::{
88
ast::{self, edit::AstNodeEdit, HasName},
9-
AstNode, TextRange,
9+
AstNode, SmolStr, TextRange,
1010
};
1111

1212
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -58,9 +58,18 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt
5858
}
5959
let segments = iter::successors(Some(module_ast.clone()), |module| module.parent())
6060
.filter_map(|it| it.name())
61+
.map(|name| SmolStr::from(name.text().trim_start_matches("r#")))
6162
.collect::<Vec<_>>();
63+
6264
format_to!(buf, "{}", segments.into_iter().rev().format("/"));
63-
format_to!(buf, ".rs");
65+
66+
// We need to special case mod named `r#mod` and place the file in a
67+
// subdirectory as "mod.rs" would be of its parent module otherwise.
68+
if module_name.text() == "r#mod" {
69+
format_to!(buf, "/mod.rs");
70+
} else {
71+
format_to!(buf, ".rs");
72+
}
6473
buf
6574
};
6675
let contents = {
@@ -251,4 +260,78 @@ mod bar {
251260
"#,
252261
);
253262
}
263+
264+
#[test]
265+
fn extract_mod_with_raw_ident() {
266+
check_assist(
267+
move_module_to_file,
268+
r#"
269+
//- /main.rs
270+
mod $0r#static {}
271+
"#,
272+
r#"
273+
//- /main.rs
274+
mod r#static;
275+
//- /static.rs
276+
"#,
277+
)
278+
}
279+
280+
#[test]
281+
fn extract_r_mod() {
282+
check_assist(
283+
move_module_to_file,
284+
r#"
285+
//- /main.rs
286+
mod $0r#mod {}
287+
"#,
288+
r#"
289+
//- /main.rs
290+
mod r#mod;
291+
//- /mod/mod.rs
292+
"#,
293+
)
294+
}
295+
296+
#[test]
297+
fn extract_r_mod_from_mod_rs() {
298+
check_assist(
299+
move_module_to_file,
300+
r#"
301+
//- /main.rs
302+
mod foo;
303+
//- /foo/mod.rs
304+
mod $0r#mod {}
305+
"#,
306+
r#"
307+
//- /foo/mod.rs
308+
mod r#mod;
309+
//- /foo/mod/mod.rs
310+
"#,
311+
)
312+
}
313+
314+
#[test]
315+
fn extract_nested_r_mod() {
316+
check_assist(
317+
move_module_to_file,
318+
r#"
319+
//- /main.rs
320+
mod r#mod {
321+
mod foo {
322+
mod $0r#mod {}
323+
}
324+
}
325+
"#,
326+
r#"
327+
//- /main.rs
328+
mod r#mod {
329+
mod foo {
330+
mod r#mod;
331+
}
332+
}
333+
//- /mod/foo/mod/mod.rs
334+
"#,
335+
)
336+
}
254337
}

0 commit comments

Comments
 (0)