paths.rs

   1use anyhow::Context;
   2use globset::{Glob, GlobSet, GlobSetBuilder};
   3use itertools::Itertools;
   4use regex::Regex;
   5use serde::{Deserialize, Serialize};
   6use std::cmp::Ordering;
   7use std::fmt::{Display, Formatter};
   8use std::mem;
   9use std::path::StripPrefixError;
  10use std::sync::{Arc, OnceLock};
  11use std::{
  12    ffi::OsStr,
  13    path::{Path, PathBuf},
  14    sync::LazyLock,
  15};
  16
  17use crate::rel_path::RelPath;
  18
  19static HOME_DIR: OnceLock<PathBuf> = OnceLock::new();
  20
  21/// Returns the path to the user's home directory.
  22pub fn home_dir() -> &'static PathBuf {
  23    HOME_DIR.get_or_init(|| {
  24        if cfg!(any(test, feature = "test-support")) {
  25            if cfg!(target_os = "macos") {
  26                PathBuf::from("/Users/zed")
  27            } else if cfg!(target_os = "windows") {
  28                PathBuf::from("C:\\Users\\zed")
  29            } else {
  30                PathBuf::from("/home/zed")
  31            }
  32        } else {
  33            dirs::home_dir().expect("failed to determine home directory")
  34        }
  35    })
  36}
  37
  38pub trait PathExt {
  39    /// Compacts a given file path by replacing the user's home directory
  40    /// prefix with a tilde (`~`).
  41    ///
  42    /// # Returns
  43    ///
  44    /// * A `PathBuf` containing the compacted file path. If the input path
  45    ///   does not have the user's home directory prefix, or if we are not on
  46    ///   Linux or macOS, the original path is returned unchanged.
  47    fn compact(&self) -> PathBuf;
  48
  49    /// Returns a file's extension or, if the file is hidden, its name without the leading dot
  50    fn extension_or_hidden_file_name(&self) -> Option<&str>;
  51
  52    fn try_from_bytes<'a>(bytes: &'a [u8]) -> anyhow::Result<Self>
  53    where
  54        Self: From<&'a Path>,
  55    {
  56        #[cfg(unix)]
  57        {
  58            use std::os::unix::prelude::OsStrExt;
  59            Ok(Self::from(Path::new(OsStr::from_bytes(bytes))))
  60        }
  61        #[cfg(windows)]
  62        {
  63            use tendril::fmt::{Format, WTF8};
  64            WTF8::validate(bytes)
  65                .then(|| {
  66                    // Safety: bytes are valid WTF-8 sequence.
  67                    Self::from(Path::new(unsafe {
  68                        OsStr::from_encoded_bytes_unchecked(bytes)
  69                    }))
  70                })
  71                .with_context(|| format!("Invalid WTF-8 sequence: {bytes:?}"))
  72        }
  73    }
  74
  75    /// Converts a local path to one that can be used inside of WSL.
  76    /// Returns `None` if the path cannot be converted into a WSL one (network share).
  77    fn local_to_wsl(&self) -> Option<PathBuf>;
  78
  79    /// Returns a file's "full" joined collection of extensions, in the case where a file does not
  80    /// just have a singular extension but instead has multiple (e.g File.tar.gz, Component.stories.tsx)
  81    ///
  82    /// Will provide back the extensions joined together such as tar.gz or stories.tsx
  83    fn multiple_extensions(&self) -> Option<String>;
  84
  85    /// Try to make a shell-safe representation of the path.
  86    ///
  87    /// For Unix, the path is escaped to be safe for POSIX shells
  88    fn try_shell_safe(&self) -> anyhow::Result<String>;
  89}
  90
  91impl<T: AsRef<Path>> PathExt for T {
  92    fn compact(&self) -> PathBuf {
  93        if cfg!(any(target_os = "linux", target_os = "freebsd")) || cfg!(target_os = "macos") {
  94            match self.as_ref().strip_prefix(home_dir().as_path()) {
  95                Ok(relative_path) => {
  96                    let mut shortened_path = PathBuf::new();
  97                    shortened_path.push("~");
  98                    shortened_path.push(relative_path);
  99                    shortened_path
 100                }
 101                Err(_) => self.as_ref().to_path_buf(),
 102            }
 103        } else {
 104            self.as_ref().to_path_buf()
 105        }
 106    }
 107
 108    fn extension_or_hidden_file_name(&self) -> Option<&str> {
 109        let path = self.as_ref();
 110        let file_name = path.file_name()?.to_str()?;
 111        if file_name.starts_with('.') {
 112            return file_name.strip_prefix('.');
 113        }
 114
 115        path.extension()
 116            .and_then(|e| e.to_str())
 117            .or_else(|| path.file_stem()?.to_str())
 118    }
 119
 120    fn local_to_wsl(&self) -> Option<PathBuf> {
 121        // quite sketchy to convert this back to path at the end, but a lot of functions only accept paths
 122        // todo: ideally rework them..?
 123        let mut new_path = std::ffi::OsString::new();
 124        for component in self.as_ref().components() {
 125            match component {
 126                std::path::Component::Prefix(prefix) => {
 127                    let drive_letter = prefix.as_os_str().to_string_lossy().to_lowercase();
 128                    let drive_letter = drive_letter.strip_suffix(':')?;
 129
 130                    new_path.push(format!("/mnt/{}", drive_letter));
 131                }
 132                std::path::Component::RootDir => {}
 133                std::path::Component::CurDir => {
 134                    new_path.push("/.");
 135                }
 136                std::path::Component::ParentDir => {
 137                    new_path.push("/..");
 138                }
 139                std::path::Component::Normal(os_str) => {
 140                    new_path.push("/");
 141                    new_path.push(os_str);
 142                }
 143            }
 144        }
 145
 146        Some(new_path.into())
 147    }
 148
 149    fn multiple_extensions(&self) -> Option<String> {
 150        let path = self.as_ref();
 151        let file_name = path.file_name()?.to_str()?;
 152
 153        let parts: Vec<&str> = file_name
 154            .split('.')
 155            // Skip the part with the file name extension
 156            .skip(1)
 157            .collect();
 158
 159        if parts.len() < 2 {
 160            return None;
 161        }
 162
 163        Some(parts.into_iter().join("."))
 164    }
 165
 166    fn try_shell_safe(&self) -> anyhow::Result<String> {
 167        #[cfg(target_os = "windows")]
 168        {
 169            Ok(self.as_ref().to_string_lossy().to_string())
 170        }
 171
 172        #[cfg(not(target_os = "windows"))]
 173        {
 174            let path_str = self
 175                .as_ref()
 176                .to_str()
 177                .with_context(|| "Path contains invalid UTF-8")?;
 178
 179            // As of writing, this can only be fail if the path contains a null byte, which shouldn't be possible
 180            // but shlex has annotated the error as #[non_exhaustive] so we can't make it a compile error if other
 181            // errors are introduced in the future :(
 182            Ok(shlex::try_quote(path_str)?.into_owned())
 183        }
 184    }
 185}
 186
 187/// In memory, this is identical to `Path`. On non-Windows conversions to this type are no-ops. On
 188/// windows, these conversions sanitize UNC paths by removing the `\\\\?\\` prefix.
 189#[derive(Eq, PartialEq, Hash, Ord, PartialOrd)]
 190#[repr(transparent)]
 191pub struct SanitizedPath(Path);
 192
 193impl SanitizedPath {
 194    pub fn new<T: AsRef<Path> + ?Sized>(path: &T) -> &Self {
 195        #[cfg(not(target_os = "windows"))]
 196        return Self::unchecked_new(path.as_ref());
 197
 198        #[cfg(target_os = "windows")]
 199        return Self::unchecked_new(dunce::simplified(path.as_ref()));
 200    }
 201
 202    pub fn unchecked_new<T: AsRef<Path> + ?Sized>(path: &T) -> &Self {
 203        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
 204        unsafe { mem::transmute::<&Path, &Self>(path.as_ref()) }
 205    }
 206
 207    pub fn from_arc(path: Arc<Path>) -> Arc<Self> {
 208        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
 209        #[cfg(not(target_os = "windows"))]
 210        return unsafe { mem::transmute::<Arc<Path>, Arc<Self>>(path) };
 211
 212        // TODO: could avoid allocating here if dunce::simplified results in the same path
 213        #[cfg(target_os = "windows")]
 214        return Self::new(&path).into();
 215    }
 216
 217    pub fn new_arc<T: AsRef<Path> + ?Sized>(path: &T) -> Arc<Self> {
 218        Self::new(path).into()
 219    }
 220
 221    pub fn cast_arc(path: Arc<Self>) -> Arc<Path> {
 222        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
 223        unsafe { mem::transmute::<Arc<Self>, Arc<Path>>(path) }
 224    }
 225
 226    pub fn cast_arc_ref(path: &Arc<Self>) -> &Arc<Path> {
 227        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
 228        unsafe { mem::transmute::<&Arc<Self>, &Arc<Path>>(path) }
 229    }
 230
 231    pub fn starts_with(&self, prefix: &Self) -> bool {
 232        self.0.starts_with(&prefix.0)
 233    }
 234
 235    pub fn as_path(&self) -> &Path {
 236        &self.0
 237    }
 238
 239    pub fn file_name(&self) -> Option<&std::ffi::OsStr> {
 240        self.0.file_name()
 241    }
 242
 243    pub fn extension(&self) -> Option<&std::ffi::OsStr> {
 244        self.0.extension()
 245    }
 246
 247    pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
 248        self.0.join(path)
 249    }
 250
 251    pub fn parent(&self) -> Option<&Self> {
 252        self.0.parent().map(Self::unchecked_new)
 253    }
 254
 255    pub fn strip_prefix(&self, base: &Self) -> Result<&Path, StripPrefixError> {
 256        self.0.strip_prefix(base.as_path())
 257    }
 258
 259    pub fn to_str(&self) -> Option<&str> {
 260        self.0.to_str()
 261    }
 262
 263    pub fn to_path_buf(&self) -> PathBuf {
 264        self.0.to_path_buf()
 265    }
 266}
 267
 268impl std::fmt::Debug for SanitizedPath {
 269    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
 270        std::fmt::Debug::fmt(&self.0, formatter)
 271    }
 272}
 273
 274impl Display for SanitizedPath {
 275    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
 276        write!(f, "{}", self.0.display())
 277    }
 278}
 279
 280impl From<&SanitizedPath> for Arc<SanitizedPath> {
 281    fn from(sanitized_path: &SanitizedPath) -> Self {
 282        let path: Arc<Path> = sanitized_path.0.into();
 283        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
 284        unsafe { mem::transmute(path) }
 285    }
 286}
 287
 288impl From<&SanitizedPath> for PathBuf {
 289    fn from(sanitized_path: &SanitizedPath) -> Self {
 290        sanitized_path.as_path().into()
 291    }
 292}
 293
 294impl AsRef<Path> for SanitizedPath {
 295    fn as_ref(&self) -> &Path {
 296        &self.0
 297    }
 298}
 299
 300#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
 301pub enum PathStyle {
 302    Posix,
 303    Windows,
 304}
 305
 306impl PathStyle {
 307    #[cfg(target_os = "windows")]
 308    pub const fn local() -> Self {
 309        PathStyle::Windows
 310    }
 311
 312    #[cfg(not(target_os = "windows"))]
 313    pub const fn local() -> Self {
 314        PathStyle::Posix
 315    }
 316
 317    #[inline]
 318    pub fn separator(&self) -> &'static str {
 319        match self {
 320            PathStyle::Posix => "/",
 321            PathStyle::Windows => "\\",
 322        }
 323    }
 324
 325    pub fn is_windows(&self) -> bool {
 326        *self == PathStyle::Windows
 327    }
 328
 329    pub fn join(self, left: impl AsRef<Path>, right: impl AsRef<Path>) -> Option<String> {
 330        let right = right.as_ref().to_str()?;
 331        if is_absolute(right, self) {
 332            return None;
 333        }
 334        let left = left.as_ref().to_str()?;
 335        if left.is_empty() {
 336            Some(right.into())
 337        } else {
 338            Some(format!(
 339                "{left}{}{right}",
 340                if left.ends_with(self.separator()) {
 341                    ""
 342                } else {
 343                    self.separator()
 344                }
 345            ))
 346        }
 347    }
 348
 349    pub fn split(self, path_like: &str) -> (Option<&str>, &str) {
 350        let Some(pos) = path_like.rfind(self.separator()) else {
 351            return (None, path_like);
 352        };
 353        let filename_start = pos + self.separator().len();
 354        (
 355            Some(&path_like[..filename_start]),
 356            &path_like[filename_start..],
 357        )
 358    }
 359}
 360
 361#[derive(Debug, Clone)]
 362pub struct RemotePathBuf {
 363    style: PathStyle,
 364    string: String,
 365}
 366
 367impl RemotePathBuf {
 368    pub fn new(string: String, style: PathStyle) -> Self {
 369        Self { style, string }
 370    }
 371
 372    pub fn from_str(path: &str, style: PathStyle) -> Self {
 373        Self::new(path.to_string(), style)
 374    }
 375
 376    pub fn path_style(&self) -> PathStyle {
 377        self.style
 378    }
 379
 380    pub fn to_proto(self) -> String {
 381        self.string
 382    }
 383}
 384
 385impl Display for RemotePathBuf {
 386    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 387        write!(f, "{}", self.string)
 388    }
 389}
 390
 391pub fn is_absolute(path_like: &str, path_style: PathStyle) -> bool {
 392    path_like.starts_with('/')
 393        || path_style == PathStyle::Windows
 394            && (path_like.starts_with('\\')
 395                || path_like
 396                    .chars()
 397                    .next()
 398                    .is_some_and(|c| c.is_ascii_alphabetic())
 399                    && path_like[1..]
 400                        .strip_prefix(':')
 401                        .is_some_and(|path| path.starts_with('/') || path.starts_with('\\')))
 402}
 403
 404/// A delimiter to use in `path_query:row_number:column_number` strings parsing.
 405pub const FILE_ROW_COLUMN_DELIMITER: char = ':';
 406
 407const ROW_COL_CAPTURE_REGEX: &str = r"(?xs)
 408    ([^\(]+)\:(?:
 409        \((\d+)[,:](\d+)\) # filename:(row,column), filename:(row:column)
 410        |
 411        \((\d+)\)()     # filename:(row)
 412    )
 413    |
 414    ([^\(]+)(?:
 415        \((\d+)[,:](\d+)\) # filename(row,column), filename(row:column)
 416        |
 417        \((\d+)\)()     # filename(row)
 418    )
 419    |
 420    (.+?)(?:
 421        \:+(\d+)\:(\d+)\:*$  # filename:row:column
 422        |
 423        \:+(\d+)\:*()$       # filename:row
 424        |
 425        \:+()()$
 426    )";
 427
 428/// A representation of a path-like string with optional row and column numbers.
 429/// Matching values example: `te`, `test.rs:22`, `te:22:5`, `test.c(22)`, `test.c(22,5)`etc.
 430#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
 431pub struct PathWithPosition {
 432    pub path: PathBuf,
 433    pub row: Option<u32>,
 434    // Absent if row is absent.
 435    pub column: Option<u32>,
 436}
 437
 438impl PathWithPosition {
 439    /// Returns a PathWithPosition from a path.
 440    pub fn from_path(path: PathBuf) -> Self {
 441        Self {
 442            path,
 443            row: None,
 444            column: None,
 445        }
 446    }
 447
 448    /// Parses a string that possibly has `:row:column` or `(row, column)` suffix.
 449    /// Parenthesis format is used by [MSBuild](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-diagnostic-format-for-tasks) compatible tools
 450    /// Ignores trailing `:`s, so `test.rs:22:` is parsed as `test.rs:22`.
 451    /// If the suffix parsing fails, the whole string is parsed as a path.
 452    ///
 453    /// Be mindful that `test_file:10:1:` is a valid posix filename.
 454    /// `PathWithPosition` class assumes that the ending position-like suffix is **not** part of the filename.
 455    ///
 456    /// # Examples
 457    ///
 458    /// ```
 459    /// # use zed_util::paths::PathWithPosition;
 460    /// # use std::path::PathBuf;
 461    /// assert_eq!(PathWithPosition::parse_str("test_file"), PathWithPosition {
 462    ///     path: PathBuf::from("test_file"),
 463    ///     row: None,
 464    ///     column: None,
 465    /// });
 466    /// assert_eq!(PathWithPosition::parse_str("test_file:10"), PathWithPosition {
 467    ///     path: PathBuf::from("test_file"),
 468    ///     row: Some(10),
 469    ///     column: None,
 470    /// });
 471    /// assert_eq!(PathWithPosition::parse_str("test_file.rs"), PathWithPosition {
 472    ///     path: PathBuf::from("test_file.rs"),
 473    ///     row: None,
 474    ///     column: None,
 475    /// });
 476    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1"), PathWithPosition {
 477    ///     path: PathBuf::from("test_file.rs"),
 478    ///     row: Some(1),
 479    ///     column: None,
 480    /// });
 481    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1:2"), PathWithPosition {
 482    ///     path: PathBuf::from("test_file.rs"),
 483    ///     row: Some(1),
 484    ///     column: Some(2),
 485    /// });
 486    /// ```
 487    ///
 488    /// # Expected parsing results when encounter ill-formatted inputs.
 489    /// ```
 490    /// # use zed_util::paths::PathWithPosition;
 491    /// # use std::path::PathBuf;
 492    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:a"), PathWithPosition {
 493    ///     path: PathBuf::from("test_file.rs:a"),
 494    ///     row: None,
 495    ///     column: None,
 496    /// });
 497    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:a:b"), PathWithPosition {
 498    ///     path: PathBuf::from("test_file.rs:a:b"),
 499    ///     row: None,
 500    ///     column: None,
 501    /// });
 502    /// assert_eq!(PathWithPosition::parse_str("test_file.rs"), PathWithPosition {
 503    ///     path: PathBuf::from("test_file.rs"),
 504    ///     row: None,
 505    ///     column: None,
 506    /// });
 507    /// assert_eq!(PathWithPosition::parse_str("test_file.rs::1"), PathWithPosition {
 508    ///     path: PathBuf::from("test_file.rs"),
 509    ///     row: Some(1),
 510    ///     column: None,
 511    /// });
 512    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1::"), PathWithPosition {
 513    ///     path: PathBuf::from("test_file.rs"),
 514    ///     row: Some(1),
 515    ///     column: None,
 516    /// });
 517    /// assert_eq!(PathWithPosition::parse_str("test_file.rs::1:2"), PathWithPosition {
 518    ///     path: PathBuf::from("test_file.rs"),
 519    ///     row: Some(1),
 520    ///     column: Some(2),
 521    /// });
 522    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1::2"), PathWithPosition {
 523    ///     path: PathBuf::from("test_file.rs:1"),
 524    ///     row: Some(2),
 525    ///     column: None,
 526    /// });
 527    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1:2:3"), PathWithPosition {
 528    ///     path: PathBuf::from("test_file.rs:1"),
 529    ///     row: Some(2),
 530    ///     column: Some(3),
 531    /// });
 532    /// ```
 533    pub fn parse_str(s: &str) -> Self {
 534        let trimmed = s.trim();
 535        let path = Path::new(trimmed);
 536        let maybe_file_name_with_row_col = path.file_name().unwrap_or_default().to_string_lossy();
 537        if maybe_file_name_with_row_col.is_empty() {
 538            return Self {
 539                path: Path::new(s).to_path_buf(),
 540                row: None,
 541                column: None,
 542            };
 543        }
 544
 545        // Let's avoid repeated init cost on this. It is subject to thread contention, but
 546        // so far this code isn't called from multiple hot paths. Getting contention here
 547        // in the future seems unlikely.
 548        static SUFFIX_RE: LazyLock<Regex> =
 549            LazyLock::new(|| Regex::new(ROW_COL_CAPTURE_REGEX).unwrap());
 550        match SUFFIX_RE
 551            .captures(&maybe_file_name_with_row_col)
 552            .map(|caps| caps.extract())
 553        {
 554            Some((_, [file_name, maybe_row, maybe_column])) => {
 555                let row = maybe_row.parse::<u32>().ok();
 556                let column = maybe_column.parse::<u32>().ok();
 557
 558                let suffix_length = maybe_file_name_with_row_col.len() - file_name.len();
 559                let path_without_suffix = &trimmed[..trimmed.len() - suffix_length];
 560
 561                Self {
 562                    path: Path::new(path_without_suffix).to_path_buf(),
 563                    row,
 564                    column,
 565                }
 566            }
 567            None => {
 568                // The `ROW_COL_CAPTURE_REGEX` deals with separated digits only,
 569                // but in reality there could be `foo/bar.py:22:in` inputs which we want to match too.
 570                // The regex mentioned is not very extendable with "digit or random string" checks, so do this here instead.
 571                let delimiter = ':';
 572                let mut path_parts = s
 573                    .rsplitn(3, delimiter)
 574                    .collect::<Vec<_>>()
 575                    .into_iter()
 576                    .rev()
 577                    .fuse();
 578                let mut path_string = path_parts.next().expect("rsplitn should have the rest of the string as its last parameter that we reversed").to_owned();
 579                let mut row = None;
 580                let mut column = None;
 581                if let Some(maybe_row) = path_parts.next() {
 582                    if let Ok(parsed_row) = maybe_row.parse::<u32>() {
 583                        row = Some(parsed_row);
 584                        if let Some(parsed_column) = path_parts
 585                            .next()
 586                            .and_then(|maybe_col| maybe_col.parse::<u32>().ok())
 587                        {
 588                            column = Some(parsed_column);
 589                        }
 590                    } else {
 591                        path_string.push(delimiter);
 592                        path_string.push_str(maybe_row);
 593                    }
 594                }
 595                for split in path_parts {
 596                    path_string.push(delimiter);
 597                    path_string.push_str(split);
 598                }
 599
 600                Self {
 601                    path: PathBuf::from(path_string),
 602                    row,
 603                    column,
 604                }
 605            }
 606        }
 607    }
 608
 609    pub fn map_path<E>(
 610        self,
 611        mapping: impl FnOnce(PathBuf) -> Result<PathBuf, E>,
 612    ) -> Result<PathWithPosition, E> {
 613        Ok(PathWithPosition {
 614            path: mapping(self.path)?,
 615            row: self.row,
 616            column: self.column,
 617        })
 618    }
 619
 620    pub fn to_string(&self, path_to_string: impl Fn(&PathBuf) -> String) -> String {
 621        let path_string = path_to_string(&self.path);
 622        if let Some(row) = self.row {
 623            if let Some(column) = self.column {
 624                format!("{path_string}:{row}:{column}")
 625            } else {
 626                format!("{path_string}:{row}")
 627            }
 628        } else {
 629            path_string
 630        }
 631    }
 632}
 633
 634#[derive(Clone, Debug)]
 635pub struct PathMatcher {
 636    sources: Vec<String>,
 637    glob: GlobSet,
 638    path_style: PathStyle,
 639}
 640
 641// impl std::fmt::Display for PathMatcher {
 642//     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 643//         self.sources.fmt(f)
 644//     }
 645// }
 646
 647impl PartialEq for PathMatcher {
 648    fn eq(&self, other: &Self) -> bool {
 649        self.sources.eq(&other.sources)
 650    }
 651}
 652
 653impl Eq for PathMatcher {}
 654
 655impl PathMatcher {
 656    pub fn new(
 657        globs: impl IntoIterator<Item = impl AsRef<str>>,
 658        path_style: PathStyle,
 659    ) -> Result<Self, globset::Error> {
 660        let globs = globs
 661            .into_iter()
 662            .map(|as_str| Glob::new(as_str.as_ref()))
 663            .collect::<Result<Vec<_>, _>>()?;
 664        let sources = globs.iter().map(|glob| glob.glob().to_owned()).collect();
 665        let mut glob_builder = GlobSetBuilder::new();
 666        for single_glob in globs {
 667            glob_builder.add(single_glob);
 668        }
 669        let glob = glob_builder.build()?;
 670        Ok(PathMatcher {
 671            glob,
 672            sources,
 673            path_style,
 674        })
 675    }
 676
 677    pub fn sources(&self) -> &[String] {
 678        &self.sources
 679    }
 680
 681    pub fn is_match<P: AsRef<Path>>(&self, other: P) -> bool {
 682        let other_path = other.as_ref();
 683        self.sources.iter().any(|source| {
 684            let as_bytes = other_path.as_os_str().as_encoded_bytes();
 685            as_bytes.starts_with(source.as_bytes()) || as_bytes.ends_with(source.as_bytes())
 686        }) || self.glob.is_match(other_path)
 687            || self.check_with_end_separator(other_path)
 688    }
 689
 690    fn check_with_end_separator(&self, path: &Path) -> bool {
 691        let path_str = path.to_string_lossy();
 692        let separator = self.path_style.separator();
 693        if path_str.ends_with(separator) {
 694            false
 695        } else {
 696            self.glob.is_match(path_str.to_string() + separator)
 697        }
 698    }
 699}
 700
 701impl Default for PathMatcher {
 702    fn default() -> Self {
 703        Self {
 704            path_style: PathStyle::local(),
 705            glob: GlobSet::empty(),
 706            sources: vec![],
 707        }
 708    }
 709}
 710
 711/// Custom character comparison that prioritizes lowercase for same letters
 712fn compare_chars(a: char, b: char) -> Ordering {
 713    // First compare case-insensitive
 714    match a.to_ascii_lowercase().cmp(&b.to_ascii_lowercase()) {
 715        Ordering::Equal => {
 716            // If same letter, prioritize lowercase (lowercase < uppercase)
 717            match (a.is_ascii_lowercase(), b.is_ascii_lowercase()) {
 718                (true, false) => Ordering::Less,    // lowercase comes first
 719                (false, true) => Ordering::Greater, // uppercase comes after
 720                _ => Ordering::Equal,               // both same case or both non-ascii
 721            }
 722        }
 723        other => other,
 724    }
 725}
 726
 727/// Compares two sequences of consecutive digits for natural sorting.
 728///
 729/// This function is a core component of natural sorting that handles numeric comparison
 730/// in a way that feels natural to humans. It extracts and compares consecutive digit
 731/// sequences from two iterators, handling various cases like leading zeros and very large numbers.
 732///
 733/// # Behavior
 734///
 735/// The function implements the following comparison rules:
 736/// 1. Different numeric values: Compares by actual numeric value (e.g., "2" < "10")
 737/// 2. Leading zeros: When values are equal, longer sequence wins (e.g., "002" > "2")
 738/// 3. Large numbers: Falls back to string comparison for numbers that would overflow u128
 739///
 740/// # Examples
 741///
 742/// ```text
 743/// "1" vs "2"      -> Less       (different values)
 744/// "2" vs "10"     -> Less       (numeric comparison)
 745/// "002" vs "2"    -> Greater    (leading zeros)
 746/// "10" vs "010"   -> Less       (leading zeros)
 747/// "999..." vs "1000..." -> Less (large number comparison)
 748/// ```
 749///
 750/// # Implementation Details
 751///
 752/// 1. Extracts consecutive digits into strings
 753/// 2. Compares sequence lengths for leading zero handling
 754/// 3. For equal lengths, compares digit by digit
 755/// 4. For different lengths:
 756///    - Attempts numeric comparison first (for numbers up to 2^128 - 1)
 757///    - Falls back to string comparison if numbers would overflow
 758///
 759/// The function advances both iterators past their respective numeric sequences,
 760/// regardless of the comparison result.
 761fn compare_numeric_segments<I>(
 762    a_iter: &mut std::iter::Peekable<I>,
 763    b_iter: &mut std::iter::Peekable<I>,
 764) -> Ordering
 765where
 766    I: Iterator<Item = char>,
 767{
 768    // Collect all consecutive digits into strings
 769    let mut a_num_str = String::new();
 770    let mut b_num_str = String::new();
 771
 772    while let Some(&c) = a_iter.peek() {
 773        if !c.is_ascii_digit() {
 774            break;
 775        }
 776
 777        a_num_str.push(c);
 778        a_iter.next();
 779    }
 780
 781    while let Some(&c) = b_iter.peek() {
 782        if !c.is_ascii_digit() {
 783            break;
 784        }
 785
 786        b_num_str.push(c);
 787        b_iter.next();
 788    }
 789
 790    // First compare lengths (handle leading zeros)
 791    match a_num_str.len().cmp(&b_num_str.len()) {
 792        Ordering::Equal => {
 793            // Same length, compare digit by digit
 794            match a_num_str.cmp(&b_num_str) {
 795                Ordering::Equal => Ordering::Equal,
 796                ordering => ordering,
 797            }
 798        }
 799
 800        // Different lengths but same value means leading zeros
 801        ordering => {
 802            // Try parsing as numbers first
 803            if let (Ok(a_val), Ok(b_val)) = (a_num_str.parse::<u128>(), b_num_str.parse::<u128>()) {
 804                match a_val.cmp(&b_val) {
 805                    Ordering::Equal => ordering, // Same value, longer one is greater (leading zeros)
 806                    ord => ord,
 807                }
 808            } else {
 809                // If parsing fails (overflow), compare as strings
 810                a_num_str.cmp(&b_num_str)
 811            }
 812        }
 813    }
 814}
 815
 816/// Performs natural sorting comparison between two strings.
 817///
 818/// Natural sorting is an ordering that handles numeric sequences in a way that matches human expectations.
 819/// For example, "file2" comes before "file10" (unlike standard lexicographic sorting).
 820///
 821/// # Characteristics
 822///
 823/// * Case-sensitive with lowercase priority: When comparing same letters, lowercase comes before uppercase
 824/// * Numbers are compared by numeric value, not character by character
 825/// * Leading zeros affect ordering when numeric values are equal
 826/// * Can handle numbers larger than u128::MAX (falls back to string comparison)
 827///
 828/// # Algorithm
 829///
 830/// The function works by:
 831/// 1. Processing strings character by character
 832/// 2. When encountering digits, treating consecutive digits as a single number
 833/// 3. Comparing numbers by their numeric value rather than lexicographically
 834/// 4. For non-numeric characters, using case-sensitive comparison with lowercase priority
 835fn natural_sort(a: &str, b: &str) -> Ordering {
 836    let mut a_iter = a.chars().peekable();
 837    let mut b_iter = b.chars().peekable();
 838
 839    loop {
 840        match (a_iter.peek(), b_iter.peek()) {
 841            (None, None) => return Ordering::Equal,
 842            (None, _) => return Ordering::Less,
 843            (_, None) => return Ordering::Greater,
 844            (Some(&a_char), Some(&b_char)) => {
 845                if a_char.is_ascii_digit() && b_char.is_ascii_digit() {
 846                    match compare_numeric_segments(&mut a_iter, &mut b_iter) {
 847                        Ordering::Equal => continue,
 848                        ordering => return ordering,
 849                    }
 850                } else {
 851                    match compare_chars(a_char, b_char) {
 852                        Ordering::Equal => {
 853                            a_iter.next();
 854                            b_iter.next();
 855                        }
 856                        ordering => return ordering,
 857                    }
 858                }
 859            }
 860        }
 861    }
 862}
 863pub fn compare_rel_paths(
 864    (path_a, a_is_file): (&RelPath, bool),
 865    (path_b, b_is_file): (&RelPath, bool),
 866) -> Ordering {
 867    let mut components_a = path_a.components();
 868    let mut components_b = path_b.components();
 869
 870    fn stem_and_extension(filename: &str) -> (Option<&str>, Option<&str>) {
 871        if filename.is_empty() {
 872            return (None, None);
 873        }
 874
 875        match filename.rsplit_once('.') {
 876            // Case 1: No dot was found. The entire name is the stem.
 877            None => (Some(filename), None),
 878
 879            // Case 2: A dot was found.
 880            Some((before, after)) => {
 881                // This is the crucial check for dotfiles like ".bashrc".
 882                // If `before` is empty, the dot was the first character.
 883                // In that case, we revert to the "whole name is the stem" logic.
 884                if before.is_empty() {
 885                    (Some(filename), None)
 886                } else {
 887                    // Otherwise, we have a standard stem and extension.
 888                    (Some(before), Some(after))
 889                }
 890            }
 891        }
 892    }
 893    loop {
 894        match (components_a.next(), components_b.next()) {
 895            (Some(component_a), Some(component_b)) => {
 896                let a_is_file = a_is_file && components_a.rest().is_empty();
 897                let b_is_file = b_is_file && components_b.rest().is_empty();
 898
 899                let ordering = a_is_file.cmp(&b_is_file).then_with(|| {
 900                    let (a_stem, a_extension) = a_is_file
 901                        .then(|| stem_and_extension(component_a))
 902                        .unwrap_or_default();
 903                    let path_string_a = if a_is_file { a_stem } else { Some(component_a) };
 904
 905                    let (b_stem, b_extension) = b_is_file
 906                        .then(|| stem_and_extension(component_b))
 907                        .unwrap_or_default();
 908                    let path_string_b = if b_is_file { b_stem } else { Some(component_b) };
 909
 910                    let compare_components = match (path_string_a, path_string_b) {
 911                        (Some(a), Some(b)) => natural_sort(&a, &b),
 912                        (Some(_), None) => Ordering::Greater,
 913                        (None, Some(_)) => Ordering::Less,
 914                        (None, None) => Ordering::Equal,
 915                    };
 916
 917                    compare_components.then_with(|| {
 918                        if a_is_file && b_is_file {
 919                            let ext_a = a_extension.unwrap_or_default();
 920                            let ext_b = b_extension.unwrap_or_default();
 921                            ext_a.cmp(ext_b)
 922                        } else {
 923                            Ordering::Equal
 924                        }
 925                    })
 926                });
 927
 928                if !ordering.is_eq() {
 929                    return ordering;
 930                }
 931            }
 932            (Some(_), None) => break Ordering::Greater,
 933            (None, Some(_)) => break Ordering::Less,
 934            (None, None) => break Ordering::Equal,
 935        }
 936    }
 937}
 938
 939pub fn compare_paths(
 940    (path_a, a_is_file): (&Path, bool),
 941    (path_b, b_is_file): (&Path, bool),
 942) -> Ordering {
 943    let mut components_a = path_a.components().peekable();
 944    let mut components_b = path_b.components().peekable();
 945
 946    loop {
 947        match (components_a.next(), components_b.next()) {
 948            (Some(component_a), Some(component_b)) => {
 949                let a_is_file = components_a.peek().is_none() && a_is_file;
 950                let b_is_file = components_b.peek().is_none() && b_is_file;
 951
 952                let ordering = a_is_file.cmp(&b_is_file).then_with(|| {
 953                    let path_a = Path::new(component_a.as_os_str());
 954                    let path_string_a = if a_is_file {
 955                        path_a.file_stem()
 956                    } else {
 957                        path_a.file_name()
 958                    }
 959                    .map(|s| s.to_string_lossy());
 960
 961                    let path_b = Path::new(component_b.as_os_str());
 962                    let path_string_b = if b_is_file {
 963                        path_b.file_stem()
 964                    } else {
 965                        path_b.file_name()
 966                    }
 967                    .map(|s| s.to_string_lossy());
 968
 969                    let compare_components = match (path_string_a, path_string_b) {
 970                        (Some(a), Some(b)) => natural_sort(&a, &b),
 971                        (Some(_), None) => Ordering::Greater,
 972                        (None, Some(_)) => Ordering::Less,
 973                        (None, None) => Ordering::Equal,
 974                    };
 975
 976                    compare_components.then_with(|| {
 977                        if a_is_file && b_is_file {
 978                            let ext_a = path_a.extension().unwrap_or_default();
 979                            let ext_b = path_b.extension().unwrap_or_default();
 980                            ext_a.cmp(ext_b)
 981                        } else {
 982                            Ordering::Equal
 983                        }
 984                    })
 985                });
 986
 987                if !ordering.is_eq() {
 988                    return ordering;
 989                }
 990            }
 991            (Some(_), None) => break Ordering::Greater,
 992            (None, Some(_)) => break Ordering::Less,
 993            (None, None) => break Ordering::Equal,
 994        }
 995    }
 996}
 997
 998#[cfg(test)]
 999mod tests {
1000    use super::*;
1001    use util_macros::perf;
1002
1003    #[perf]
1004    fn compare_paths_with_dots() {
1005        let mut paths = vec![
1006            (Path::new("test_dirs"), false),
1007            (Path::new("test_dirs/1.46"), false),
1008            (Path::new("test_dirs/1.46/bar_1"), true),
1009            (Path::new("test_dirs/1.46/bar_2"), true),
1010            (Path::new("test_dirs/1.45"), false),
1011            (Path::new("test_dirs/1.45/foo_2"), true),
1012            (Path::new("test_dirs/1.45/foo_1"), true),
1013        ];
1014        paths.sort_by(|&a, &b| compare_paths(a, b));
1015        assert_eq!(
1016            paths,
1017            vec![
1018                (Path::new("test_dirs"), false),
1019                (Path::new("test_dirs/1.45"), false),
1020                (Path::new("test_dirs/1.45/foo_1"), true),
1021                (Path::new("test_dirs/1.45/foo_2"), true),
1022                (Path::new("test_dirs/1.46"), false),
1023                (Path::new("test_dirs/1.46/bar_1"), true),
1024                (Path::new("test_dirs/1.46/bar_2"), true),
1025            ]
1026        );
1027        let mut paths = vec![
1028            (Path::new("root1/one.txt"), true),
1029            (Path::new("root1/one.two.txt"), true),
1030        ];
1031        paths.sort_by(|&a, &b| compare_paths(a, b));
1032        assert_eq!(
1033            paths,
1034            vec![
1035                (Path::new("root1/one.txt"), true),
1036                (Path::new("root1/one.two.txt"), true),
1037            ]
1038        );
1039    }
1040
1041    #[perf]
1042    fn compare_paths_with_same_name_different_extensions() {
1043        let mut paths = vec![
1044            (Path::new("test_dirs/file.rs"), true),
1045            (Path::new("test_dirs/file.txt"), true),
1046            (Path::new("test_dirs/file.md"), true),
1047            (Path::new("test_dirs/file"), true),
1048            (Path::new("test_dirs/file.a"), true),
1049        ];
1050        paths.sort_by(|&a, &b| compare_paths(a, b));
1051        assert_eq!(
1052            paths,
1053            vec![
1054                (Path::new("test_dirs/file"), true),
1055                (Path::new("test_dirs/file.a"), true),
1056                (Path::new("test_dirs/file.md"), true),
1057                (Path::new("test_dirs/file.rs"), true),
1058                (Path::new("test_dirs/file.txt"), true),
1059            ]
1060        );
1061    }
1062
1063    #[perf]
1064    fn compare_paths_case_semi_sensitive() {
1065        let mut paths = vec![
1066            (Path::new("test_DIRS"), false),
1067            (Path::new("test_DIRS/foo_1"), true),
1068            (Path::new("test_DIRS/foo_2"), true),
1069            (Path::new("test_DIRS/bar"), true),
1070            (Path::new("test_DIRS/BAR"), true),
1071            (Path::new("test_dirs"), false),
1072            (Path::new("test_dirs/foo_1"), true),
1073            (Path::new("test_dirs/foo_2"), true),
1074            (Path::new("test_dirs/bar"), true),
1075            (Path::new("test_dirs/BAR"), true),
1076        ];
1077        paths.sort_by(|&a, &b| compare_paths(a, b));
1078        assert_eq!(
1079            paths,
1080            vec![
1081                (Path::new("test_dirs"), false),
1082                (Path::new("test_dirs/bar"), true),
1083                (Path::new("test_dirs/BAR"), true),
1084                (Path::new("test_dirs/foo_1"), true),
1085                (Path::new("test_dirs/foo_2"), true),
1086                (Path::new("test_DIRS"), false),
1087                (Path::new("test_DIRS/bar"), true),
1088                (Path::new("test_DIRS/BAR"), true),
1089                (Path::new("test_DIRS/foo_1"), true),
1090                (Path::new("test_DIRS/foo_2"), true),
1091            ]
1092        );
1093    }
1094
1095    #[perf]
1096    fn path_with_position_parse_posix_path() {
1097        // Test POSIX filename edge cases
1098        // Read more at https://en.wikipedia.org/wiki/Filename
1099        assert_eq!(
1100            PathWithPosition::parse_str("test_file"),
1101            PathWithPosition {
1102                path: PathBuf::from("test_file"),
1103                row: None,
1104                column: None
1105            }
1106        );
1107
1108        assert_eq!(
1109            PathWithPosition::parse_str("a:bc:.zip:1"),
1110            PathWithPosition {
1111                path: PathBuf::from("a:bc:.zip"),
1112                row: Some(1),
1113                column: None
1114            }
1115        );
1116
1117        assert_eq!(
1118            PathWithPosition::parse_str("one.second.zip:1"),
1119            PathWithPosition {
1120                path: PathBuf::from("one.second.zip"),
1121                row: Some(1),
1122                column: None
1123            }
1124        );
1125
1126        // Trim off trailing `:`s for otherwise valid input.
1127        assert_eq!(
1128            PathWithPosition::parse_str("test_file:10:1:"),
1129            PathWithPosition {
1130                path: PathBuf::from("test_file"),
1131                row: Some(10),
1132                column: Some(1)
1133            }
1134        );
1135
1136        assert_eq!(
1137            PathWithPosition::parse_str("test_file.rs:"),
1138            PathWithPosition {
1139                path: PathBuf::from("test_file.rs"),
1140                row: None,
1141                column: None
1142            }
1143        );
1144
1145        assert_eq!(
1146            PathWithPosition::parse_str("test_file.rs:1:"),
1147            PathWithPosition {
1148                path: PathBuf::from("test_file.rs"),
1149                row: Some(1),
1150                column: None
1151            }
1152        );
1153
1154        assert_eq!(
1155            PathWithPosition::parse_str("ab\ncd"),
1156            PathWithPosition {
1157                path: PathBuf::from("ab\ncd"),
1158                row: None,
1159                column: None
1160            }
1161        );
1162
1163        assert_eq!(
1164            PathWithPosition::parse_str("👋\nab"),
1165            PathWithPosition {
1166                path: PathBuf::from("👋\nab"),
1167                row: None,
1168                column: None
1169            }
1170        );
1171
1172        assert_eq!(
1173            PathWithPosition::parse_str("Types.hs:(617,9)-(670,28):"),
1174            PathWithPosition {
1175                path: PathBuf::from("Types.hs"),
1176                row: Some(617),
1177                column: Some(9),
1178            }
1179        );
1180    }
1181
1182    #[perf]
1183    #[cfg(not(target_os = "windows"))]
1184    fn path_with_position_parse_posix_path_with_suffix() {
1185        assert_eq!(
1186            PathWithPosition::parse_str("foo/bar:34:in"),
1187            PathWithPosition {
1188                path: PathBuf::from("foo/bar"),
1189                row: Some(34),
1190                column: None,
1191            }
1192        );
1193        assert_eq!(
1194            PathWithPosition::parse_str("foo/bar.rs:1902:::15:"),
1195            PathWithPosition {
1196                path: PathBuf::from("foo/bar.rs:1902"),
1197                row: Some(15),
1198                column: None
1199            }
1200        );
1201
1202        assert_eq!(
1203            PathWithPosition::parse_str("app-editors:zed-0.143.6:20240710-201212.log:34:"),
1204            PathWithPosition {
1205                path: PathBuf::from("app-editors:zed-0.143.6:20240710-201212.log"),
1206                row: Some(34),
1207                column: None,
1208            }
1209        );
1210
1211        assert_eq!(
1212            PathWithPosition::parse_str("crates/file_finder/src/file_finder.rs:1902:13:"),
1213            PathWithPosition {
1214                path: PathBuf::from("crates/file_finder/src/file_finder.rs"),
1215                row: Some(1902),
1216                column: Some(13),
1217            }
1218        );
1219
1220        assert_eq!(
1221            PathWithPosition::parse_str("crate/utils/src/test:today.log:34"),
1222            PathWithPosition {
1223                path: PathBuf::from("crate/utils/src/test:today.log"),
1224                row: Some(34),
1225                column: None,
1226            }
1227        );
1228        assert_eq!(
1229            PathWithPosition::parse_str("/testing/out/src/file_finder.odin(7:15)"),
1230            PathWithPosition {
1231                path: PathBuf::from("/testing/out/src/file_finder.odin"),
1232                row: Some(7),
1233                column: Some(15),
1234            }
1235        );
1236    }
1237
1238    #[perf]
1239    #[cfg(target_os = "windows")]
1240    fn path_with_position_parse_windows_path() {
1241        assert_eq!(
1242            PathWithPosition::parse_str("crates\\utils\\paths.rs"),
1243            PathWithPosition {
1244                path: PathBuf::from("crates\\utils\\paths.rs"),
1245                row: None,
1246                column: None
1247            }
1248        );
1249
1250        assert_eq!(
1251            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs"),
1252            PathWithPosition {
1253                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1254                row: None,
1255                column: None
1256            }
1257        );
1258    }
1259
1260    #[perf]
1261    #[cfg(target_os = "windows")]
1262    fn path_with_position_parse_windows_path_with_suffix() {
1263        assert_eq!(
1264            PathWithPosition::parse_str("crates\\utils\\paths.rs:101"),
1265            PathWithPosition {
1266                path: PathBuf::from("crates\\utils\\paths.rs"),
1267                row: Some(101),
1268                column: None
1269            }
1270        );
1271
1272        assert_eq!(
1273            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1:20"),
1274            PathWithPosition {
1275                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1276                row: Some(1),
1277                column: Some(20)
1278            }
1279        );
1280
1281        assert_eq!(
1282            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13)"),
1283            PathWithPosition {
1284                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1285                row: Some(1902),
1286                column: Some(13)
1287            }
1288        );
1289
1290        // Trim off trailing `:`s for otherwise valid input.
1291        assert_eq!(
1292            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:"),
1293            PathWithPosition {
1294                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1295                row: Some(1902),
1296                column: Some(13)
1297            }
1298        );
1299
1300        assert_eq!(
1301            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:"),
1302            PathWithPosition {
1303                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
1304                row: Some(13),
1305                column: Some(15)
1306            }
1307        );
1308
1309        assert_eq!(
1310            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:"),
1311            PathWithPosition {
1312                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
1313                row: Some(15),
1314                column: None
1315            }
1316        );
1317
1318        assert_eq!(
1319            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902,13):"),
1320            PathWithPosition {
1321                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1322                row: Some(1902),
1323                column: Some(13),
1324            }
1325        );
1326
1327        assert_eq!(
1328            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902):"),
1329            PathWithPosition {
1330                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1331                row: Some(1902),
1332                column: None,
1333            }
1334        );
1335
1336        assert_eq!(
1337            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs:1902:13:"),
1338            PathWithPosition {
1339                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1340                row: Some(1902),
1341                column: Some(13),
1342            }
1343        );
1344
1345        assert_eq!(
1346            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13):"),
1347            PathWithPosition {
1348                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1349                row: Some(1902),
1350                column: Some(13),
1351            }
1352        );
1353
1354        assert_eq!(
1355            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902):"),
1356            PathWithPosition {
1357                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1358                row: Some(1902),
1359                column: None,
1360            }
1361        );
1362
1363        assert_eq!(
1364            PathWithPosition::parse_str("crates/utils/paths.rs:101"),
1365            PathWithPosition {
1366                path: PathBuf::from("crates\\utils\\paths.rs"),
1367                row: Some(101),
1368                column: None,
1369            }
1370        );
1371    }
1372
1373    #[perf]
1374    fn test_path_compact() {
1375        let path: PathBuf = [
1376            home_dir().to_string_lossy().into_owned(),
1377            "some_file.txt".to_string(),
1378        ]
1379        .iter()
1380        .collect();
1381        if cfg!(any(target_os = "linux", target_os = "freebsd")) || cfg!(target_os = "macos") {
1382            assert_eq!(path.compact().to_str(), Some("~/some_file.txt"));
1383        } else {
1384            assert_eq!(path.compact().to_str(), path.to_str());
1385        }
1386    }
1387
1388    #[perf]
1389    fn test_extension_or_hidden_file_name() {
1390        // No dots in name
1391        let path = Path::new("/a/b/c/file_name.rs");
1392        assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
1393
1394        // Single dot in name
1395        let path = Path::new("/a/b/c/file.name.rs");
1396        assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
1397
1398        // Multiple dots in name
1399        let path = Path::new("/a/b/c/long.file.name.rs");
1400        assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
1401
1402        // Hidden file, no extension
1403        let path = Path::new("/a/b/c/.gitignore");
1404        assert_eq!(path.extension_or_hidden_file_name(), Some("gitignore"));
1405
1406        // Hidden file, with extension
1407        let path = Path::new("/a/b/c/.eslintrc.js");
1408        assert_eq!(path.extension_or_hidden_file_name(), Some("eslintrc.js"));
1409    }
1410
1411    #[perf]
1412    fn edge_of_glob() {
1413        let path = Path::new("/work/node_modules");
1414        let path_matcher =
1415            PathMatcher::new(&["**/node_modules/**".to_owned()], PathStyle::Posix).unwrap();
1416        assert!(
1417            path_matcher.is_match(path),
1418            "Path matcher should match {path:?}"
1419        );
1420    }
1421
1422    #[perf]
1423    fn project_search() {
1424        let path = Path::new("/Users/someonetoignore/work/zed/zed.dev/node_modules");
1425        let path_matcher =
1426            PathMatcher::new(&["**/node_modules/**".to_owned()], PathStyle::Posix).unwrap();
1427        assert!(
1428            path_matcher.is_match(path),
1429            "Path matcher should match {path:?}"
1430        );
1431    }
1432
1433    #[perf]
1434    #[cfg(target_os = "windows")]
1435    fn test_sanitized_path() {
1436        let path = Path::new("C:\\Users\\someone\\test_file.rs");
1437        let sanitized_path = SanitizedPath::new(path);
1438        assert_eq!(
1439            sanitized_path.to_string(),
1440            "C:\\Users\\someone\\test_file.rs"
1441        );
1442
1443        let path = Path::new("\\\\?\\C:\\Users\\someone\\test_file.rs");
1444        let sanitized_path = SanitizedPath::new(path);
1445        assert_eq!(
1446            sanitized_path.to_string(),
1447            "C:\\Users\\someone\\test_file.rs"
1448        );
1449    }
1450
1451    #[perf]
1452    fn test_compare_numeric_segments() {
1453        // Helper function to create peekable iterators and test
1454        fn compare(a: &str, b: &str) -> Ordering {
1455            let mut a_iter = a.chars().peekable();
1456            let mut b_iter = b.chars().peekable();
1457
1458            let result = compare_numeric_segments(&mut a_iter, &mut b_iter);
1459
1460            // Verify iterators advanced correctly
1461            assert!(
1462                !a_iter.next().is_some_and(|c| c.is_ascii_digit()),
1463                "Iterator a should have consumed all digits"
1464            );
1465            assert!(
1466                !b_iter.next().is_some_and(|c| c.is_ascii_digit()),
1467                "Iterator b should have consumed all digits"
1468            );
1469
1470            result
1471        }
1472
1473        // Basic numeric comparisons
1474        assert_eq!(compare("0", "0"), Ordering::Equal);
1475        assert_eq!(compare("1", "2"), Ordering::Less);
1476        assert_eq!(compare("9", "10"), Ordering::Less);
1477        assert_eq!(compare("10", "9"), Ordering::Greater);
1478        assert_eq!(compare("99", "100"), Ordering::Less);
1479
1480        // Leading zeros
1481        assert_eq!(compare("0", "00"), Ordering::Less);
1482        assert_eq!(compare("00", "0"), Ordering::Greater);
1483        assert_eq!(compare("01", "1"), Ordering::Greater);
1484        assert_eq!(compare("001", "1"), Ordering::Greater);
1485        assert_eq!(compare("001", "01"), Ordering::Greater);
1486
1487        // Same value different representation
1488        assert_eq!(compare("000100", "100"), Ordering::Greater);
1489        assert_eq!(compare("100", "0100"), Ordering::Less);
1490        assert_eq!(compare("0100", "00100"), Ordering::Less);
1491
1492        // Large numbers
1493        assert_eq!(compare("9999999999", "10000000000"), Ordering::Less);
1494        assert_eq!(
1495            compare(
1496                "340282366920938463463374607431768211455", // u128::MAX
1497                "340282366920938463463374607431768211456"
1498            ),
1499            Ordering::Less
1500        );
1501        assert_eq!(
1502            compare(
1503                "340282366920938463463374607431768211456", // > u128::MAX
1504                "340282366920938463463374607431768211455"
1505            ),
1506            Ordering::Greater
1507        );
1508
1509        // Iterator advancement verification
1510        let mut a_iter = "123abc".chars().peekable();
1511        let mut b_iter = "456def".chars().peekable();
1512
1513        compare_numeric_segments(&mut a_iter, &mut b_iter);
1514
1515        assert_eq!(a_iter.collect::<String>(), "abc");
1516        assert_eq!(b_iter.collect::<String>(), "def");
1517    }
1518
1519    #[perf]
1520    fn test_natural_sort() {
1521        // Basic alphanumeric
1522        assert_eq!(natural_sort("a", "b"), Ordering::Less);
1523        assert_eq!(natural_sort("b", "a"), Ordering::Greater);
1524        assert_eq!(natural_sort("a", "a"), Ordering::Equal);
1525
1526        // Case sensitivity
1527        assert_eq!(natural_sort("a", "A"), Ordering::Less);
1528        assert_eq!(natural_sort("A", "a"), Ordering::Greater);
1529        assert_eq!(natural_sort("aA", "aa"), Ordering::Greater);
1530        assert_eq!(natural_sort("aa", "aA"), Ordering::Less);
1531
1532        // Numbers
1533        assert_eq!(natural_sort("1", "2"), Ordering::Less);
1534        assert_eq!(natural_sort("2", "10"), Ordering::Less);
1535        assert_eq!(natural_sort("02", "10"), Ordering::Less);
1536        assert_eq!(natural_sort("02", "2"), Ordering::Greater);
1537
1538        // Mixed alphanumeric
1539        assert_eq!(natural_sort("a1", "a2"), Ordering::Less);
1540        assert_eq!(natural_sort("a2", "a10"), Ordering::Less);
1541        assert_eq!(natural_sort("a02", "a2"), Ordering::Greater);
1542        assert_eq!(natural_sort("a1b", "a1c"), Ordering::Less);
1543
1544        // Multiple numeric segments
1545        assert_eq!(natural_sort("1a2", "1a10"), Ordering::Less);
1546        assert_eq!(natural_sort("1a10", "1a2"), Ordering::Greater);
1547        assert_eq!(natural_sort("2a1", "10a1"), Ordering::Less);
1548
1549        // Special characters
1550        assert_eq!(natural_sort("a-1", "a-2"), Ordering::Less);
1551        assert_eq!(natural_sort("a_1", "a_2"), Ordering::Less);
1552        assert_eq!(natural_sort("a.1", "a.2"), Ordering::Less);
1553
1554        // Unicode
1555        assert_eq!(natural_sort("文1", "文2"), Ordering::Less);
1556        assert_eq!(natural_sort("文2", "文10"), Ordering::Less);
1557        assert_eq!(natural_sort("🔤1", "🔤2"), Ordering::Less);
1558
1559        // Empty and special cases
1560        assert_eq!(natural_sort("", ""), Ordering::Equal);
1561        assert_eq!(natural_sort("", "a"), Ordering::Less);
1562        assert_eq!(natural_sort("a", ""), Ordering::Greater);
1563        assert_eq!(natural_sort(" ", "  "), Ordering::Less);
1564
1565        // Mixed everything
1566        assert_eq!(natural_sort("File-1.txt", "File-2.txt"), Ordering::Less);
1567        assert_eq!(natural_sort("File-02.txt", "File-2.txt"), Ordering::Greater);
1568        assert_eq!(natural_sort("File-2.txt", "File-10.txt"), Ordering::Less);
1569        assert_eq!(natural_sort("File_A1", "File_A2"), Ordering::Less);
1570        assert_eq!(natural_sort("File_a1", "File_A1"), Ordering::Less);
1571    }
1572
1573    #[perf]
1574    fn test_compare_paths() {
1575        // Helper function for cleaner tests
1576        fn compare(a: &str, is_a_file: bool, b: &str, is_b_file: bool) -> Ordering {
1577            compare_paths((Path::new(a), is_a_file), (Path::new(b), is_b_file))
1578        }
1579
1580        // Basic path comparison
1581        assert_eq!(compare("a", true, "b", true), Ordering::Less);
1582        assert_eq!(compare("b", true, "a", true), Ordering::Greater);
1583        assert_eq!(compare("a", true, "a", true), Ordering::Equal);
1584
1585        // Files vs Directories
1586        assert_eq!(compare("a", true, "a", false), Ordering::Greater);
1587        assert_eq!(compare("a", false, "a", true), Ordering::Less);
1588        assert_eq!(compare("b", false, "a", true), Ordering::Less);
1589
1590        // Extensions
1591        assert_eq!(compare("a.txt", true, "a.md", true), Ordering::Greater);
1592        assert_eq!(compare("a.md", true, "a.txt", true), Ordering::Less);
1593        assert_eq!(compare("a", true, "a.txt", true), Ordering::Less);
1594
1595        // Nested paths
1596        assert_eq!(compare("dir/a", true, "dir/b", true), Ordering::Less);
1597        assert_eq!(compare("dir1/a", true, "dir2/a", true), Ordering::Less);
1598        assert_eq!(compare("dir/sub/a", true, "dir/a", true), Ordering::Less);
1599
1600        // Case sensitivity in paths
1601        assert_eq!(
1602            compare("Dir/file", true, "dir/file", true),
1603            Ordering::Greater
1604        );
1605        assert_eq!(
1606            compare("dir/File", true, "dir/file", true),
1607            Ordering::Greater
1608        );
1609        assert_eq!(compare("dir/file", true, "Dir/File", true), Ordering::Less);
1610
1611        // Hidden files and special names
1612        assert_eq!(compare(".hidden", true, "visible", true), Ordering::Less);
1613        assert_eq!(compare("_special", true, "normal", true), Ordering::Less);
1614        assert_eq!(compare(".config", false, ".data", false), Ordering::Less);
1615
1616        // Mixed numeric paths
1617        assert_eq!(
1618            compare("dir1/file", true, "dir2/file", true),
1619            Ordering::Less
1620        );
1621        assert_eq!(
1622            compare("dir2/file", true, "dir10/file", true),
1623            Ordering::Less
1624        );
1625        assert_eq!(
1626            compare("dir02/file", true, "dir2/file", true),
1627            Ordering::Greater
1628        );
1629
1630        // Root paths
1631        assert_eq!(compare("/a", true, "/b", true), Ordering::Less);
1632        assert_eq!(compare("/", false, "/a", true), Ordering::Less);
1633
1634        // Complex real-world examples
1635        assert_eq!(
1636            compare("project/src/main.rs", true, "project/src/lib.rs", true),
1637            Ordering::Greater
1638        );
1639        assert_eq!(
1640            compare(
1641                "project/tests/test_1.rs",
1642                true,
1643                "project/tests/test_2.rs",
1644                true
1645            ),
1646            Ordering::Less
1647        );
1648        assert_eq!(
1649            compare(
1650                "project/v1.0.0/README.md",
1651                true,
1652                "project/v1.10.0/README.md",
1653                true
1654            ),
1655            Ordering::Less
1656        );
1657    }
1658
1659    #[perf]
1660    fn test_natural_sort_case_sensitivity() {
1661        std::thread::sleep(std::time::Duration::from_millis(100));
1662        // Same letter different case - lowercase should come first
1663        assert_eq!(natural_sort("a", "A"), Ordering::Less);
1664        assert_eq!(natural_sort("A", "a"), Ordering::Greater);
1665        assert_eq!(natural_sort("a", "a"), Ordering::Equal);
1666        assert_eq!(natural_sort("A", "A"), Ordering::Equal);
1667
1668        // Mixed case strings
1669        assert_eq!(natural_sort("aaa", "AAA"), Ordering::Less);
1670        assert_eq!(natural_sort("AAA", "aaa"), Ordering::Greater);
1671        assert_eq!(natural_sort("aAa", "AaA"), Ordering::Less);
1672
1673        // Different letters
1674        assert_eq!(natural_sort("a", "b"), Ordering::Less);
1675        assert_eq!(natural_sort("A", "b"), Ordering::Less);
1676        assert_eq!(natural_sort("a", "B"), Ordering::Less);
1677    }
1678
1679    #[perf]
1680    fn test_natural_sort_with_numbers() {
1681        // Basic number ordering
1682        assert_eq!(natural_sort("file1", "file2"), Ordering::Less);
1683        assert_eq!(natural_sort("file2", "file10"), Ordering::Less);
1684        assert_eq!(natural_sort("file10", "file2"), Ordering::Greater);
1685
1686        // Numbers in different positions
1687        assert_eq!(natural_sort("1file", "2file"), Ordering::Less);
1688        assert_eq!(natural_sort("file1text", "file2text"), Ordering::Less);
1689        assert_eq!(natural_sort("text1file", "text2file"), Ordering::Less);
1690
1691        // Multiple numbers in string
1692        assert_eq!(natural_sort("file1-2", "file1-10"), Ordering::Less);
1693        assert_eq!(natural_sort("2-1file", "10-1file"), Ordering::Less);
1694
1695        // Leading zeros
1696        assert_eq!(natural_sort("file002", "file2"), Ordering::Greater);
1697        assert_eq!(natural_sort("file002", "file10"), Ordering::Less);
1698
1699        // Very large numbers
1700        assert_eq!(
1701            natural_sort("file999999999999999999999", "file999999999999999999998"),
1702            Ordering::Greater
1703        );
1704
1705        // u128 edge cases
1706
1707        // Numbers near u128::MAX (340,282,366,920,938,463,463,374,607,431,768,211,455)
1708        assert_eq!(
1709            natural_sort(
1710                "file340282366920938463463374607431768211454",
1711                "file340282366920938463463374607431768211455"
1712            ),
1713            Ordering::Less
1714        );
1715
1716        // Equal length numbers that overflow u128
1717        assert_eq!(
1718            natural_sort(
1719                "file340282366920938463463374607431768211456",
1720                "file340282366920938463463374607431768211455"
1721            ),
1722            Ordering::Greater
1723        );
1724
1725        // Different length numbers that overflow u128
1726        assert_eq!(
1727            natural_sort(
1728                "file3402823669209384634633746074317682114560",
1729                "file340282366920938463463374607431768211455"
1730            ),
1731            Ordering::Greater
1732        );
1733
1734        // Leading zeros with numbers near u128::MAX
1735        assert_eq!(
1736            natural_sort(
1737                "file0340282366920938463463374607431768211455",
1738                "file340282366920938463463374607431768211455"
1739            ),
1740            Ordering::Greater
1741        );
1742
1743        // Very large numbers with different lengths (both overflow u128)
1744        assert_eq!(
1745            natural_sort(
1746                "file999999999999999999999999999999999999999999999999",
1747                "file9999999999999999999999999999999999999999999999999"
1748            ),
1749            Ordering::Less
1750        );
1751
1752        // Mixed case with numbers
1753        assert_eq!(natural_sort("File1", "file2"), Ordering::Greater);
1754        assert_eq!(natural_sort("file1", "File2"), Ordering::Less);
1755    }
1756
1757    #[perf]
1758    fn test_natural_sort_edge_cases() {
1759        // Empty strings
1760        assert_eq!(natural_sort("", ""), Ordering::Equal);
1761        assert_eq!(natural_sort("", "a"), Ordering::Less);
1762        assert_eq!(natural_sort("a", ""), Ordering::Greater);
1763
1764        // Special characters
1765        assert_eq!(natural_sort("file-1", "file_1"), Ordering::Less);
1766        assert_eq!(natural_sort("file.1", "file_1"), Ordering::Less);
1767        assert_eq!(natural_sort("file 1", "file_1"), Ordering::Less);
1768
1769        // Unicode characters
1770        // 9312 vs 9313
1771        assert_eq!(natural_sort("file①", "file②"), Ordering::Less);
1772        // 9321 vs 9313
1773        assert_eq!(natural_sort("file⑩", "file②"), Ordering::Greater);
1774        // 28450 vs 23383
1775        assert_eq!(natural_sort("file漢", "file字"), Ordering::Greater);
1776
1777        // Mixed alphanumeric with special chars
1778        assert_eq!(natural_sort("file-1a", "file-1b"), Ordering::Less);
1779        assert_eq!(natural_sort("file-1.2", "file-1.10"), Ordering::Less);
1780        assert_eq!(natural_sort("file-1.10", "file-1.2"), Ordering::Greater);
1781    }
1782
1783    #[test]
1784    fn test_multiple_extensions() {
1785        // No extensions
1786        let path = Path::new("/a/b/c/file_name");
1787        assert_eq!(path.multiple_extensions(), None);
1788
1789        // Only one extension
1790        let path = Path::new("/a/b/c/file_name.tsx");
1791        assert_eq!(path.multiple_extensions(), None);
1792
1793        // Stories sample extension
1794        let path = Path::new("/a/b/c/file_name.stories.tsx");
1795        assert_eq!(path.multiple_extensions(), Some("stories.tsx".to_string()));
1796
1797        // Longer sample extension
1798        let path = Path::new("/a/b/c/long.app.tar.gz");
1799        assert_eq!(path.multiple_extensions(), Some("app.tar.gz".to_string()));
1800    }
1801}