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: impl 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 == b => Ordering::Greater,
1188                            (false, true) if a == 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_with_nested_paths() {
1831        // Test that nested paths still work correctly
1832        let mut paths = vec![
1833            (RelPath::unix("src/main.rs").unwrap(), true),
1834            (RelPath::unix("Cargo.toml").unwrap(), true),
1835            (RelPath::unix("src").unwrap(), false),
1836            (RelPath::unix("target").unwrap(), false),
1837        ];
1838        paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1839        assert_eq!(
1840            paths,
1841            vec![
1842                (RelPath::unix("Cargo.toml").unwrap(), true),
1843                (RelPath::unix("src").unwrap(), false),
1844                (RelPath::unix("src/main.rs").unwrap(), true),
1845                (RelPath::unix("target").unwrap(), false),
1846            ]
1847        );
1848    }
1849
1850    #[perf]
1851    fn compare_rel_paths_files_first_with_nested() {
1852        // Files come before directories, even with nested paths
1853        let mut paths = vec![
1854            (RelPath::unix("src/lib.rs").unwrap(), true),
1855            (RelPath::unix("README.md").unwrap(), true),
1856            (RelPath::unix("src").unwrap(), false),
1857            (RelPath::unix("tests").unwrap(), false),
1858        ];
1859        paths.sort_by(|&a, &b| compare_rel_paths_files_first(a, b));
1860        assert_eq!(
1861            paths,
1862            vec![
1863                (RelPath::unix("README.md").unwrap(), true),
1864                (RelPath::unix("src").unwrap(), false),
1865                (RelPath::unix("src/lib.rs").unwrap(), true),
1866                (RelPath::unix("tests").unwrap(), false),
1867            ]
1868        );
1869    }
1870
1871    #[perf]
1872    fn compare_rel_paths_mixed_dotfiles() {
1873        // Test that dotfiles are handled correctly in mixed mode
1874        let mut paths = vec![
1875            (RelPath::unix(".gitignore").unwrap(), true),
1876            (RelPath::unix("README.md").unwrap(), true),
1877            (RelPath::unix(".github").unwrap(), false),
1878            (RelPath::unix("src").unwrap(), false),
1879        ];
1880        paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1881        assert_eq!(
1882            paths,
1883            vec![
1884                (RelPath::unix(".github").unwrap(), false),
1885                (RelPath::unix(".gitignore").unwrap(), true),
1886                (RelPath::unix("README.md").unwrap(), true),
1887                (RelPath::unix("src").unwrap(), false),
1888            ]
1889        );
1890    }
1891
1892    #[perf]
1893    fn compare_rel_paths_files_first_dotfiles() {
1894        // Test that dotfiles come first when they're files
1895        let mut paths = vec![
1896            (RelPath::unix(".gitignore").unwrap(), true),
1897            (RelPath::unix("README.md").unwrap(), true),
1898            (RelPath::unix(".github").unwrap(), false),
1899            (RelPath::unix("src").unwrap(), false),
1900        ];
1901        paths.sort_by(|&a, &b| compare_rel_paths_files_first(a, b));
1902        assert_eq!(
1903            paths,
1904            vec![
1905                (RelPath::unix(".gitignore").unwrap(), true),
1906                (RelPath::unix("README.md").unwrap(), true),
1907                (RelPath::unix(".github").unwrap(), false),
1908                (RelPath::unix("src").unwrap(), false),
1909            ]
1910        );
1911    }
1912
1913    #[perf]
1914    fn compare_rel_paths_mixed_same_stem_different_extension() {
1915        // Files with same stem but different extensions should sort by extension
1916        let mut paths = vec![
1917            (RelPath::unix("file.rs").unwrap(), true),
1918            (RelPath::unix("file.md").unwrap(), true),
1919            (RelPath::unix("file.txt").unwrap(), true),
1920        ];
1921        paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1922        assert_eq!(
1923            paths,
1924            vec![
1925                (RelPath::unix("file.txt").unwrap(), true),
1926                (RelPath::unix("file.rs").unwrap(), true),
1927                (RelPath::unix("file.md").unwrap(), true),
1928            ]
1929        );
1930    }
1931
1932    #[perf]
1933    fn compare_rel_paths_files_first_same_stem() {
1934        // Same stem files should still sort by extension with files_first
1935        let mut paths = vec![
1936            (RelPath::unix("main.rs").unwrap(), true),
1937            (RelPath::unix("main.c").unwrap(), true),
1938            (RelPath::unix("main").unwrap(), false),
1939        ];
1940        paths.sort_by(|&a, &b| compare_rel_paths_files_first(a, b));
1941        assert_eq!(
1942            paths,
1943            vec![
1944                (RelPath::unix("main.c").unwrap(), true),
1945                (RelPath::unix("main.rs").unwrap(), true),
1946                (RelPath::unix("main").unwrap(), false),
1947            ]
1948        );
1949    }
1950
1951    #[perf]
1952    fn compare_rel_paths_mixed_deep_nesting() {
1953        // Test sorting with deeply nested paths
1954        let mut paths = vec![
1955            (RelPath::unix("a/b/c.txt").unwrap(), true),
1956            (RelPath::unix("A/B.txt").unwrap(), true),
1957            (RelPath::unix("a.txt").unwrap(), true),
1958            (RelPath::unix("A.txt").unwrap(), true),
1959        ];
1960        paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1961        assert_eq!(
1962            paths,
1963            vec![
1964                (RelPath::unix("A/B.txt").unwrap(), true),
1965                (RelPath::unix("a/b/c.txt").unwrap(), true),
1966                (RelPath::unix("a.txt").unwrap(), true),
1967                (RelPath::unix("A.txt").unwrap(), true),
1968            ]
1969        );
1970    }
1971
1972    #[perf]
1973    fn path_with_position_parse_posix_path() {
1974        // Test POSIX filename edge cases
1975        // Read more at https://en.wikipedia.org/wiki/Filename
1976        assert_eq!(
1977            PathWithPosition::parse_str("test_file"),
1978            PathWithPosition {
1979                path: PathBuf::from("test_file"),
1980                row: None,
1981                column: None
1982            }
1983        );
1984
1985        assert_eq!(
1986            PathWithPosition::parse_str("a:bc:.zip:1"),
1987            PathWithPosition {
1988                path: PathBuf::from("a:bc:.zip"),
1989                row: Some(1),
1990                column: None
1991            }
1992        );
1993
1994        assert_eq!(
1995            PathWithPosition::parse_str("one.second.zip:1"),
1996            PathWithPosition {
1997                path: PathBuf::from("one.second.zip"),
1998                row: Some(1),
1999                column: None
2000            }
2001        );
2002
2003        // Trim off trailing `:`s for otherwise valid input.
2004        assert_eq!(
2005            PathWithPosition::parse_str("test_file:10:1:"),
2006            PathWithPosition {
2007                path: PathBuf::from("test_file"),
2008                row: Some(10),
2009                column: Some(1)
2010            }
2011        );
2012
2013        assert_eq!(
2014            PathWithPosition::parse_str("test_file.rs:"),
2015            PathWithPosition {
2016                path: PathBuf::from("test_file.rs"),
2017                row: None,
2018                column: None
2019            }
2020        );
2021
2022        assert_eq!(
2023            PathWithPosition::parse_str("test_file.rs:1:"),
2024            PathWithPosition {
2025                path: PathBuf::from("test_file.rs"),
2026                row: Some(1),
2027                column: None
2028            }
2029        );
2030
2031        assert_eq!(
2032            PathWithPosition::parse_str("ab\ncd"),
2033            PathWithPosition {
2034                path: PathBuf::from("ab\ncd"),
2035                row: None,
2036                column: None
2037            }
2038        );
2039
2040        assert_eq!(
2041            PathWithPosition::parse_str("👋\nab"),
2042            PathWithPosition {
2043                path: PathBuf::from("👋\nab"),
2044                row: None,
2045                column: None
2046            }
2047        );
2048
2049        assert_eq!(
2050            PathWithPosition::parse_str("Types.hs:(617,9)-(670,28):"),
2051            PathWithPosition {
2052                path: PathBuf::from("Types.hs"),
2053                row: Some(617),
2054                column: Some(9),
2055            }
2056        );
2057    }
2058
2059    #[perf]
2060    #[cfg(not(target_os = "windows"))]
2061    fn path_with_position_parse_posix_path_with_suffix() {
2062        assert_eq!(
2063            PathWithPosition::parse_str("foo/bar:34:in"),
2064            PathWithPosition {
2065                path: PathBuf::from("foo/bar"),
2066                row: Some(34),
2067                column: None,
2068            }
2069        );
2070        assert_eq!(
2071            PathWithPosition::parse_str("foo/bar.rs:1902:::15:"),
2072            PathWithPosition {
2073                path: PathBuf::from("foo/bar.rs:1902"),
2074                row: Some(15),
2075                column: None
2076            }
2077        );
2078
2079        assert_eq!(
2080            PathWithPosition::parse_str("app-editors:zed-0.143.6:20240710-201212.log:34:"),
2081            PathWithPosition {
2082                path: PathBuf::from("app-editors:zed-0.143.6:20240710-201212.log"),
2083                row: Some(34),
2084                column: None,
2085            }
2086        );
2087
2088        assert_eq!(
2089            PathWithPosition::parse_str("crates/file_finder/src/file_finder.rs:1902:13:"),
2090            PathWithPosition {
2091                path: PathBuf::from("crates/file_finder/src/file_finder.rs"),
2092                row: Some(1902),
2093                column: Some(13),
2094            }
2095        );
2096
2097        assert_eq!(
2098            PathWithPosition::parse_str("crate/utils/src/test:today.log:34"),
2099            PathWithPosition {
2100                path: PathBuf::from("crate/utils/src/test:today.log"),
2101                row: Some(34),
2102                column: None,
2103            }
2104        );
2105        assert_eq!(
2106            PathWithPosition::parse_str("/testing/out/src/file_finder.odin(7:15)"),
2107            PathWithPosition {
2108                path: PathBuf::from("/testing/out/src/file_finder.odin"),
2109                row: Some(7),
2110                column: Some(15),
2111            }
2112        );
2113    }
2114
2115    #[perf]
2116    #[cfg(target_os = "windows")]
2117    fn path_with_position_parse_windows_path() {
2118        assert_eq!(
2119            PathWithPosition::parse_str("crates\\utils\\paths.rs"),
2120            PathWithPosition {
2121                path: PathBuf::from("crates\\utils\\paths.rs"),
2122                row: None,
2123                column: None
2124            }
2125        );
2126
2127        assert_eq!(
2128            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs"),
2129            PathWithPosition {
2130                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
2131                row: None,
2132                column: None
2133            }
2134        );
2135    }
2136
2137    #[perf]
2138    #[cfg(target_os = "windows")]
2139    fn path_with_position_parse_windows_path_with_suffix() {
2140        assert_eq!(
2141            PathWithPosition::parse_str("crates\\utils\\paths.rs:101"),
2142            PathWithPosition {
2143                path: PathBuf::from("crates\\utils\\paths.rs"),
2144                row: Some(101),
2145                column: None
2146            }
2147        );
2148
2149        assert_eq!(
2150            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1:20"),
2151            PathWithPosition {
2152                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
2153                row: Some(1),
2154                column: Some(20)
2155            }
2156        );
2157
2158        assert_eq!(
2159            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13)"),
2160            PathWithPosition {
2161                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
2162                row: Some(1902),
2163                column: Some(13)
2164            }
2165        );
2166
2167        // Trim off trailing `:`s for otherwise valid input.
2168        assert_eq!(
2169            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:"),
2170            PathWithPosition {
2171                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
2172                row: Some(1902),
2173                column: Some(13)
2174            }
2175        );
2176
2177        assert_eq!(
2178            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:"),
2179            PathWithPosition {
2180                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
2181                row: Some(13),
2182                column: Some(15)
2183            }
2184        );
2185
2186        assert_eq!(
2187            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:"),
2188            PathWithPosition {
2189                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
2190                row: Some(15),
2191                column: None
2192            }
2193        );
2194
2195        assert_eq!(
2196            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902,13):"),
2197            PathWithPosition {
2198                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
2199                row: Some(1902),
2200                column: Some(13),
2201            }
2202        );
2203
2204        assert_eq!(
2205            PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902):"),
2206            PathWithPosition {
2207                path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
2208                row: Some(1902),
2209                column: None,
2210            }
2211        );
2212
2213        assert_eq!(
2214            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs:1902:13:"),
2215            PathWithPosition {
2216                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
2217                row: Some(1902),
2218                column: Some(13),
2219            }
2220        );
2221
2222        assert_eq!(
2223            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13):"),
2224            PathWithPosition {
2225                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
2226                row: Some(1902),
2227                column: Some(13),
2228            }
2229        );
2230
2231        assert_eq!(
2232            PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902):"),
2233            PathWithPosition {
2234                path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
2235                row: Some(1902),
2236                column: None,
2237            }
2238        );
2239
2240        assert_eq!(
2241            PathWithPosition::parse_str("crates/utils/paths.rs:101"),
2242            PathWithPosition {
2243                path: PathBuf::from("crates\\utils\\paths.rs"),
2244                row: Some(101),
2245                column: None,
2246            }
2247        );
2248    }
2249
2250    #[perf]
2251    fn test_path_compact() {
2252        let path: PathBuf = [
2253            home_dir().to_string_lossy().into_owned(),
2254            "some_file.txt".to_string(),
2255        ]
2256        .iter()
2257        .collect();
2258        if cfg!(any(target_os = "linux", target_os = "freebsd")) || cfg!(target_os = "macos") {
2259            assert_eq!(path.compact().to_str(), Some("~/some_file.txt"));
2260        } else {
2261            assert_eq!(path.compact().to_str(), path.to_str());
2262        }
2263    }
2264
2265    #[perf]
2266    fn test_extension_or_hidden_file_name() {
2267        // No dots in name
2268        let path = Path::new("/a/b/c/file_name.rs");
2269        assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
2270
2271        // Single dot in name
2272        let path = Path::new("/a/b/c/file.name.rs");
2273        assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
2274
2275        // Multiple dots in name
2276        let path = Path::new("/a/b/c/long.file.name.rs");
2277        assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
2278
2279        // Hidden file, no extension
2280        let path = Path::new("/a/b/c/.gitignore");
2281        assert_eq!(path.extension_or_hidden_file_name(), Some("gitignore"));
2282
2283        // Hidden file, with extension
2284        let path = Path::new("/a/b/c/.eslintrc.js");
2285        assert_eq!(path.extension_or_hidden_file_name(), Some("eslintrc.js"));
2286    }
2287
2288    #[perf]
2289    // fn edge_of_glob() {
2290    //     let path = Path::new("/work/node_modules");
2291    //     let path_matcher =
2292    //         PathMatcher::new(&["**/node_modules/**".to_owned()], PathStyle::Posix).unwrap();
2293    //     assert!(
2294    //         path_matcher.is_match(path),
2295    //         "Path matcher should match {path:?}"
2296    //     );
2297    // }
2298
2299    // #[perf]
2300    // fn file_in_dirs() {
2301    //     let path = Path::new("/work/.env");
2302    //     let path_matcher = PathMatcher::new(&["**/.env".to_owned()], PathStyle::Posix).unwrap();
2303    //     assert!(
2304    //         path_matcher.is_match(path),
2305    //         "Path matcher should match {path:?}"
2306    //     );
2307    //     let path = Path::new("/work/package.json");
2308    //     assert!(
2309    //         !path_matcher.is_match(path),
2310    //         "Path matcher should not match {path:?}"
2311    //     );
2312    // }
2313
2314    // #[perf]
2315    // fn project_search() {
2316    //     let path = Path::new("/Users/someonetoignore/work/zed/zed.dev/node_modules");
2317    //     let path_matcher =
2318    //         PathMatcher::new(&["**/node_modules/**".to_owned()], PathStyle::Posix).unwrap();
2319    //     assert!(
2320    //         path_matcher.is_match(path),
2321    //         "Path matcher should match {path:?}"
2322    //     );
2323    // }
2324    #[perf]
2325    #[cfg(target_os = "windows")]
2326    fn test_sanitized_path() {
2327        let path = Path::new("C:\\Users\\someone\\test_file.rs");
2328        let sanitized_path = SanitizedPath::new(path);
2329        assert_eq!(
2330            sanitized_path.to_string(),
2331            "C:\\Users\\someone\\test_file.rs"
2332        );
2333
2334        let path = Path::new("\\\\?\\C:\\Users\\someone\\test_file.rs");
2335        let sanitized_path = SanitizedPath::new(path);
2336        assert_eq!(
2337            sanitized_path.to_string(),
2338            "C:\\Users\\someone\\test_file.rs"
2339        );
2340    }
2341
2342    #[perf]
2343    fn test_compare_numeric_segments() {
2344        // Helper function to create peekable iterators and test
2345        fn compare(a: &str, b: &str) -> Ordering {
2346            let mut a_iter = a.chars().peekable();
2347            let mut b_iter = b.chars().peekable();
2348
2349            let result = compare_numeric_segments(&mut a_iter, &mut b_iter);
2350
2351            // Verify iterators advanced correctly
2352            assert!(
2353                !a_iter.next().is_some_and(|c| c.is_ascii_digit()),
2354                "Iterator a should have consumed all digits"
2355            );
2356            assert!(
2357                !b_iter.next().is_some_and(|c| c.is_ascii_digit()),
2358                "Iterator b should have consumed all digits"
2359            );
2360
2361            result
2362        }
2363
2364        // Basic numeric comparisons
2365        assert_eq!(compare("0", "0"), Ordering::Equal);
2366        assert_eq!(compare("1", "2"), Ordering::Less);
2367        assert_eq!(compare("9", "10"), Ordering::Less);
2368        assert_eq!(compare("10", "9"), Ordering::Greater);
2369        assert_eq!(compare("99", "100"), Ordering::Less);
2370
2371        // Leading zeros
2372        assert_eq!(compare("0", "00"), Ordering::Less);
2373        assert_eq!(compare("00", "0"), Ordering::Greater);
2374        assert_eq!(compare("01", "1"), Ordering::Greater);
2375        assert_eq!(compare("001", "1"), Ordering::Greater);
2376        assert_eq!(compare("001", "01"), Ordering::Greater);
2377
2378        // Same value different representation
2379        assert_eq!(compare("000100", "100"), Ordering::Greater);
2380        assert_eq!(compare("100", "0100"), Ordering::Less);
2381        assert_eq!(compare("0100", "00100"), Ordering::Less);
2382
2383        // Large numbers
2384        assert_eq!(compare("9999999999", "10000000000"), Ordering::Less);
2385        assert_eq!(
2386            compare(
2387                "340282366920938463463374607431768211455", // u128::MAX
2388                "340282366920938463463374607431768211456"
2389            ),
2390            Ordering::Less
2391        );
2392        assert_eq!(
2393            compare(
2394                "340282366920938463463374607431768211456", // > u128::MAX
2395                "340282366920938463463374607431768211455"
2396            ),
2397            Ordering::Greater
2398        );
2399
2400        // Iterator advancement verification
2401        let mut a_iter = "123abc".chars().peekable();
2402        let mut b_iter = "456def".chars().peekable();
2403
2404        compare_numeric_segments(&mut a_iter, &mut b_iter);
2405
2406        assert_eq!(a_iter.collect::<String>(), "abc");
2407        assert_eq!(b_iter.collect::<String>(), "def");
2408    }
2409
2410    #[perf]
2411    fn test_natural_sort() {
2412        // Basic alphanumeric
2413        assert_eq!(natural_sort("a", "b"), Ordering::Less);
2414        assert_eq!(natural_sort("b", "a"), Ordering::Greater);
2415        assert_eq!(natural_sort("a", "a"), Ordering::Equal);
2416
2417        // Case sensitivity
2418        assert_eq!(natural_sort("a", "A"), Ordering::Less);
2419        assert_eq!(natural_sort("A", "a"), Ordering::Greater);
2420        assert_eq!(natural_sort("aA", "aa"), Ordering::Greater);
2421        assert_eq!(natural_sort("aa", "aA"), Ordering::Less);
2422
2423        // Numbers
2424        assert_eq!(natural_sort("1", "2"), Ordering::Less);
2425        assert_eq!(natural_sort("2", "10"), Ordering::Less);
2426        assert_eq!(natural_sort("02", "10"), Ordering::Less);
2427        assert_eq!(natural_sort("02", "2"), Ordering::Greater);
2428
2429        // Mixed alphanumeric
2430        assert_eq!(natural_sort("a1", "a2"), Ordering::Less);
2431        assert_eq!(natural_sort("a2", "a10"), Ordering::Less);
2432        assert_eq!(natural_sort("a02", "a2"), Ordering::Greater);
2433        assert_eq!(natural_sort("a1b", "a1c"), Ordering::Less);
2434
2435        // Multiple numeric segments
2436        assert_eq!(natural_sort("1a2", "1a10"), Ordering::Less);
2437        assert_eq!(natural_sort("1a10", "1a2"), Ordering::Greater);
2438        assert_eq!(natural_sort("2a1", "10a1"), Ordering::Less);
2439
2440        // Special characters
2441        assert_eq!(natural_sort("a-1", "a-2"), Ordering::Less);
2442        assert_eq!(natural_sort("a_1", "a_2"), Ordering::Less);
2443        assert_eq!(natural_sort("a.1", "a.2"), Ordering::Less);
2444
2445        // Unicode
2446        assert_eq!(natural_sort("文1", "文2"), Ordering::Less);
2447        assert_eq!(natural_sort("文2", "文10"), Ordering::Less);
2448        assert_eq!(natural_sort("🔤1", "🔤2"), Ordering::Less);
2449
2450        // Empty and special cases
2451        assert_eq!(natural_sort("", ""), Ordering::Equal);
2452        assert_eq!(natural_sort("", "a"), Ordering::Less);
2453        assert_eq!(natural_sort("a", ""), Ordering::Greater);
2454        assert_eq!(natural_sort(" ", "  "), Ordering::Less);
2455
2456        // Mixed everything
2457        assert_eq!(natural_sort("File-1.txt", "File-2.txt"), Ordering::Less);
2458        assert_eq!(natural_sort("File-02.txt", "File-2.txt"), Ordering::Greater);
2459        assert_eq!(natural_sort("File-2.txt", "File-10.txt"), Ordering::Less);
2460        assert_eq!(natural_sort("File_A1", "File_A2"), Ordering::Less);
2461        assert_eq!(natural_sort("File_a1", "File_A1"), Ordering::Less);
2462    }
2463
2464    #[perf]
2465    fn test_compare_paths() {
2466        // Helper function for cleaner tests
2467        fn compare(a: &str, is_a_file: bool, b: &str, is_b_file: bool) -> Ordering {
2468            compare_paths((Path::new(a), is_a_file), (Path::new(b), is_b_file))
2469        }
2470
2471        // Basic path comparison
2472        assert_eq!(compare("a", true, "b", true), Ordering::Less);
2473        assert_eq!(compare("b", true, "a", true), Ordering::Greater);
2474        assert_eq!(compare("a", true, "a", true), Ordering::Equal);
2475
2476        // Files vs Directories
2477        assert_eq!(compare("a", true, "a", false), Ordering::Greater);
2478        assert_eq!(compare("a", false, "a", true), Ordering::Less);
2479        assert_eq!(compare("b", false, "a", true), Ordering::Less);
2480
2481        // Extensions
2482        assert_eq!(compare("a.txt", true, "a.md", true), Ordering::Greater);
2483        assert_eq!(compare("a.md", true, "a.txt", true), Ordering::Less);
2484        assert_eq!(compare("a", true, "a.txt", true), Ordering::Less);
2485
2486        // Nested paths
2487        assert_eq!(compare("dir/a", true, "dir/b", true), Ordering::Less);
2488        assert_eq!(compare("dir1/a", true, "dir2/a", true), Ordering::Less);
2489        assert_eq!(compare("dir/sub/a", true, "dir/a", true), Ordering::Less);
2490
2491        // Case sensitivity in paths
2492        assert_eq!(
2493            compare("Dir/file", true, "dir/file", true),
2494            Ordering::Greater
2495        );
2496        assert_eq!(
2497            compare("dir/File", true, "dir/file", true),
2498            Ordering::Greater
2499        );
2500        assert_eq!(compare("dir/file", true, "Dir/File", true), Ordering::Less);
2501
2502        // Hidden files and special names
2503        assert_eq!(compare(".hidden", true, "visible", true), Ordering::Less);
2504        assert_eq!(compare("_special", true, "normal", true), Ordering::Less);
2505        assert_eq!(compare(".config", false, ".data", false), Ordering::Less);
2506
2507        // Mixed numeric paths
2508        assert_eq!(
2509            compare("dir1/file", true, "dir2/file", true),
2510            Ordering::Less
2511        );
2512        assert_eq!(
2513            compare("dir2/file", true, "dir10/file", true),
2514            Ordering::Less
2515        );
2516        assert_eq!(
2517            compare("dir02/file", true, "dir2/file", true),
2518            Ordering::Greater
2519        );
2520
2521        // Root paths
2522        assert_eq!(compare("/a", true, "/b", true), Ordering::Less);
2523        assert_eq!(compare("/", false, "/a", true), Ordering::Less);
2524
2525        // Complex real-world examples
2526        assert_eq!(
2527            compare("project/src/main.rs", true, "project/src/lib.rs", true),
2528            Ordering::Greater
2529        );
2530        assert_eq!(
2531            compare(
2532                "project/tests/test_1.rs",
2533                true,
2534                "project/tests/test_2.rs",
2535                true
2536            ),
2537            Ordering::Less
2538        );
2539        assert_eq!(
2540            compare(
2541                "project/v1.0.0/README.md",
2542                true,
2543                "project/v1.10.0/README.md",
2544                true
2545            ),
2546            Ordering::Less
2547        );
2548    }
2549
2550    #[perf]
2551    fn test_natural_sort_case_sensitivity() {
2552        std::thread::sleep(std::time::Duration::from_millis(100));
2553        // Same letter different case - lowercase should come first
2554        assert_eq!(natural_sort("a", "A"), Ordering::Less);
2555        assert_eq!(natural_sort("A", "a"), Ordering::Greater);
2556        assert_eq!(natural_sort("a", "a"), Ordering::Equal);
2557        assert_eq!(natural_sort("A", "A"), Ordering::Equal);
2558
2559        // Mixed case strings
2560        assert_eq!(natural_sort("aaa", "AAA"), Ordering::Less);
2561        assert_eq!(natural_sort("AAA", "aaa"), Ordering::Greater);
2562        assert_eq!(natural_sort("aAa", "AaA"), Ordering::Less);
2563
2564        // Different letters
2565        assert_eq!(natural_sort("a", "b"), Ordering::Less);
2566        assert_eq!(natural_sort("A", "b"), Ordering::Less);
2567        assert_eq!(natural_sort("a", "B"), Ordering::Less);
2568    }
2569
2570    #[perf]
2571    fn test_natural_sort_with_numbers() {
2572        // Basic number ordering
2573        assert_eq!(natural_sort("file1", "file2"), Ordering::Less);
2574        assert_eq!(natural_sort("file2", "file10"), Ordering::Less);
2575        assert_eq!(natural_sort("file10", "file2"), Ordering::Greater);
2576
2577        // Numbers in different positions
2578        assert_eq!(natural_sort("1file", "2file"), Ordering::Less);
2579        assert_eq!(natural_sort("file1text", "file2text"), Ordering::Less);
2580        assert_eq!(natural_sort("text1file", "text2file"), Ordering::Less);
2581
2582        // Multiple numbers in string
2583        assert_eq!(natural_sort("file1-2", "file1-10"), Ordering::Less);
2584        assert_eq!(natural_sort("2-1file", "10-1file"), Ordering::Less);
2585
2586        // Leading zeros
2587        assert_eq!(natural_sort("file002", "file2"), Ordering::Greater);
2588        assert_eq!(natural_sort("file002", "file10"), Ordering::Less);
2589
2590        // Very large numbers
2591        assert_eq!(
2592            natural_sort("file999999999999999999999", "file999999999999999999998"),
2593            Ordering::Greater
2594        );
2595
2596        // u128 edge cases
2597
2598        // Numbers near u128::MAX (340,282,366,920,938,463,463,374,607,431,768,211,455)
2599        assert_eq!(
2600            natural_sort(
2601                "file340282366920938463463374607431768211454",
2602                "file340282366920938463463374607431768211455"
2603            ),
2604            Ordering::Less
2605        );
2606
2607        // Equal length numbers that overflow u128
2608        assert_eq!(
2609            natural_sort(
2610                "file340282366920938463463374607431768211456",
2611                "file340282366920938463463374607431768211455"
2612            ),
2613            Ordering::Greater
2614        );
2615
2616        // Different length numbers that overflow u128
2617        assert_eq!(
2618            natural_sort(
2619                "file3402823669209384634633746074317682114560",
2620                "file340282366920938463463374607431768211455"
2621            ),
2622            Ordering::Greater
2623        );
2624
2625        // Leading zeros with numbers near u128::MAX
2626        assert_eq!(
2627            natural_sort(
2628                "file0340282366920938463463374607431768211455",
2629                "file340282366920938463463374607431768211455"
2630            ),
2631            Ordering::Greater
2632        );
2633
2634        // Very large numbers with different lengths (both overflow u128)
2635        assert_eq!(
2636            natural_sort(
2637                "file999999999999999999999999999999999999999999999999",
2638                "file9999999999999999999999999999999999999999999999999"
2639            ),
2640            Ordering::Less
2641        );
2642    }
2643
2644    #[perf]
2645    fn test_natural_sort_case_sensitive() {
2646        // Numerically smaller values come first.
2647        assert_eq!(natural_sort("File1", "file2"), Ordering::Less);
2648        assert_eq!(natural_sort("file1", "File2"), Ordering::Less);
2649
2650        // Numerically equal values: the case-insensitive comparison decides first.
2651        // Case-sensitive comparison only occurs when both are equal case-insensitively.
2652        assert_eq!(natural_sort("Dir1", "dir01"), Ordering::Less);
2653        assert_eq!(natural_sort("dir2", "Dir02"), Ordering::Less);
2654        assert_eq!(natural_sort("dir2", "dir02"), Ordering::Less);
2655
2656        // Numerically equal and case-insensitively equal:
2657        // the lexicographically smaller (case-sensitive) one wins.
2658        assert_eq!(natural_sort("dir1", "Dir1"), Ordering::Less);
2659        assert_eq!(natural_sort("dir02", "Dir02"), Ordering::Less);
2660        assert_eq!(natural_sort("dir10", "Dir10"), Ordering::Less);
2661    }
2662
2663    #[perf]
2664    fn test_natural_sort_edge_cases() {
2665        // Empty strings
2666        assert_eq!(natural_sort("", ""), Ordering::Equal);
2667        assert_eq!(natural_sort("", "a"), Ordering::Less);
2668        assert_eq!(natural_sort("a", ""), Ordering::Greater);
2669
2670        // Special characters
2671        assert_eq!(natural_sort("file-1", "file_1"), Ordering::Less);
2672        assert_eq!(natural_sort("file.1", "file_1"), Ordering::Less);
2673        assert_eq!(natural_sort("file 1", "file_1"), Ordering::Less);
2674
2675        // Unicode characters
2676        // 9312 vs 9313
2677        assert_eq!(natural_sort("file①", "file②"), Ordering::Less);
2678        // 9321 vs 9313
2679        assert_eq!(natural_sort("file⑩", "file②"), Ordering::Greater);
2680        // 28450 vs 23383
2681        assert_eq!(natural_sort("file漢", "file字"), Ordering::Greater);
2682
2683        // Mixed alphanumeric with special chars
2684        assert_eq!(natural_sort("file-1a", "file-1b"), Ordering::Less);
2685        assert_eq!(natural_sort("file-1.2", "file-1.10"), Ordering::Less);
2686        assert_eq!(natural_sort("file-1.10", "file-1.2"), Ordering::Greater);
2687    }
2688
2689    #[test]
2690    fn test_multiple_extensions() {
2691        // No extensions
2692        let path = Path::new("/a/b/c/file_name");
2693        assert_eq!(path.multiple_extensions(), None);
2694
2695        // Only one extension
2696        let path = Path::new("/a/b/c/file_name.tsx");
2697        assert_eq!(path.multiple_extensions(), None);
2698
2699        // Stories sample extension
2700        let path = Path::new("/a/b/c/file_name.stories.tsx");
2701        assert_eq!(path.multiple_extensions(), Some("stories.tsx".to_string()));
2702
2703        // Longer sample extension
2704        let path = Path::new("/a/b/c/long.app.tar.gz");
2705        assert_eq!(path.multiple_extensions(), Some("app.tar.gz".to_string()));
2706    }
2707
2708    #[test]
2709    fn test_strip_path_suffix() {
2710        let base = Path::new("/a/b/c/file_name");
2711        let suffix = Path::new("file_name");
2712        assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a/b/c")));
2713
2714        let base = Path::new("/a/b/c/file_name.tsx");
2715        let suffix = Path::new("file_name.tsx");
2716        assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a/b/c")));
2717
2718        let base = Path::new("/a/b/c/file_name.stories.tsx");
2719        let suffix = Path::new("c/file_name.stories.tsx");
2720        assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a/b")));
2721
2722        let base = Path::new("/a/b/c/long.app.tar.gz");
2723        let suffix = Path::new("b/c/long.app.tar.gz");
2724        assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a")));
2725
2726        let base = Path::new("/a/b/c/long.app.tar.gz");
2727        let suffix = Path::new("/a/b/c/long.app.tar.gz");
2728        assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("")));
2729
2730        let base = Path::new("/a/b/c/long.app.tar.gz");
2731        let suffix = Path::new("/a/b/c/no_match.app.tar.gz");
2732        assert_eq!(strip_path_suffix(base, suffix), None);
2733
2734        let base = Path::new("/a/b/c/long.app.tar.gz");
2735        let suffix = Path::new("app.tar.gz");
2736        assert_eq!(strip_path_suffix(base, suffix), None);
2737    }
2738
2739    #[test]
2740    fn test_strip_prefix() {
2741        let expected = [
2742            (
2743                PathStyle::Posix,
2744                "/a/b/c",
2745                "/a/b",
2746                Some(rel_path("c").into_arc()),
2747            ),
2748            (
2749                PathStyle::Posix,
2750                "/a/b/c",
2751                "/a/b/",
2752                Some(rel_path("c").into_arc()),
2753            ),
2754            (
2755                PathStyle::Posix,
2756                "/a/b/c",
2757                "/",
2758                Some(rel_path("a/b/c").into_arc()),
2759            ),
2760            (PathStyle::Posix, "/a/b/c", "", None),
2761            (PathStyle::Posix, "/a/b//c", "/a/b/", None),
2762            (PathStyle::Posix, "/a/bc", "/a/b", None),
2763            (
2764                PathStyle::Posix,
2765                "/a/b/c",
2766                "/a/b/c",
2767                Some(rel_path("").into_arc()),
2768            ),
2769            (
2770                PathStyle::Windows,
2771                "C:\\a\\b\\c",
2772                "C:\\a\\b",
2773                Some(rel_path("c").into_arc()),
2774            ),
2775            (
2776                PathStyle::Windows,
2777                "C:\\a\\b\\c",
2778                "C:\\a\\b\\",
2779                Some(rel_path("c").into_arc()),
2780            ),
2781            (
2782                PathStyle::Windows,
2783                "C:\\a\\b\\c",
2784                "C:\\",
2785                Some(rel_path("a/b/c").into_arc()),
2786            ),
2787            (PathStyle::Windows, "C:\\a\\b\\c", "", None),
2788            (PathStyle::Windows, "C:\\a\\b\\\\c", "C:\\a\\b\\", None),
2789            (PathStyle::Windows, "C:\\a\\bc", "C:\\a\\b", None),
2790            (
2791                PathStyle::Windows,
2792                "C:\\a\\b/c",
2793                "C:\\a\\b",
2794                Some(rel_path("c").into_arc()),
2795            ),
2796            (
2797                PathStyle::Windows,
2798                "C:\\a\\b/c",
2799                "C:\\a\\b\\",
2800                Some(rel_path("c").into_arc()),
2801            ),
2802            (
2803                PathStyle::Windows,
2804                "C:\\a\\b/c",
2805                "C:\\a\\b/",
2806                Some(rel_path("c").into_arc()),
2807            ),
2808        ];
2809        let actual = expected.clone().map(|(style, child, parent, _)| {
2810            (
2811                style,
2812                child,
2813                parent,
2814                style
2815                    .strip_prefix(child.as_ref(), parent.as_ref())
2816                    .map(|rel_path| rel_path.into_arc()),
2817            )
2818        });
2819        pretty_assertions::assert_eq!(actual, expected);
2820    }
2821
2822    #[cfg(target_os = "windows")]
2823    #[test]
2824    fn test_wsl_path() {
2825        use super::WslPath;
2826        let path = "/a/b/c";
2827        assert_eq!(WslPath::from_path(&path), None);
2828
2829        let path = r"\\wsl.localhost";
2830        assert_eq!(WslPath::from_path(&path), None);
2831
2832        let path = r"\\wsl.localhost\Distro";
2833        assert_eq!(
2834            WslPath::from_path(&path),
2835            Some(WslPath {
2836                distro: "Distro".to_owned(),
2837                path: "/".into(),
2838            })
2839        );
2840
2841        let path = r"\\wsl.localhost\Distro\blue";
2842        assert_eq!(
2843            WslPath::from_path(&path),
2844            Some(WslPath {
2845                distro: "Distro".to_owned(),
2846                path: "/blue".into()
2847            })
2848        );
2849
2850        let path = r"\\wsl$\archlinux\tomato\.\paprika\..\aubergine.txt";
2851        assert_eq!(
2852            WslPath::from_path(&path),
2853            Some(WslPath {
2854                distro: "archlinux".to_owned(),
2855                path: "/tomato/paprika/../aubergine.txt".into()
2856            })
2857        );
2858
2859        let path = r"\\windows.localhost\Distro\foo";
2860        assert_eq!(WslPath::from_path(&path), None);
2861    }
2862
2863    #[test]
2864    fn test_url_to_file_path_ext_posix_basic() {
2865        use super::UrlExt;
2866
2867        let url = url::Url::parse("file:///home/user/file.txt").unwrap();
2868        assert_eq!(
2869            url.to_file_path_ext(PathStyle::Posix),
2870            Ok(PathBuf::from("/home/user/file.txt"))
2871        );
2872
2873        let url = url::Url::parse("file:///").unwrap();
2874        assert_eq!(
2875            url.to_file_path_ext(PathStyle::Posix),
2876            Ok(PathBuf::from("/"))
2877        );
2878
2879        let url = url::Url::parse("file:///a/b/c/d/e").unwrap();
2880        assert_eq!(
2881            url.to_file_path_ext(PathStyle::Posix),
2882            Ok(PathBuf::from("/a/b/c/d/e"))
2883        );
2884    }
2885
2886    #[test]
2887    fn test_url_to_file_path_ext_posix_percent_encoding() {
2888        use super::UrlExt;
2889
2890        let url = url::Url::parse("file:///home/user/file%20with%20spaces.txt").unwrap();
2891        assert_eq!(
2892            url.to_file_path_ext(PathStyle::Posix),
2893            Ok(PathBuf::from("/home/user/file with spaces.txt"))
2894        );
2895
2896        let url = url::Url::parse("file:///path%2Fwith%2Fencoded%2Fslashes").unwrap();
2897        assert_eq!(
2898            url.to_file_path_ext(PathStyle::Posix),
2899            Ok(PathBuf::from("/path/with/encoded/slashes"))
2900        );
2901
2902        let url = url::Url::parse("file:///special%23chars%3F.txt").unwrap();
2903        assert_eq!(
2904            url.to_file_path_ext(PathStyle::Posix),
2905            Ok(PathBuf::from("/special#chars?.txt"))
2906        );
2907    }
2908
2909    #[test]
2910    fn test_url_to_file_path_ext_posix_localhost() {
2911        use super::UrlExt;
2912
2913        let url = url::Url::parse("file://localhost/home/user/file.txt").unwrap();
2914        assert_eq!(
2915            url.to_file_path_ext(PathStyle::Posix),
2916            Ok(PathBuf::from("/home/user/file.txt"))
2917        );
2918    }
2919
2920    #[test]
2921    fn test_url_to_file_path_ext_posix_rejects_host() {
2922        use super::UrlExt;
2923
2924        let url = url::Url::parse("file://somehost/home/user/file.txt").unwrap();
2925        assert_eq!(url.to_file_path_ext(PathStyle::Posix), Err(()));
2926    }
2927
2928    #[test]
2929    fn test_url_to_file_path_ext_posix_windows_drive_letter() {
2930        use super::UrlExt;
2931
2932        let url = url::Url::parse("file:///C:").unwrap();
2933        assert_eq!(
2934            url.to_file_path_ext(PathStyle::Posix),
2935            Ok(PathBuf::from("/C:/"))
2936        );
2937
2938        let url = url::Url::parse("file:///D|").unwrap();
2939        assert_eq!(
2940            url.to_file_path_ext(PathStyle::Posix),
2941            Ok(PathBuf::from("/D|/"))
2942        );
2943    }
2944
2945    #[test]
2946    fn test_url_to_file_path_ext_windows_basic() {
2947        use super::UrlExt;
2948
2949        let url = url::Url::parse("file:///C:/Users/user/file.txt").unwrap();
2950        assert_eq!(
2951            url.to_file_path_ext(PathStyle::Windows),
2952            Ok(PathBuf::from("C:\\Users\\user\\file.txt"))
2953        );
2954
2955        let url = url::Url::parse("file:///D:/folder/subfolder/file.rs").unwrap();
2956        assert_eq!(
2957            url.to_file_path_ext(PathStyle::Windows),
2958            Ok(PathBuf::from("D:\\folder\\subfolder\\file.rs"))
2959        );
2960
2961        let url = url::Url::parse("file:///C:/").unwrap();
2962        assert_eq!(
2963            url.to_file_path_ext(PathStyle::Windows),
2964            Ok(PathBuf::from("C:\\"))
2965        );
2966    }
2967
2968    #[test]
2969    fn test_url_to_file_path_ext_windows_encoded_drive_letter() {
2970        use super::UrlExt;
2971
2972        let url = url::Url::parse("file:///C%3A/Users/file.txt").unwrap();
2973        assert_eq!(
2974            url.to_file_path_ext(PathStyle::Windows),
2975            Ok(PathBuf::from("C:\\Users\\file.txt"))
2976        );
2977
2978        let url = url::Url::parse("file:///c%3a/Users/file.txt").unwrap();
2979        assert_eq!(
2980            url.to_file_path_ext(PathStyle::Windows),
2981            Ok(PathBuf::from("c:\\Users\\file.txt"))
2982        );
2983
2984        let url = url::Url::parse("file:///D%3A/folder/file.txt").unwrap();
2985        assert_eq!(
2986            url.to_file_path_ext(PathStyle::Windows),
2987            Ok(PathBuf::from("D:\\folder\\file.txt"))
2988        );
2989
2990        let url = url::Url::parse("file:///d%3A/folder/file.txt").unwrap();
2991        assert_eq!(
2992            url.to_file_path_ext(PathStyle::Windows),
2993            Ok(PathBuf::from("d:\\folder\\file.txt"))
2994        );
2995    }
2996
2997    #[test]
2998    fn test_url_to_file_path_ext_windows_unc_path() {
2999        use super::UrlExt;
3000
3001        let url = url::Url::parse("file://server/share/path/file.txt").unwrap();
3002        assert_eq!(
3003            url.to_file_path_ext(PathStyle::Windows),
3004            Ok(PathBuf::from("\\\\server\\share\\path\\file.txt"))
3005        );
3006
3007        let url = url::Url::parse("file://server/share").unwrap();
3008        assert_eq!(
3009            url.to_file_path_ext(PathStyle::Windows),
3010            Ok(PathBuf::from("\\\\server\\share"))
3011        );
3012    }
3013
3014    #[test]
3015    fn test_url_to_file_path_ext_windows_percent_encoding() {
3016        use super::UrlExt;
3017
3018        let url = url::Url::parse("file:///C:/Users/user/file%20with%20spaces.txt").unwrap();
3019        assert_eq!(
3020            url.to_file_path_ext(PathStyle::Windows),
3021            Ok(PathBuf::from("C:\\Users\\user\\file with spaces.txt"))
3022        );
3023
3024        let url = url::Url::parse("file:///C:/special%23chars%3F.txt").unwrap();
3025        assert_eq!(
3026            url.to_file_path_ext(PathStyle::Windows),
3027            Ok(PathBuf::from("C:\\special#chars?.txt"))
3028        );
3029    }
3030
3031    #[test]
3032    fn test_url_to_file_path_ext_windows_invalid_drive() {
3033        use super::UrlExt;
3034
3035        let url = url::Url::parse("file:///1:/path/file.txt").unwrap();
3036        assert_eq!(url.to_file_path_ext(PathStyle::Windows), Err(()));
3037
3038        let url = url::Url::parse("file:///CC:/path/file.txt").unwrap();
3039        assert_eq!(url.to_file_path_ext(PathStyle::Windows), Err(()));
3040
3041        let url = url::Url::parse("file:///C/path/file.txt").unwrap();
3042        assert_eq!(url.to_file_path_ext(PathStyle::Windows), Err(()));
3043
3044        let url = url::Url::parse("file:///invalid").unwrap();
3045        assert_eq!(url.to_file_path_ext(PathStyle::Windows), Err(()));
3046    }
3047
3048    #[test]
3049    fn test_url_to_file_path_ext_non_file_scheme() {
3050        use super::UrlExt;
3051
3052        let url = url::Url::parse("http://example.com/path").unwrap();
3053        assert_eq!(url.to_file_path_ext(PathStyle::Posix), Err(()));
3054        assert_eq!(url.to_file_path_ext(PathStyle::Windows), Err(()));
3055
3056        let url = url::Url::parse("https://example.com/path").unwrap();
3057        assert_eq!(url.to_file_path_ext(PathStyle::Posix), Err(()));
3058        assert_eq!(url.to_file_path_ext(PathStyle::Windows), Err(()));
3059    }
3060
3061    #[test]
3062    fn test_url_to_file_path_ext_windows_localhost() {
3063        use super::UrlExt;
3064
3065        let url = url::Url::parse("file://localhost/C:/Users/file.txt").unwrap();
3066        assert_eq!(
3067            url.to_file_path_ext(PathStyle::Windows),
3068            Ok(PathBuf::from("C:\\Users\\file.txt"))
3069        );
3070    }
3071}