paths.rs

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