Add PathExt
trait (#2823)
This PR adds a `PathExt` trait. It pulls in our existing `compact()` function, as a method, and then adds a method, and testing, for `icon_suffix()`. A test was added to fix: - https://github.com/zed-industries/community/issues/1877 Release Notes: - Fixed a bug where file icons would not be registered for files with with `.` characters in their name ([#1877](https://github.com/zed-industries/community/issues/1877)).
This commit is contained in:
parent
ad4fd7619b
commit
ee1b4a52cc
5 changed files with 88 additions and 56 deletions
|
@ -30,49 +30,47 @@ pub mod legacy {
|
|||
}
|
||||
}
|
||||
|
||||
/// Compacts a given file path by replacing the user's home directory
|
||||
/// prefix with a tilde (`~`).
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `path` - A reference to a `Path` representing the file path to compact.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::path::{Path, PathBuf};
|
||||
/// use util::paths::compact;
|
||||
/// let path: PathBuf = [
|
||||
/// util::paths::HOME.to_string_lossy().to_string(),
|
||||
/// "some_file.txt".to_string(),
|
||||
/// ]
|
||||
/// .iter()
|
||||
/// .collect();
|
||||
/// if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
|
||||
/// assert_eq!(compact(&path).to_str(), Some("~/some_file.txt"));
|
||||
/// } else {
|
||||
/// assert_eq!(compact(&path).to_str(), path.to_str());
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * A `PathBuf` containing the compacted file path. If the input path
|
||||
/// does not have the user's home directory prefix, or if we are not on
|
||||
/// Linux or macOS, the original path is returned unchanged.
|
||||
pub fn compact(path: &Path) -> PathBuf {
|
||||
if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
|
||||
match path.strip_prefix(HOME.as_path()) {
|
||||
Ok(relative_path) => {
|
||||
let mut shortened_path = PathBuf::new();
|
||||
shortened_path.push("~");
|
||||
shortened_path.push(relative_path);
|
||||
shortened_path
|
||||
pub trait PathExt {
|
||||
fn compact(&self) -> PathBuf;
|
||||
fn icon_suffix(&self) -> Option<&str>;
|
||||
}
|
||||
|
||||
impl<T: AsRef<Path>> PathExt for T {
|
||||
/// Compacts a given file path by replacing the user's home directory
|
||||
/// prefix with a tilde (`~`).
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * A `PathBuf` containing the compacted file path. If the input path
|
||||
/// does not have the user's home directory prefix, or if we are not on
|
||||
/// Linux or macOS, the original path is returned unchanged.
|
||||
fn compact(&self) -> PathBuf {
|
||||
if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
|
||||
match self.as_ref().strip_prefix(HOME.as_path()) {
|
||||
Ok(relative_path) => {
|
||||
let mut shortened_path = PathBuf::new();
|
||||
shortened_path.push("~");
|
||||
shortened_path.push(relative_path);
|
||||
shortened_path
|
||||
}
|
||||
Err(_) => self.as_ref().to_path_buf(),
|
||||
}
|
||||
Err(_) => path.to_path_buf(),
|
||||
} else {
|
||||
self.as_ref().to_path_buf()
|
||||
}
|
||||
} else {
|
||||
path.to_path_buf()
|
||||
}
|
||||
|
||||
fn icon_suffix(&self) -> Option<&str> {
|
||||
let file_name = self.as_ref().file_name()?.to_str()?;
|
||||
|
||||
if file_name.starts_with(".") {
|
||||
return file_name.strip_prefix(".");
|
||||
}
|
||||
|
||||
self.as_ref()
|
||||
.extension()
|
||||
.map(|extension| extension.to_str())
|
||||
.flatten()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -279,4 +277,42 @@ mod tests {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_path_compact() {
|
||||
let path: PathBuf = [
|
||||
HOME.to_string_lossy().to_string(),
|
||||
"some_file.txt".to_string(),
|
||||
]
|
||||
.iter()
|
||||
.collect();
|
||||
if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
|
||||
assert_eq!(path.compact().to_str(), Some("~/some_file.txt"));
|
||||
} else {
|
||||
assert_eq!(path.compact().to_str(), path.to_str());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_path_suffix() {
|
||||
// No dots in name
|
||||
let path = Path::new("/a/b/c/file_name.rs");
|
||||
assert_eq!(path.icon_suffix(), Some("rs"));
|
||||
|
||||
// Single dot in name
|
||||
let path = Path::new("/a/b/c/file.name.rs");
|
||||
assert_eq!(path.icon_suffix(), Some("rs"));
|
||||
|
||||
// Multiple dots in name
|
||||
let path = Path::new("/a/b/c/long.file.name.rs");
|
||||
assert_eq!(path.icon_suffix(), Some("rs"));
|
||||
|
||||
// Hidden file, no extension
|
||||
let path = Path::new("/a/b/c/.gitignore");
|
||||
assert_eq!(path.icon_suffix(), Some("gitignore"));
|
||||
|
||||
// Hidden file, with extension
|
||||
let path = Path::new("/a/b/c/.eslintrc.js");
|
||||
assert_eq!(path.icon_suffix(), Some("eslintrc.js"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue