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