Auto merge of #12089 - lowr:fix/move-raw-ident-module-to-file, r=jonas-schievink

fix: handle raw identifiers in move_module_to_file

Fixes #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.
This commit is contained in:
bors 2022-04-27 12:59:52 +00:00
commit 6c6aaeee7a

View File

@ -6,7 +6,7 @@
use stdx::format_to; use stdx::format_to;
use syntax::{ use syntax::{
ast::{self, edit::AstNodeEdit, HasName}, ast::{self, edit::AstNodeEdit, HasName},
AstNode, TextRange, AstNode, SmolStr, TextRange,
}; };
use crate::{AssistContext, AssistId, AssistKind, Assists}; use crate::{AssistContext, AssistId, AssistKind, Assists};
@ -58,9 +58,18 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt
} }
let segments = iter::successors(Some(module_ast.clone()), |module| module.parent()) let segments = iter::successors(Some(module_ast.clone()), |module| module.parent())
.filter_map(|it| it.name()) .filter_map(|it| it.name())
.map(|name| SmolStr::from(name.text().trim_start_matches("r#")))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
format_to!(buf, "{}", segments.into_iter().rev().format("/")); format_to!(buf, "{}", segments.into_iter().rev().format("/"));
// We need to special case mod named `r#mod` and place the file in a
// subdirectory as "mod.rs" would be of its parent module otherwise.
if module_name.text() == "r#mod" {
format_to!(buf, "/mod.rs");
} else {
format_to!(buf, ".rs"); format_to!(buf, ".rs");
}
buf buf
}; };
let contents = { let contents = {
@ -251,4 +260,78 @@ mod baz {
"#, "#,
); );
} }
#[test]
fn extract_mod_with_raw_ident() {
check_assist(
move_module_to_file,
r#"
//- /main.rs
mod $0r#static {}
"#,
r#"
//- /main.rs
mod r#static;
//- /static.rs
"#,
)
}
#[test]
fn extract_r_mod() {
check_assist(
move_module_to_file,
r#"
//- /main.rs
mod $0r#mod {}
"#,
r#"
//- /main.rs
mod r#mod;
//- /mod/mod.rs
"#,
)
}
#[test]
fn extract_r_mod_from_mod_rs() {
check_assist(
move_module_to_file,
r#"
//- /main.rs
mod foo;
//- /foo/mod.rs
mod $0r#mod {}
"#,
r#"
//- /foo/mod.rs
mod r#mod;
//- /foo/mod/mod.rs
"#,
)
}
#[test]
fn extract_nested_r_mod() {
check_assist(
move_module_to_file,
r#"
//- /main.rs
mod r#mod {
mod foo {
mod $0r#mod {}
}
}
"#,
r#"
//- /main.rs
mod r#mod {
mod foo {
mod r#mod;
}
}
//- /mod/foo/mod/mod.rs
"#,
)
}
} }