diff options
author | Kirill Bulatov <[email protected]> | 2020-09-09 23:45:49 +0100 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-09-09 23:45:49 +0100 |
commit | 9863798480aa9642e31bfd41ee899d2e7329b5e5 (patch) | |
tree | d1a9c8ce7df146d13faa6a783833ba4d31957390 | |
parent | f4ee885b3b3019b32d5c481bddf1b2667fba7fb3 (diff) |
Rename the method to avoid false promises
-rw-r--r-- | crates/ide/src/completion/complete_mod.rs | 10 | ||||
-rw-r--r-- | crates/vfs/src/vfs_path.rs | 22 |
2 files changed, 15 insertions, 17 deletions
diff --git a/crates/ide/src/completion/complete_mod.rs b/crates/ide/src/completion/complete_mod.rs index d457ff6bf..2ea0d5034 100644 --- a/crates/ide/src/completion/complete_mod.rs +++ b/crates/ide/src/completion/complete_mod.rs | |||
@@ -52,11 +52,11 @@ pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op | |||
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 | let directory_with_submodule = submodule_path.parent()?; | 54 | let directory_with_submodule = submodule_path.parent()?; |
55 | match submodule_path.file_name_and_extension()? { | 55 | match submodule_path.name_and_extension()? { |
56 | ("lib", Some("rs")) | ("main", Some("rs")) => None, | 56 | ("lib", Some("rs")) | ("main", Some("rs")) => None, |
57 | ("mod", Some("rs")) => { | 57 | ("mod", Some("rs")) => { |
58 | if directory_with_submodule.parent()? == directory_to_look_for_submodules { | 58 | if directory_with_submodule.parent()? == directory_to_look_for_submodules { |
59 | match directory_with_submodule.file_name_and_extension()? { | 59 | match directory_with_submodule.name_and_extension()? { |
60 | (directory_name, None) => Some(directory_name.to_owned()), | 60 | (directory_name, None) => Some(directory_name.to_owned()), |
61 | _ => None, | 61 | _ => None, |
62 | } | 62 | } |
@@ -93,7 +93,7 @@ fn directory_to_look_for_submodules( | |||
93 | module_file_path: &VfsPath, | 93 | module_file_path: &VfsPath, |
94 | ) -> Option<VfsPath> { | 94 | ) -> Option<VfsPath> { |
95 | let directory_with_module_path = module_file_path.parent()?; | 95 | let directory_with_module_path = module_file_path.parent()?; |
96 | let base_directory = match module_file_path.file_name_and_extension()? { | 96 | let base_directory = match module_file_path.name_and_extension()? { |
97 | ("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => { | 97 | ("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => { |
98 | Some(directory_with_module_path) | 98 | Some(directory_with_module_path) |
99 | } | 99 | } |
@@ -103,8 +103,8 @@ fn directory_to_look_for_submodules( | |||
103 | directory_with_module_path | 103 | directory_with_module_path |
104 | .parent() | 104 | .parent() |
105 | .as_ref() | 105 | .as_ref() |
106 | .and_then(|path| path.file_name_and_extension()), | 106 | .and_then(|path| path.name_and_extension()), |
107 | directory_with_module_path.file_name_and_extension(), | 107 | directory_with_module_path.name_and_extension(), |
108 | ), | 108 | ), |
109 | (Some(("src", None)), Some(("bin", None))) | 109 | (Some(("src", None)), Some(("bin", None))) |
110 | ) { | 110 | ) { |
diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs index dac8393ea..022a0be1e 100644 --- a/crates/vfs/src/vfs_path.rs +++ b/crates/vfs/src/vfs_path.rs | |||
@@ -57,13 +57,13 @@ impl VfsPath { | |||
57 | } | 57 | } |
58 | } | 58 | } |
59 | 59 | ||
60 | pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> { | 60 | pub fn name_and_extension(&self) -> Option<(&str, Option<&str>)> { |
61 | match &self.0 { | 61 | match &self.0 { |
62 | VfsPathRepr::PathBuf(p) => Some(( | 62 | VfsPathRepr::PathBuf(p) => Some(( |
63 | p.file_stem()?.to_str()?, | 63 | p.file_stem()?.to_str()?, |
64 | p.extension().and_then(|extension| extension.to_str()), | 64 | p.extension().and_then(|extension| extension.to_str()), |
65 | )), | 65 | )), |
66 | VfsPathRepr::VirtualPath(p) => p.file_name_and_extension(), | 66 | VfsPathRepr::VirtualPath(p) => p.name_and_extension(), |
67 | } | 67 | } |
68 | } | 68 | } |
69 | 69 | ||
@@ -287,9 +287,7 @@ impl VirtualPath { | |||
287 | Some(res) | 287 | Some(res) |
288 | } | 288 | } |
289 | 289 | ||
290 | // FIXME: Currently VirtualPath does is unable to distinguish a directory from a file | 290 | pub fn name_and_extension(&self) -> Option<(&str, Option<&str>)> { |
291 | // hence this method will return `Some("directory_name", None)` for a directory | ||
292 | pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> { | ||
293 | let file_path = if self.0.ends_with('/') { &self.0[..&self.0.len() - 1] } else { &self.0 }; | 291 | let file_path = if self.0.ends_with('/') { &self.0[..&self.0.len() - 1] } else { &self.0 }; |
294 | let file_name = match file_path.rfind('/') { | 292 | let file_name = match file_path.rfind('/') { |
295 | Some(position) => &file_path[position + 1..], | 293 | Some(position) => &file_path[position + 1..], |
@@ -318,29 +316,29 @@ mod tests { | |||
318 | 316 | ||
319 | #[test] | 317 | #[test] |
320 | fn virtual_path_extensions() { | 318 | fn virtual_path_extensions() { |
321 | assert_eq!(VirtualPath("/".to_string()).file_name_and_extension(), None); | 319 | assert_eq!(VirtualPath("/".to_string()).name_and_extension(), None); |
322 | assert_eq!( | 320 | assert_eq!( |
323 | VirtualPath("/directory".to_string()).file_name_and_extension(), | 321 | VirtualPath("/directory".to_string()).name_and_extension(), |
324 | Some(("directory", None)) | 322 | Some(("directory", None)) |
325 | ); | 323 | ); |
326 | assert_eq!( | 324 | assert_eq!( |
327 | VirtualPath("/directory/".to_string()).file_name_and_extension(), | 325 | VirtualPath("/directory/".to_string()).name_and_extension(), |
328 | Some(("directory", None)) | 326 | Some(("directory", None)) |
329 | ); | 327 | ); |
330 | assert_eq!( | 328 | assert_eq!( |
331 | VirtualPath("/directory/file".to_string()).file_name_and_extension(), | 329 | VirtualPath("/directory/file".to_string()).name_and_extension(), |
332 | Some(("file", None)) | 330 | Some(("file", None)) |
333 | ); | 331 | ); |
334 | assert_eq!( | 332 | assert_eq!( |
335 | VirtualPath("/directory/.file".to_string()).file_name_and_extension(), | 333 | VirtualPath("/directory/.file".to_string()).name_and_extension(), |
336 | Some((".file", None)) | 334 | Some((".file", None)) |
337 | ); | 335 | ); |
338 | assert_eq!( | 336 | assert_eq!( |
339 | VirtualPath("/directory/.file.rs".to_string()).file_name_and_extension(), | 337 | VirtualPath("/directory/.file.rs".to_string()).name_and_extension(), |
340 | Some((".file", Some("rs"))) | 338 | Some((".file", Some("rs"))) |
341 | ); | 339 | ); |
342 | assert_eq!( | 340 | assert_eq!( |
343 | VirtualPath("/directory/file.rs".to_string()).file_name_and_extension(), | 341 | VirtualPath("/directory/file.rs".to_string()).name_and_extension(), |
344 | Some(("file", Some("rs"))) | 342 | Some(("file", Some("rs"))) |
345 | ); | 343 | ); |
346 | } | 344 | } |