Fix #24314 - File icons missing for hidden files (#24391)

- **fix ignoring ignored files when matching icons**
- **remove poorly named and confusing method
`PathExt.icon_stem_or_suffix` and refactor
`PathExt.extension_or_hidden_file_name` to actually do what it says it
does**

Closes #24314

Release Notes:

- Fixed an issue where hidden files would have the default icon instead
of the correct one
- Fixed an issue where files with specific icons (such as
`eslint.config.js`) would not have the their specific icon without a
leading `.` (`.eslint.config.js`)
This commit is contained in:
Ben Kunkle 2025-02-06 14:26:42 -06:00 committed by GitHub
parent ad46c5b567
commit 9c132fece5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 52 deletions

View file

@ -21,7 +21,6 @@ pub fn home_dir() -> &'static PathBuf {
pub trait PathExt {
fn compact(&self) -> PathBuf;
fn icon_stem_or_suffix(&self) -> Option<&str>;
fn extension_or_hidden_file_name(&self) -> Option<&str>;
fn to_sanitized_string(&self) -> String;
fn try_from_bytes<'a>(bytes: &'a [u8]) -> anyhow::Result<Self>
@ -74,8 +73,8 @@ impl<T: AsRef<Path>> PathExt for T {
}
}
/// Returns either the suffix if available, or the file stem otherwise to determine which file icon to use
fn icon_stem_or_suffix(&self) -> Option<&str> {
/// Returns a file's extension or, if the file is hidden, its name without the leading dot
fn extension_or_hidden_file_name(&self) -> Option<&str> {
let path = self.as_ref();
let file_name = path.file_name()?.to_str()?;
if file_name.starts_with('.') {
@ -87,15 +86,6 @@ impl<T: AsRef<Path>> PathExt for T {
.or_else(|| path.file_stem()?.to_str())
}
/// Returns a file's extension or, if the file is hidden, its name without the leading dot
fn extension_or_hidden_file_name(&self) -> Option<&str> {
if let Some(extension) = self.as_ref().extension() {
return extension.to_str();
}
self.as_ref().file_name()?.to_str()?.split('.').last()
}
/// Returns a sanitized string representation of the path.
/// Note, on Windows, this assumes that the path is a valid UTF-8 string and
/// is not a UNC path.
@ -811,33 +801,6 @@ mod tests {
}
}
#[test]
fn test_icon_stem_or_suffix() {
// No dots in name
let path = Path::new("/a/b/c/file_name.rs");
assert_eq!(path.icon_stem_or_suffix(), Some("rs"));
// Single dot in name
let path = Path::new("/a/b/c/file.name.rs");
assert_eq!(path.icon_stem_or_suffix(), Some("rs"));
// No suffix
let path = Path::new("/a/b/c/file");
assert_eq!(path.icon_stem_or_suffix(), Some("file"));
// Multiple dots in name
let path = Path::new("/a/b/c/long.file.name.rs");
assert_eq!(path.icon_stem_or_suffix(), Some("rs"));
// Hidden file, no extension
let path = Path::new("/a/b/c/.gitignore");
assert_eq!(path.icon_stem_or_suffix(), Some("gitignore"));
// Hidden file, with extension
let path = Path::new("/a/b/c/.eslintrc.js");
assert_eq!(path.icon_stem_or_suffix(), Some("eslintrc.js"));
}
#[test]
fn test_extension_or_hidden_file_name() {
// No dots in name
@ -858,7 +821,7 @@ mod tests {
// Hidden file, with extension
let path = Path::new("/a/b/c/.eslintrc.js");
assert_eq!(path.extension_or_hidden_file_name(), Some("js"));
assert_eq!(path.extension_or_hidden_file_name(), Some("eslintrc.js"));
}
#[test]