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