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