Rename the method to avoid false promises

This commit is contained in:
Kirill Bulatov 2020-09-10 01:45:49 +03:00
parent f4ee885b3b
commit 9863798480
2 changed files with 15 additions and 17 deletions

View file

@ -52,11 +52,11 @@ pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
.filter_map(|submodule_file| { .filter_map(|submodule_file| {
let submodule_path = source_root.path_for_file(&submodule_file)?; let submodule_path = source_root.path_for_file(&submodule_file)?;
let directory_with_submodule = submodule_path.parent()?; let directory_with_submodule = submodule_path.parent()?;
match submodule_path.file_name_and_extension()? { match submodule_path.name_and_extension()? {
("lib", Some("rs")) | ("main", Some("rs")) => None, ("lib", Some("rs")) | ("main", Some("rs")) => None,
("mod", Some("rs")) => { ("mod", Some("rs")) => {
if directory_with_submodule.parent()? == directory_to_look_for_submodules { if directory_with_submodule.parent()? == directory_to_look_for_submodules {
match directory_with_submodule.file_name_and_extension()? { match directory_with_submodule.name_and_extension()? {
(directory_name, None) => Some(directory_name.to_owned()), (directory_name, None) => Some(directory_name.to_owned()),
_ => None, _ => None,
} }
@ -93,7 +93,7 @@ fn directory_to_look_for_submodules(
module_file_path: &VfsPath, module_file_path: &VfsPath,
) -> Option<VfsPath> { ) -> Option<VfsPath> {
let directory_with_module_path = module_file_path.parent()?; let directory_with_module_path = module_file_path.parent()?;
let base_directory = match module_file_path.file_name_and_extension()? { let base_directory = match module_file_path.name_and_extension()? {
("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => { ("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => {
Some(directory_with_module_path) Some(directory_with_module_path)
} }
@ -103,8 +103,8 @@ fn directory_to_look_for_submodules(
directory_with_module_path directory_with_module_path
.parent() .parent()
.as_ref() .as_ref()
.and_then(|path| path.file_name_and_extension()), .and_then(|path| path.name_and_extension()),
directory_with_module_path.file_name_and_extension(), directory_with_module_path.name_and_extension(),
), ),
(Some(("src", None)), Some(("bin", None))) (Some(("src", None)), Some(("bin", None)))
) { ) {

View file

@ -57,13 +57,13 @@ impl VfsPath {
} }
} }
pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> { pub fn name_and_extension(&self) -> Option<(&str, Option<&str>)> {
match &self.0 { match &self.0 {
VfsPathRepr::PathBuf(p) => Some(( VfsPathRepr::PathBuf(p) => Some((
p.file_stem()?.to_str()?, p.file_stem()?.to_str()?,
p.extension().and_then(|extension| extension.to_str()), p.extension().and_then(|extension| extension.to_str()),
)), )),
VfsPathRepr::VirtualPath(p) => p.file_name_and_extension(), VfsPathRepr::VirtualPath(p) => p.name_and_extension(),
} }
} }
@ -287,9 +287,7 @@ impl VirtualPath {
Some(res) Some(res)
} }
// FIXME: Currently VirtualPath does is unable to distinguish a directory from a file pub fn name_and_extension(&self) -> Option<(&str, Option<&str>)> {
// hence this method will return `Some("directory_name", None)` for a directory
pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> {
let file_path = if self.0.ends_with('/') { &self.0[..&self.0.len() - 1] } else { &self.0 }; let file_path = if self.0.ends_with('/') { &self.0[..&self.0.len() - 1] } else { &self.0 };
let file_name = match file_path.rfind('/') { let file_name = match file_path.rfind('/') {
Some(position) => &file_path[position + 1..], Some(position) => &file_path[position + 1..],
@ -318,29 +316,29 @@ mod tests {
#[test] #[test]
fn virtual_path_extensions() { fn virtual_path_extensions() {
assert_eq!(VirtualPath("/".to_string()).file_name_and_extension(), None); assert_eq!(VirtualPath("/".to_string()).name_and_extension(), None);
assert_eq!( assert_eq!(
VirtualPath("/directory".to_string()).file_name_and_extension(), VirtualPath("/directory".to_string()).name_and_extension(),
Some(("directory", None)) Some(("directory", None))
); );
assert_eq!( assert_eq!(
VirtualPath("/directory/".to_string()).file_name_and_extension(), VirtualPath("/directory/".to_string()).name_and_extension(),
Some(("directory", None)) Some(("directory", None))
); );
assert_eq!( assert_eq!(
VirtualPath("/directory/file".to_string()).file_name_and_extension(), VirtualPath("/directory/file".to_string()).name_and_extension(),
Some(("file", None)) Some(("file", None))
); );
assert_eq!( assert_eq!(
VirtualPath("/directory/.file".to_string()).file_name_and_extension(), VirtualPath("/directory/.file".to_string()).name_and_extension(),
Some((".file", None)) Some((".file", None))
); );
assert_eq!( assert_eq!(
VirtualPath("/directory/.file.rs".to_string()).file_name_and_extension(), VirtualPath("/directory/.file.rs".to_string()).name_and_extension(),
Some((".file", Some("rs"))) Some((".file", Some("rs")))
); );
assert_eq!( assert_eq!(
VirtualPath("/directory/file.rs".to_string()).file_name_and_extension(), VirtualPath("/directory/file.rs".to_string()).name_and_extension(),
Some(("file", Some("rs"))) Some(("file", Some("rs")))
); );
} }