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