1use std::sync::OnceLock;
2use std::{
3 ffi::OsStr,
4 path::{Path, PathBuf},
5};
6
7use globset::{Glob, GlobSet, GlobSetBuilder};
8use serde::{Deserialize, Serialize};
9
10/// Returns the path to the user's home directory.
11pub fn home_dir() -> &'static PathBuf {
12 static HOME_DIR: OnceLock<PathBuf> = OnceLock::new();
13 HOME_DIR.get_or_init(|| dirs::home_dir().expect("failed to determine home directory"))
14}
15
16pub trait PathExt {
17 fn compact(&self) -> PathBuf;
18 fn icon_stem_or_suffix(&self) -> Option<&str>;
19 fn extension_or_hidden_file_name(&self) -> Option<&str>;
20 fn try_from_bytes<'a>(bytes: &'a [u8]) -> anyhow::Result<Self>
21 where
22 Self: From<&'a Path>,
23 {
24 #[cfg(unix)]
25 {
26 use std::os::unix::prelude::OsStrExt;
27 Ok(Self::from(Path::new(OsStr::from_bytes(bytes))))
28 }
29 #[cfg(windows)]
30 {
31 use anyhow::anyhow;
32 use tendril::fmt::{Format, WTF8};
33 WTF8::validate(bytes)
34 .then(|| {
35 // Safety: bytes are valid WTF-8 sequence.
36 Self::from(Path::new(unsafe {
37 OsStr::from_encoded_bytes_unchecked(bytes)
38 }))
39 })
40 .ok_or_else(|| anyhow!("Invalid WTF-8 sequence: {bytes:?}"))
41 }
42 }
43}
44
45impl<T: AsRef<Path>> PathExt for T {
46 /// Compacts a given file path by replacing the user's home directory
47 /// prefix with a tilde (`~`).
48 ///
49 /// # Returns
50 ///
51 /// * A `PathBuf` containing the compacted file path. If the input path
52 /// does not have the user's home directory prefix, or if we are not on
53 /// Linux or macOS, the original path is returned unchanged.
54 fn compact(&self) -> PathBuf {
55 if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
56 match self.as_ref().strip_prefix(home_dir().as_path()) {
57 Ok(relative_path) => {
58 let mut shortened_path = PathBuf::new();
59 shortened_path.push("~");
60 shortened_path.push(relative_path);
61 shortened_path
62 }
63 Err(_) => self.as_ref().to_path_buf(),
64 }
65 } else {
66 self.as_ref().to_path_buf()
67 }
68 }
69
70 /// Returns either the suffix if available, or the file stem otherwise to determine which file icon to use
71 fn icon_stem_or_suffix(&self) -> Option<&str> {
72 let path = self.as_ref();
73 let file_name = path.file_name()?.to_str()?;
74 if file_name.starts_with('.') {
75 return file_name.strip_prefix('.');
76 }
77
78 path.extension()
79 .and_then(|e| e.to_str())
80 .or_else(|| path.file_stem()?.to_str())
81 }
82
83 /// Returns a file's extension or, if the file is hidden, its name without the leading dot
84 fn extension_or_hidden_file_name(&self) -> Option<&str> {
85 if let Some(extension) = self.as_ref().extension() {
86 return extension.to_str();
87 }
88
89 self.as_ref().file_name()?.to_str()?.split('.').last()
90 }
91}
92
93/// A delimiter to use in `path_query:row_number:column_number` strings parsing.
94pub const FILE_ROW_COLUMN_DELIMITER: char = ':';
95
96/// A representation of a path-like string with optional row and column numbers.
97/// Matching values example: `te`, `test.rs:22`, `te:22:5`, etc.
98#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
99pub struct PathLikeWithPosition<P> {
100 pub path_like: P,
101 pub row: Option<u32>,
102 // Absent if row is absent.
103 pub column: Option<u32>,
104}
105
106impl<P> PathLikeWithPosition<P> {
107 /// Returns a PathLikeWithPosition from a path.
108 pub fn from_path(path: P) -> Self {
109 Self {
110 path_like: path,
111 row: None,
112 column: None,
113 }
114 }
115 /// Parses a string that possibly has `:row:column` suffix.
116 /// Ignores trailing `:`s, so `test.rs:22:` is parsed as `test.rs:22`.
117 /// If any of the row/column component parsing fails, the whole string is then parsed as a path like.
118 /// If on Windows, `s` will replace `/` with `\` for compatibility.
119 pub fn parse_str<E>(
120 s: &str,
121 parse_path_like_str: impl Fn(&str, &str) -> Result<P, E>,
122 ) -> Result<Self, E> {
123 #[cfg(target_os = "windows")]
124 let s = &s.replace('/', "\\");
125
126 let fallback = |fallback_str| {
127 Ok(Self {
128 path_like: parse_path_like_str(s, fallback_str)?,
129 row: None,
130 column: None,
131 })
132 };
133
134 let trimmed = s.trim();
135
136 #[cfg(target_os = "windows")]
137 {
138 let is_absolute = trimmed.starts_with(r"\\?\");
139 if is_absolute {
140 return Self::parse_absolute_path(trimmed, |p| parse_path_like_str(s, p));
141 }
142 }
143
144 match trimmed.split_once(FILE_ROW_COLUMN_DELIMITER) {
145 Some((path_like_str, maybe_row_and_col_str)) => {
146 let path_like_str = path_like_str.trim();
147 let maybe_row_and_col_str = maybe_row_and_col_str.trim();
148 if path_like_str.is_empty() {
149 fallback(s)
150 } else if maybe_row_and_col_str.is_empty() {
151 fallback(path_like_str)
152 } else {
153 let (row_parse_result, maybe_col_str) =
154 match maybe_row_and_col_str.split_once(FILE_ROW_COLUMN_DELIMITER) {
155 Some((maybe_row_str, maybe_col_str)) => {
156 (maybe_row_str.parse::<u32>(), maybe_col_str.trim())
157 }
158 None => (maybe_row_and_col_str.parse::<u32>(), ""),
159 };
160
161 match row_parse_result {
162 Ok(row) => {
163 if maybe_col_str.is_empty() {
164 Ok(Self {
165 path_like: parse_path_like_str(s, path_like_str)?,
166 row: Some(row),
167 column: None,
168 })
169 } else {
170 let (maybe_col_str, _) =
171 maybe_col_str.split_once(':').unwrap_or((maybe_col_str, ""));
172 match maybe_col_str.parse::<u32>() {
173 Ok(col) => Ok(Self {
174 path_like: parse_path_like_str(s, path_like_str)?,
175 row: Some(row),
176 column: Some(col),
177 }),
178 Err(_) => Ok(Self {
179 path_like: parse_path_like_str(s, path_like_str)?,
180 row: Some(row),
181 column: None,
182 }),
183 }
184 }
185 }
186 Err(_) => Ok(Self {
187 path_like: parse_path_like_str(s, path_like_str)?,
188 row: None,
189 column: None,
190 }),
191 }
192 }
193 }
194 None => fallback(s),
195 }
196 }
197
198 /// This helper function is used for parsing absolute paths on Windows. It exists because absolute paths on Windows are quite different from other platforms. See [this page](https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#dos-device-paths) for more information.
199 #[cfg(target_os = "windows")]
200 fn parse_absolute_path<E>(
201 s: &str,
202 parse_path_like_str: impl Fn(&str) -> Result<P, E>,
203 ) -> Result<Self, E> {
204 let fallback = |fallback_str| {
205 Ok(Self {
206 path_like: parse_path_like_str(fallback_str)?,
207 row: None,
208 column: None,
209 })
210 };
211
212 let mut iterator = s.split(FILE_ROW_COLUMN_DELIMITER);
213
214 let drive_prefix = iterator.next().unwrap_or_default();
215 let file_path = iterator.next().unwrap_or_default();
216
217 // TODO: How to handle drives without a letter? UNC paths?
218 let complete_path = drive_prefix.replace("\\\\?\\", "") + ":" + &file_path;
219
220 if let Some(row_str) = iterator.next() {
221 if let Some(column_str) = iterator.next() {
222 match row_str.parse::<u32>() {
223 Ok(row) => match column_str.parse::<u32>() {
224 Ok(col) => {
225 return Ok(Self {
226 path_like: parse_path_like_str(&complete_path)?,
227 row: Some(row),
228 column: Some(col),
229 });
230 }
231
232 Err(_) => {
233 return Ok(Self {
234 path_like: parse_path_like_str(&complete_path)?,
235 row: Some(row),
236 column: None,
237 });
238 }
239 },
240
241 Err(_) => {
242 return fallback(&complete_path);
243 }
244 }
245 }
246 }
247 return fallback(&complete_path);
248 }
249
250 pub fn map_path_like<P2, E>(
251 self,
252 mapping: impl FnOnce(P) -> Result<P2, E>,
253 ) -> Result<PathLikeWithPosition<P2>, E> {
254 Ok(PathLikeWithPosition {
255 path_like: mapping(self.path_like)?,
256 row: self.row,
257 column: self.column,
258 })
259 }
260
261 pub fn to_string(&self, path_like_to_string: impl Fn(&P) -> String) -> String {
262 let path_like_string = path_like_to_string(&self.path_like);
263 if let Some(row) = self.row {
264 if let Some(column) = self.column {
265 format!("{path_like_string}:{row}:{column}")
266 } else {
267 format!("{path_like_string}:{row}")
268 }
269 } else {
270 path_like_string
271 }
272 }
273}
274
275#[derive(Clone, Debug, Default)]
276pub struct PathMatcher {
277 sources: Vec<String>,
278 glob: GlobSet,
279}
280
281// impl std::fmt::Display for PathMatcher {
282// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
283// self.sources.fmt(f)
284// }
285// }
286
287impl PartialEq for PathMatcher {
288 fn eq(&self, other: &Self) -> bool {
289 self.sources.eq(&other.sources)
290 }
291}
292
293impl Eq for PathMatcher {}
294
295impl PathMatcher {
296 pub fn new(globs: &[String]) -> Result<Self, globset::Error> {
297 let globs = globs
298 .into_iter()
299 .map(|glob| Glob::new(&glob))
300 .collect::<Result<Vec<_>, _>>()?;
301 let sources = globs.iter().map(|glob| glob.glob().to_owned()).collect();
302 let mut glob_builder = GlobSetBuilder::new();
303 for single_glob in globs {
304 glob_builder.add(single_glob);
305 }
306 let glob = glob_builder.build()?;
307 Ok(PathMatcher { glob, sources })
308 }
309
310 pub fn sources(&self) -> &[String] {
311 &self.sources
312 }
313
314 pub fn is_match<P: AsRef<Path>>(&self, other: P) -> bool {
315 let other_path = other.as_ref();
316 self.sources.iter().any(|source| {
317 let as_bytes = other_path.as_os_str().as_encoded_bytes();
318 as_bytes.starts_with(source.as_bytes()) || as_bytes.ends_with(source.as_bytes())
319 }) || self.glob.is_match(other_path)
320 || self.check_with_end_separator(other_path)
321 }
322
323 fn check_with_end_separator(&self, path: &Path) -> bool {
324 let path_str = path.to_string_lossy();
325 let separator = std::path::MAIN_SEPARATOR_STR;
326 if path_str.ends_with(separator) {
327 self.glob.is_match(path)
328 } else {
329 self.glob.is_match(path_str.to_string() + separator)
330 }
331 }
332}
333
334#[cfg(test)]
335mod tests {
336 use super::*;
337
338 type TestPath = PathLikeWithPosition<(String, String)>;
339
340 fn parse_str(s: &str) -> TestPath {
341 TestPath::parse_str(s, |normalized, s| {
342 Ok::<_, std::convert::Infallible>((normalized.to_string(), s.to_string()))
343 })
344 .expect("infallible")
345 }
346
347 #[test]
348 fn path_with_position_parsing_positive() {
349 let input_and_expected = [
350 (
351 "test_file.rs",
352 PathLikeWithPosition {
353 path_like: ("test_file.rs".to_string(), "test_file.rs".to_string()),
354 row: None,
355 column: None,
356 },
357 ),
358 (
359 "test_file.rs:1",
360 PathLikeWithPosition {
361 path_like: ("test_file.rs:1".to_string(), "test_file.rs".to_string()),
362 row: Some(1),
363 column: None,
364 },
365 ),
366 (
367 "test_file.rs:1:2",
368 PathLikeWithPosition {
369 path_like: ("test_file.rs:1:2".to_string(), "test_file.rs".to_string()),
370 row: Some(1),
371 column: Some(2),
372 },
373 ),
374 ];
375
376 for (input, expected) in input_and_expected {
377 let actual = parse_str(input);
378 assert_eq!(
379 actual, expected,
380 "For positive case input str '{input}', got a parse mismatch"
381 );
382 }
383 }
384
385 #[test]
386 fn path_with_position_parsing_negative() {
387 for (input, row, column) in [
388 ("test_file.rs:a", None, None),
389 ("test_file.rs:a:b", None, None),
390 ("test_file.rs::", None, None),
391 ("test_file.rs::1", None, None),
392 ("test_file.rs:1::", Some(1), None),
393 ("test_file.rs::1:2", None, None),
394 ("test_file.rs:1::2", Some(1), None),
395 ("test_file.rs:1:2:3", Some(1), Some(2)),
396 ] {
397 let actual = parse_str(input);
398 assert_eq!(
399 actual,
400 PathLikeWithPosition {
401 path_like: (input.to_string(), "test_file.rs".to_string()),
402 row,
403 column,
404 },
405 "For negative case input str '{input}', got a parse mismatch"
406 );
407 }
408 }
409
410 // Trim off trailing `:`s for otherwise valid input.
411 #[test]
412 fn path_with_position_parsing_special() {
413 #[cfg(not(target_os = "windows"))]
414 let input_and_expected = [
415 (
416 "test_file.rs:",
417 PathLikeWithPosition {
418 path_like: ("test_file.rs:".to_string(), "test_file.rs".to_string()),
419 row: None,
420 column: None,
421 },
422 ),
423 (
424 "test_file.rs:1:",
425 PathLikeWithPosition {
426 path_like: ("test_file.rs:1:".to_string(), "test_file.rs".to_string()),
427 row: Some(1),
428 column: None,
429 },
430 ),
431 (
432 "crates/file_finder/src/file_finder.rs:1902:13:",
433 PathLikeWithPosition {
434 path_like: (
435 "crates/file_finder/src/file_finder.rs:1902:13:".to_string(),
436 "crates/file_finder/src/file_finder.rs".to_string(),
437 ),
438 row: Some(1902),
439 column: Some(13),
440 },
441 ),
442 ];
443
444 #[cfg(target_os = "windows")]
445 let input_and_expected = [
446 (
447 "test_file.rs:",
448 PathLikeWithPosition {
449 path_like: ("test_file.rs:".to_string(), "test_file.rs".to_string()),
450 row: None,
451 column: None,
452 },
453 ),
454 (
455 "test_file.rs:1:",
456 PathLikeWithPosition {
457 path_like: ("test_file.rs:1:".to_string(), "test_file.rs".to_string()),
458 row: Some(1),
459 column: None,
460 },
461 ),
462 (
463 "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:",
464 PathLikeWithPosition {
465 path_like: (
466 "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:".to_string(),
467 "C:\\Users\\someone\\test_file.rs".to_string(),
468 ),
469 row: Some(1902),
470 column: Some(13),
471 },
472 ),
473 (
474 "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:",
475 PathLikeWithPosition {
476 path_like: (
477 "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:".to_string(),
478 "C:\\Users\\someone\\test_file.rs".to_string(),
479 ),
480 row: Some(1902),
481 column: Some(13),
482 },
483 ),
484 (
485 "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:",
486 PathLikeWithPosition {
487 path_like: (
488 "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:".to_string(),
489 "C:\\Users\\someone\\test_file.rs".to_string(),
490 ),
491 row: Some(1902),
492 column: None,
493 },
494 ),
495 (
496 "crates/utils/paths.rs",
497 PathLikeWithPosition {
498 path_like: (
499 "crates\\utils\\paths.rs".to_string(),
500 "crates\\utils\\paths.rs".to_string(),
501 ),
502 row: None,
503 column: None,
504 },
505 ),
506 (
507 "crates/utils/paths.rs:101",
508 PathLikeWithPosition {
509 path_like: (
510 "crates\\utils\\paths.rs:101".to_string(),
511 "crates\\utils\\paths.rs".to_string(),
512 ),
513 row: Some(101),
514 column: None,
515 },
516 ),
517 ];
518
519 for (input, expected) in input_and_expected {
520 let actual = parse_str(input);
521 assert_eq!(
522 actual, expected,
523 "For special case input str '{input}', got a parse mismatch"
524 );
525 }
526 }
527
528 #[test]
529 fn test_path_compact() {
530 let path: PathBuf = [
531 home_dir().to_string_lossy().to_string(),
532 "some_file.txt".to_string(),
533 ]
534 .iter()
535 .collect();
536 if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
537 assert_eq!(path.compact().to_str(), Some("~/some_file.txt"));
538 } else {
539 assert_eq!(path.compact().to_str(), path.to_str());
540 }
541 }
542
543 #[test]
544 fn test_icon_stem_or_suffix() {
545 // No dots in name
546 let path = Path::new("/a/b/c/file_name.rs");
547 assert_eq!(path.icon_stem_or_suffix(), Some("rs"));
548
549 // Single dot in name
550 let path = Path::new("/a/b/c/file.name.rs");
551 assert_eq!(path.icon_stem_or_suffix(), Some("rs"));
552
553 // No suffix
554 let path = Path::new("/a/b/c/file");
555 assert_eq!(path.icon_stem_or_suffix(), Some("file"));
556
557 // Multiple dots in name
558 let path = Path::new("/a/b/c/long.file.name.rs");
559 assert_eq!(path.icon_stem_or_suffix(), Some("rs"));
560
561 // Hidden file, no extension
562 let path = Path::new("/a/b/c/.gitignore");
563 assert_eq!(path.icon_stem_or_suffix(), Some("gitignore"));
564
565 // Hidden file, with extension
566 let path = Path::new("/a/b/c/.eslintrc.js");
567 assert_eq!(path.icon_stem_or_suffix(), Some("eslintrc.js"));
568 }
569
570 #[test]
571 fn test_extension_or_hidden_file_name() {
572 // No dots in name
573 let path = Path::new("/a/b/c/file_name.rs");
574 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
575
576 // Single dot in name
577 let path = Path::new("/a/b/c/file.name.rs");
578 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
579
580 // Multiple dots in name
581 let path = Path::new("/a/b/c/long.file.name.rs");
582 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
583
584 // Hidden file, no extension
585 let path = Path::new("/a/b/c/.gitignore");
586 assert_eq!(path.extension_or_hidden_file_name(), Some("gitignore"));
587
588 // Hidden file, with extension
589 let path = Path::new("/a/b/c/.eslintrc.js");
590 assert_eq!(path.extension_or_hidden_file_name(), Some("js"));
591 }
592
593 #[test]
594 fn edge_of_glob() {
595 let path = Path::new("/work/node_modules");
596 let path_matcher = PathMatcher::new(&["**/node_modules/**".to_owned()]).unwrap();
597 assert!(
598 path_matcher.is_match(path),
599 "Path matcher should match {path:?}"
600 );
601 }
602
603 #[test]
604 fn project_search() {
605 let path = Path::new("/Users/someonetoignore/work/zed/zed.dev/node_modules");
606 let path_matcher = PathMatcher::new(&["**/node_modules/**".to_owned()]).unwrap();
607 assert!(
608 path_matcher.is_match(path),
609 "Path matcher should match {path:?}"
610 );
611 }
612}