@@ -6,7 +6,7 @@ use itertools::Itertools;
6
6
use stdx:: format_to;
7
7
use syntax:: {
8
8
ast:: { self , edit:: AstNodeEdit , HasName } ,
9
- AstNode , TextRange ,
9
+ AstNode , SmolStr , TextRange ,
10
10
} ;
11
11
12
12
use crate :: { AssistContext , AssistId , AssistKind , Assists } ;
@@ -58,9 +58,18 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt
58
58
}
59
59
let segments = iter:: successors ( Some ( module_ast. clone ( ) ) , |module| module. parent ( ) )
60
60
. filter_map ( |it| it. name ( ) )
61
+ . map ( |name| SmolStr :: from ( name. text ( ) . trim_start_matches ( "r#" ) ) )
61
62
. collect :: < Vec < _ > > ( ) ;
63
+
62
64
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
+ }
64
73
buf
65
74
} ;
66
75
let contents = {
@@ -251,4 +260,78 @@ mod bar {
251
260
"# ,
252
261
) ;
253
262
}
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
+ }
254
337
}
0 commit comments