@@ -26,38 +26,70 @@ impl FileSet {
26
26
self . files . get ( & path) . copied ( )
27
27
}
28
28
29
- pub fn file_name_and_extension ( & self , file : FileId ) -> Option < ( & str , Option < & str > ) > {
30
- self . paths [ & file] . file_name_and_extension ( )
31
- }
32
-
33
- pub fn list_files_with_extensions (
34
- & self ,
35
- anchor : FileId ,
36
- anchor_relative_path : Option < & str > ,
37
- ) -> Vec < ( & str , Option < & str > ) > {
38
- let anchor_directory = {
39
- let path = self . paths [ & anchor] . clone ( ) ;
40
- match anchor_relative_path {
41
- Some ( anchor_relative_path) => path. join ( anchor_relative_path) ,
42
- None => path. parent ( ) ,
43
- }
29
+ pub fn possible_sudmobule_names ( & self , module_file : FileId ) -> Vec < String > {
30
+ let directory_to_look_for_submodules = match self . get_directory_with_submodules ( module_file)
31
+ {
32
+ Some ( directory) => directory,
33
+ None => return Vec :: new ( ) ,
44
34
} ;
45
-
46
- if let Some ( anchor_directory) = anchor_directory {
47
- self . paths
48
- . iter ( )
49
- . filter_map ( |( _, path) | {
50
- if path. parent ( ) ? == anchor_directory {
51
- path. file_name_and_extension ( )
35
+ self . paths
36
+ . iter ( )
37
+ . filter_map ( |( _, path) | {
38
+ if path. parent ( ) ? == directory_to_look_for_submodules {
39
+ path. file_name_and_extension ( )
40
+ } else {
41
+ None
42
+ }
43
+ } )
44
+ . filter_map ( |file_name_and_extension| match file_name_and_extension {
45
+ // TODO kb do not include the module file name itself, if present
46
+ // TODO kb wrong resolution for nesСпаted non-file modules (mod tests {mod <|>)
47
+ // TODO kb in src/bin when a module is included into another,
48
+ // the included file gets "moved" into a directory below and now cannot add any other modules
49
+ ( "mod" , Some ( "rs" ) ) | ( "lib" , Some ( "rs" ) ) | ( "main" , Some ( "rs" ) ) => None ,
50
+ ( file_name, Some ( "rs" ) ) => Some ( file_name. to_owned ( ) ) ,
51
+ ( subdirectory_name, None ) => {
52
+ let mod_rs_path =
53
+ directory_to_look_for_submodules. join ( subdirectory_name) ?. join ( "mod.rs" ) ?;
54
+ if self . files . contains_key ( & mod_rs_path) {
55
+ Some ( subdirectory_name. to_owned ( ) )
52
56
} else {
53
57
None
54
58
}
55
- } )
56
- . collect ( )
57
- } else {
58
- Vec :: new ( )
59
+ }
60
+ _ => None ,
61
+ } )
62
+ . collect ( )
63
+ }
64
+
65
+ fn get_directory_with_submodules ( & self , module_file : FileId ) -> Option < VfsPath > {
66
+ let module_file_path = & self . paths [ & module_file] ;
67
+ let module_directory_path = module_file_path. parent ( ) ?;
68
+ match module_file_path. file_name_and_extension ( ) {
69
+ Some ( ( "mod" , Some ( "rs" ) ) ) | Some ( ( "lib" , Some ( "rs" ) ) ) | Some ( ( "main" , Some ( "rs" ) ) ) => {
70
+ Some ( module_directory_path)
71
+ }
72
+ Some ( ( regular_rust_file_name, Some ( "rs" ) ) ) => {
73
+ if matches ! (
74
+ (
75
+ module_directory_path
76
+ . parent( )
77
+ . as_ref( )
78
+ . and_then( |path| path. file_name_and_extension( ) ) ,
79
+ module_directory_path. file_name_and_extension( ) ,
80
+ ) ,
81
+ ( Some ( ( "src" , None ) ) , Some ( ( "bin" , None ) ) )
82
+ ) {
83
+ // files in /src/bin/ can import each other directly
84
+ Some ( module_directory_path)
85
+ } else {
86
+ module_directory_path. join ( regular_rust_file_name)
87
+ }
88
+ }
89
+ _ => None ,
59
90
}
60
91
}
92
+
61
93
pub fn insert ( & mut self , file_id : FileId , path : VfsPath ) {
62
94
self . files . insert ( path. clone ( ) , file_id) ;
63
95
self . paths . insert ( file_id, path) ;
0 commit comments