aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-09-07 23:45:05 +0100
committerKirill Bulatov <[email protected]>2020-09-09 23:42:20 +0100
commitdbf70cd015454cf125fda9b006251fa2782fbc7f (patch)
tree5e68fe0a963c55ab5a45c53ebb8afbc4a7981bb5 /crates
parent9fb83211f95a450fdadf05f8f64be053f14dc57e (diff)
Properly handle mod.rs imports
Diffstat (limited to 'crates')
-rw-r--r--crates/ide/src/completion/complete_mod.rs87
1 files changed, 40 insertions, 47 deletions
diff --git a/crates/ide/src/completion/complete_mod.rs b/crates/ide/src/completion/complete_mod.rs
index f1795d2f7..b5f2d636c 100644
--- a/crates/ide/src/completion/complete_mod.rs
+++ b/crates/ide/src/completion/complete_mod.rs
@@ -51,26 +51,26 @@ pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
51 }) 51 })
52 .filter_map(|submodule_file| { 52 .filter_map(|submodule_file| {
53 let submodule_path = source_root.path_for_file(&submodule_file)?; 53 let submodule_path = source_root.path_for_file(&submodule_file)?;
54 if !is_special_rust_file_path(&submodule_path) 54 let directory_with_submodule = submodule_path.parent()?;
55 && submodule_path.parent()? == directory_to_look_for_submodules 55 match submodule_path.file_name_and_extension()? {
56 { 56 ("lib", Some("rs")) | ("main", Some("rs")) => None,
57 submodule_path.file_name_and_extension() 57 ("mod", Some("rs")) => {
58 } else { 58 if directory_with_submodule.parent()? == directory_to_look_for_submodules {
59 None 59 match directory_with_submodule.file_name_and_extension()? {
60 } 60 (directory_name, None) => Some(directory_name.to_owned()),
61 }) 61 _ => None,
62 .filter_map(|submodule_file_name_and_extension| match submodule_file_name_and_extension { 62 }
63 (file_name, Some("rs")) => Some(file_name.to_owned()), 63 } else {
64 (subdirectory_name, None) => { 64 None
65 let mod_rs_path = 65 }
66 directory_to_look_for_submodules.join(subdirectory_name)?.join("mod.rs")?;
67 if source_root.file_for_path(&mod_rs_path).is_some() {
68 Some(subdirectory_name.to_owned())
69 } else {
70 None
71 } 66 }
67 (file_name, Some("rs"))
68 if directory_with_submodule == directory_to_look_for_submodules =>
69 {
70 Some(file_name.to_owned())
71 }
72 _ => None,
72 } 73 }
73 _ => None,
74 }) 74 })
75 .filter(|name| !existing_mod_declarations.contains(name)) 75 .filter(|name| !existing_mod_declarations.contains(name))
76 .for_each(|submodule_name| { 76 .for_each(|submodule_name| {
@@ -87,41 +87,34 @@ pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
87 Some(()) 87 Some(())
88} 88}
89 89
90fn is_special_rust_file_path(path: &VfsPath) -> bool {
91 matches!(
92 path.file_name_and_extension(),
93 Some(("mod", Some("rs"))) | Some(("lib", Some("rs"))) | Some(("main", Some("rs")))
94 )
95}
96
97fn directory_to_look_for_submodules( 90fn directory_to_look_for_submodules(
98 module: Module, 91 module: Module,
99 db: &RootDatabase, 92 db: &RootDatabase,
100 module_file_path: &VfsPath, 93 module_file_path: &VfsPath,
101) -> Option<VfsPath> { 94) -> Option<VfsPath> {
102 let module_directory_path = module_file_path.parent()?; 95 let directory_with_module_path = module_file_path.parent()?;
103 let base_directory = if is_special_rust_file_path(module_file_path) { 96 let base_directory = match module_file_path.file_name_and_extension()? {
104 Some(module_directory_path) 97 ("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => {
105 } else if let (regular_rust_file_name, Some("rs")) = 98 Some(directory_with_module_path)
106 module_file_path.file_name_and_extension()? 99 }
107 { 100 (regular_rust_file_name, Some("rs")) => {
108 if matches!( 101 if matches!(
109 ( 102 (
110 module_directory_path 103 directory_with_module_path
111 .parent() 104 .parent()
112 .as_ref() 105 .as_ref()
113 .and_then(|path| path.file_name_and_extension()), 106 .and_then(|path| path.file_name_and_extension()),
114 module_directory_path.file_name_and_extension(), 107 directory_with_module_path.file_name_and_extension(),
115 ), 108 ),
116 (Some(("src", None)), Some(("bin", None))) 109 (Some(("src", None)), Some(("bin", None)))
117 ) { 110 ) {
118 // files in /src/bin/ can import each other directly 111 // files in /src/bin/ can import each other directly
119 Some(module_directory_path) 112 Some(directory_with_module_path)
120 } else { 113 } else {
121 module_directory_path.join(regular_rust_file_name) 114 directory_with_module_path.join(regular_rust_file_name)
115 }
122 } 116 }
123 } else { 117 _ => None,
124 None
125 }?; 118 }?;
126 119
127 let mut resulting_path = base_directory; 120 let mut resulting_path = base_directory;