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
|
@ -28,7 +28,10 @@ use std::{
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
use text::Selection;
|
use text::Selection;
|
||||||
use util::{paths::FILE_ROW_COLUMN_DELIMITER, ResultExt, TryFutureExt};
|
use util::{
|
||||||
|
paths::{PathExt, FILE_ROW_COLUMN_DELIMITER},
|
||||||
|
ResultExt, TryFutureExt,
|
||||||
|
};
|
||||||
use workspace::item::{BreadcrumbText, FollowableItemHandle};
|
use workspace::item::{BreadcrumbText, FollowableItemHandle};
|
||||||
use workspace::{
|
use workspace::{
|
||||||
item::{FollowableItem, Item, ItemEvent, ItemHandle, ProjectItem},
|
item::{FollowableItem, Item, ItemEvent, ItemHandle, ProjectItem},
|
||||||
|
@ -546,9 +549,7 @@ impl Item for Editor {
|
||||||
.and_then(|f| f.as_local())?
|
.and_then(|f| f.as_local())?
|
||||||
.abs_path(cx);
|
.abs_path(cx);
|
||||||
|
|
||||||
let file_path = util::paths::compact(&file_path)
|
let file_path = file_path.compact().to_string_lossy().to_string();
|
||||||
.to_string_lossy()
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
Some(file_path.into())
|
Some(file_path.into())
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ use collections::HashMap;
|
||||||
|
|
||||||
use gpui::{AppContext, AssetSource};
|
use gpui::{AppContext, AssetSource};
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
use util::iife;
|
use util::{iife, paths::PathExt};
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct TypeConfig {
|
struct TypeConfig {
|
||||||
|
@ -48,14 +48,7 @@ impl FileAssociations {
|
||||||
// FIXME: Associate a type with the languages and have the file's langauge
|
// FIXME: Associate a type with the languages and have the file's langauge
|
||||||
// override these associations
|
// override these associations
|
||||||
iife!({
|
iife!({
|
||||||
let suffix = path
|
let suffix = path.icon_suffix()?;
|
||||||
.file_name()
|
|
||||||
.and_then(|os_str| os_str.to_str())
|
|
||||||
.and_then(|file_name| {
|
|
||||||
file_name
|
|
||||||
.find('.')
|
|
||||||
.and_then(|dot_index| file_name.get(dot_index + 1..))
|
|
||||||
})?;
|
|
||||||
|
|
||||||
this.suffixes
|
this.suffixes
|
||||||
.get(suffix)
|
.get(suffix)
|
||||||
|
|
|
@ -5,6 +5,7 @@ use gpui::{
|
||||||
elements::{Label, LabelStyle},
|
elements::{Label, LabelStyle},
|
||||||
AnyElement, Element, View,
|
AnyElement, Element, View,
|
||||||
};
|
};
|
||||||
|
use util::paths::PathExt;
|
||||||
use workspace::WorkspaceLocation;
|
use workspace::WorkspaceLocation;
|
||||||
|
|
||||||
pub struct HighlightedText {
|
pub struct HighlightedText {
|
||||||
|
@ -61,7 +62,7 @@ impl HighlightedWorkspaceLocation {
|
||||||
.paths()
|
.paths()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|path| {
|
.map(|path| {
|
||||||
let path = util::paths::compact(&path);
|
let path = path.compact();
|
||||||
let highlighted_text = Self::highlights_for_path(
|
let highlighted_text = Self::highlights_for_path(
|
||||||
path.as_ref(),
|
path.as_ref(),
|
||||||
&string_match.positions,
|
&string_match.positions,
|
||||||
|
|
|
@ -11,6 +11,7 @@ use highlighted_workspace_location::HighlightedWorkspaceLocation;
|
||||||
use ordered_float::OrderedFloat;
|
use ordered_float::OrderedFloat;
|
||||||
use picker::{Picker, PickerDelegate, PickerEvent};
|
use picker::{Picker, PickerDelegate, PickerEvent};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use util::paths::PathExt;
|
||||||
use workspace::{
|
use workspace::{
|
||||||
notifications::simple_message_notification::MessageNotification, Workspace, WorkspaceLocation,
|
notifications::simple_message_notification::MessageNotification, Workspace, WorkspaceLocation,
|
||||||
WORKSPACE_DB,
|
WORKSPACE_DB,
|
||||||
|
@ -134,7 +135,7 @@ impl PickerDelegate for RecentProjectsDelegate {
|
||||||
let combined_string = location
|
let combined_string = location
|
||||||
.paths()
|
.paths()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|path| util::paths::compact(&path).to_string_lossy().into_owned())
|
.map(|path| path.compact().to_string_lossy().into_owned())
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("");
|
.join("");
|
||||||
StringMatchCandidate::new(id, combined_string)
|
StringMatchCandidate::new(id, combined_string)
|
||||||
|
|
|
@ -30,49 +30,47 @@ pub mod legacy {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
/// Compacts a given file path by replacing the user's home directory
|
||||||
/// prefix with a tilde (`~`).
|
/// 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
|
/// # Returns
|
||||||
///
|
///
|
||||||
/// * A `PathBuf` containing the compacted file path. If the input path
|
/// * 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
|
/// does not have the user's home directory prefix, or if we are not on
|
||||||
/// Linux or macOS, the original path is returned unchanged.
|
/// Linux or macOS, the original path is returned unchanged.
|
||||||
pub fn compact(path: &Path) -> PathBuf {
|
fn compact(&self) -> PathBuf {
|
||||||
if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
|
if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
|
||||||
match path.strip_prefix(HOME.as_path()) {
|
match self.as_ref().strip_prefix(HOME.as_path()) {
|
||||||
Ok(relative_path) => {
|
Ok(relative_path) => {
|
||||||
let mut shortened_path = PathBuf::new();
|
let mut shortened_path = PathBuf::new();
|
||||||
shortened_path.push("~");
|
shortened_path.push("~");
|
||||||
shortened_path.push(relative_path);
|
shortened_path.push(relative_path);
|
||||||
shortened_path
|
shortened_path
|
||||||
}
|
}
|
||||||
Err(_) => path.to_path_buf(),
|
Err(_) => self.as_ref().to_path_buf(),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
path.to_path_buf()
|
self.as_ref().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