diff options
| author | LowR <ryo.eab.middle@gmail.com> | 2022-04-27 03:34:32 +0900 |
|---|---|---|
| committer | LowR <ryo.eab.middle@gmail.com> | 2022-04-27 05:12:10 +0900 |
| commit | bc7cb00a2733d8a5d0d70fd288d80d2f12c1ad3d (patch) | |
| tree | 7869a9098056b2f5e746c99cfd085235ed0d8e1f | |
| parent | a8bc625f5ad14e6289741cf4a5299eddcdd96680 (diff) | |
| download | rust-bc7cb00a2733d8a5d0d70fd288d80d2f12c1ad3d.tar.gz rust-bc7cb00a2733d8a5d0d70fd288d80d2f12c1ad3d.zip | |
fix: handle raw identifiers in move_module_to_file
| -rw-r--r-- | crates/ide_assists/src/handlers/move_module_to_file.rs | 87 |
1 files changed, 85 insertions, 2 deletions
diff --git a/crates/ide_assists/src/handlers/move_module_to_file.rs b/crates/ide_assists/src/handlers/move_module_to_file.rs index 7dc71cf7103..759494dcf02 100644 --- a/crates/ide_assists/src/handlers/move_module_to_file.rs +++ b/crates/ide_assists/src/handlers/move_module_to_file.rs @@ -6,7 +6,7 @@ use itertools::Itertools; use stdx::format_to; use syntax::{ ast::{self, edit::AstNodeEdit, HasName}, - AstNode, TextRange, + AstNode, SmolStr, TextRange, }; 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()) .filter_map(|it| it.name()) + .map(|name| SmolStr::from(name.text().trim_start_matches("r#"))) .collect::<Vec<_>>(); + format_to!(buf, "{}", segments.into_iter().rev().format("/")); - format_to!(buf, ".rs"); + + // 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"); + } buf }; let contents = { @@ -251,4 +260,78 @@ mod bar { "#, ); } + + #[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 +"#, + ) + } } |
