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