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