paths.rs

  1use std::cmp;
  2use std::sync::OnceLock;
  3use std::{
  4    ffi::OsStr,
  5    path::{Path, PathBuf},
  6    sync::LazyLock,
  7};
  8
  9use globset::{Glob, GlobSet, GlobSetBuilder};
 10use regex::Regex;
 11use serde::{Deserialize, Serialize};
 12
 13use crate::NumericPrefixWithSuffix;
 14
 15/// Returns the path to the user's home directory.
 16pub fn home_dir() -> &'static PathBuf {
 17    static HOME_DIR: OnceLock<PathBuf> = OnceLock::new();
 18    HOME_DIR.get_or_init(|| dirs::home_dir().expect("failed to determine home directory"))
 19}
 20
 21pub trait PathExt {
 22    fn compact(&self) -> PathBuf;
 23    fn icon_stem_or_suffix(&self) -> Option<&str>;
 24    fn extension_or_hidden_file_name(&self) -> Option<&str>;
 25    fn try_from_bytes<'a>(bytes: &'a [u8]) -> anyhow::Result<Self>
 26    where
 27        Self: From<&'a Path>,
 28    {
 29        #[cfg(unix)]
 30        {
 31            use std::os::unix::prelude::OsStrExt;
 32            Ok(Self::from(Path::new(OsStr::from_bytes(bytes))))
 33        }
 34        #[cfg(windows)]
 35        {
 36            use anyhow::anyhow;
 37            use tendril::fmt::{Format, WTF8};
 38            WTF8::validate(bytes)
 39                .then(|| {
 40                    // Safety: bytes are valid WTF-8 sequence.
 41                    Self::from(Path::new(unsafe {
 42                        OsStr::from_encoded_bytes_unchecked(bytes)
 43                    }))
 44                })
 45                .ok_or_else(|| anyhow!("Invalid WTF-8 sequence: {bytes:?}"))
 46        }
 47    }
 48}
 49
 50impl<T: AsRef<Path>> PathExt for T {
 51    /// Compacts a given file path by replacing the user's home directory
 52    /// prefix with a tilde (`~`).
 53    ///
 54    /// # Returns
 55    ///
 56    /// * A `PathBuf` containing the compacted file path. If the input path
 57    ///   does not have the user's home directory prefix, or if we are not on
 58    ///   Linux or macOS, the original path is returned unchanged.
 59    fn compact(&self) -> PathBuf {
 60        if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
 61            match self.as_ref().strip_prefix(home_dir().as_path()) {
 62                Ok(relative_path) => {
 63                    let mut shortened_path = PathBuf::new();
 64                    shortened_path.push("~");
 65                    shortened_path.push(relative_path);
 66                    shortened_path
 67                }
 68                Err(_) => self.as_ref().to_path_buf(),
 69            }
 70        } else {
 71            self.as_ref().to_path_buf()
 72        }
 73    }
 74
 75    /// Returns either the suffix if available, or the file stem otherwise to determine which file icon to use
 76    fn icon_stem_or_suffix(&self) -> Option<&str> {
 77        let path = self.as_ref();
 78        let file_name = path.file_name()?.to_str()?;
 79        if file_name.starts_with('.') {
 80            return file_name.strip_prefix('.');
 81        }
 82
 83        path.extension()
 84            .and_then(|e| e.to_str())
 85            .or_else(|| path.file_stem()?.to_str())
 86    }
 87
 88    /// Returns a file's extension or, if the file is hidden, its name without the leading dot
 89    fn extension_or_hidden_file_name(&self) -> Option<&str> {
 90        if let Some(extension) = self.as_ref().extension() {
 91            return extension.to_str();
 92        }
 93
 94        self.as_ref().file_name()?.to_str()?.split('.').last()
 95    }
 96}
 97
 98/// A delimiter to use in `path_query:row_number:column_number` strings parsing.
 99pub const FILE_ROW_COLUMN_DELIMITER: char = ':';
100
101const ROW_COL_CAPTURE_REGEX: &str = r"(?x)
102    ([^\(]+)(?:
103        \((\d+),(\d+)\) # filename(row,column)
104        |
105        \((\d+)\)()     # filename(row)
106    )
107    |
108    (.+?)(?:
109        \:+(\d+)\:(\d+)\:*$  # filename:row:column
110        |
111        \:+(\d+)\:*()$       # filename:row
112        |
113        \:*()()$             # filename:
114    )";
115
116/// A representation of a path-like string with optional row and column numbers.
117/// Matching values example: `te`, `test.rs:22`, `te:22:5`, `test.c(22)`, `test.c(22,5)`etc.
118#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
119pub struct PathWithPosition {
120    pub path: PathBuf,
121    pub row: Option<u32>,
122    // Absent if row is absent.
123    pub column: Option<u32>,
124}
125
126impl PathWithPosition {
127    /// Returns a PathWithPosition from a path.
128    pub fn from_path(path: PathBuf) -> Self {
129        Self {
130            path,
131            row: None,
132            column: None,
133        }
134    }
135
136    /// Parses a string that possibly has `:row:column` or `(row, column)` suffix.
137    /// Parenthesis format is used by [MSBuild](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-diagnostic-format-for-tasks) compatible tools
138    /// Ignores trailing `:`s, so `test.rs:22:` is parsed as `test.rs:22`.
139    /// If the suffix parsing fails, the whole string is parsed as a path.
140    ///
141    /// Be mindful that `test_file:10:1:` is a valid posix filename.
142    /// `PathWithPosition` class assumes that the ending position-like suffix is **not** part of the filename.
143    ///
144    /// # Examples
145    ///
146    /// ```
147    /// # use util::paths::PathWithPosition;
148    /// # use std::path::PathBuf;
149    /// assert_eq!(PathWithPosition::parse_str("test_file"), PathWithPosition {
150    ///     path: PathBuf::from("test_file"),
151    ///     row: None,
152    ///     column: None,
153    /// });
154    /// assert_eq!(PathWithPosition::parse_str("test_file:10"), PathWithPosition {
155    ///     path: PathBuf::from("test_file"),
156    ///     row: Some(10),
157    ///     column: None,
158    /// });
159    /// assert_eq!(PathWithPosition::parse_str("test_file.rs"), PathWithPosition {
160    ///     path: PathBuf::from("test_file.rs"),
161    ///     row: None,
162    ///     column: None,
163    /// });
164    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1"), PathWithPosition {
165    ///     path: PathBuf::from("test_file.rs"),
166    ///     row: Some(1),
167    ///     column: None,
168    /// });
169    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1:2"), PathWithPosition {
170    ///     path: PathBuf::from("test_file.rs"),
171    ///     row: Some(1),
172    ///     column: Some(2),
173    /// });
174    /// ```
175    ///
176    /// # Expected parsing results when encounter ill-formatted inputs.
177    /// ```
178    /// # use util::paths::PathWithPosition;
179    /// # use std::path::PathBuf;
180    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:a"), PathWithPosition {
181    ///     path: PathBuf::from("test_file.rs:a"),
182    ///     row: None,
183    ///     column: None,
184    /// });
185    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:a:b"), PathWithPosition {
186    ///     path: PathBuf::from("test_file.rs:a:b"),
187    ///     row: None,
188    ///     column: None,
189    /// });
190    /// assert_eq!(PathWithPosition::parse_str("test_file.rs::"), PathWithPosition {
191    ///     path: PathBuf::from("test_file.rs"),
192    ///     row: None,
193    ///     column: None,
194    /// });
195    /// assert_eq!(PathWithPosition::parse_str("test_file.rs::1"), PathWithPosition {
196    ///     path: PathBuf::from("test_file.rs"),
197    ///     row: Some(1),
198    ///     column: None,
199    /// });
200    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1::"), PathWithPosition {
201    ///     path: PathBuf::from("test_file.rs"),
202    ///     row: Some(1),
203    ///     column: None,
204    /// });
205    /// assert_eq!(PathWithPosition::parse_str("test_file.rs::1:2"), PathWithPosition {
206    ///     path: PathBuf::from("test_file.rs"),
207    ///     row: Some(1),
208    ///     column: Some(2),
209    /// });
210    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1::2"), PathWithPosition {
211    ///     path: PathBuf::from("test_file.rs:1"),
212    ///     row: Some(2),
213    ///     column: None,
214    /// });
215    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1:2:3"), PathWithPosition {
216    ///     path: PathBuf::from("test_file.rs:1"),
217    ///     row: Some(2),
218    ///     column: Some(3),
219    /// });
220    /// ```
221    pub fn parse_str(s: &str) -> Self {
222        let trimmed = s.trim();
223        let path = Path::new(trimmed);
224        let maybe_file_name_with_row_col = path
225            .file_name()
226            .unwrap_or_default()
227            .to_str()
228            .unwrap_or_default();
229        if maybe_file_name_with_row_col.is_empty() {
230            return Self {
231                path: Path::new(s).to_path_buf(),
232                row: None,
233                column: None,
234            };
235        }
236
237        // Let's avoid repeated init cost on this. It is subject to thread contention, but
238        // so far this code isn't called from multiple hot paths. Getting contention here
239        // in the future seems unlikely.
240        static SUFFIX_RE: LazyLock<Regex> =
241            LazyLock::new(|| Regex::new(ROW_COL_CAPTURE_REGEX).unwrap());
242        match SUFFIX_RE
243            .captures(maybe_file_name_with_row_col)
244            .map(|caps| caps.extract())
245        {
246            Some((_, [file_name, maybe_row, maybe_column])) => {
247                let row = maybe_row.parse::<u32>().ok();
248                let column = maybe_column.parse::<u32>().ok();
249
250                let suffix_length = maybe_file_name_with_row_col.len() - file_name.len();
251                let path_without_suffix = &trimmed[..trimmed.len() - suffix_length];
252
253                Self {
254                    path: Path::new(path_without_suffix).to_path_buf(),
255                    row,
256                    column,
257                }
258            }
259            None => Self {
260                path: Path::new(s).to_path_buf(),
261                row: None,
262                column: None,
263            },
264        }
265    }
266
267    pub fn map_path<E>(
268        self,
269        mapping: impl FnOnce(PathBuf) -> Result<PathBuf, E>,
270    ) -> Result<PathWithPosition, E> {
271        Ok(PathWithPosition {
272            path: mapping(self.path)?,
273            row: self.row,
274            column: self.column,
275        })
276    }
277
278    pub fn to_string(&self, path_to_string: impl Fn(&PathBuf) -> String) -> String {
279        let path_string = path_to_string(&self.path);
280        if let Some(row) = self.row {
281            if let Some(column) = self.column {
282                format!("{path_string}:{row}:{column}")
283            } else {
284                format!("{path_string}:{row}")
285            }
286        } else {
287            path_string
288        }
289    }
290}
291
292#[derive(Clone, Debug, Default)]
293pub struct PathMatcher {
294    sources: Vec<String>,
295    glob: GlobSet,
296}
297
298// impl std::fmt::Display for PathMatcher {
299//     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
300//         self.sources.fmt(f)
301//     }
302// }
303
304impl PartialEq for PathMatcher {
305    fn eq(&self, other: &Self) -> bool {
306        self.sources.eq(&other.sources)
307    }
308}
309
310impl Eq for PathMatcher {}
311
312impl PathMatcher {
313    pub fn new(globs: &[String]) -> Result<Self, globset::Error> {
314        let globs = globs
315            .iter()
316            .map(|glob| Glob::new(glob))
317            .collect::<Result<Vec<_>, _>>()?;
318        let sources = globs.iter().map(|glob| glob.glob().to_owned()).collect();
319        let mut glob_builder = GlobSetBuilder::new();
320        for single_glob in globs {
321            glob_builder.add(single_glob);
322        }
323        let glob = glob_builder.build()?;
324        Ok(PathMatcher { glob, sources })
325    }
326
327    pub fn sources(&self) -> &[String] {
328        &self.sources
329    }
330
331    pub fn is_match<P: AsRef<Path>>(&self, other: P) -> bool {
332        let other_path = other.as_ref();
333        self.sources.iter().any(|source| {
334            let as_bytes = other_path.as_os_str().as_encoded_bytes();
335            as_bytes.starts_with(source.as_bytes()) || as_bytes.ends_with(source.as_bytes())
336        }) || self.glob.is_match(other_path)
337            || self.check_with_end_separator(other_path)
338    }
339
340    fn check_with_end_separator(&self, path: &Path) -> bool {
341        let path_str = path.to_string_lossy();
342        let separator = std::path::MAIN_SEPARATOR_STR;
343        if path_str.ends_with(separator) {
344            false
345        } else {
346            self.glob.is_match(path_str.to_string() + separator)
347        }
348    }
349}
350
351pub fn compare_paths(
352    (path_a, a_is_file): (&Path, bool),
353    (path_b, b_is_file): (&Path, bool),
354) -> cmp::Ordering {
355    let mut components_a = path_a.components().peekable();
356    let mut components_b = path_b.components().peekable();
357    loop {
358        match (components_a.next(), components_b.next()) {
359            (Some(component_a), Some(component_b)) => {
360                let a_is_file = components_a.peek().is_none() && a_is_file;
361                let b_is_file = components_b.peek().is_none() && b_is_file;
362                let ordering = a_is_file.cmp(&b_is_file).then_with(|| {
363                    let path_a = Path::new(component_a.as_os_str());
364                    let num_and_remainder_a = NumericPrefixWithSuffix::from_numeric_prefixed_str(
365                        if a_is_file {
366                            path_a.file_stem()
367                        } else {
368                            path_a.file_name()
369                        }
370                        .and_then(|s| s.to_str())
371                        .unwrap_or_default(),
372                    );
373
374                    let path_b = Path::new(component_b.as_os_str());
375                    let num_and_remainder_b = NumericPrefixWithSuffix::from_numeric_prefixed_str(
376                        if b_is_file {
377                            path_b.file_stem()
378                        } else {
379                            path_b.file_name()
380                        }
381                        .and_then(|s| s.to_str())
382                        .unwrap_or_default(),
383                    );
384
385                    num_and_remainder_a.cmp(&num_and_remainder_b)
386                });
387                if !ordering.is_eq() {
388                    return ordering;
389                }
390            }
391            (Some(_), None) => break cmp::Ordering::Greater,
392            (None, Some(_)) => break cmp::Ordering::Less,
393            (None, None) => break cmp::Ordering::Equal,
394        }
395    }
396}
397
398#[cfg(test)]
399mod tests {
400    use super::*;
401
402    #[test]
403    fn compare_paths_with_dots() {
404        let mut paths = vec![
405            (Path::new("test_dirs"), false),
406            (Path::new("test_dirs/1.46"), false),
407            (Path::new("test_dirs/1.46/bar_1"), true),
408            (Path::new("test_dirs/1.46/bar_2"), true),
409            (Path::new("test_dirs/1.45"), false),
410            (Path::new("test_dirs/1.45/foo_2"), true),
411            (Path::new("test_dirs/1.45/foo_1"), true),
412        ];
413        paths.sort_by(|&a, &b| compare_paths(a, b));
414        assert_eq!(
415            paths,
416            vec![
417                (Path::new("test_dirs"), false),
418                (Path::new("test_dirs/1.45"), false),
419                (Path::new("test_dirs/1.45/foo_1"), true),
420                (Path::new("test_dirs/1.45/foo_2"), true),
421                (Path::new("test_dirs/1.46"), false),
422                (Path::new("test_dirs/1.46/bar_1"), true),
423                (Path::new("test_dirs/1.46/bar_2"), true),
424            ]
425        );
426        let mut paths = vec![
427            (Path::new("root1/one.txt"), true),
428            (Path::new("root1/one.two.txt"), true),
429        ];
430        paths.sort_by(|&a, &b| compare_paths(a, b));
431        assert_eq!(
432            paths,
433            vec![
434                (Path::new("root1/one.txt"), true),
435                (Path::new("root1/one.two.txt"), true),
436            ]
437        );
438    }
439
440    #[test]
441    fn path_with_position_parse_posix_path() {
442        // Test POSIX filename edge cases
443        // Read more at https://en.wikipedia.org/wiki/Filename
444        assert_eq!(
445            PathWithPosition::parse_str(" test_file"),
446            PathWithPosition {
447                path: PathBuf::from("test_file"),
448                row: None,
449                column: None
450            }
451        );
452
453        assert_eq!(
454            PathWithPosition::parse_str("a:bc:.zip:1"),
455            PathWithPosition {
456                path: PathBuf::from("a:bc:.zip"),
457                row: Some(1),
458                column: None
459            }
460        );
461
462        assert_eq!(
463            PathWithPosition::parse_str("one.second.zip:1"),
464            PathWithPosition {
465                path: PathBuf::from("one.second.zip"),
466                row: Some(1),
467                column: None
468            }
469        );
470
471        // Trim off trailing `:`s for otherwise valid input.
472        assert_eq!(
473            PathWithPosition::parse_str("test_file:10:1:"),
474            PathWithPosition {
475                path: PathBuf::from("test_file"),
476                row: Some(10),
477                column: Some(1)
478            }
479        );
480
481        assert_eq!(
482            PathWithPosition::parse_str("test_file.rs:"),
483            PathWithPosition {
484                path: PathBuf::from("test_file.rs"),
485                row: None,
486                column: None
487            }
488        );
489
490        assert_eq!(
491            PathWithPosition::parse_str("test_file.rs:1:"),
492            PathWithPosition {
493                path: PathBuf::from("test_file.rs"),
494                row: Some(1),
495                column: None
496            }
497        );
498    }
499
500    #[test]
501    #[cfg(not(target_os = "windows"))]
502    fn path_with_position_parse_posix_path_with_suffix() {
503        assert_eq!(
504            PathWithPosition::parse_str("app-editors:zed-0.143.6:20240710-201212.log:34:"),
505            PathWithPosition {
506                path: PathBuf::from("app-editors:zed-0.143.6:20240710-201212.log"),
507                row: Some(34),
508                column: None,
509            }
510        );
511
512        assert_eq!(
513            PathWithPosition::parse_str("crates/file_finder/src/file_finder.rs:1902:13:"),
514            PathWithPosition {
515                path: PathBuf::from("crates/file_finder/src/file_finder.rs"),
516                row: Some(1902),
517                column: Some(13),
518            }
519        );
520
521        assert_eq!(
522            PathWithPosition::parse_str("crate/utils/src/test:today.log:34"),
523            PathWithPosition {
524                path: PathBuf::from("crate/utils/src/test:today.log"),
525                row: Some(34),
526                column: None,
527            }
528        );
529    }
530
531    #[test]
532    #[cfg(target_os = "windows")]
533    fn path_with_position_parse_windows_path() {
534        assert_eq!(
535            PathWithPosition::parse_str("crates\\utils\\paths.rs"),
536            PathWithPosition {
537                path: PathBuf::from("crates\\utils\\paths.rs"),
538                row: None,
539                column: None
540            }
541        );
542
543        assert_eq!(
544            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs"),
545            PathWithPosition {
546                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
547                row: None,
548                column: None
549            }
550        );
551    }
552
553    #[test]
554    #[cfg(target_os = "windows")]
555    fn path_with_position_parse_windows_path_with_suffix() {
556        assert_eq!(
557            PathWithPosition::parse_str("crates\\utils\\paths.rs:101"),
558            PathWithPosition {
559                path: PathBuf::from("crates\\utils\\paths.rs"),
560                row: Some(101),
561                column: None
562            }
563        );
564
565        assert_eq!(
566            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1:20"),
567            PathWithPosition {
568                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
569                row: Some(1),
570                column: Some(20)
571            }
572        );
573
574        assert_eq!(
575            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13)"),
576            PathWithPosition {
577                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
578                row: Some(1902),
579                column: Some(13)
580            }
581        );
582
583        // Trim off trailing `:`s for otherwise valid input.
584        assert_eq!(
585            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:"),
586            PathWithPosition {
587                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
588                row: Some(1902),
589                column: Some(13)
590            }
591        );
592
593        assert_eq!(
594            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:"),
595            PathWithPosition {
596                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
597                row: Some(13),
598                column: Some(15)
599            }
600        );
601
602        assert_eq!(
603            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:"),
604            PathWithPosition {
605                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
606                row: Some(15),
607                column: None
608            }
609        );
610
611        assert_eq!(
612            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902,13):"),
613            PathWithPosition {
614                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
615                row: Some(1902),
616                column: Some(13),
617            }
618        );
619
620        assert_eq!(
621            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902):"),
622            PathWithPosition {
623                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
624                row: Some(1902),
625                column: None,
626            }
627        );
628
629        assert_eq!(
630            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs:1902:13:"),
631            PathWithPosition {
632                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
633                row: Some(1902),
634                column: Some(13),
635            }
636        );
637
638        assert_eq!(
639            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13):"),
640            PathWithPosition {
641                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
642                row: Some(1902),
643                column: Some(13),
644            }
645        );
646
647        assert_eq!(
648            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902):"),
649            PathWithPosition {
650                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
651                row: Some(1902),
652                column: None,
653            }
654        );
655
656        assert_eq!(
657            PathWithPosition::parse_str("crates/utils/paths.rs:101"),
658            PathWithPosition {
659                path: PathBuf::from("crates\\utils\\paths.rs"),
660                row: Some(101),
661                column: None,
662            }
663        );
664    }
665
666    #[test]
667    fn test_path_compact() {
668        let path: PathBuf = [
669            home_dir().to_string_lossy().to_string(),
670            "some_file.txt".to_string(),
671        ]
672        .iter()
673        .collect();
674        if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
675            assert_eq!(path.compact().to_str(), Some("~/some_file.txt"));
676        } else {
677            assert_eq!(path.compact().to_str(), path.to_str());
678        }
679    }
680
681    #[test]
682    fn test_icon_stem_or_suffix() {
683        // No dots in name
684        let path = Path::new("/a/b/c/file_name.rs");
685        assert_eq!(path.icon_stem_or_suffix(), Some("rs"));
686
687        // Single dot in name
688        let path = Path::new("/a/b/c/file.name.rs");
689        assert_eq!(path.icon_stem_or_suffix(), Some("rs"));
690
691        // No suffix
692        let path = Path::new("/a/b/c/file");
693        assert_eq!(path.icon_stem_or_suffix(), Some("file"));
694
695        // Multiple dots in name
696        let path = Path::new("/a/b/c/long.file.name.rs");
697        assert_eq!(path.icon_stem_or_suffix(), Some("rs"));
698
699        // Hidden file, no extension
700        let path = Path::new("/a/b/c/.gitignore");
701        assert_eq!(path.icon_stem_or_suffix(), Some("gitignore"));
702
703        // Hidden file, with extension
704        let path = Path::new("/a/b/c/.eslintrc.js");
705        assert_eq!(path.icon_stem_or_suffix(), Some("eslintrc.js"));
706    }
707
708    #[test]
709    fn test_extension_or_hidden_file_name() {
710        // No dots in name
711        let path = Path::new("/a/b/c/file_name.rs");
712        assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
713
714        // Single dot in name
715        let path = Path::new("/a/b/c/file.name.rs");
716        assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
717
718        // Multiple dots in name
719        let path = Path::new("/a/b/c/long.file.name.rs");
720        assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
721
722        // Hidden file, no extension
723        let path = Path::new("/a/b/c/.gitignore");
724        assert_eq!(path.extension_or_hidden_file_name(), Some("gitignore"));
725
726        // Hidden file, with extension
727        let path = Path::new("/a/b/c/.eslintrc.js");
728        assert_eq!(path.extension_or_hidden_file_name(), Some("js"));
729    }
730
731    #[test]
732    fn edge_of_glob() {
733        let path = Path::new("/work/node_modules");
734        let path_matcher = PathMatcher::new(&["**/node_modules/**".to_owned()]).unwrap();
735        assert!(
736            path_matcher.is_match(path),
737            "Path matcher should match {path:?}"
738        );
739    }
740
741    #[test]
742    fn project_search() {
743        let path = Path::new("/Users/someonetoignore/work/zed/zed.dev/node_modules");
744        let path_matcher = PathMatcher::new(&["**/node_modules/**".to_owned()]).unwrap();
745        assert!(
746            path_matcher.is_match(path),
747            "Path matcher should match {path:?}"
748        );
749    }
750}