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 == b => Ordering::Greater,
1178 (false, true) if a == 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_with_nested_paths() {
1821 // Test that nested paths still work correctly
1822 let mut paths = vec![
1823 (RelPath::unix("src/main.rs").unwrap(), true),
1824 (RelPath::unix("Cargo.toml").unwrap(), true),
1825 (RelPath::unix("src").unwrap(), false),
1826 (RelPath::unix("target").unwrap(), false),
1827 ];
1828 paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1829 assert_eq!(
1830 paths,
1831 vec![
1832 (RelPath::unix("Cargo.toml").unwrap(), true),
1833 (RelPath::unix("src").unwrap(), false),
1834 (RelPath::unix("src/main.rs").unwrap(), true),
1835 (RelPath::unix("target").unwrap(), false),
1836 ]
1837 );
1838 }
1839
1840 #[perf]
1841 fn compare_rel_paths_files_first_with_nested() {
1842 // Files come before directories, even with nested paths
1843 let mut paths = vec![
1844 (RelPath::unix("src/lib.rs").unwrap(), true),
1845 (RelPath::unix("README.md").unwrap(), true),
1846 (RelPath::unix("src").unwrap(), false),
1847 (RelPath::unix("tests").unwrap(), false),
1848 ];
1849 paths.sort_by(|&a, &b| compare_rel_paths_files_first(a, b));
1850 assert_eq!(
1851 paths,
1852 vec![
1853 (RelPath::unix("README.md").unwrap(), true),
1854 (RelPath::unix("src").unwrap(), false),
1855 (RelPath::unix("src/lib.rs").unwrap(), true),
1856 (RelPath::unix("tests").unwrap(), false),
1857 ]
1858 );
1859 }
1860
1861 #[perf]
1862 fn compare_rel_paths_mixed_dotfiles() {
1863 // Test that dotfiles are handled correctly in mixed mode
1864 let mut paths = vec![
1865 (RelPath::unix(".gitignore").unwrap(), true),
1866 (RelPath::unix("README.md").unwrap(), true),
1867 (RelPath::unix(".github").unwrap(), false),
1868 (RelPath::unix("src").unwrap(), false),
1869 ];
1870 paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1871 assert_eq!(
1872 paths,
1873 vec![
1874 (RelPath::unix(".github").unwrap(), false),
1875 (RelPath::unix(".gitignore").unwrap(), true),
1876 (RelPath::unix("README.md").unwrap(), true),
1877 (RelPath::unix("src").unwrap(), false),
1878 ]
1879 );
1880 }
1881
1882 #[perf]
1883 fn compare_rel_paths_files_first_dotfiles() {
1884 // Test that dotfiles come first when they're files
1885 let mut paths = vec![
1886 (RelPath::unix(".gitignore").unwrap(), true),
1887 (RelPath::unix("README.md").unwrap(), true),
1888 (RelPath::unix(".github").unwrap(), false),
1889 (RelPath::unix("src").unwrap(), false),
1890 ];
1891 paths.sort_by(|&a, &b| compare_rel_paths_files_first(a, b));
1892 assert_eq!(
1893 paths,
1894 vec![
1895 (RelPath::unix(".gitignore").unwrap(), true),
1896 (RelPath::unix("README.md").unwrap(), true),
1897 (RelPath::unix(".github").unwrap(), false),
1898 (RelPath::unix("src").unwrap(), false),
1899 ]
1900 );
1901 }
1902
1903 #[perf]
1904 fn compare_rel_paths_mixed_same_stem_different_extension() {
1905 // Files with same stem but different extensions should sort by extension
1906 let mut paths = vec![
1907 (RelPath::unix("file.rs").unwrap(), true),
1908 (RelPath::unix("file.md").unwrap(), true),
1909 (RelPath::unix("file.txt").unwrap(), true),
1910 ];
1911 paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1912 assert_eq!(
1913 paths,
1914 vec![
1915 (RelPath::unix("file.txt").unwrap(), true),
1916 (RelPath::unix("file.rs").unwrap(), true),
1917 (RelPath::unix("file.md").unwrap(), true),
1918 ]
1919 );
1920 }
1921
1922 #[perf]
1923 fn compare_rel_paths_files_first_same_stem() {
1924 // Same stem files should still sort by extension with files_first
1925 let mut paths = vec![
1926 (RelPath::unix("main.rs").unwrap(), true),
1927 (RelPath::unix("main.c").unwrap(), true),
1928 (RelPath::unix("main").unwrap(), false),
1929 ];
1930 paths.sort_by(|&a, &b| compare_rel_paths_files_first(a, b));
1931 assert_eq!(
1932 paths,
1933 vec![
1934 (RelPath::unix("main.c").unwrap(), true),
1935 (RelPath::unix("main.rs").unwrap(), true),
1936 (RelPath::unix("main").unwrap(), false),
1937 ]
1938 );
1939 }
1940
1941 #[perf]
1942 fn compare_rel_paths_mixed_deep_nesting() {
1943 // Test sorting with deeply nested paths
1944 let mut paths = vec![
1945 (RelPath::unix("a/b/c.txt").unwrap(), true),
1946 (RelPath::unix("A/B.txt").unwrap(), true),
1947 (RelPath::unix("a.txt").unwrap(), true),
1948 (RelPath::unix("A.txt").unwrap(), true),
1949 ];
1950 paths.sort_by(|&a, &b| compare_rel_paths_mixed(a, b));
1951 assert_eq!(
1952 paths,
1953 vec![
1954 (RelPath::unix("A/B.txt").unwrap(), true),
1955 (RelPath::unix("a/b/c.txt").unwrap(), true),
1956 (RelPath::unix("a.txt").unwrap(), true),
1957 (RelPath::unix("A.txt").unwrap(), true),
1958 ]
1959 );
1960 }
1961
1962 #[perf]
1963 fn path_with_position_parse_posix_path() {
1964 // Test POSIX filename edge cases
1965 // Read more at https://en.wikipedia.org/wiki/Filename
1966 assert_eq!(
1967 PathWithPosition::parse_str("test_file"),
1968 PathWithPosition {
1969 path: PathBuf::from("test_file"),
1970 row: None,
1971 column: None
1972 }
1973 );
1974
1975 assert_eq!(
1976 PathWithPosition::parse_str("a:bc:.zip:1"),
1977 PathWithPosition {
1978 path: PathBuf::from("a:bc:.zip"),
1979 row: Some(1),
1980 column: None
1981 }
1982 );
1983
1984 assert_eq!(
1985 PathWithPosition::parse_str("one.second.zip:1"),
1986 PathWithPosition {
1987 path: PathBuf::from("one.second.zip"),
1988 row: Some(1),
1989 column: None
1990 }
1991 );
1992
1993 // Trim off trailing `:`s for otherwise valid input.
1994 assert_eq!(
1995 PathWithPosition::parse_str("test_file:10:1:"),
1996 PathWithPosition {
1997 path: PathBuf::from("test_file"),
1998 row: Some(10),
1999 column: Some(1)
2000 }
2001 );
2002
2003 assert_eq!(
2004 PathWithPosition::parse_str("test_file.rs:"),
2005 PathWithPosition {
2006 path: PathBuf::from("test_file.rs"),
2007 row: None,
2008 column: None
2009 }
2010 );
2011
2012 assert_eq!(
2013 PathWithPosition::parse_str("test_file.rs:1:"),
2014 PathWithPosition {
2015 path: PathBuf::from("test_file.rs"),
2016 row: Some(1),
2017 column: None
2018 }
2019 );
2020
2021 assert_eq!(
2022 PathWithPosition::parse_str("ab\ncd"),
2023 PathWithPosition {
2024 path: PathBuf::from("ab\ncd"),
2025 row: None,
2026 column: None
2027 }
2028 );
2029
2030 assert_eq!(
2031 PathWithPosition::parse_str("👋\nab"),
2032 PathWithPosition {
2033 path: PathBuf::from("👋\nab"),
2034 row: None,
2035 column: None
2036 }
2037 );
2038
2039 assert_eq!(
2040 PathWithPosition::parse_str("Types.hs:(617,9)-(670,28):"),
2041 PathWithPosition {
2042 path: PathBuf::from("Types.hs"),
2043 row: Some(617),
2044 column: Some(9),
2045 }
2046 );
2047 }
2048
2049 #[perf]
2050 #[cfg(not(target_os = "windows"))]
2051 fn path_with_position_parse_posix_path_with_suffix() {
2052 assert_eq!(
2053 PathWithPosition::parse_str("foo/bar:34:in"),
2054 PathWithPosition {
2055 path: PathBuf::from("foo/bar"),
2056 row: Some(34),
2057 column: None,
2058 }
2059 );
2060 assert_eq!(
2061 PathWithPosition::parse_str("foo/bar.rs:1902:::15:"),
2062 PathWithPosition {
2063 path: PathBuf::from("foo/bar.rs:1902"),
2064 row: Some(15),
2065 column: None
2066 }
2067 );
2068
2069 assert_eq!(
2070 PathWithPosition::parse_str("app-editors:zed-0.143.6:20240710-201212.log:34:"),
2071 PathWithPosition {
2072 path: PathBuf::from("app-editors:zed-0.143.6:20240710-201212.log"),
2073 row: Some(34),
2074 column: None,
2075 }
2076 );
2077
2078 assert_eq!(
2079 PathWithPosition::parse_str("crates/file_finder/src/file_finder.rs:1902:13:"),
2080 PathWithPosition {
2081 path: PathBuf::from("crates/file_finder/src/file_finder.rs"),
2082 row: Some(1902),
2083 column: Some(13),
2084 }
2085 );
2086
2087 assert_eq!(
2088 PathWithPosition::parse_str("crate/utils/src/test:today.log:34"),
2089 PathWithPosition {
2090 path: PathBuf::from("crate/utils/src/test:today.log"),
2091 row: Some(34),
2092 column: None,
2093 }
2094 );
2095 assert_eq!(
2096 PathWithPosition::parse_str("/testing/out/src/file_finder.odin(7:15)"),
2097 PathWithPosition {
2098 path: PathBuf::from("/testing/out/src/file_finder.odin"),
2099 row: Some(7),
2100 column: Some(15),
2101 }
2102 );
2103 }
2104
2105 #[perf]
2106 #[cfg(target_os = "windows")]
2107 fn path_with_position_parse_windows_path() {
2108 assert_eq!(
2109 PathWithPosition::parse_str("crates\\utils\\paths.rs"),
2110 PathWithPosition {
2111 path: PathBuf::from("crates\\utils\\paths.rs"),
2112 row: None,
2113 column: None
2114 }
2115 );
2116
2117 assert_eq!(
2118 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs"),
2119 PathWithPosition {
2120 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
2121 row: None,
2122 column: None
2123 }
2124 );
2125 }
2126
2127 #[perf]
2128 #[cfg(target_os = "windows")]
2129 fn path_with_position_parse_windows_path_with_suffix() {
2130 assert_eq!(
2131 PathWithPosition::parse_str("crates\\utils\\paths.rs:101"),
2132 PathWithPosition {
2133 path: PathBuf::from("crates\\utils\\paths.rs"),
2134 row: Some(101),
2135 column: None
2136 }
2137 );
2138
2139 assert_eq!(
2140 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1:20"),
2141 PathWithPosition {
2142 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
2143 row: Some(1),
2144 column: Some(20)
2145 }
2146 );
2147
2148 assert_eq!(
2149 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13)"),
2150 PathWithPosition {
2151 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
2152 row: Some(1902),
2153 column: Some(13)
2154 }
2155 );
2156
2157 // Trim off trailing `:`s for otherwise valid input.
2158 assert_eq!(
2159 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:"),
2160 PathWithPosition {
2161 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
2162 row: Some(1902),
2163 column: Some(13)
2164 }
2165 );
2166
2167 assert_eq!(
2168 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:"),
2169 PathWithPosition {
2170 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
2171 row: Some(13),
2172 column: Some(15)
2173 }
2174 );
2175
2176 assert_eq!(
2177 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:"),
2178 PathWithPosition {
2179 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs:1902"),
2180 row: Some(15),
2181 column: None
2182 }
2183 );
2184
2185 assert_eq!(
2186 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902,13):"),
2187 PathWithPosition {
2188 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
2189 row: Some(1902),
2190 column: Some(13),
2191 }
2192 );
2193
2194 assert_eq!(
2195 PathWithPosition::parse_str("\\\\?\\C:\\Users\\someone\\test_file.rs(1902):"),
2196 PathWithPosition {
2197 path: PathBuf::from("\\\\?\\C:\\Users\\someone\\test_file.rs"),
2198 row: Some(1902),
2199 column: None,
2200 }
2201 );
2202
2203 assert_eq!(
2204 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs:1902:13:"),
2205 PathWithPosition {
2206 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
2207 row: Some(1902),
2208 column: Some(13),
2209 }
2210 );
2211
2212 assert_eq!(
2213 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902,13):"),
2214 PathWithPosition {
2215 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
2216 row: Some(1902),
2217 column: Some(13),
2218 }
2219 );
2220
2221 assert_eq!(
2222 PathWithPosition::parse_str("C:\\Users\\someone\\test_file.rs(1902):"),
2223 PathWithPosition {
2224 path: PathBuf::from("C:\\Users\\someone\\test_file.rs"),
2225 row: Some(1902),
2226 column: None,
2227 }
2228 );
2229
2230 assert_eq!(
2231 PathWithPosition::parse_str("crates/utils/paths.rs:101"),
2232 PathWithPosition {
2233 path: PathBuf::from("crates\\utils\\paths.rs"),
2234 row: Some(101),
2235 column: None,
2236 }
2237 );
2238 }
2239
2240 #[perf]
2241 fn test_path_compact() {
2242 let path: PathBuf = [
2243 home_dir().to_string_lossy().into_owned(),
2244 "some_file.txt".to_string(),
2245 ]
2246 .iter()
2247 .collect();
2248 if cfg!(any(target_os = "linux", target_os = "freebsd")) || cfg!(target_os = "macos") {
2249 assert_eq!(path.compact().to_str(), Some("~/some_file.txt"));
2250 } else {
2251 assert_eq!(path.compact().to_str(), path.to_str());
2252 }
2253 }
2254
2255 #[perf]
2256 fn test_extension_or_hidden_file_name() {
2257 // No dots in name
2258 let path = Path::new("/a/b/c/file_name.rs");
2259 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
2260
2261 // Single dot in name
2262 let path = Path::new("/a/b/c/file.name.rs");
2263 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
2264
2265 // Multiple dots in name
2266 let path = Path::new("/a/b/c/long.file.name.rs");
2267 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
2268
2269 // Hidden file, no extension
2270 let path = Path::new("/a/b/c/.gitignore");
2271 assert_eq!(path.extension_or_hidden_file_name(), Some("gitignore"));
2272
2273 // Hidden file, with extension
2274 let path = Path::new("/a/b/c/.eslintrc.js");
2275 assert_eq!(path.extension_or_hidden_file_name(), Some("eslintrc.js"));
2276 }
2277
2278 #[perf]
2279 // fn edge_of_glob() {
2280 // let path = Path::new("/work/node_modules");
2281 // let path_matcher =
2282 // PathMatcher::new(&["**/node_modules/**".to_owned()], PathStyle::Posix).unwrap();
2283 // assert!(
2284 // path_matcher.is_match(path),
2285 // "Path matcher should match {path:?}"
2286 // );
2287 // }
2288
2289 // #[perf]
2290 // fn file_in_dirs() {
2291 // let path = Path::new("/work/.env");
2292 // let path_matcher = PathMatcher::new(&["**/.env".to_owned()], PathStyle::Posix).unwrap();
2293 // assert!(
2294 // path_matcher.is_match(path),
2295 // "Path matcher should match {path:?}"
2296 // );
2297 // let path = Path::new("/work/package.json");
2298 // assert!(
2299 // !path_matcher.is_match(path),
2300 // "Path matcher should not match {path:?}"
2301 // );
2302 // }
2303
2304 // #[perf]
2305 // fn project_search() {
2306 // let path = Path::new("/Users/someonetoignore/work/zed/zed.dev/node_modules");
2307 // let path_matcher =
2308 // PathMatcher::new(&["**/node_modules/**".to_owned()], PathStyle::Posix).unwrap();
2309 // assert!(
2310 // path_matcher.is_match(path),
2311 // "Path matcher should match {path:?}"
2312 // );
2313 // }
2314 #[perf]
2315 #[cfg(target_os = "windows")]
2316 fn test_sanitized_path() {
2317 let path = Path::new("C:\\Users\\someone\\test_file.rs");
2318 let sanitized_path = SanitizedPath::new(path);
2319 assert_eq!(
2320 sanitized_path.to_string(),
2321 "C:\\Users\\someone\\test_file.rs"
2322 );
2323
2324 let path = Path::new("\\\\?\\C:\\Users\\someone\\test_file.rs");
2325 let sanitized_path = SanitizedPath::new(path);
2326 assert_eq!(
2327 sanitized_path.to_string(),
2328 "C:\\Users\\someone\\test_file.rs"
2329 );
2330 }
2331
2332 #[perf]
2333 fn test_compare_numeric_segments() {
2334 // Helper function to create peekable iterators and test
2335 fn compare(a: &str, b: &str) -> Ordering {
2336 let mut a_iter = a.chars().peekable();
2337 let mut b_iter = b.chars().peekable();
2338
2339 let result = compare_numeric_segments(&mut a_iter, &mut b_iter);
2340
2341 // Verify iterators advanced correctly
2342 assert!(
2343 !a_iter.next().is_some_and(|c| c.is_ascii_digit()),
2344 "Iterator a should have consumed all digits"
2345 );
2346 assert!(
2347 !b_iter.next().is_some_and(|c| c.is_ascii_digit()),
2348 "Iterator b should have consumed all digits"
2349 );
2350
2351 result
2352 }
2353
2354 // Basic numeric comparisons
2355 assert_eq!(compare("0", "0"), Ordering::Equal);
2356 assert_eq!(compare("1", "2"), Ordering::Less);
2357 assert_eq!(compare("9", "10"), Ordering::Less);
2358 assert_eq!(compare("10", "9"), Ordering::Greater);
2359 assert_eq!(compare("99", "100"), Ordering::Less);
2360
2361 // Leading zeros
2362 assert_eq!(compare("0", "00"), Ordering::Less);
2363 assert_eq!(compare("00", "0"), Ordering::Greater);
2364 assert_eq!(compare("01", "1"), Ordering::Greater);
2365 assert_eq!(compare("001", "1"), Ordering::Greater);
2366 assert_eq!(compare("001", "01"), Ordering::Greater);
2367
2368 // Same value different representation
2369 assert_eq!(compare("000100", "100"), Ordering::Greater);
2370 assert_eq!(compare("100", "0100"), Ordering::Less);
2371 assert_eq!(compare("0100", "00100"), Ordering::Less);
2372
2373 // Large numbers
2374 assert_eq!(compare("9999999999", "10000000000"), Ordering::Less);
2375 assert_eq!(
2376 compare(
2377 "340282366920938463463374607431768211455", // u128::MAX
2378 "340282366920938463463374607431768211456"
2379 ),
2380 Ordering::Less
2381 );
2382 assert_eq!(
2383 compare(
2384 "340282366920938463463374607431768211456", // > u128::MAX
2385 "340282366920938463463374607431768211455"
2386 ),
2387 Ordering::Greater
2388 );
2389
2390 // Iterator advancement verification
2391 let mut a_iter = "123abc".chars().peekable();
2392 let mut b_iter = "456def".chars().peekable();
2393
2394 compare_numeric_segments(&mut a_iter, &mut b_iter);
2395
2396 assert_eq!(a_iter.collect::<String>(), "abc");
2397 assert_eq!(b_iter.collect::<String>(), "def");
2398 }
2399
2400 #[perf]
2401 fn test_natural_sort() {
2402 // Basic alphanumeric
2403 assert_eq!(natural_sort("a", "b"), Ordering::Less);
2404 assert_eq!(natural_sort("b", "a"), Ordering::Greater);
2405 assert_eq!(natural_sort("a", "a"), Ordering::Equal);
2406
2407 // Case sensitivity
2408 assert_eq!(natural_sort("a", "A"), Ordering::Less);
2409 assert_eq!(natural_sort("A", "a"), Ordering::Greater);
2410 assert_eq!(natural_sort("aA", "aa"), Ordering::Greater);
2411 assert_eq!(natural_sort("aa", "aA"), Ordering::Less);
2412
2413 // Numbers
2414 assert_eq!(natural_sort("1", "2"), Ordering::Less);
2415 assert_eq!(natural_sort("2", "10"), Ordering::Less);
2416 assert_eq!(natural_sort("02", "10"), Ordering::Less);
2417 assert_eq!(natural_sort("02", "2"), Ordering::Greater);
2418
2419 // Mixed alphanumeric
2420 assert_eq!(natural_sort("a1", "a2"), Ordering::Less);
2421 assert_eq!(natural_sort("a2", "a10"), Ordering::Less);
2422 assert_eq!(natural_sort("a02", "a2"), Ordering::Greater);
2423 assert_eq!(natural_sort("a1b", "a1c"), Ordering::Less);
2424
2425 // Multiple numeric segments
2426 assert_eq!(natural_sort("1a2", "1a10"), Ordering::Less);
2427 assert_eq!(natural_sort("1a10", "1a2"), Ordering::Greater);
2428 assert_eq!(natural_sort("2a1", "10a1"), Ordering::Less);
2429
2430 // Special characters
2431 assert_eq!(natural_sort("a-1", "a-2"), Ordering::Less);
2432 assert_eq!(natural_sort("a_1", "a_2"), Ordering::Less);
2433 assert_eq!(natural_sort("a.1", "a.2"), Ordering::Less);
2434
2435 // Unicode
2436 assert_eq!(natural_sort("文1", "文2"), Ordering::Less);
2437 assert_eq!(natural_sort("文2", "文10"), Ordering::Less);
2438 assert_eq!(natural_sort("🔤1", "🔤2"), Ordering::Less);
2439
2440 // Empty and special cases
2441 assert_eq!(natural_sort("", ""), Ordering::Equal);
2442 assert_eq!(natural_sort("", "a"), Ordering::Less);
2443 assert_eq!(natural_sort("a", ""), Ordering::Greater);
2444 assert_eq!(natural_sort(" ", " "), Ordering::Less);
2445
2446 // Mixed everything
2447 assert_eq!(natural_sort("File-1.txt", "File-2.txt"), Ordering::Less);
2448 assert_eq!(natural_sort("File-02.txt", "File-2.txt"), Ordering::Greater);
2449 assert_eq!(natural_sort("File-2.txt", "File-10.txt"), Ordering::Less);
2450 assert_eq!(natural_sort("File_A1", "File_A2"), Ordering::Less);
2451 assert_eq!(natural_sort("File_a1", "File_A1"), Ordering::Less);
2452 }
2453
2454 #[perf]
2455 fn test_compare_paths() {
2456 // Helper function for cleaner tests
2457 fn compare(a: &str, is_a_file: bool, b: &str, is_b_file: bool) -> Ordering {
2458 compare_paths((Path::new(a), is_a_file), (Path::new(b), is_b_file))
2459 }
2460
2461 // Basic path comparison
2462 assert_eq!(compare("a", true, "b", true), Ordering::Less);
2463 assert_eq!(compare("b", true, "a", true), Ordering::Greater);
2464 assert_eq!(compare("a", true, "a", true), Ordering::Equal);
2465
2466 // Files vs Directories
2467 assert_eq!(compare("a", true, "a", false), Ordering::Greater);
2468 assert_eq!(compare("a", false, "a", true), Ordering::Less);
2469 assert_eq!(compare("b", false, "a", true), Ordering::Less);
2470
2471 // Extensions
2472 assert_eq!(compare("a.txt", true, "a.md", true), Ordering::Greater);
2473 assert_eq!(compare("a.md", true, "a.txt", true), Ordering::Less);
2474 assert_eq!(compare("a", true, "a.txt", true), Ordering::Less);
2475
2476 // Nested paths
2477 assert_eq!(compare("dir/a", true, "dir/b", true), Ordering::Less);
2478 assert_eq!(compare("dir1/a", true, "dir2/a", true), Ordering::Less);
2479 assert_eq!(compare("dir/sub/a", true, "dir/a", true), Ordering::Less);
2480
2481 // Case sensitivity in paths
2482 assert_eq!(
2483 compare("Dir/file", true, "dir/file", true),
2484 Ordering::Greater
2485 );
2486 assert_eq!(
2487 compare("dir/File", true, "dir/file", true),
2488 Ordering::Greater
2489 );
2490 assert_eq!(compare("dir/file", true, "Dir/File", true), Ordering::Less);
2491
2492 // Hidden files and special names
2493 assert_eq!(compare(".hidden", true, "visible", true), Ordering::Less);
2494 assert_eq!(compare("_special", true, "normal", true), Ordering::Less);
2495 assert_eq!(compare(".config", false, ".data", false), Ordering::Less);
2496
2497 // Mixed numeric paths
2498 assert_eq!(
2499 compare("dir1/file", true, "dir2/file", true),
2500 Ordering::Less
2501 );
2502 assert_eq!(
2503 compare("dir2/file", true, "dir10/file", true),
2504 Ordering::Less
2505 );
2506 assert_eq!(
2507 compare("dir02/file", true, "dir2/file", true),
2508 Ordering::Greater
2509 );
2510
2511 // Root paths
2512 assert_eq!(compare("/a", true, "/b", true), Ordering::Less);
2513 assert_eq!(compare("/", false, "/a", true), Ordering::Less);
2514
2515 // Complex real-world examples
2516 assert_eq!(
2517 compare("project/src/main.rs", true, "project/src/lib.rs", true),
2518 Ordering::Greater
2519 );
2520 assert_eq!(
2521 compare(
2522 "project/tests/test_1.rs",
2523 true,
2524 "project/tests/test_2.rs",
2525 true
2526 ),
2527 Ordering::Less
2528 );
2529 assert_eq!(
2530 compare(
2531 "project/v1.0.0/README.md",
2532 true,
2533 "project/v1.10.0/README.md",
2534 true
2535 ),
2536 Ordering::Less
2537 );
2538 }
2539
2540 #[perf]
2541 fn test_natural_sort_case_sensitivity() {
2542 std::thread::sleep(std::time::Duration::from_millis(100));
2543 // Same letter different case - lowercase should come first
2544 assert_eq!(natural_sort("a", "A"), Ordering::Less);
2545 assert_eq!(natural_sort("A", "a"), Ordering::Greater);
2546 assert_eq!(natural_sort("a", "a"), Ordering::Equal);
2547 assert_eq!(natural_sort("A", "A"), Ordering::Equal);
2548
2549 // Mixed case strings
2550 assert_eq!(natural_sort("aaa", "AAA"), Ordering::Less);
2551 assert_eq!(natural_sort("AAA", "aaa"), Ordering::Greater);
2552 assert_eq!(natural_sort("aAa", "AaA"), Ordering::Less);
2553
2554 // Different letters
2555 assert_eq!(natural_sort("a", "b"), Ordering::Less);
2556 assert_eq!(natural_sort("A", "b"), Ordering::Less);
2557 assert_eq!(natural_sort("a", "B"), Ordering::Less);
2558 }
2559
2560 #[perf]
2561 fn test_natural_sort_with_numbers() {
2562 // Basic number ordering
2563 assert_eq!(natural_sort("file1", "file2"), Ordering::Less);
2564 assert_eq!(natural_sort("file2", "file10"), Ordering::Less);
2565 assert_eq!(natural_sort("file10", "file2"), Ordering::Greater);
2566
2567 // Numbers in different positions
2568 assert_eq!(natural_sort("1file", "2file"), Ordering::Less);
2569 assert_eq!(natural_sort("file1text", "file2text"), Ordering::Less);
2570 assert_eq!(natural_sort("text1file", "text2file"), Ordering::Less);
2571
2572 // Multiple numbers in string
2573 assert_eq!(natural_sort("file1-2", "file1-10"), Ordering::Less);
2574 assert_eq!(natural_sort("2-1file", "10-1file"), Ordering::Less);
2575
2576 // Leading zeros
2577 assert_eq!(natural_sort("file002", "file2"), Ordering::Greater);
2578 assert_eq!(natural_sort("file002", "file10"), Ordering::Less);
2579
2580 // Very large numbers
2581 assert_eq!(
2582 natural_sort("file999999999999999999999", "file999999999999999999998"),
2583 Ordering::Greater
2584 );
2585
2586 // u128 edge cases
2587
2588 // Numbers near u128::MAX (340,282,366,920,938,463,463,374,607,431,768,211,455)
2589 assert_eq!(
2590 natural_sort(
2591 "file340282366920938463463374607431768211454",
2592 "file340282366920938463463374607431768211455"
2593 ),
2594 Ordering::Less
2595 );
2596
2597 // Equal length numbers that overflow u128
2598 assert_eq!(
2599 natural_sort(
2600 "file340282366920938463463374607431768211456",
2601 "file340282366920938463463374607431768211455"
2602 ),
2603 Ordering::Greater
2604 );
2605
2606 // Different length numbers that overflow u128
2607 assert_eq!(
2608 natural_sort(
2609 "file3402823669209384634633746074317682114560",
2610 "file340282366920938463463374607431768211455"
2611 ),
2612 Ordering::Greater
2613 );
2614
2615 // Leading zeros with numbers near u128::MAX
2616 assert_eq!(
2617 natural_sort(
2618 "file0340282366920938463463374607431768211455",
2619 "file340282366920938463463374607431768211455"
2620 ),
2621 Ordering::Greater
2622 );
2623
2624 // Very large numbers with different lengths (both overflow u128)
2625 assert_eq!(
2626 natural_sort(
2627 "file999999999999999999999999999999999999999999999999",
2628 "file9999999999999999999999999999999999999999999999999"
2629 ),
2630 Ordering::Less
2631 );
2632 }
2633
2634 #[perf]
2635 fn test_natural_sort_case_sensitive() {
2636 // Numerically smaller values come first.
2637 assert_eq!(natural_sort("File1", "file2"), Ordering::Less);
2638 assert_eq!(natural_sort("file1", "File2"), Ordering::Less);
2639
2640 // Numerically equal values: the case-insensitive comparison decides first.
2641 // Case-sensitive comparison only occurs when both are equal case-insensitively.
2642 assert_eq!(natural_sort("Dir1", "dir01"), Ordering::Less);
2643 assert_eq!(natural_sort("dir2", "Dir02"), Ordering::Less);
2644 assert_eq!(natural_sort("dir2", "dir02"), Ordering::Less);
2645
2646 // Numerically equal and case-insensitively equal:
2647 // the lexicographically smaller (case-sensitive) one wins.
2648 assert_eq!(natural_sort("dir1", "Dir1"), Ordering::Less);
2649 assert_eq!(natural_sort("dir02", "Dir02"), Ordering::Less);
2650 assert_eq!(natural_sort("dir10", "Dir10"), Ordering::Less);
2651 }
2652
2653 #[perf]
2654 fn test_natural_sort_edge_cases() {
2655 // Empty strings
2656 assert_eq!(natural_sort("", ""), Ordering::Equal);
2657 assert_eq!(natural_sort("", "a"), Ordering::Less);
2658 assert_eq!(natural_sort("a", ""), Ordering::Greater);
2659
2660 // Special characters
2661 assert_eq!(natural_sort("file-1", "file_1"), Ordering::Less);
2662 assert_eq!(natural_sort("file.1", "file_1"), Ordering::Less);
2663 assert_eq!(natural_sort("file 1", "file_1"), Ordering::Less);
2664
2665 // Unicode characters
2666 // 9312 vs 9313
2667 assert_eq!(natural_sort("file①", "file②"), Ordering::Less);
2668 // 9321 vs 9313
2669 assert_eq!(natural_sort("file⑩", "file②"), Ordering::Greater);
2670 // 28450 vs 23383
2671 assert_eq!(natural_sort("file漢", "file字"), Ordering::Greater);
2672
2673 // Mixed alphanumeric with special chars
2674 assert_eq!(natural_sort("file-1a", "file-1b"), Ordering::Less);
2675 assert_eq!(natural_sort("file-1.2", "file-1.10"), Ordering::Less);
2676 assert_eq!(natural_sort("file-1.10", "file-1.2"), Ordering::Greater);
2677 }
2678
2679 #[test]
2680 fn test_multiple_extensions() {
2681 // No extensions
2682 let path = Path::new("/a/b/c/file_name");
2683 assert_eq!(path.multiple_extensions(), None);
2684
2685 // Only one extension
2686 let path = Path::new("/a/b/c/file_name.tsx");
2687 assert_eq!(path.multiple_extensions(), None);
2688
2689 // Stories sample extension
2690 let path = Path::new("/a/b/c/file_name.stories.tsx");
2691 assert_eq!(path.multiple_extensions(), Some("stories.tsx".to_string()));
2692
2693 // Longer sample extension
2694 let path = Path::new("/a/b/c/long.app.tar.gz");
2695 assert_eq!(path.multiple_extensions(), Some("app.tar.gz".to_string()));
2696 }
2697
2698 #[test]
2699 fn test_strip_path_suffix() {
2700 let base = Path::new("/a/b/c/file_name");
2701 let suffix = Path::new("file_name");
2702 assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a/b/c")));
2703
2704 let base = Path::new("/a/b/c/file_name.tsx");
2705 let suffix = Path::new("file_name.tsx");
2706 assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a/b/c")));
2707
2708 let base = Path::new("/a/b/c/file_name.stories.tsx");
2709 let suffix = Path::new("c/file_name.stories.tsx");
2710 assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a/b")));
2711
2712 let base = Path::new("/a/b/c/long.app.tar.gz");
2713 let suffix = Path::new("b/c/long.app.tar.gz");
2714 assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("/a")));
2715
2716 let base = Path::new("/a/b/c/long.app.tar.gz");
2717 let suffix = Path::new("/a/b/c/long.app.tar.gz");
2718 assert_eq!(strip_path_suffix(base, suffix), Some(Path::new("")));
2719
2720 let base = Path::new("/a/b/c/long.app.tar.gz");
2721 let suffix = Path::new("/a/b/c/no_match.app.tar.gz");
2722 assert_eq!(strip_path_suffix(base, suffix), None);
2723
2724 let base = Path::new("/a/b/c/long.app.tar.gz");
2725 let suffix = Path::new("app.tar.gz");
2726 assert_eq!(strip_path_suffix(base, suffix), None);
2727 }
2728
2729 #[test]
2730 fn test_strip_prefix() {
2731 let expected = [
2732 (
2733 PathStyle::Posix,
2734 "/a/b/c",
2735 "/a/b",
2736 Some(rel_path("c").into_arc()),
2737 ),
2738 (
2739 PathStyle::Posix,
2740 "/a/b/c",
2741 "/a/b/",
2742 Some(rel_path("c").into_arc()),
2743 ),
2744 (
2745 PathStyle::Posix,
2746 "/a/b/c",
2747 "/",
2748 Some(rel_path("a/b/c").into_arc()),
2749 ),
2750 (PathStyle::Posix, "/a/b/c", "", None),
2751 (PathStyle::Posix, "/a/b//c", "/a/b/", None),
2752 (PathStyle::Posix, "/a/bc", "/a/b", None),
2753 (
2754 PathStyle::Posix,
2755 "/a/b/c",
2756 "/a/b/c",
2757 Some(rel_path("").into_arc()),
2758 ),
2759 (
2760 PathStyle::Windows,
2761 "C:\\a\\b\\c",
2762 "C:\\a\\b",
2763 Some(rel_path("c").into_arc()),
2764 ),
2765 (
2766 PathStyle::Windows,
2767 "C:\\a\\b\\c",
2768 "C:\\a\\b\\",
2769 Some(rel_path("c").into_arc()),
2770 ),
2771 (
2772 PathStyle::Windows,
2773 "C:\\a\\b\\c",
2774 "C:\\",
2775 Some(rel_path("a/b/c").into_arc()),
2776 ),
2777 (PathStyle::Windows, "C:\\a\\b\\c", "", None),
2778 (PathStyle::Windows, "C:\\a\\b\\\\c", "C:\\a\\b\\", None),
2779 (PathStyle::Windows, "C:\\a\\bc", "C:\\a\\b", None),
2780 (
2781 PathStyle::Windows,
2782 "C:\\a\\b/c",
2783 "C:\\a\\b",
2784 Some(rel_path("c").into_arc()),
2785 ),
2786 (
2787 PathStyle::Windows,
2788 "C:\\a\\b/c",
2789 "C:\\a\\b\\",
2790 Some(rel_path("c").into_arc()),
2791 ),
2792 (
2793 PathStyle::Windows,
2794 "C:\\a\\b/c",
2795 "C:\\a\\b/",
2796 Some(rel_path("c").into_arc()),
2797 ),
2798 ];
2799 let actual = expected.clone().map(|(style, child, parent, _)| {
2800 (
2801 style,
2802 child,
2803 parent,
2804 style
2805 .strip_prefix(child.as_ref(), parent.as_ref())
2806 .map(|rel_path| rel_path.into_arc()),
2807 )
2808 });
2809 pretty_assertions::assert_eq!(actual, expected);
2810 }
2811
2812 #[cfg(target_os = "windows")]
2813 #[test]
2814 fn test_wsl_path() {
2815 use super::WslPath;
2816 let path = "/a/b/c";
2817 assert_eq!(WslPath::from_path(&path), None);
2818
2819 let path = r"\\wsl.localhost";
2820 assert_eq!(WslPath::from_path(&path), None);
2821
2822 let path = r"\\wsl.localhost\Distro";
2823 assert_eq!(
2824 WslPath::from_path(&path),
2825 Some(WslPath {
2826 distro: "Distro".to_owned(),
2827 path: "/".into(),
2828 })
2829 );
2830
2831 let path = r"\\wsl.localhost\Distro\blue";
2832 assert_eq!(
2833 WslPath::from_path(&path),
2834 Some(WslPath {
2835 distro: "Distro".to_owned(),
2836 path: "/blue".into()
2837 })
2838 );
2839
2840 let path = r"\\wsl$\archlinux\tomato\.\paprika\..\aubergine.txt";
2841 assert_eq!(
2842 WslPath::from_path(&path),
2843 Some(WslPath {
2844 distro: "archlinux".to_owned(),
2845 path: "/tomato/paprika/../aubergine.txt".into()
2846 })
2847 );
2848
2849 let path = r"\\windows.localhost\Distro\foo";
2850 assert_eq!(WslPath::from_path(&path), None);
2851 }
2852
2853 #[test]
2854 fn test_url_to_file_path_ext_posix_basic() {
2855 use super::UrlExt;
2856
2857 let url = url::Url::parse("file:///home/user/file.txt").unwrap();
2858 assert_eq!(
2859 url.to_file_path_ext(PathStyle::Posix),
2860 Ok(PathBuf::from("/home/user/file.txt"))
2861 );
2862
2863 let url = url::Url::parse("file:///").unwrap();
2864 assert_eq!(
2865 url.to_file_path_ext(PathStyle::Posix),
2866 Ok(PathBuf::from("/"))
2867 );
2868
2869 let url = url::Url::parse("file:///a/b/c/d/e").unwrap();
2870 assert_eq!(
2871 url.to_file_path_ext(PathStyle::Posix),
2872 Ok(PathBuf::from("/a/b/c/d/e"))
2873 );
2874 }
2875
2876 #[test]
2877 fn test_url_to_file_path_ext_posix_percent_encoding() {
2878 use super::UrlExt;
2879
2880 let url = url::Url::parse("file:///home/user/file%20with%20spaces.txt").unwrap();
2881 assert_eq!(
2882 url.to_file_path_ext(PathStyle::Posix),
2883 Ok(PathBuf::from("/home/user/file with spaces.txt"))
2884 );
2885
2886 let url = url::Url::parse("file:///path%2Fwith%2Fencoded%2Fslashes").unwrap();
2887 assert_eq!(
2888 url.to_file_path_ext(PathStyle::Posix),
2889 Ok(PathBuf::from("/path/with/encoded/slashes"))
2890 );
2891
2892 let url = url::Url::parse("file:///special%23chars%3F.txt").unwrap();
2893 assert_eq!(
2894 url.to_file_path_ext(PathStyle::Posix),
2895 Ok(PathBuf::from("/special#chars?.txt"))
2896 );
2897 }
2898
2899 #[test]
2900 fn test_url_to_file_path_ext_posix_localhost() {
2901 use super::UrlExt;
2902
2903 let url = url::Url::parse("file://localhost/home/user/file.txt").unwrap();
2904 assert_eq!(
2905 url.to_file_path_ext(PathStyle::Posix),
2906 Ok(PathBuf::from("/home/user/file.txt"))
2907 );
2908 }
2909
2910 #[test]
2911 fn test_url_to_file_path_ext_posix_rejects_host() {
2912 use super::UrlExt;
2913
2914 let url = url::Url::parse("file://somehost/home/user/file.txt").unwrap();
2915 assert_eq!(url.to_file_path_ext(PathStyle::Posix), Err(()));
2916 }
2917
2918 #[test]
2919 fn test_url_to_file_path_ext_posix_windows_drive_letter() {
2920 use super::UrlExt;
2921
2922 let url = url::Url::parse("file:///C:").unwrap();
2923 assert_eq!(
2924 url.to_file_path_ext(PathStyle::Posix),
2925 Ok(PathBuf::from("/C:/"))
2926 );
2927
2928 let url = url::Url::parse("file:///D|").unwrap();
2929 assert_eq!(
2930 url.to_file_path_ext(PathStyle::Posix),
2931 Ok(PathBuf::from("/D|/"))
2932 );
2933 }
2934
2935 #[test]
2936 fn test_url_to_file_path_ext_windows_basic() {
2937 use super::UrlExt;
2938
2939 let url = url::Url::parse("file:///C:/Users/user/file.txt").unwrap();
2940 assert_eq!(
2941 url.to_file_path_ext(PathStyle::Windows),
2942 Ok(PathBuf::from("C:\\Users\\user\\file.txt"))
2943 );
2944
2945 let url = url::Url::parse("file:///D:/folder/subfolder/file.rs").unwrap();
2946 assert_eq!(
2947 url.to_file_path_ext(PathStyle::Windows),
2948 Ok(PathBuf::from("D:\\folder\\subfolder\\file.rs"))
2949 );
2950
2951 let url = url::Url::parse("file:///C:/").unwrap();
2952 assert_eq!(
2953 url.to_file_path_ext(PathStyle::Windows),
2954 Ok(PathBuf::from("C:\\"))
2955 );
2956 }
2957
2958 #[test]
2959 fn test_url_to_file_path_ext_windows_encoded_drive_letter() {
2960 use super::UrlExt;
2961
2962 let url = url::Url::parse("file:///C%3A/Users/file.txt").unwrap();
2963 assert_eq!(
2964 url.to_file_path_ext(PathStyle::Windows),
2965 Ok(PathBuf::from("C:\\Users\\file.txt"))
2966 );
2967
2968 let url = url::Url::parse("file:///c%3a/Users/file.txt").unwrap();
2969 assert_eq!(
2970 url.to_file_path_ext(PathStyle::Windows),
2971 Ok(PathBuf::from("c:\\Users\\file.txt"))
2972 );
2973
2974 let url = url::Url::parse("file:///D%3A/folder/file.txt").unwrap();
2975 assert_eq!(
2976 url.to_file_path_ext(PathStyle::Windows),
2977 Ok(PathBuf::from("D:\\folder\\file.txt"))
2978 );
2979
2980 let url = url::Url::parse("file:///d%3A/folder/file.txt").unwrap();
2981 assert_eq!(
2982 url.to_file_path_ext(PathStyle::Windows),
2983 Ok(PathBuf::from("d:\\folder\\file.txt"))
2984 );
2985 }
2986
2987 #[test]
2988 fn test_url_to_file_path_ext_windows_unc_path() {
2989 use super::UrlExt;
2990
2991 let url = url::Url::parse("file://server/share/path/file.txt").unwrap();
2992 assert_eq!(
2993 url.to_file_path_ext(PathStyle::Windows),
2994 Ok(PathBuf::from("\\\\server\\share\\path\\file.txt"))
2995 );
2996
2997 let url = url::Url::parse("file://server/share").unwrap();
2998 assert_eq!(
2999 url.to_file_path_ext(PathStyle::Windows),
3000 Ok(PathBuf::from("\\\\server\\share"))
3001 );
3002 }
3003
3004 #[test]
3005 fn test_url_to_file_path_ext_windows_percent_encoding() {
3006 use super::UrlExt;
3007
3008 let url = url::Url::parse("file:///C:/Users/user/file%20with%20spaces.txt").unwrap();
3009 assert_eq!(
3010 url.to_file_path_ext(PathStyle::Windows),
3011 Ok(PathBuf::from("C:\\Users\\user\\file with spaces.txt"))
3012 );
3013
3014 let url = url::Url::parse("file:///C:/special%23chars%3F.txt").unwrap();
3015 assert_eq!(
3016 url.to_file_path_ext(PathStyle::Windows),
3017 Ok(PathBuf::from("C:\\special#chars?.txt"))
3018 );
3019 }
3020
3021 #[test]
3022 fn test_url_to_file_path_ext_windows_invalid_drive() {
3023 use super::UrlExt;
3024
3025 let url = url::Url::parse("file:///1:/path/file.txt").unwrap();
3026 assert_eq!(url.to_file_path_ext(PathStyle::Windows), Err(()));
3027
3028 let url = url::Url::parse("file:///CC:/path/file.txt").unwrap();
3029 assert_eq!(url.to_file_path_ext(PathStyle::Windows), Err(()));
3030
3031 let url = url::Url::parse("file:///C/path/file.txt").unwrap();
3032 assert_eq!(url.to_file_path_ext(PathStyle::Windows), Err(()));
3033
3034 let url = url::Url::parse("file:///invalid").unwrap();
3035 assert_eq!(url.to_file_path_ext(PathStyle::Windows), Err(()));
3036 }
3037
3038 #[test]
3039 fn test_url_to_file_path_ext_non_file_scheme() {
3040 use super::UrlExt;
3041
3042 let url = url::Url::parse("http://example.com/path").unwrap();
3043 assert_eq!(url.to_file_path_ext(PathStyle::Posix), Err(()));
3044 assert_eq!(url.to_file_path_ext(PathStyle::Windows), Err(()));
3045
3046 let url = url::Url::parse("https://example.com/path").unwrap();
3047 assert_eq!(url.to_file_path_ext(PathStyle::Posix), Err(()));
3048 assert_eq!(url.to_file_path_ext(PathStyle::Windows), Err(()));
3049 }
3050
3051 #[test]
3052 fn test_url_to_file_path_ext_windows_localhost() {
3053 use super::UrlExt;
3054
3055 let url = url::Url::parse("file://localhost/C:/Users/file.txt").unwrap();
3056 assert_eq!(
3057 url.to_file_path_ext(PathStyle::Windows),
3058 Ok(PathBuf::from("C:\\Users\\file.txt"))
3059 );
3060 }
3061}