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::error::Error;
   8use std::fmt::{Display, Formatter};
   9use std::mem;
  10use std::path::StripPrefixError;
  11use std::sync::{Arc, OnceLock};
  12use std::{
  13    ffi::OsStr,
  14    path::{Path, PathBuf},
  15    sync::LazyLock,
  16};
  17
  18use crate::{rel_path::RelPath, shell::ShellKind};
  19
  20static HOME_DIR: OnceLock<PathBuf> = OnceLock::new();
  21
  22/// Returns the path to the user's home directory.
  23pub fn home_dir() -> &'static PathBuf {
  24    HOME_DIR.get_or_init(|| {
  25        if cfg!(any(test, feature = "test-support")) {
  26            if cfg!(target_os = "macos") {
  27                PathBuf::from("/Users/zed")
  28            } else if cfg!(target_os = "windows") {
  29                PathBuf::from("C:\\Users\\zed")
  30            } else {
  31                PathBuf::from("/home/zed")
  32            }
  33        } else {
  34            dirs::home_dir().expect("failed to determine home directory")
  35        }
  36    })
  37}
  38
  39pub trait PathExt {
  40    /// Compacts a given file path by replacing the user's home directory
  41    /// prefix with a tilde (`~`).
  42    ///
  43    /// # Returns
  44    ///
  45    /// * A `PathBuf` containing the compacted file path. If the input path
  46    ///   does not have the user's home directory prefix, or if we are not on
  47    ///   Linux or macOS, the original path is returned unchanged.
  48    fn compact(&self) -> PathBuf;
  49
  50    /// Returns a file's extension or, if the file is hidden, its name without the leading dot
  51    fn extension_or_hidden_file_name(&self) -> Option<&str>;
  52
  53    fn try_from_bytes<'a>(bytes: &'a [u8]) -> anyhow::Result<Self>
  54    where
  55        Self: From<&'a Path>,
  56    {
  57        #[cfg(unix)]
  58        {
  59            use std::os::unix::prelude::OsStrExt;
  60            Ok(Self::from(Path::new(OsStr::from_bytes(bytes))))
  61        }
  62        #[cfg(windows)]
  63        {
  64            use tendril::fmt::{Format, WTF8};
  65            WTF8::validate(bytes)
  66                .then(|| {
  67                    // Safety: bytes are valid WTF-8 sequence.
  68                    Self::from(Path::new(unsafe {
  69                        OsStr::from_encoded_bytes_unchecked(bytes)
  70                    }))
  71                })
  72                .with_context(|| format!("Invalid WTF-8 sequence: {bytes:?}"))
  73        }
  74    }
  75
  76    /// Converts a local path to one that can be used inside of WSL.
  77    /// Returns `None` if the path cannot be converted into a WSL one (network share).
  78    fn local_to_wsl(&self) -> Option<PathBuf>;
  79
  80    /// Returns a file's "full" joined collection of extensions, in the case where a file does not
  81    /// just have a singular extension but instead has multiple (e.g File.tar.gz, Component.stories.tsx)
  82    ///
  83    /// Will provide back the extensions joined together such as tar.gz or stories.tsx
  84    fn multiple_extensions(&self) -> Option<String>;
  85
  86    /// Try to make a shell-safe representation of the path.
  87    fn try_shell_safe(&self, shell_kind: ShellKind) -> anyhow::Result<String>;
  88}
  89
  90impl<T: AsRef<Path>> PathExt for T {
  91    fn compact(&self) -> PathBuf {
  92        if cfg!(any(target_os = "linux", target_os = "freebsd")) || cfg!(target_os = "macos") {
  93            match self.as_ref().strip_prefix(home_dir().as_path()) {
  94                Ok(relative_path) => {
  95                    let mut shortened_path = PathBuf::new();
  96                    shortened_path.push("~");
  97                    shortened_path.push(relative_path);
  98                    shortened_path
  99                }
 100                Err(_) => self.as_ref().to_path_buf(),
 101            }
 102        } else {
 103            self.as_ref().to_path_buf()
 104        }
 105    }
 106
 107    fn extension_or_hidden_file_name(&self) -> Option<&str> {
 108        let path = self.as_ref();
 109        let file_name = path.file_name()?.to_str()?;
 110        if file_name.starts_with('.') {
 111            return file_name.strip_prefix('.');
 112        }
 113
 114        path.extension()
 115            .and_then(|e| e.to_str())
 116            .or_else(|| path.file_stem()?.to_str())
 117    }
 118
 119    fn local_to_wsl(&self) -> Option<PathBuf> {
 120        // quite sketchy to convert this back to path at the end, but a lot of functions only accept paths
 121        // todo: ideally rework them..?
 122        let mut new_path = std::ffi::OsString::new();
 123        for component in self.as_ref().components() {
 124            match component {
 125                std::path::Component::Prefix(prefix) => {
 126                    let drive_letter = prefix.as_os_str().to_string_lossy().to_lowercase();
 127                    let drive_letter = drive_letter.strip_suffix(':')?;
 128
 129                    new_path.push(format!("/mnt/{}", drive_letter));
 130                }
 131                std::path::Component::RootDir => {}
 132                std::path::Component::CurDir => {
 133                    new_path.push("/.");
 134                }
 135                std::path::Component::ParentDir => {
 136                    new_path.push("/..");
 137                }
 138                std::path::Component::Normal(os_str) => {
 139                    new_path.push("/");
 140                    new_path.push(os_str);
 141                }
 142            }
 143        }
 144
 145        Some(new_path.into())
 146    }
 147
 148    fn multiple_extensions(&self) -> Option<String> {
 149        let path = self.as_ref();
 150        let file_name = path.file_name()?.to_str()?;
 151
 152        let parts: Vec<&str> = file_name
 153            .split('.')
 154            // Skip the part with the file name extension
 155            .skip(1)
 156            .collect();
 157
 158        if parts.len() < 2 {
 159            return None;
 160        }
 161
 162        Some(parts.into_iter().join("."))
 163    }
 164
 165    fn try_shell_safe(&self, shell_kind: ShellKind) -> anyhow::Result<String> {
 166        let path_str = self
 167            .as_ref()
 168            .to_str()
 169            .with_context(|| "Path contains invalid UTF-8")?;
 170        shell_kind
 171            .try_quote(path_str)
 172            .as_deref()
 173            .map(ToOwned::to_owned)
 174            .context("Failed to quote path")
 175    }
 176}
 177
 178pub fn path_ends_with(base: &Path, suffix: &Path) -> bool {
 179    strip_path_suffix(base, suffix).is_some()
 180}
 181
 182pub fn strip_path_suffix<'a>(base: &'a Path, suffix: &Path) -> Option<&'a Path> {
 183    if let Some(remainder) = base
 184        .as_os_str()
 185        .as_encoded_bytes()
 186        .strip_suffix(suffix.as_os_str().as_encoded_bytes())
 187    {
 188        if remainder
 189            .last()
 190            .is_none_or(|last_byte| std::path::is_separator(*last_byte as char))
 191        {
 192            let os_str = unsafe {
 193                OsStr::from_encoded_bytes_unchecked(
 194                    &remainder[0..remainder.len().saturating_sub(1)],
 195                )
 196            };
 197            return Some(Path::new(os_str));
 198        }
 199    }
 200    None
 201}
 202
 203/// In memory, this is identical to `Path`. On non-Windows conversions to this type are no-ops. On
 204/// windows, these conversions sanitize UNC paths by removing the `\\\\?\\` prefix.
 205#[derive(Eq, PartialEq, Hash, Ord, PartialOrd)]
 206#[repr(transparent)]
 207pub struct SanitizedPath(Path);
 208
 209impl SanitizedPath {
 210    pub fn new<T: AsRef<Path> + ?Sized>(path: &T) -> &Self {
 211        #[cfg(not(target_os = "windows"))]
 212        return Self::unchecked_new(path.as_ref());
 213
 214        #[cfg(target_os = "windows")]
 215        return Self::unchecked_new(dunce::simplified(path.as_ref()));
 216    }
 217
 218    pub fn unchecked_new<T: AsRef<Path> + ?Sized>(path: &T) -> &Self {
 219        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
 220        unsafe { mem::transmute::<&Path, &Self>(path.as_ref()) }
 221    }
 222
 223    pub fn from_arc(path: Arc<Path>) -> Arc<Self> {
 224        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
 225        #[cfg(not(target_os = "windows"))]
 226        return unsafe { mem::transmute::<Arc<Path>, Arc<Self>>(path) };
 227
 228        // TODO: could avoid allocating here if dunce::simplified results in the same path
 229        #[cfg(target_os = "windows")]
 230        return Self::new(&path).into();
 231    }
 232
 233    pub fn new_arc<T: AsRef<Path> + ?Sized>(path: &T) -> Arc<Self> {
 234        Self::new(path).into()
 235    }
 236
 237    pub fn cast_arc(path: Arc<Self>) -> Arc<Path> {
 238        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
 239        unsafe { mem::transmute::<Arc<Self>, Arc<Path>>(path) }
 240    }
 241
 242    pub fn cast_arc_ref(path: &Arc<Self>) -> &Arc<Path> {
 243        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
 244        unsafe { mem::transmute::<&Arc<Self>, &Arc<Path>>(path) }
 245    }
 246
 247    pub fn starts_with(&self, prefix: &Self) -> bool {
 248        self.0.starts_with(&prefix.0)
 249    }
 250
 251    pub fn as_path(&self) -> &Path {
 252        &self.0
 253    }
 254
 255    pub fn file_name(&self) -> Option<&std::ffi::OsStr> {
 256        self.0.file_name()
 257    }
 258
 259    pub fn extension(&self) -> Option<&std::ffi::OsStr> {
 260        self.0.extension()
 261    }
 262
 263    pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
 264        self.0.join(path)
 265    }
 266
 267    pub fn parent(&self) -> Option<&Self> {
 268        self.0.parent().map(Self::unchecked_new)
 269    }
 270
 271    pub fn strip_prefix(&self, base: &Self) -> Result<&Path, StripPrefixError> {
 272        self.0.strip_prefix(base.as_path())
 273    }
 274
 275    pub fn to_str(&self) -> Option<&str> {
 276        self.0.to_str()
 277    }
 278
 279    pub fn to_path_buf(&self) -> PathBuf {
 280        self.0.to_path_buf()
 281    }
 282}
 283
 284impl std::fmt::Debug for SanitizedPath {
 285    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
 286        std::fmt::Debug::fmt(&self.0, formatter)
 287    }
 288}
 289
 290impl Display for SanitizedPath {
 291    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
 292        write!(f, "{}", self.0.display())
 293    }
 294}
 295
 296impl From<&SanitizedPath> for Arc<SanitizedPath> {
 297    fn from(sanitized_path: &SanitizedPath) -> Self {
 298        let path: Arc<Path> = sanitized_path.0.into();
 299        // safe because `Path` and `SanitizedPath` have the same repr and Drop impl
 300        unsafe { mem::transmute(path) }
 301    }
 302}
 303
 304impl From<&SanitizedPath> for PathBuf {
 305    fn from(sanitized_path: &SanitizedPath) -> Self {
 306        sanitized_path.as_path().into()
 307    }
 308}
 309
 310impl AsRef<Path> for SanitizedPath {
 311    fn as_ref(&self) -> &Path {
 312        &self.0
 313    }
 314}
 315
 316#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
 317pub enum PathStyle {
 318    Posix,
 319    Windows,
 320}
 321
 322impl PathStyle {
 323    #[cfg(target_os = "windows")]
 324    pub const fn local() -> Self {
 325        PathStyle::Windows
 326    }
 327
 328    #[cfg(not(target_os = "windows"))]
 329    pub const fn local() -> Self {
 330        PathStyle::Posix
 331    }
 332
 333    #[inline]
 334    pub fn separator(&self) -> &'static str {
 335        match self {
 336            PathStyle::Posix => "/",
 337            PathStyle::Windows => "\\",
 338        }
 339    }
 340
 341    pub fn is_windows(&self) -> bool {
 342        *self == PathStyle::Windows
 343    }
 344
 345    pub fn join(self, left: impl AsRef<Path>, right: impl AsRef<Path>) -> Option<String> {
 346        let right = right.as_ref().to_str()?;
 347        if is_absolute(right, self) {
 348            return None;
 349        }
 350        let left = left.as_ref().to_str()?;
 351        if left.is_empty() {
 352            Some(right.into())
 353        } else {
 354            Some(format!(
 355                "{left}{}{right}",
 356                if left.ends_with(self.separator()) {
 357                    ""
 358                } else {
 359                    self.separator()
 360                }
 361            ))
 362        }
 363    }
 364
 365    pub fn split(self, path_like: &str) -> (Option<&str>, &str) {
 366        let Some(pos) = path_like.rfind(self.separator()) else {
 367            return (None, path_like);
 368        };
 369        let filename_start = pos + self.separator().len();
 370        (
 371            Some(&path_like[..filename_start]),
 372            &path_like[filename_start..],
 373        )
 374    }
 375}
 376
 377#[derive(Debug, Clone)]
 378pub struct RemotePathBuf {
 379    style: PathStyle,
 380    string: String,
 381}
 382
 383impl RemotePathBuf {
 384    pub fn new(string: String, style: PathStyle) -> Self {
 385        Self { style, string }
 386    }
 387
 388    pub fn from_str(path: &str, style: PathStyle) -> Self {
 389        Self::new(path.to_string(), style)
 390    }
 391
 392    pub fn path_style(&self) -> PathStyle {
 393        self.style
 394    }
 395
 396    pub fn to_proto(self) -> String {
 397        self.string
 398    }
 399}
 400
 401impl Display for RemotePathBuf {
 402    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 403        write!(f, "{}", self.string)
 404    }
 405}
 406
 407pub fn is_absolute(path_like: &str, path_style: PathStyle) -> bool {
 408    path_like.starts_with('/')
 409        || path_style == PathStyle::Windows
 410            && (path_like.starts_with('\\')
 411                || path_like
 412                    .chars()
 413                    .next()
 414                    .is_some_and(|c| c.is_ascii_alphabetic())
 415                    && path_like[1..]
 416                        .strip_prefix(':')
 417                        .is_some_and(|path| path.starts_with('/') || path.starts_with('\\')))
 418}
 419
 420#[derive(Debug, PartialEq)]
 421#[non_exhaustive]
 422pub struct NormalizeError;
 423
 424impl Error for NormalizeError {}
 425
 426impl std::fmt::Display for NormalizeError {
 427    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 428        f.write_str("parent reference `..` points outside of base directory")
 429    }
 430}
 431
 432/// Copied from stdlib where it's unstable.
 433///
 434/// Normalize a path, including `..` without traversing the filesystem.
 435///
 436/// Returns an error if normalization would leave leading `..` components.
 437///
 438/// <div class="warning">
 439///
 440/// This function always resolves `..` to the "lexical" parent.
 441/// That is "a/b/../c" will always resolve to `a/c` which can change the meaning of the path.
 442/// In particular, `a/c` and `a/b/../c` are distinct on many systems because `b` may be a symbolic link, so its parent isn't `a`.
 443///
 444/// </div>
 445///
 446/// [`path::absolute`](absolute) is an alternative that preserves `..`.
 447/// Or [`Path::canonicalize`] can be used to resolve any `..` by querying the filesystem.
 448pub fn normalize_lexically(path: &Path) -> Result<PathBuf, NormalizeError> {
 449    use std::path::Component;
 450
 451    let mut lexical = PathBuf::new();
 452    let mut iter = path.components().peekable();
 453
 454    // Find the root, if any, and add it to the lexical path.
 455    // Here we treat the Windows path "C:\" as a single "root" even though
 456    // `components` splits it into two: (Prefix, RootDir).
 457    let root = match iter.peek() {
 458        Some(Component::ParentDir) => return Err(NormalizeError),
 459        Some(p @ Component::RootDir) | Some(p @ Component::CurDir) => {
 460            lexical.push(p);
 461            iter.next();
 462            lexical.as_os_str().len()
 463        }
 464        Some(Component::Prefix(prefix)) => {
 465            lexical.push(prefix.as_os_str());
 466            iter.next();
 467            if let Some(p @ Component::RootDir) = iter.peek() {
 468                lexical.push(p);
 469                iter.next();
 470            }
 471            lexical.as_os_str().len()
 472        }
 473        None => return Ok(PathBuf::new()),
 474        Some(Component::Normal(_)) => 0,
 475    };
 476
 477    for component in iter {
 478        match component {
 479            Component::RootDir => unreachable!(),
 480            Component::Prefix(_) => return Err(NormalizeError),
 481            Component::CurDir => continue,
 482            Component::ParentDir => {
 483                // It's an error if ParentDir causes us to go above the "root".
 484                if lexical.as_os_str().len() == root {
 485                    return Err(NormalizeError);
 486                } else {
 487                    lexical.pop();
 488                }
 489            }
 490            Component::Normal(path) => lexical.push(path),
 491        }
 492    }
 493    Ok(lexical)
 494}
 495
 496/// A delimiter to use in `path_query:row_number:column_number` strings parsing.
 497pub const FILE_ROW_COLUMN_DELIMITER: char = ':';
 498
 499const ROW_COL_CAPTURE_REGEX: &str = r"(?xs)
 500    ([^\(]+)\:(?:
 501        \((\d+)[,:](\d+)\) # filename:(row,column), filename:(row:column)
 502        |
 503        \((\d+)\)()     # filename:(row)
 504    )
 505    |
 506    ([^\(]+)(?:
 507        \((\d+)[,:](\d+)\) # filename(row,column), filename(row:column)
 508        |
 509        \((\d+)\)()     # filename(row)
 510    )
 511    |
 512    (.+?)(?:
 513        \:+(\d+)\:(\d+)\:*$  # filename:row:column
 514        |
 515        \:+(\d+)\:*()$       # filename:row
 516        |
 517        \:+()()$
 518    )";
 519
 520/// A representation of a path-like string with optional row and column numbers.
 521/// Matching values example: `te`, `test.rs:22`, `te:22:5`, `test.c(22)`, `test.c(22,5)`etc.
 522#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
 523pub struct PathWithPosition {
 524    pub path: PathBuf,
 525    pub row: Option<u32>,
 526    // Absent if row is absent.
 527    pub column: Option<u32>,
 528}
 529
 530impl PathWithPosition {
 531    /// Returns a PathWithPosition from a path.
 532    pub fn from_path(path: PathBuf) -> Self {
 533        Self {
 534            path,
 535            row: None,
 536            column: None,
 537        }
 538    }
 539
 540    /// Parses a string that possibly has `:row:column` or `(row, column)` suffix.
 541    /// Parenthesis format is used by [MSBuild](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-diagnostic-format-for-tasks) compatible tools
 542    /// Ignores trailing `:`s, so `test.rs:22:` is parsed as `test.rs:22`.
 543    /// If the suffix parsing fails, the whole string is parsed as a path.
 544    ///
 545    /// Be mindful that `test_file:10:1:` is a valid posix filename.
 546    /// `PathWithPosition` class assumes that the ending position-like suffix is **not** part of the filename.
 547    ///
 548    /// # Examples
 549    ///
 550    /// ```
 551    /// # use util::paths::PathWithPosition;
 552    /// # use std::path::PathBuf;
 553    /// assert_eq!(PathWithPosition::parse_str("test_file"), PathWithPosition {
 554    ///     path: PathBuf::from("test_file"),
 555    ///     row: None,
 556    ///     column: None,
 557    /// });
 558    /// assert_eq!(PathWithPosition::parse_str("test_file:10"), PathWithPosition {
 559    ///     path: PathBuf::from("test_file"),
 560    ///     row: Some(10),
 561    ///     column: None,
 562    /// });
 563    /// assert_eq!(PathWithPosition::parse_str("test_file.rs"), PathWithPosition {
 564    ///     path: PathBuf::from("test_file.rs"),
 565    ///     row: None,
 566    ///     column: None,
 567    /// });
 568    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1"), PathWithPosition {
 569    ///     path: PathBuf::from("test_file.rs"),
 570    ///     row: Some(1),
 571    ///     column: None,
 572    /// });
 573    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1:2"), PathWithPosition {
 574    ///     path: PathBuf::from("test_file.rs"),
 575    ///     row: Some(1),
 576    ///     column: Some(2),
 577    /// });
 578    /// ```
 579    ///
 580    /// # Expected parsing results when encounter ill-formatted inputs.
 581    /// ```
 582    /// # use util::paths::PathWithPosition;
 583    /// # use std::path::PathBuf;
 584    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:a"), PathWithPosition {
 585    ///     path: PathBuf::from("test_file.rs:a"),
 586    ///     row: None,
 587    ///     column: None,
 588    /// });
 589    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:a:b"), PathWithPosition {
 590    ///     path: PathBuf::from("test_file.rs:a:b"),
 591    ///     row: None,
 592    ///     column: None,
 593    /// });
 594    /// assert_eq!(PathWithPosition::parse_str("test_file.rs"), PathWithPosition {
 595    ///     path: PathBuf::from("test_file.rs"),
 596    ///     row: None,
 597    ///     column: None,
 598    /// });
 599    /// assert_eq!(PathWithPosition::parse_str("test_file.rs::1"), PathWithPosition {
 600    ///     path: PathBuf::from("test_file.rs"),
 601    ///     row: Some(1),
 602    ///     column: None,
 603    /// });
 604    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1::"), PathWithPosition {
 605    ///     path: PathBuf::from("test_file.rs"),
 606    ///     row: Some(1),
 607    ///     column: None,
 608    /// });
 609    /// assert_eq!(PathWithPosition::parse_str("test_file.rs::1:2"), PathWithPosition {
 610    ///     path: PathBuf::from("test_file.rs"),
 611    ///     row: Some(1),
 612    ///     column: Some(2),
 613    /// });
 614    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1::2"), PathWithPosition {
 615    ///     path: PathBuf::from("test_file.rs:1"),
 616    ///     row: Some(2),
 617    ///     column: None,
 618    /// });
 619    /// assert_eq!(PathWithPosition::parse_str("test_file.rs:1:2:3"), PathWithPosition {
 620    ///     path: PathBuf::from("test_file.rs:1"),
 621    ///     row: Some(2),
 622    ///     column: Some(3),
 623    /// });
 624    /// ```
 625    pub fn parse_str(s: &str) -> Self {
 626        let trimmed = s.trim();
 627        let path = Path::new(trimmed);
 628        let Some(maybe_file_name_with_row_col) = path.file_name().unwrap_or_default().to_str()
 629        else {
 630            return Self {
 631                path: Path::new(s).to_path_buf(),
 632                row: None,
 633                column: None,
 634            };
 635        };
 636        if maybe_file_name_with_row_col.is_empty() {
 637            return Self {
 638                path: Path::new(s).to_path_buf(),
 639                row: None,
 640                column: None,
 641            };
 642        }
 643
 644        // Let's avoid repeated init cost on this. It is subject to thread contention, but
 645        // so far this code isn't called from multiple hot paths. Getting contention here
 646        // in the future seems unlikely.
 647        static SUFFIX_RE: LazyLock<Regex> =
 648            LazyLock::new(|| Regex::new(ROW_COL_CAPTURE_REGEX).unwrap());
 649        match SUFFIX_RE
 650            .captures(maybe_file_name_with_row_col)
 651            .map(|caps| caps.extract())
 652        {
 653            Some((_, [file_name, maybe_row, maybe_column])) => {
 654                let row = maybe_row.parse::<u32>().ok();
 655                let column = maybe_column.parse::<u32>().ok();
 656
 657                let (_, suffix) = trimmed.split_once(file_name).unwrap();
 658                let path_without_suffix = &trimmed[..trimmed.len() - suffix.len()];
 659
 660                Self {
 661                    path: Path::new(path_without_suffix).to_path_buf(),
 662                    row,
 663                    column,
 664                }
 665            }
 666            None => {
 667                // The `ROW_COL_CAPTURE_REGEX` deals with separated digits only,
 668                // but in reality there could be `foo/bar.py:22:in` inputs which we want to match too.
 669                // The regex mentioned is not very extendable with "digit or random string" checks, so do this here instead.
 670                let delimiter = ':';
 671                let mut path_parts = s
 672                    .rsplitn(3, delimiter)
 673                    .collect::<Vec<_>>()
 674                    .into_iter()
 675                    .rev()
 676                    .fuse();
 677                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();
 678                let mut row = None;
 679                let mut column = None;
 680                if let Some(maybe_row) = path_parts.next() {
 681                    if let Ok(parsed_row) = maybe_row.parse::<u32>() {
 682                        row = Some(parsed_row);
 683                        if let Some(parsed_column) = path_parts
 684                            .next()
 685                            .and_then(|maybe_col| maybe_col.parse::<u32>().ok())
 686                        {
 687                            column = Some(parsed_column);
 688                        }
 689                    } else {
 690                        path_string.push(delimiter);
 691                        path_string.push_str(maybe_row);
 692                    }
 693                }
 694                for split in path_parts {
 695                    path_string.push(delimiter);
 696                    path_string.push_str(split);
 697                }
 698
 699                Self {
 700                    path: PathBuf::from(path_string),
 701                    row,
 702                    column,
 703                }
 704            }
 705        }
 706    }
 707
 708    pub fn map_path<E>(
 709        self,
 710        mapping: impl FnOnce(PathBuf) -> Result<PathBuf, E>,
 711    ) -> Result<PathWithPosition, E> {
 712        Ok(PathWithPosition {
 713            path: mapping(self.path)?,
 714            row: self.row,
 715            column: self.column,
 716        })
 717    }
 718
 719    pub fn to_string(&self, path_to_string: impl Fn(&PathBuf) -> String) -> String {
 720        let path_string = path_to_string(&self.path);
 721        if let Some(row) = self.row {
 722            if let Some(column) = self.column {
 723                format!("{path_string}:{row}:{column}")
 724            } else {
 725                format!("{path_string}:{row}")
 726            }
 727        } else {
 728            path_string
 729        }
 730    }
 731}
 732
 733#[derive(Clone, Debug)]
 734pub struct PathMatcher {
 735    sources: Vec<String>,
 736    glob: GlobSet,
 737    path_style: PathStyle,
 738}
 739
 740// impl std::fmt::Display for PathMatcher {
 741//     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 742//         self.sources.fmt(f)
 743//     }
 744// }
 745
 746impl PartialEq for PathMatcher {
 747    fn eq(&self, other: &Self) -> bool {
 748        self.sources.eq(&other.sources)
 749    }
 750}
 751
 752impl Eq for PathMatcher {}
 753
 754impl PathMatcher {
 755    pub fn new(
 756        globs: impl IntoIterator<Item = impl AsRef<str>>,
 757        path_style: PathStyle,
 758    ) -> Result<Self, globset::Error> {
 759        let globs = globs
 760            .into_iter()
 761            .map(|as_str| Glob::new(as_str.as_ref()))
 762            .collect::<Result<Vec<_>, _>>()?;
 763        let sources = globs.iter().map(|glob| glob.glob().to_owned()).collect();
 764        let mut glob_builder = GlobSetBuilder::new();
 765        for single_glob in globs {
 766            glob_builder.add(single_glob);
 767        }
 768        let glob = glob_builder.build()?;
 769        Ok(PathMatcher {
 770            glob,
 771            sources,
 772            path_style,
 773        })
 774    }
 775
 776    pub fn sources(&self) -> &[String] {
 777        &self.sources
 778    }
 779
 780    pub fn is_match<P: AsRef<Path>>(&self, other: P) -> bool {
 781        let other_path = other.as_ref();
 782        self.sources.iter().any(|source| {
 783            let as_bytes = other_path.as_os_str().as_encoded_bytes();
 784            as_bytes.starts_with(source.as_bytes()) || as_bytes.ends_with(source.as_bytes())
 785        }) || self.glob.is_match(other_path)
 786            || self.check_with_end_separator(other_path)
 787    }
 788
 789    fn check_with_end_separator(&self, path: &Path) -> bool {
 790        let path_str = path.to_string_lossy();
 791        let separator = self.path_style.separator();
 792        if path_str.ends_with(separator) {
 793            false
 794        } else {
 795            self.glob.is_match(path_str.to_string() + separator)
 796        }
 797    }
 798}
 799
 800impl Default for PathMatcher {
 801    fn default() -> Self {
 802        Self {
 803            path_style: PathStyle::local(),
 804            glob: GlobSet::empty(),
 805            sources: vec![],
 806        }
 807    }
 808}
 809
 810/// Compares two sequences of consecutive digits for natural sorting.
 811///
 812/// This function is a core component of natural sorting that handles numeric comparison
 813/// in a way that feels natural to humans. It extracts and compares consecutive digit
 814/// sequences from two iterators, handling various cases like leading zeros and very large numbers.
 815///
 816/// # Behavior
 817///
 818/// The function implements the following comparison rules:
 819/// 1. Different numeric values: Compares by actual numeric value (e.g., "2" < "10")
 820/// 2. Leading zeros: When values are equal, longer sequence wins (e.g., "002" > "2")
 821/// 3. Large numbers: Falls back to string comparison for numbers that would overflow u128
 822///
 823/// # Examples
 824///
 825/// ```text
 826/// "1" vs "2"      -> Less       (different values)
 827/// "2" vs "10"     -> Less       (numeric comparison)
 828/// "002" vs "2"    -> Greater    (leading zeros)
 829/// "10" vs "010"   -> Less       (leading zeros)
 830/// "999..." vs "1000..." -> Less (large number comparison)
 831/// ```
 832///
 833/// # Implementation Details
 834///
 835/// 1. Extracts consecutive digits into strings
 836/// 2. Compares sequence lengths for leading zero handling
 837/// 3. For equal lengths, compares digit by digit
 838/// 4. For different lengths:
 839///    - Attempts numeric comparison first (for numbers up to 2^128 - 1)
 840///    - Falls back to string comparison if numbers would overflow
 841///
 842/// The function advances both iterators past their respective numeric sequences,
 843/// regardless of the comparison result.
 844fn compare_numeric_segments<I>(
 845    a_iter: &mut std::iter::Peekable<I>,
 846    b_iter: &mut std::iter::Peekable<I>,
 847) -> Ordering
 848where
 849    I: Iterator<Item = char>,
 850{
 851    // Collect all consecutive digits into strings
 852    let mut a_num_str = String::new();
 853    let mut b_num_str = String::new();
 854
 855    while let Some(&c) = a_iter.peek() {
 856        if !c.is_ascii_digit() {
 857            break;
 858        }
 859
 860        a_num_str.push(c);
 861        a_iter.next();
 862    }
 863
 864    while let Some(&c) = b_iter.peek() {
 865        if !c.is_ascii_digit() {
 866            break;
 867        }
 868
 869        b_num_str.push(c);
 870        b_iter.next();
 871    }
 872
 873    // First compare lengths (handle leading zeros)
 874    match a_num_str.len().cmp(&b_num_str.len()) {
 875        Ordering::Equal => {
 876            // Same length, compare digit by digit
 877            match a_num_str.cmp(&b_num_str) {
 878                Ordering::Equal => Ordering::Equal,
 879                ordering => ordering,
 880            }
 881        }
 882
 883        // Different lengths but same value means leading zeros
 884        ordering => {
 885            // Try parsing as numbers first
 886            if let (Ok(a_val), Ok(b_val)) = (a_num_str.parse::<u128>(), b_num_str.parse::<u128>()) {
 887                match a_val.cmp(&b_val) {
 888                    Ordering::Equal => ordering, // Same value, longer one is greater (leading zeros)
 889                    ord => ord,
 890                }
 891            } else {
 892                // If parsing fails (overflow), compare as strings
 893                a_num_str.cmp(&b_num_str)
 894            }
 895        }
 896    }
 897}
 898
 899/// Performs natural sorting comparison between two strings.
 900///
 901/// Natural sorting is an ordering that handles numeric sequences in a way that matches human expectations.
 902/// For example, "file2" comes before "file10" (unlike standard lexicographic sorting).
 903///
 904/// # Characteristics
 905///
 906/// * Case-sensitive with lowercase priority: When comparing same letters, lowercase comes before uppercase
 907/// * Numbers are compared by numeric value, not character by character
 908/// * Leading zeros affect ordering when numeric values are equal
 909/// * Can handle numbers larger than u128::MAX (falls back to string comparison)
 910/// * When strings are equal case-insensitively, lowercase is prioritized (lowercase < uppercase)
 911///
 912/// # Algorithm
 913///
 914/// The function works by:
 915/// 1. Processing strings character by character in a case-insensitive manner
 916/// 2. When encountering digits, treating consecutive digits as a single number
 917/// 3. Comparing numbers by their numeric value rather than lexicographically
 918/// 4. For non-numeric characters, using case-insensitive comparison
 919/// 5. If everything is equal case-insensitively, using case-sensitive comparison as final tie-breaker
 920pub fn natural_sort(a: &str, b: &str) -> Ordering {
 921    let mut a_iter = a.chars().peekable();
 922    let mut b_iter = b.chars().peekable();
 923
 924    loop {
 925        match (a_iter.peek(), b_iter.peek()) {
 926            (None, None) => {
 927                return b.cmp(a);
 928            }
 929            (None, _) => return Ordering::Less,
 930            (_, None) => return Ordering::Greater,
 931            (Some(&a_char), Some(&b_char)) => {
 932                if a_char.is_ascii_digit() && b_char.is_ascii_digit() {
 933                    match compare_numeric_segments(&mut a_iter, &mut b_iter) {
 934                        Ordering::Equal => continue,
 935                        ordering => return ordering,
 936                    }
 937                } else {
 938                    match a_char
 939                        .to_ascii_lowercase()
 940                        .cmp(&b_char.to_ascii_lowercase())
 941                    {
 942                        Ordering::Equal => {
 943                            a_iter.next();
 944                            b_iter.next();
 945                        }
 946                        ordering => return ordering,
 947                    }
 948                }
 949            }
 950        }
 951    }
 952}
 953
 954/// Case-insensitive natural sort without applying the final lowercase/uppercase tie-breaker.
 955/// This is useful when comparing individual path components where we want to keep walking
 956/// deeper components before deciding on casing.
 957fn natural_sort_no_tiebreak(a: &str, b: &str) -> Ordering {
 958    if a.eq_ignore_ascii_case(b) {
 959        Ordering::Equal
 960    } else {
 961        natural_sort(a, b)
 962    }
 963}
 964
 965fn stem_and_extension(filename: &str) -> (Option<&str>, Option<&str>) {
 966    if filename.is_empty() {
 967        return (None, None);
 968    }
 969
 970    match filename.rsplit_once('.') {
 971        // Case 1: No dot was found. The entire name is the stem.
 972        None => (Some(filename), None),
 973
 974        // Case 2: A dot was found.
 975        Some((before, after)) => {
 976            // This is the crucial check for dotfiles like ".bashrc".
 977            // If `before` is empty, the dot was the first character.
 978            // In that case, we revert to the "whole name is the stem" logic.
 979            if before.is_empty() {
 980                (Some(filename), None)
 981            } else {
 982                // Otherwise, we have a standard stem and extension.
 983                (Some(before), Some(after))
 984            }
 985        }
 986    }
 987}
 988
 989pub fn compare_rel_paths(
 990    (path_a, a_is_file): (&RelPath, bool),
 991    (path_b, b_is_file): (&RelPath, bool),
 992) -> Ordering {
 993    let mut components_a = path_a.components();
 994    let mut components_b = path_b.components();
 995    loop {
 996        match (components_a.next(), components_b.next()) {
 997            (Some(component_a), Some(component_b)) => {
 998                let a_is_file = a_is_file && components_a.rest().is_empty();
 999                let b_is_file = b_is_file && components_b.rest().is_empty();
1000
1001                let ordering = a_is_file.cmp(&b_is_file).then_with(|| {
1002                    let (a_stem, a_extension) = a_is_file
1003                        .then(|| stem_and_extension(component_a))
1004                        .unwrap_or_default();
1005                    let path_string_a = if a_is_file { a_stem } else { Some(component_a) };
1006
1007                    let (b_stem, b_extension) = b_is_file
1008                        .then(|| stem_and_extension(component_b))
1009                        .unwrap_or_default();
1010                    let path_string_b = if b_is_file { b_stem } else { Some(component_b) };
1011
1012                    let compare_components = match (path_string_a, path_string_b) {
1013                        (Some(a), Some(b)) => natural_sort(&a, &b),
1014                        (Some(_), None) => Ordering::Greater,
1015                        (None, Some(_)) => Ordering::Less,
1016                        (None, None) => Ordering::Equal,
1017                    };
1018
1019                    compare_components.then_with(|| {
1020                        if a_is_file && b_is_file {
1021                            let ext_a = a_extension.unwrap_or_default();
1022                            let ext_b = b_extension.unwrap_or_default();
1023                            ext_a.cmp(ext_b)
1024                        } else {
1025                            Ordering::Equal
1026                        }
1027                    })
1028                });
1029
1030                if !ordering.is_eq() {
1031                    return ordering;
1032                }
1033            }
1034            (Some(_), None) => break Ordering::Greater,
1035            (None, Some(_)) => break Ordering::Less,
1036            (None, None) => break Ordering::Equal,
1037        }
1038    }
1039}
1040
1041/// Compare two relative paths with mixed files and directories using
1042/// case-insensitive natural sorting. For example, "Apple", "aardvark.txt",
1043/// and "Zebra" would be sorted as: aardvark.txt, Apple, Zebra
1044/// (case-insensitive alphabetical).
1045pub fn compare_rel_paths_mixed(
1046    (path_a, a_is_file): (&RelPath, bool),
1047    (path_b, b_is_file): (&RelPath, bool),
1048) -> Ordering {
1049    let original_paths_equal = std::ptr::eq(path_a, path_b) || path_a == path_b;
1050    let mut components_a = path_a.components();
1051    let mut components_b = path_b.components();
1052
1053    loop {
1054        match (components_a.next(), components_b.next()) {
1055            (Some(component_a), Some(component_b)) => {
1056                let a_leaf_file = a_is_file && components_a.rest().is_empty();
1057                let b_leaf_file = b_is_file && components_b.rest().is_empty();
1058
1059                let (a_stem, a_ext) = a_leaf_file
1060                    .then(|| stem_and_extension(component_a))
1061                    .unwrap_or_default();
1062                let (b_stem, b_ext) = b_leaf_file
1063                    .then(|| stem_and_extension(component_b))
1064                    .unwrap_or_default();
1065                let a_key = if a_leaf_file {
1066                    a_stem
1067                } else {
1068                    Some(component_a)
1069                };
1070                let b_key = if b_leaf_file {
1071                    b_stem
1072                } else {
1073                    Some(component_b)
1074                };
1075
1076                let ordering = match (a_key, b_key) {
1077                    (Some(a), Some(b)) => natural_sort_no_tiebreak(a, b)
1078                        .then_with(|| match (a_leaf_file, b_leaf_file) {
1079                            (true, false) if a == b => Ordering::Greater,
1080                            (false, true) if a == b => Ordering::Less,
1081                            _ => Ordering::Equal,
1082                        })
1083                        .then_with(|| {
1084                            if a_leaf_file && b_leaf_file {
1085                                let a_ext_str = a_ext.unwrap_or_default().to_lowercase();
1086                                let b_ext_str = b_ext.unwrap_or_default().to_lowercase();
1087                                b_ext_str.cmp(&a_ext_str)
1088                            } else {
1089                                Ordering::Equal
1090                            }
1091                        }),
1092                    (Some(_), None) => Ordering::Greater,
1093                    (None, Some(_)) => Ordering::Less,
1094                    (None, None) => Ordering::Equal,
1095                };
1096
1097                if !ordering.is_eq() {
1098                    return ordering;
1099                }
1100            }
1101            (Some(_), None) => return Ordering::Greater,
1102            (None, Some(_)) => return Ordering::Less,
1103            (None, None) => {
1104                // Deterministic tie-break: use natural sort to prefer lowercase when paths
1105                // are otherwise equal but still differ in casing.
1106                if !original_paths_equal {
1107                    return natural_sort(path_a.as_unix_str(), path_b.as_unix_str());
1108                }
1109                return Ordering::Equal;
1110            }
1111        }
1112    }
1113}
1114
1115/// Compare two relative paths with files before directories using
1116/// case-insensitive natural sorting. At each directory level, all files
1117/// are sorted before all directories, with case-insensitive alphabetical
1118/// ordering within each group.
1119pub fn compare_rel_paths_files_first(
1120    (path_a, a_is_file): (&RelPath, bool),
1121    (path_b, b_is_file): (&RelPath, bool),
1122) -> Ordering {
1123    let original_paths_equal = std::ptr::eq(path_a, path_b) || path_a == path_b;
1124    let mut components_a = path_a.components();
1125    let mut components_b = path_b.components();
1126
1127    loop {
1128        match (components_a.next(), components_b.next()) {
1129            (Some(component_a), Some(component_b)) => {
1130                let a_leaf_file = a_is_file && components_a.rest().is_empty();
1131                let b_leaf_file = b_is_file && components_b.rest().is_empty();
1132
1133                let (a_stem, a_ext) = a_leaf_file
1134                    .then(|| stem_and_extension(component_a))
1135                    .unwrap_or_default();
1136                let (b_stem, b_ext) = b_leaf_file
1137                    .then(|| stem_and_extension(component_b))
1138                    .unwrap_or_default();
1139                let a_key = if a_leaf_file {
1140                    a_stem
1141                } else {
1142                    Some(component_a)
1143                };
1144                let b_key = if b_leaf_file {
1145                    b_stem
1146                } else {
1147                    Some(component_b)
1148                };
1149
1150                let ordering = match (a_key, b_key) {
1151                    (Some(a), Some(b)) => {
1152                        if a_leaf_file && !b_leaf_file {
1153                            Ordering::Less
1154                        } else if !a_leaf_file && b_leaf_file {
1155                            Ordering::Greater
1156                        } else {
1157                            natural_sort_no_tiebreak(a, b).then_with(|| {
1158                                if a_leaf_file && b_leaf_file {
1159                                    let a_ext_str = a_ext.unwrap_or_default().to_lowercase();
1160                                    let b_ext_str = b_ext.unwrap_or_default().to_lowercase();
1161                                    a_ext_str.cmp(&b_ext_str)
1162                                } else {
1163                                    Ordering::Equal
1164                                }
1165                            })
1166                        }
1167                    }
1168                    (Some(_), None) => Ordering::Greater,
1169                    (None, Some(_)) => Ordering::Less,
1170                    (None, None) => Ordering::Equal,
1171                };
1172
1173                if !ordering.is_eq() {
1174                    return ordering;
1175                }
1176            }
1177            (Some(_), None) => return Ordering::Greater,
1178            (None, Some(_)) => return Ordering::Less,
1179            (None, None) => {
1180                // Deterministic tie-break: use natural sort to prefer lowercase when paths
1181                // are otherwise equal but still differ in casing.
1182                if !original_paths_equal {
1183                    return natural_sort(path_a.as_unix_str(), path_b.as_unix_str());
1184                }
1185                return Ordering::Equal;
1186            }
1187        }
1188    }
1189}
1190
1191pub fn compare_paths(
1192    (path_a, a_is_file): (&Path, bool),
1193    (path_b, b_is_file): (&Path, bool),
1194) -> Ordering {
1195    let mut components_a = path_a.components().peekable();
1196    let mut components_b = path_b.components().peekable();
1197
1198    loop {
1199        match (components_a.next(), components_b.next()) {
1200            (Some(component_a), Some(component_b)) => {
1201                let a_is_file = components_a.peek().is_none() && a_is_file;
1202                let b_is_file = components_b.peek().is_none() && b_is_file;
1203
1204                let ordering = a_is_file.cmp(&b_is_file).then_with(|| {
1205                    let path_a = Path::new(component_a.as_os_str());
1206                    let path_string_a = if a_is_file {
1207                        path_a.file_stem()
1208                    } else {
1209                        path_a.file_name()
1210                    }
1211                    .map(|s| s.to_string_lossy());
1212
1213                    let path_b = Path::new(component_b.as_os_str());
1214                    let path_string_b = if b_is_file {
1215                        path_b.file_stem()
1216                    } else {
1217                        path_b.file_name()
1218                    }
1219                    .map(|s| s.to_string_lossy());
1220
1221                    let compare_components = match (path_string_a, path_string_b) {
1222                        (Some(a), Some(b)) => natural_sort(&a, &b),
1223                        (Some(_), None) => Ordering::Greater,
1224                        (None, Some(_)) => Ordering::Less,
1225                        (None, None) => Ordering::Equal,
1226                    };
1227
1228                    compare_components.then_with(|| {
1229                        if a_is_file && b_is_file {
1230                            let ext_a = path_a.extension().unwrap_or_default();
1231                            let ext_b = path_b.extension().unwrap_or_default();
1232                            ext_a.cmp(ext_b)
1233                        } else {
1234                            Ordering::Equal
1235                        }
1236                    })
1237                });
1238
1239                if !ordering.is_eq() {
1240                    return ordering;
1241                }
1242            }
1243            (Some(_), None) => break Ordering::Greater,
1244            (None, Some(_)) => break Ordering::Less,
1245            (None, None) => break Ordering::Equal,
1246        }
1247    }
1248}
1249
1250#[derive(Debug, Clone, PartialEq, Eq)]
1251pub struct WslPath {
1252    pub distro: String,
1253
1254    // the reason this is an OsString and not any of the path types is that it needs to
1255    // represent a unix path (with '/' separators) on windows. `from_path` does this by
1256    // manually constructing it from the path components of a given windows path.
1257    pub path: std::ffi::OsString,
1258}
1259
1260impl WslPath {
1261    pub fn from_path<P: AsRef<Path>>(path: P) -> Option<WslPath> {
1262        if cfg!(not(target_os = "windows")) {
1263            return None;
1264        }
1265        use std::{
1266            ffi::OsString,
1267            path::{Component, Prefix},
1268        };
1269
1270        let mut components = path.as_ref().components();
1271        let Some(Component::Prefix(prefix)) = components.next() else {
1272            return None;
1273        };
1274        let (server, distro) = match prefix.kind() {
1275            Prefix::UNC(server, distro) => (server, distro),
1276            Prefix::VerbatimUNC(server, distro) => (server, distro),
1277            _ => return None,
1278        };
1279        let Some(Component::RootDir) = components.next() else {
1280            return None;
1281        };
1282
1283        let server_str = server.to_string_lossy();
1284        if server_str == "wsl.localhost" || server_str == "wsl$" {
1285            let mut result = OsString::from("");
1286            for c in components {
1287                use Component::*;
1288                match c {
1289                    Prefix(p) => unreachable!("got {p:?}, but already stripped prefix"),
1290                    RootDir => unreachable!("got root dir, but already stripped root"),
1291                    CurDir => continue,
1292                    ParentDir => result.push("/.."),
1293                    Normal(s) => {
1294                        result.push("/");
1295                        result.push(s);
1296                    }
1297                }
1298            }
1299            if result.is_empty() {
1300                result.push("/");
1301            }
1302            Some(WslPath {
1303                distro: distro.to_string_lossy().to_string(),
1304                path: result,
1305            })
1306        } else {
1307            None
1308        }
1309    }
1310}
1311
1312#[cfg(test)]
1313mod tests {
1314    use super::*;
1315    use util_macros::perf;
1316
1317    #[perf]
1318    fn compare_paths_with_dots() {
1319        let mut paths = vec![
1320            (Path::new("test_dirs"), false),
1321            (Path::new("test_dirs/1.46"), false),
1322            (Path::new("test_dirs/1.46/bar_1"), true),
1323            (Path::new("test_dirs/1.46/bar_2"), true),
1324            (Path::new("test_dirs/1.45"), false),
1325            (Path::new("test_dirs/1.45/foo_2"), true),
1326            (Path::new("test_dirs/1.45/foo_1"), true),
1327        ];
1328        paths.sort_by(|&a, &b| compare_paths(a, b));
1329        assert_eq!(
1330            paths,
1331            vec![
1332                (Path::new("test_dirs"), false),
1333                (Path::new("test_dirs/1.45"), false),
1334                (Path::new("test_dirs/1.45/foo_1"), true),
1335                (Path::new("test_dirs/1.45/foo_2"), true),
1336                (Path::new("test_dirs/1.46"), false),
1337                (Path::new("test_dirs/1.46/bar_1"), true),
1338                (Path::new("test_dirs/1.46/bar_2"), true),
1339            ]
1340        );
1341        let mut paths = vec![
1342            (Path::new("root1/one.txt"), true),
1343            (Path::new("root1/one.two.txt"), true),
1344        ];
1345        paths.sort_by(|&a, &b| compare_paths(a, b));
1346        assert_eq!(
1347            paths,
1348            vec![
1349                (Path::new("root1/one.txt"), true),
1350                (Path::new("root1/one.two.txt"), true),
1351            ]
1352        );
1353    }
1354
1355    #[perf]
1356    fn compare_paths_with_same_name_different_extensions() {
1357        let mut paths = vec![
1358            (Path::new("test_dirs/file.rs"), true),
1359            (Path::new("test_dirs/file.txt"), true),
1360            (Path::new("test_dirs/file.md"), true),
1361            (Path::new("test_dirs/file"), true),
1362            (Path::new("test_dirs/file.a"), true),
1363        ];
1364        paths.sort_by(|&a, &b| compare_paths(a, b));
1365        assert_eq!(
1366            paths,
1367            vec![
1368                (Path::new("test_dirs/file"), true),
1369                (Path::new("test_dirs/file.a"), true),
1370                (Path::new("test_dirs/file.md"), true),
1371                (Path::new("test_dirs/file.rs"), true),
1372                (Path::new("test_dirs/file.txt"), true),
1373            ]
1374        );
1375    }
1376
1377    #[perf]
1378    fn compare_paths_case_semi_sensitive() {
1379        let mut paths = vec![
1380            (Path::new("test_DIRS"), false),
1381            (Path::new("test_DIRS/foo_1"), true),
1382            (Path::new("test_DIRS/foo_2"), true),
1383            (Path::new("test_DIRS/bar"), true),
1384            (Path::new("test_DIRS/BAR"), true),
1385            (Path::new("test_dirs"), false),
1386            (Path::new("test_dirs/foo_1"), true),
1387            (Path::new("test_dirs/foo_2"), true),
1388            (Path::new("test_dirs/bar"), true),
1389            (Path::new("test_dirs/BAR"), true),
1390        ];
1391        paths.sort_by(|&a, &b| compare_paths(a, b));
1392        assert_eq!(
1393            paths,
1394            vec![
1395                (Path::new("test_dirs"), false),
1396                (Path::new("test_dirs/bar"), true),
1397                (Path::new("test_dirs/BAR"), true),
1398                (Path::new("test_dirs/foo_1"), true),
1399                (Path::new("test_dirs/foo_2"), true),
1400                (Path::new("test_DIRS"), false),
1401                (Path::new("test_DIRS/bar"), true),
1402                (Path::new("test_DIRS/BAR"), true),
1403                (Path::new("test_DIRS/foo_1"), true),
1404                (Path::new("test_DIRS/foo_2"), true),
1405            ]
1406        );
1407    }
1408
1409    #[perf]
1410    fn compare_paths_mixed_case_numeric_ordering() {
1411        let mut entries = [
1412            (Path::new(".config"), false),
1413            (Path::new("Dir1"), false),
1414            (Path::new("dir01"), false),
1415            (Path::new("dir2"), false),
1416            (Path::new("Dir02"), false),
1417            (Path::new("dir10"), false),
1418            (Path::new("Dir10"), false),
1419        ];
1420
1421        entries.sort_by(|&a, &b| compare_paths(a, b));
1422
1423        let ordered: Vec<&str> = entries
1424            .iter()
1425            .map(|(path, _)| path.to_str().unwrap())
1426            .collect();
1427
1428        assert_eq!(
1429            ordered,
1430            vec![
1431                ".config", "Dir1", "dir01", "dir2", "Dir02", "dir10", "Dir10"
1432            ]
1433        );
1434    }
1435
1436    #[perf]
1437    fn compare_rel_paths_mixed_case_insensitive() {
1438        // Test that mixed mode is case-insensitive
1439        let mut paths = vec![
1440            (RelPath::unix("zebra.txt").unwrap(), true),
1441            (RelPath::unix("Apple").unwrap(), false),
1442            (RelPath::unix("banana.rs").unwrap(), true),
1443            (RelPath::unix("Carrot").unwrap(), false),
1444            (RelPath::unix("aardvark.txt").unwrap(), true),
1445        ];
1446        paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1447        // Case-insensitive: aardvark < Apple < banana < Carrot < zebra
1448        assert_eq!(
1449            paths,
1450            vec![
1451                (RelPath::unix("aardvark.txt").unwrap(), true),
1452                (RelPath::unix("Apple").unwrap(), false),
1453                (RelPath::unix("banana.rs").unwrap(), true),
1454                (RelPath::unix("Carrot").unwrap(), false),
1455                (RelPath::unix("zebra.txt").unwrap(), true),
1456            ]
1457        );
1458    }
1459
1460    #[perf]
1461    fn compare_rel_paths_files_first_basic() {
1462        // Test that files come before directories
1463        let mut paths = vec![
1464            (RelPath::unix("zebra.txt").unwrap(), true),
1465            (RelPath::unix("Apple").unwrap(), false),
1466            (RelPath::unix("banana.rs").unwrap(), true),
1467            (RelPath::unix("Carrot").unwrap(), false),
1468            (RelPath::unix("aardvark.txt").unwrap(), true),
1469        ];
1470        paths.sort_by(|&a, &b| compare_rel_paths_files_first(a, b));
1471        // Files first (case-insensitive), then directories (case-insensitive)
1472        assert_eq!(
1473            paths,
1474            vec![
1475                (RelPath::unix("aardvark.txt").unwrap(), true),
1476                (RelPath::unix("banana.rs").unwrap(), true),
1477                (RelPath::unix("zebra.txt").unwrap(), true),
1478                (RelPath::unix("Apple").unwrap(), false),
1479                (RelPath::unix("Carrot").unwrap(), false),
1480            ]
1481        );
1482    }
1483
1484    #[perf]
1485    fn compare_rel_paths_files_first_case_insensitive() {
1486        // Test case-insensitive sorting within files and directories
1487        let mut paths = vec![
1488            (RelPath::unix("Zebra.txt").unwrap(), true),
1489            (RelPath::unix("apple").unwrap(), false),
1490            (RelPath::unix("Banana.rs").unwrap(), true),
1491            (RelPath::unix("carrot").unwrap(), false),
1492            (RelPath::unix("Aardvark.txt").unwrap(), true),
1493        ];
1494        paths.sort_by(|&a, &b| compare_rel_paths_files_first(a, b));
1495        assert_eq!(
1496            paths,
1497            vec![
1498                (RelPath::unix("Aardvark.txt").unwrap(), true),
1499                (RelPath::unix("Banana.rs").unwrap(), true),
1500                (RelPath::unix("Zebra.txt").unwrap(), true),
1501                (RelPath::unix("apple").unwrap(), false),
1502                (RelPath::unix("carrot").unwrap(), false),
1503            ]
1504        );
1505    }
1506
1507    #[perf]
1508    fn compare_rel_paths_files_first_numeric() {
1509        // Test natural number sorting with files first
1510        let mut paths = vec![
1511            (RelPath::unix("file10.txt").unwrap(), true),
1512            (RelPath::unix("dir2").unwrap(), false),
1513            (RelPath::unix("file2.txt").unwrap(), true),
1514            (RelPath::unix("dir10").unwrap(), false),
1515            (RelPath::unix("file1.txt").unwrap(), true),
1516        ];
1517        paths.sort_by(|&a, &b| compare_rel_paths_files_first(a, b));
1518        assert_eq!(
1519            paths,
1520            vec![
1521                (RelPath::unix("file1.txt").unwrap(), true),
1522                (RelPath::unix("file2.txt").unwrap(), true),
1523                (RelPath::unix("file10.txt").unwrap(), true),
1524                (RelPath::unix("dir2").unwrap(), false),
1525                (RelPath::unix("dir10").unwrap(), false),
1526            ]
1527        );
1528    }
1529
1530    #[perf]
1531    fn compare_rel_paths_mixed_case() {
1532        // Test case-insensitive sorting with varied capitalization
1533        let mut paths = vec![
1534            (RelPath::unix("README.md").unwrap(), true),
1535            (RelPath::unix("readme.txt").unwrap(), true),
1536            (RelPath::unix("ReadMe.rs").unwrap(), true),
1537        ];
1538        paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1539        // All "readme" variants should group together, sorted by extension
1540        assert_eq!(
1541            paths,
1542            vec![
1543                (RelPath::unix("readme.txt").unwrap(), true),
1544                (RelPath::unix("ReadMe.rs").unwrap(), true),
1545                (RelPath::unix("README.md").unwrap(), true),
1546            ]
1547        );
1548    }
1549
1550    #[perf]
1551    fn compare_rel_paths_mixed_files_and_dirs() {
1552        // Verify directories and files are still mixed
1553        let mut paths = vec![
1554            (RelPath::unix("file2.txt").unwrap(), true),
1555            (RelPath::unix("Dir1").unwrap(), false),
1556            (RelPath::unix("file1.txt").unwrap(), true),
1557            (RelPath::unix("dir2").unwrap(), false),
1558        ];
1559        paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1560        // Case-insensitive: dir1, dir2, file1, file2 (all mixed)
1561        assert_eq!(
1562            paths,
1563            vec![
1564                (RelPath::unix("Dir1").unwrap(), false),
1565                (RelPath::unix("dir2").unwrap(), false),
1566                (RelPath::unix("file1.txt").unwrap(), true),
1567                (RelPath::unix("file2.txt").unwrap(), true),
1568            ]
1569        );
1570    }
1571
1572    #[perf]
1573    fn compare_rel_paths_mixed_with_nested_paths() {
1574        // Test that nested paths still work correctly
1575        let mut paths = vec![
1576            (RelPath::unix("src/main.rs").unwrap(), true),
1577            (RelPath::unix("Cargo.toml").unwrap(), true),
1578            (RelPath::unix("src").unwrap(), false),
1579            (RelPath::unix("target").unwrap(), false),
1580        ];
1581        paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1582        assert_eq!(
1583            paths,
1584            vec![
1585                (RelPath::unix("Cargo.toml").unwrap(), true),
1586                (RelPath::unix("src").unwrap(), false),
1587                (RelPath::unix("src/main.rs").unwrap(), true),
1588                (RelPath::unix("target").unwrap(), false),
1589            ]
1590        );
1591    }
1592
1593    #[perf]
1594    fn compare_rel_paths_files_first_with_nested() {
1595        // Files come before directories, even with nested paths
1596        let mut paths = vec![
1597            (RelPath::unix("src/lib.rs").unwrap(), true),
1598            (RelPath::unix("README.md").unwrap(), true),
1599            (RelPath::unix("src").unwrap(), false),
1600            (RelPath::unix("tests").unwrap(), false),
1601        ];
1602        paths.sort_by(|&a, &b| compare_rel_paths_files_first(a, b));
1603        assert_eq!(
1604            paths,
1605            vec![
1606                (RelPath::unix("README.md").unwrap(), true),
1607                (RelPath::unix("src").unwrap(), false),
1608                (RelPath::unix("src/lib.rs").unwrap(), true),
1609                (RelPath::unix("tests").unwrap(), false),
1610            ]
1611        );
1612    }
1613
1614    #[perf]
1615    fn compare_rel_paths_mixed_dotfiles() {
1616        // Test that dotfiles are handled correctly in mixed mode
1617        let mut paths = vec![
1618            (RelPath::unix(".gitignore").unwrap(), true),
1619            (RelPath::unix("README.md").unwrap(), true),
1620            (RelPath::unix(".github").unwrap(), false),
1621            (RelPath::unix("src").unwrap(), false),
1622        ];
1623        paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1624        assert_eq!(
1625            paths,
1626            vec![
1627                (RelPath::unix(".github").unwrap(), false),
1628                (RelPath::unix(".gitignore").unwrap(), true),
1629                (RelPath::unix("README.md").unwrap(), true),
1630                (RelPath::unix("src").unwrap(), false),
1631            ]
1632        );
1633    }
1634
1635    #[perf]
1636    fn compare_rel_paths_files_first_dotfiles() {
1637        // Test that dotfiles come first when they're files
1638        let mut paths = vec![
1639            (RelPath::unix(".gitignore").unwrap(), true),
1640            (RelPath::unix("README.md").unwrap(), true),
1641            (RelPath::unix(".github").unwrap(), false),
1642            (RelPath::unix("src").unwrap(), false),
1643        ];
1644        paths.sort_by(|&a, &b| compare_rel_paths_files_first(a, b));
1645        assert_eq!(
1646            paths,
1647            vec![
1648                (RelPath::unix(".gitignore").unwrap(), true),
1649                (RelPath::unix("README.md").unwrap(), true),
1650                (RelPath::unix(".github").unwrap(), false),
1651                (RelPath::unix("src").unwrap(), false),
1652            ]
1653        );
1654    }
1655
1656    #[perf]
1657    fn compare_rel_paths_mixed_same_stem_different_extension() {
1658        // Files with same stem but different extensions should sort by extension
1659        let mut paths = vec![
1660            (RelPath::unix("file.rs").unwrap(), true),
1661            (RelPath::unix("file.md").unwrap(), true),
1662            (RelPath::unix("file.txt").unwrap(), true),
1663        ];
1664        paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1665        assert_eq!(
1666            paths,
1667            vec![
1668                (RelPath::unix("file.txt").unwrap(), true),
1669                (RelPath::unix("file.rs").unwrap(), true),
1670                (RelPath::unix("file.md").unwrap(), true),
1671            ]
1672        );
1673    }
1674
1675    #[perf]
1676    fn compare_rel_paths_files_first_same_stem() {
1677        // Same stem files should still sort by extension with files_first
1678        let mut paths = vec![
1679            (RelPath::unix("main.rs").unwrap(), true),
1680            (RelPath::unix("main.c").unwrap(), true),
1681            (RelPath::unix("main").unwrap(), false),
1682        ];
1683        paths.sort_by(|&a, &b| compare_rel_paths_files_first(a, b));
1684        assert_eq!(
1685            paths,
1686            vec![
1687                (RelPath::unix("main.c").unwrap(), true),
1688                (RelPath::unix("main.rs").unwrap(), true),
1689                (RelPath::unix("main").unwrap(), false),
1690            ]
1691        );
1692    }
1693
1694    #[perf]
1695    fn compare_rel_paths_mixed_deep_nesting() {
1696        // Test sorting with deeply nested paths
1697        let mut paths = vec![
1698            (RelPath::unix("a/b/c.txt").unwrap(), true),
1699            (RelPath::unix("A/B.txt").unwrap(), true),
1700            (RelPath::unix("a.txt").unwrap(), true),
1701            (RelPath::unix("A.txt").unwrap(), true),
1702        ];
1703        paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1704        assert_eq!(
1705            paths,
1706            vec![
1707                (RelPath::unix("A/B.txt").unwrap(), true),
1708                (RelPath::unix("a/b/c.txt").unwrap(), true),
1709                (RelPath::unix("a.txt").unwrap(), true),
1710                (RelPath::unix("A.txt").unwrap(), true),
1711            ]
1712        );
1713    }
1714
1715    #[perf]
1716    fn path_with_position_parse_posix_path() {
1717        // Test POSIX filename edge cases
1718        // Read more at https://en.wikipedia.org/wiki/Filename
1719        assert_eq!(
1720            PathWithPosition::parse_str("test_file"),
1721            PathWithPosition {
1722                path: PathBuf::from("test_file"),
1723                row: None,
1724                column: None
1725            }
1726        );
1727
1728        assert_eq!(
1729            PathWithPosition::parse_str("a:bc:.zip:1"),
1730            PathWithPosition {
1731                path: PathBuf::from("a:bc:.zip"),
1732                row: Some(1),
1733                column: None
1734            }
1735        );
1736
1737        assert_eq!(
1738            PathWithPosition::parse_str("one.second.zip:1"),
1739            PathWithPosition {
1740                path: PathBuf::from("one.second.zip"),
1741                row: Some(1),
1742                column: None
1743            }
1744        );
1745
1746        // Trim off trailing `:`s for otherwise valid input.
1747        assert_eq!(
1748            PathWithPosition::parse_str("test_file:10:1:"),
1749            PathWithPosition {
1750                path: PathBuf::from("test_file"),
1751                row: Some(10),
1752                column: Some(1)
1753            }
1754        );
1755
1756        assert_eq!(
1757            PathWithPosition::parse_str("test_file.rs:"),
1758            PathWithPosition {
1759                path: PathBuf::from("test_file.rs"),
1760                row: None,
1761                column: None
1762            }
1763        );
1764
1765        assert_eq!(
1766            PathWithPosition::parse_str("test_file.rs:1:"),
1767            PathWithPosition {
1768                path: PathBuf::from("test_file.rs"),
1769                row: Some(1),
1770                column: None
1771            }
1772        );
1773
1774        assert_eq!(
1775            PathWithPosition::parse_str("ab\ncd"),
1776            PathWithPosition {
1777                path: PathBuf::from("ab\ncd"),
1778                row: None,
1779                column: None
1780            }
1781        );
1782
1783        assert_eq!(
1784            PathWithPosition::parse_str("👋\nab"),
1785            PathWithPosition {
1786                path: PathBuf::from("👋\nab"),
1787                row: None,
1788                column: None
1789            }
1790        );
1791
1792        assert_eq!(
1793            PathWithPosition::parse_str("Types.hs:(617,9)-(670,28):"),
1794            PathWithPosition {
1795                path: PathBuf::from("Types.hs"),
1796                row: Some(617),
1797                column: Some(9),
1798            }
1799        );
1800    }
1801
1802    #[perf]
1803    #[cfg(not(target_os = "windows"))]
1804    fn path_with_position_parse_posix_path_with_suffix() {
1805        assert_eq!(
1806            PathWithPosition::parse_str("foo/bar:34:in"),
1807            PathWithPosition {
1808                path: PathBuf::from("foo/bar"),
1809                row: Some(34),
1810                column: None,
1811            }
1812        );
1813        assert_eq!(
1814            PathWithPosition::parse_str("foo/bar.rs:1902:::15:"),
1815            PathWithPosition {
1816                path: PathBuf::from("foo/bar.rs:1902"),
1817                row: Some(15),
1818                column: None
1819            }
1820        );
1821
1822        assert_eq!(
1823            PathWithPosition::parse_str("app-editors:zed-0.143.6:20240710-201212.log:34:"),
1824            PathWithPosition {
1825                path: PathBuf::from("app-editors:zed-0.143.6:20240710-201212.log"),
1826                row: Some(34),
1827                column: None,
1828            }
1829        );
1830
1831        assert_eq!(
1832            PathWithPosition::parse_str("crates/file_finder/src/file_finder.rs:1902:13:"),
1833            PathWithPosition {
1834                path: PathBuf::from("crates/file_finder/src/file_finder.rs"),
1835                row: Some(1902),
1836                column: Some(13),
1837            }
1838        );
1839
1840        assert_eq!(
1841            PathWithPosition::parse_str("crate/utils/src/test:today.log:34"),
1842            PathWithPosition {
1843                path: PathBuf::from("crate/utils/src/test:today.log"),
1844                row: Some(34),
1845                column: None,
1846            }
1847        );
1848        assert_eq!(
1849            PathWithPosition::parse_str("/testing/out/src/file_finder.odin(7:15)"),
1850            PathWithPosition {
1851                path: PathBuf::from("/testing/out/src/file_finder.odin"),
1852                row: Some(7),
1853                column: Some(15),
1854            }
1855        );
1856    }
1857
1858    #[perf]
1859    #[cfg(target_os = "windows")]
1860    fn path_with_position_parse_windows_path() {
1861        assert_eq!(
1862            PathWithPosition::parse_str("crates\\utils\\paths.rs"),
1863            PathWithPosition {
1864                path: PathBuf::from("crates\\utils\\paths.rs"),
1865                row: None,
1866                column: None
1867            }
1868        );
1869
1870        assert_eq!(
1871            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs"),
1872            PathWithPosition {
1873                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1874                row: None,
1875                column: None
1876            }
1877        );
1878    }
1879
1880    #[perf]
1881    #[cfg(target_os = "windows")]
1882    fn path_with_position_parse_windows_path_with_suffix() {
1883        assert_eq!(
1884            PathWithPosition::parse_str("crates\\utils\\paths.rs:101"),
1885            PathWithPosition {
1886                path: PathBuf::from("crates\\utils\\paths.rs"),
1887                row: Some(101),
1888                column: None
1889            }
1890        );
1891
1892        assert_eq!(
1893            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1:20"),
1894            PathWithPosition {
1895                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1896                row: Some(1),
1897                column: Some(20)
1898            }
1899        );
1900
1901        assert_eq!(
1902            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13)"),
1903            PathWithPosition {
1904                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1905                row: Some(1902),
1906                column: Some(13)
1907            }
1908        );
1909
1910        // Trim off trailing `:`s for otherwise valid input.
1911        assert_eq!(
1912            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:"),
1913            PathWithPosition {
1914                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1915                row: Some(1902),
1916                column: Some(13)
1917            }
1918        );
1919
1920        assert_eq!(
1921            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:"),
1922            PathWithPosition {
1923                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
1924                row: Some(13),
1925                column: Some(15)
1926            }
1927        );
1928
1929        assert_eq!(
1930            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:"),
1931            PathWithPosition {
1932                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
1933                row: Some(15),
1934                column: None
1935            }
1936        );
1937
1938        assert_eq!(
1939            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902,13):"),
1940            PathWithPosition {
1941                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1942                row: Some(1902),
1943                column: Some(13),
1944            }
1945        );
1946
1947        assert_eq!(
1948            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902):"),
1949            PathWithPosition {
1950                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
1951                row: Some(1902),
1952                column: None,
1953            }
1954        );
1955
1956        assert_eq!(
1957            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs:1902:13:"),
1958            PathWithPosition {
1959                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1960                row: Some(1902),
1961                column: Some(13),
1962            }
1963        );
1964
1965        assert_eq!(
1966            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13):"),
1967            PathWithPosition {
1968                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1969                row: Some(1902),
1970                column: Some(13),
1971            }
1972        );
1973
1974        assert_eq!(
1975            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902):"),
1976            PathWithPosition {
1977                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
1978                row: Some(1902),
1979                column: None,
1980            }
1981        );
1982
1983        assert_eq!(
1984            PathWithPosition::parse_str("crates/utils/paths.rs:101"),
1985            PathWithPosition {
1986                path: PathBuf::from("crates\\utils\\paths.rs"),
1987                row: Some(101),
1988                column: None,
1989            }
1990        );
1991    }
1992
1993    #[perf]
1994    fn test_path_compact() {
1995        let path: PathBuf = [
1996            home_dir().to_string_lossy().into_owned(),
1997            "some_file.txt".to_string(),
1998        ]
1999        .iter()
2000        .collect();
2001        if cfg!(any(target_os = "linux", target_os = "freebsd")) || cfg!(target_os = "macos") {
2002            assert_eq!(path.compact().to_str(), Some("~/some_file.txt"));
2003        } else {
2004            assert_eq!(path.compact().to_str(), path.to_str());
2005        }
2006    }
2007
2008    #[perf]
2009    fn test_extension_or_hidden_file_name() {
2010        // No dots in name
2011        let path = Path::new("/a/b/c/file_name.rs");
2012        assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
2013
2014        // Single dot in name
2015        let path = Path::new("/a/b/c/file.name.rs");
2016        assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
2017
2018        // Multiple dots in name
2019        let path = Path::new("/a/b/c/long.file.name.rs");
2020        assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
2021
2022        // Hidden file, no extension
2023        let path = Path::new("/a/b/c/.gitignore");
2024        assert_eq!(path.extension_or_hidden_file_name(), Some("gitignore"));
2025
2026        // Hidden file, with extension
2027        let path = Path::new("/a/b/c/.eslintrc.js");
2028        assert_eq!(path.extension_or_hidden_file_name(), Some("eslintrc.js"));
2029    }
2030
2031    #[perf]
2032    fn edge_of_glob() {
2033        let path = Path::new("/work/node_modules");
2034        let path_matcher =
2035            PathMatcher::new(&["**/node_modules/**".to_owned()], PathStyle::Posix).unwrap();
2036        assert!(
2037            path_matcher.is_match(path),
2038            "Path matcher should match {path:?}"
2039        );
2040    }
2041
2042    #[perf]
2043    fn file_in_dirs() {
2044        let path = Path::new("/work/.env");
2045        let path_matcher = PathMatcher::new(&["**/.env".to_owned()], PathStyle::Posix).unwrap();
2046        assert!(
2047            path_matcher.is_match(path),
2048            "Path matcher should match {path:?}"
2049        );
2050        let path = Path::new("/work/package.json");
2051        assert!(
2052            !path_matcher.is_match(path),
2053            "Path matcher should not match {path:?}"
2054        );
2055    }
2056
2057    #[perf]
2058    fn project_search() {
2059        let path = Path::new("/Users/someonetoignore/work/zed/zed.dev/node_modules");
2060        let path_matcher =
2061            PathMatcher::new(&["**/node_modules/**".to_owned()], PathStyle::Posix).unwrap();
2062        assert!(
2063            path_matcher.is_match(path),
2064            "Path matcher should match {path:?}"
2065        );
2066    }
2067
2068    #[perf]
2069    #[cfg(target_os = "windows")]
2070    fn test_sanitized_path() {
2071        let path = Path::new("C:\\Users\\someone\\test_file.rs");
2072        let sanitized_path = SanitizedPath::new(path);
2073        assert_eq!(
2074            sanitized_path.to_string(),
2075            "C:\\Users\\someone\\test_file.rs"
2076        );
2077
2078        let path = Path::new("\\\\?\\C:\\Users\\someone\\test_file.rs");
2079        let sanitized_path = SanitizedPath::new(path);
2080        assert_eq!(
2081            sanitized_path.to_string(),
2082            "C:\\Users\\someone\\test_file.rs"
2083        );
2084    }
2085
2086    #[perf]
2087    fn test_compare_numeric_segments() {
2088        // Helper function to create peekable iterators and test
2089        fn compare(a: &str, b: &str) -> Ordering {
2090            let mut a_iter = a.chars().peekable();
2091            let mut b_iter = b.chars().peekable();
2092
2093            let result = compare_numeric_segments(&mut a_iter, &mut b_iter);
2094
2095            // Verify iterators advanced correctly
2096            assert!(
2097                !a_iter.next().is_some_and(|c| c.is_ascii_digit()),
2098                "Iterator a should have consumed all digits"
2099            );
2100            assert!(
2101                !b_iter.next().is_some_and(|c| c.is_ascii_digit()),
2102                "Iterator b should have consumed all digits"
2103            );
2104
2105            result
2106        }
2107
2108        // Basic numeric comparisons
2109        assert_eq!(compare("0", "0"), Ordering::Equal);
2110        assert_eq!(compare("1", "2"), Ordering::Less);
2111        assert_eq!(compare("9", "10"), Ordering::Less);
2112        assert_eq!(compare("10", "9"), Ordering::Greater);
2113        assert_eq!(compare("99", "100"), Ordering::Less);
2114
2115        // Leading zeros
2116        assert_eq!(compare("0", "00"), Ordering::Less);
2117        assert_eq!(compare("00", "0"), Ordering::Greater);
2118        assert_eq!(compare("01", "1"), Ordering::Greater);
2119        assert_eq!(compare("001", "1"), Ordering::Greater);
2120        assert_eq!(compare("001", "01"), Ordering::Greater);
2121
2122        // Same value different representation
2123        assert_eq!(compare("000100", "100"), Ordering::Greater);
2124        assert_eq!(compare("100", "0100"), Ordering::Less);
2125        assert_eq!(compare("0100", "00100"), Ordering::Less);
2126
2127        // Large numbers
2128        assert_eq!(compare("9999999999", "10000000000"), Ordering::Less);
2129        assert_eq!(
2130            compare(
2131                "340282366920938463463374607431768211455", // u128::MAX
2132                "340282366920938463463374607431768211456"
2133            ),
2134            Ordering::Less
2135        );
2136        assert_eq!(
2137            compare(
2138                "340282366920938463463374607431768211456", // > u128::MAX
2139                "340282366920938463463374607431768211455"
2140            ),
2141            Ordering::Greater
2142        );
2143
2144        // Iterator advancement verification
2145        let mut a_iter = "123abc".chars().peekable();
2146        let mut b_iter = "456def".chars().peekable();
2147
2148        compare_numeric_segments(&mut a_iter, &mut b_iter);
2149
2150        assert_eq!(a_iter.collect::<String>(), "abc");
2151        assert_eq!(b_iter.collect::<String>(), "def");
2152    }
2153
2154    #[perf]
2155    fn test_natural_sort() {
2156        // Basic alphanumeric
2157        assert_eq!(natural_sort("a", "b"), Ordering::Less);
2158        assert_eq!(natural_sort("b", "a"), Ordering::Greater);
2159        assert_eq!(natural_sort("a", "a"), Ordering::Equal);
2160
2161        // Case sensitivity
2162        assert_eq!(natural_sort("a", "A"), Ordering::Less);
2163        assert_eq!(natural_sort("A", "a"), Ordering::Greater);
2164        assert_eq!(natural_sort("aA", "aa"), Ordering::Greater);
2165        assert_eq!(natural_sort("aa", "aA"), Ordering::Less);
2166
2167        // Numbers
2168        assert_eq!(natural_sort("1", "2"), Ordering::Less);
2169        assert_eq!(natural_sort("2", "10"), Ordering::Less);
2170        assert_eq!(natural_sort("02", "10"), Ordering::Less);
2171        assert_eq!(natural_sort("02", "2"), Ordering::Greater);
2172
2173        // Mixed alphanumeric
2174        assert_eq!(natural_sort("a1", "a2"), Ordering::Less);
2175        assert_eq!(natural_sort("a2", "a10"), Ordering::Less);
2176        assert_eq!(natural_sort("a02", "a2"), Ordering::Greater);
2177        assert_eq!(natural_sort("a1b", "a1c"), Ordering::Less);
2178
2179        // Multiple numeric segments
2180        assert_eq!(natural_sort("1a2", "1a10"), Ordering::Less);
2181        assert_eq!(natural_sort("1a10", "1a2"), Ordering::Greater);
2182        assert_eq!(natural_sort("2a1", "10a1"), Ordering::Less);
2183
2184        // Special characters
2185        assert_eq!(natural_sort("a-1", "a-2"), Ordering::Less);
2186        assert_eq!(natural_sort("a_1", "a_2"), Ordering::Less);
2187        assert_eq!(natural_sort("a.1", "a.2"), Ordering::Less);
2188
2189        // Unicode
2190        assert_eq!(natural_sort("文1", "文2"), Ordering::Less);
2191        assert_eq!(natural_sort("文2", "文10"), Ordering::Less);
2192        assert_eq!(natural_sort("🔤1", "🔤2"), Ordering::Less);
2193
2194        // Empty and special cases
2195        assert_eq!(natural_sort("", ""), Ordering::Equal);
2196        assert_eq!(natural_sort("", "a"), Ordering::Less);
2197        assert_eq!(natural_sort("a", ""), Ordering::Greater);
2198        assert_eq!(natural_sort(" ", "  "), Ordering::Less);
2199
2200        // Mixed everything
2201        assert_eq!(natural_sort("File-1.txt", "File-2.txt"), Ordering::Less);
2202        assert_eq!(natural_sort("File-02.txt", "File-2.txt"), Ordering::Greater);
2203        assert_eq!(natural_sort("File-2.txt", "File-10.txt"), Ordering::Less);
2204        assert_eq!(natural_sort("File_A1", "File_A2"), Ordering::Less);
2205        assert_eq!(natural_sort("File_a1", "File_A1"), Ordering::Less);
2206    }
2207
2208    #[perf]
2209    fn test_compare_paths() {
2210        // Helper function for cleaner tests
2211        fn compare(a: &str, is_a_file: bool, b: &str, is_b_file: bool) -> Ordering {
2212            compare_paths((Path::new(a), is_a_file), (Path::new(b), is_b_file))
2213        }
2214
2215        // Basic path comparison
2216        assert_eq!(compare("a", true, "b", true), Ordering::Less);
2217        assert_eq!(compare("b", true, "a", true), Ordering::Greater);
2218        assert_eq!(compare("a", true, "a", true), Ordering::Equal);
2219
2220        // Files vs Directories
2221        assert_eq!(compare("a", true, "a", false), Ordering::Greater);
2222        assert_eq!(compare("a", false, "a", true), Ordering::Less);
2223        assert_eq!(compare("b", false, "a", true), Ordering::Less);
2224
2225        // Extensions
2226        assert_eq!(compare("a.txt", true, "a.md", true), Ordering::Greater);
2227        assert_eq!(compare("a.md", true, "a.txt", true), Ordering::Less);
2228        assert_eq!(compare("a", true, "a.txt", true), Ordering::Less);
2229
2230        // Nested paths
2231        assert_eq!(compare("dir/a", true, "dir/b", true), Ordering::Less);
2232        assert_eq!(compare("dir1/a", true, "dir2/a", true), Ordering::Less);
2233        assert_eq!(compare("dir/sub/a", true, "dir/a", true), Ordering::Less);
2234
2235        // Case sensitivity in paths
2236        assert_eq!(
2237            compare("Dir/file", true, "dir/file", true),
2238            Ordering::Greater
2239        );
2240        assert_eq!(
2241            compare("dir/File", true, "dir/file", true),
2242            Ordering::Greater
2243        );
2244        assert_eq!(compare("dir/file", true, "Dir/File", true), Ordering::Less);
2245
2246        // Hidden files and special names
2247        assert_eq!(compare(".hidden", true, "visible", true), Ordering::Less);
2248        assert_eq!(compare("_special", true, "normal", true), Ordering::Less);
2249        assert_eq!(compare(".config", false, ".data", false), Ordering::Less);
2250
2251        // Mixed numeric paths
2252        assert_eq!(
2253            compare("dir1/file", true, "dir2/file", true),
2254            Ordering::Less
2255        );
2256        assert_eq!(
2257            compare("dir2/file", true, "dir10/file", true),
2258            Ordering::Less
2259        );
2260        assert_eq!(
2261            compare("dir02/file", true, "dir2/file", true),
2262            Ordering::Greater
2263        );
2264
2265        // Root paths
2266        assert_eq!(compare("/a", true, "/b", true), Ordering::Less);
2267        assert_eq!(compare("/", false, "/a", true), Ordering::Less);
2268
2269        // Complex real-world examples
2270        assert_eq!(
2271            compare("project/src/main.rs", true, "project/src/lib.rs", true),
2272            Ordering::Greater
2273        );
2274        assert_eq!(
2275            compare(
2276                "project/tests/test_1.rs",
2277                true,
2278                "project/tests/test_2.rs",
2279                true
2280            ),
2281            Ordering::Less
2282        );
2283        assert_eq!(
2284            compare(
2285                "project/v1.0.0/README.md",
2286                true,
2287                "project/v1.10.0/README.md",
2288                true
2289            ),
2290            Ordering::Less
2291        );
2292    }
2293
2294    #[perf]
2295    fn test_natural_sort_case_sensitivity() {
2296        std::thread::sleep(std::time::Duration::from_millis(100));
2297        // Same letter different case - lowercase should come first
2298        assert_eq!(natural_sort("a", "A"), Ordering::Less);
2299        assert_eq!(natural_sort("A", "a"), Ordering::Greater);
2300        assert_eq!(natural_sort("a", "a"), Ordering::Equal);
2301        assert_eq!(natural_sort("A", "A"), Ordering::Equal);
2302
2303        // Mixed case strings
2304        assert_eq!(natural_sort("aaa", "AAA"), Ordering::Less);
2305        assert_eq!(natural_sort("AAA", "aaa"), Ordering::Greater);
2306        assert_eq!(natural_sort("aAa", "AaA"), Ordering::Less);
2307
2308        // Different letters
2309        assert_eq!(natural_sort("a", "b"), Ordering::Less);
2310        assert_eq!(natural_sort("A", "b"), Ordering::Less);
2311        assert_eq!(natural_sort("a", "B"), Ordering::Less);
2312    }
2313
2314    #[perf]
2315    fn test_natural_sort_with_numbers() {
2316        // Basic number ordering
2317        assert_eq!(natural_sort("file1", "file2"), Ordering::Less);
2318        assert_eq!(natural_sort("file2", "file10"), Ordering::Less);
2319        assert_eq!(natural_sort("file10", "file2"), Ordering::Greater);
2320
2321        // Numbers in different positions
2322        assert_eq!(natural_sort("1file", "2file"), Ordering::Less);
2323        assert_eq!(natural_sort("file1text", "file2text"), Ordering::Less);
2324        assert_eq!(natural_sort("text1file", "text2file"), Ordering::Less);
2325
2326        // Multiple numbers in string
2327        assert_eq!(natural_sort("file1-2", "file1-10"), Ordering::Less);
2328        assert_eq!(natural_sort("2-1file", "10-1file"), Ordering::Less);
2329
2330        // Leading zeros
2331        assert_eq!(natural_sort("file002", "file2"), Ordering::Greater);
2332        assert_eq!(natural_sort("file002", "file10"), Ordering::Less);
2333
2334        // Very large numbers
2335        assert_eq!(
2336            natural_sort("file999999999999999999999", "file999999999999999999998"),
2337            Ordering::Greater
2338        );
2339
2340        // u128 edge cases
2341
2342        // Numbers near u128::MAX (340,282,366,920,938,463,463,374,607,431,768,211,455)
2343        assert_eq!(
2344            natural_sort(
2345                "file340282366920938463463374607431768211454",
2346                "file340282366920938463463374607431768211455"
2347            ),
2348            Ordering::Less
2349        );
2350
2351        // Equal length numbers that overflow u128
2352        assert_eq!(
2353            natural_sort(
2354                "file340282366920938463463374607431768211456",
2355                "file340282366920938463463374607431768211455"
2356            ),
2357            Ordering::Greater
2358        );
2359
2360        // Different length numbers that overflow u128
2361        assert_eq!(
2362            natural_sort(
2363                "file3402823669209384634633746074317682114560",
2364                "file340282366920938463463374607431768211455"
2365            ),
2366            Ordering::Greater
2367        );
2368
2369        // Leading zeros with numbers near u128::MAX
2370        assert_eq!(
2371            natural_sort(
2372                "file0340282366920938463463374607431768211455",
2373                "file340282366920938463463374607431768211455"
2374            ),
2375            Ordering::Greater
2376        );
2377
2378        // Very large numbers with different lengths (both overflow u128)
2379        assert_eq!(
2380            natural_sort(
2381                "file999999999999999999999999999999999999999999999999",
2382                "file9999999999999999999999999999999999999999999999999"
2383            ),
2384            Ordering::Less
2385        );
2386    }
2387
2388    #[perf]
2389    fn test_natural_sort_case_sensitive() {
2390        // Numerically smaller values come first.
2391        assert_eq!(natural_sort("File1", "file2"), Ordering::Less);
2392        assert_eq!(natural_sort("file1", "File2"), Ordering::Less);
2393
2394        // Numerically equal values: the case-insensitive comparison decides first.
2395        // Case-sensitive comparison only occurs when both are equal case-insensitively.
2396        assert_eq!(natural_sort("Dir1", "dir01"), Ordering::Less);
2397        assert_eq!(natural_sort("dir2", "Dir02"), Ordering::Less);
2398        assert_eq!(natural_sort("dir2", "dir02"), Ordering::Less);
2399
2400        // Numerically equal and case-insensitively equal:
2401        // the lexicographically smaller (case-sensitive) one wins.
2402        assert_eq!(natural_sort("dir1", "Dir1"), Ordering::Less);
2403        assert_eq!(natural_sort("dir02", "Dir02"), Ordering::Less);
2404        assert_eq!(natural_sort("dir10", "Dir10"), Ordering::Less);
2405    }
2406
2407    #[perf]
2408    fn test_natural_sort_edge_cases() {
2409        // Empty strings
2410        assert_eq!(natural_sort("", ""), Ordering::Equal);
2411        assert_eq!(natural_sort("", "a"), Ordering::Less);
2412        assert_eq!(natural_sort("a", ""), Ordering::Greater);
2413
2414        // Special characters
2415        assert_eq!(natural_sort("file-1", "file_1"), Ordering::Less);
2416        assert_eq!(natural_sort("file.1", "file_1"), Ordering::Less);
2417        assert_eq!(natural_sort("file 1", "file_1"), Ordering::Less);
2418
2419        // Unicode characters
2420        // 9312 vs 9313
2421        assert_eq!(natural_sort("file①", "file②"), Ordering::Less);
2422        // 9321 vs 9313
2423        assert_eq!(natural_sort("file⑩", "file②"), Ordering::Greater);
2424        // 28450 vs 23383
2425        assert_eq!(natural_sort("file漢", "file字"), Ordering::Greater);
2426
2427        // Mixed alphanumeric with special chars
2428        assert_eq!(natural_sort("file-1a", "file-1b"), Ordering::Less);
2429        assert_eq!(natural_sort("file-1.2", "file-1.10"), Ordering::Less);
2430        assert_eq!(natural_sort("file-1.10", "file-1.2"), Ordering::Greater);
2431    }
2432
2433    #[test]
2434    fn test_multiple_extensions() {
2435        // No extensions
2436        let path = Path::new("/a/b/c/file_name");
2437        assert_eq!(path.multiple_extensions(), None);
2438
2439        // Only one extension
2440        let path = Path::new("/a/b/c/file_name.tsx");
2441        assert_eq!(path.multiple_extensions(), None);
2442
2443        // Stories sample extension
2444        let path = Path::new("/a/b/c/file_name.stories.tsx");
2445        assert_eq!(path.multiple_extensions(), Some("stories.tsx".to_string()));
2446
2447        // Longer sample extension
2448        let path = Path::new("/a/b/c/long.app.tar.gz");
2449        assert_eq!(path.multiple_extensions(), Some("app.tar.gz".to_string()));
2450    }
2451
2452    #[test]
2453    fn test_strip_path_suffix() {
2454        let base = Path::new("/a/b/c/file_name");
2455        let suffix = Path::new("file_name");
2456        assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a/b/c")));
2457
2458        let base = Path::new("/a/b/c/file_name.tsx");
2459        let suffix = Path::new("file_name.tsx");
2460        assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a/b/c")));
2461
2462        let base = Path::new("/a/b/c/file_name.stories.tsx");
2463        let suffix = Path::new("c/file_name.stories.tsx");
2464        assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a/b")));
2465
2466        let base = Path::new("/a/b/c/long.app.tar.gz");
2467        let suffix = Path::new("b/c/long.app.tar.gz");
2468        assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a")));
2469
2470        let base = Path::new("/a/b/c/long.app.tar.gz");
2471        let suffix = Path::new("/a/b/c/long.app.tar.gz");
2472        assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("")));
2473
2474        let base = Path::new("/a/b/c/long.app.tar.gz");
2475        let suffix = Path::new("/a/b/c/no_match.app.tar.gz");
2476        assert_eq!(strip_path_suffix(base, suffix), None);
2477
2478        let base = Path::new("/a/b/c/long.app.tar.gz");
2479        let suffix = Path::new("app.tar.gz");
2480        assert_eq!(strip_path_suffix(base, suffix), None);
2481    }
2482
2483    #[cfg(target_os = "windows")]
2484    #[test]
2485    fn test_wsl_path() {
2486        use super::WslPath;
2487        let path = "/a/b/c";
2488        assert_eq!(WslPath::from_path(&path), None);
2489
2490        let path = r"\\wsl.localhost";
2491        assert_eq!(WslPath::from_path(&path), None);
2492
2493        let path = r"\\wsl.localhost\Distro";
2494        assert_eq!(
2495            WslPath::from_path(&path),
2496            Some(WslPath {
2497                distro: "Distro".to_owned(),
2498                path: "/".into(),
2499            })
2500        );
2501
2502        let path = r"\\wsl.localhost\Distro\blue";
2503        assert_eq!(
2504            WslPath::from_path(&path),
2505            Some(WslPath {
2506                distro: "Distro".to_owned(),
2507                path: "/blue".into()
2508            })
2509        );
2510
2511        let path = r"\\wsl$\archlinux\tomato\.\paprika\..\aubergine.txt";
2512        assert_eq!(
2513            WslPath::from_path(&path),
2514            Some(WslPath {
2515                distro: "archlinux".to_owned(),
2516                path: "/tomato/paprika/../aubergine.txt".into()
2517            })
2518        );
2519
2520        let path = r"\\windows.localhost\Distro\foo";
2521        assert_eq!(WslPath::from_path(&path), None);
2522    }
2523}