mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 13:18:47 +00:00
Implement file name & extension retrieval method for VirtualPath
This commit is contained in:
parent
d163f9f114
commit
897a4c702e
1 changed files with 20 additions and 2 deletions
|
@ -287,8 +287,26 @@ impl VirtualPath {
|
||||||
Some(res)
|
Some(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: Currently VirtualPath does is unable to distinguish a directory from a file
|
||||||
|
// hence this method will return `Some("directory_name", None)` for a directory
|
||||||
pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> {
|
pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> {
|
||||||
// TODO kb check if is a file
|
let file_name = match self.0.rfind('/') {
|
||||||
Some(("test_mod_1", Some("rs")))
|
Some(position) => &self.0[position + 1..],
|
||||||
|
None => &self.0,
|
||||||
|
};
|
||||||
|
|
||||||
|
if file_name.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
let mut file_stem_and_extension = file_name.rsplitn(2, '.');
|
||||||
|
let extension = file_stem_and_extension.next();
|
||||||
|
let file_stem = file_stem_and_extension.next();
|
||||||
|
|
||||||
|
match (file_stem, extension) {
|
||||||
|
(None, None) => None,
|
||||||
|
(None, Some(_)) | (Some(""), Some(_)) => Some((file_name, None)),
|
||||||
|
(Some(file_stem), extension) => Some((file_stem, extension)),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue