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 /// Parses a string that possibly has `:row:column` suffix.
108 /// Ignores trailing `:`s, so `test.rs:22:` is parsed as `test.rs:22`.
109 /// If any of the row/column component parsing fails, the whole string is then parsed as a path like.
110 /// If on Windows, `s` will replace `/` with `\` for compatibility.
111 pub fn parse_str<E>(
112 s: &str,
113 parse_path_like_str: impl Fn(&str, &str) -> Result<P, E>,
114 ) -> Result<Self, E> {
115 #[cfg(target_os = "windows")]
116 let s = &s.replace('/', "\\");
117
118 let fallback = |fallback_str| {
119 Ok(Self {
120 path_like: parse_path_like_str(s, fallback_str)?,
121 row: None,
122 column: None,
123 })
124 };
125
126 let trimmed = s.trim();
127
128 #[cfg(target_os = "windows")]
129 {
130 let is_absolute = trimmed.starts_with(r"\\?\");
131 if is_absolute {
132 return Self::parse_absolute_path(trimmed, |p| parse_path_like_str(s, p));
133 }
134 }
135
136 match trimmed.split_once(FILE_ROW_COLUMN_DELIMITER) {
137 Some((path_like_str, maybe_row_and_col_str)) => {
138 let path_like_str = path_like_str.trim();
139 let maybe_row_and_col_str = maybe_row_and_col_str.trim();
140 if path_like_str.is_empty() {
141 fallback(s)
142 } else if maybe_row_and_col_str.is_empty() {
143 fallback(path_like_str)
144 } else {
145 let (row_parse_result, maybe_col_str) =
146 match maybe_row_and_col_str.split_once(FILE_ROW_COLUMN_DELIMITER) {
147 Some((maybe_row_str, maybe_col_str)) => {
148 (maybe_row_str.parse::<u32>(), maybe_col_str.trim())
149 }
150 None => (maybe_row_and_col_str.parse::<u32>(), ""),
151 };
152
153 match row_parse_result {
154 Ok(row) => {
155 if maybe_col_str.is_empty() {
156 Ok(Self {
157 path_like: parse_path_like_str(s, path_like_str)?,
158 row: Some(row),
159 column: None,
160 })
161 } else {
162 let (maybe_col_str, _) =
163 maybe_col_str.split_once(':').unwrap_or((maybe_col_str, ""));
164 match maybe_col_str.parse::<u32>() {
165 Ok(col) => Ok(Self {
166 path_like: parse_path_like_str(s, path_like_str)?,
167 row: Some(row),
168 column: Some(col),
169 }),
170 Err(_) => Ok(Self {
171 path_like: parse_path_like_str(s, path_like_str)?,
172 row: Some(row),
173 column: None,
174 }),
175 }
176 }
177 }
178 Err(_) => Ok(Self {
179 path_like: parse_path_like_str(s, path_like_str)?,
180 row: None,
181 column: None,
182 }),
183 }
184 }
185 }
186 None => fallback(s),
187 }
188 }
189
190 /// 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.
191 #[cfg(target_os = "windows")]
192 fn parse_absolute_path<E>(
193 s: &str,
194 parse_path_like_str: impl Fn(&str) -> Result<P, E>,
195 ) -> Result<Self, E> {
196 let fallback = |fallback_str| {
197 Ok(Self {
198 path_like: parse_path_like_str(fallback_str)?,
199 row: None,
200 column: None,
201 })
202 };
203
204 let mut iterator = s.split(FILE_ROW_COLUMN_DELIMITER);
205
206 let drive_prefix = iterator.next().unwrap_or_default();
207 let file_path = iterator.next().unwrap_or_default();
208
209 // TODO: How to handle drives without a letter? UNC paths?
210 let complete_path = drive_prefix.replace("\\\\?\\", "") + ":" + &file_path;
211
212 if let Some(row_str) = iterator.next() {
213 if let Some(column_str) = iterator.next() {
214 match row_str.parse::<u32>() {
215 Ok(row) => match column_str.parse::<u32>() {
216 Ok(col) => {
217 return Ok(Self {
218 path_like: parse_path_like_str(&complete_path)?,
219 row: Some(row),
220 column: Some(col),
221 });
222 }
223
224 Err(_) => {
225 return Ok(Self {
226 path_like: parse_path_like_str(&complete_path)?,
227 row: Some(row),
228 column: None,
229 });
230 }
231 },
232
233 Err(_) => {
234 return fallback(&complete_path);
235 }
236 }
237 }
238 }
239 return fallback(&complete_path);
240 }
241
242 pub fn map_path_like<P2, E>(
243 self,
244 mapping: impl FnOnce(P) -> Result<P2, E>,
245 ) -> Result<PathLikeWithPosition<P2>, E> {
246 Ok(PathLikeWithPosition {
247 path_like: mapping(self.path_like)?,
248 row: self.row,
249 column: self.column,
250 })
251 }
252
253 pub fn to_string(&self, path_like_to_string: impl Fn(&P) -> String) -> String {
254 let path_like_string = path_like_to_string(&self.path_like);
255 if let Some(row) = self.row {
256 if let Some(column) = self.column {
257 format!("{path_like_string}:{row}:{column}")
258 } else {
259 format!("{path_like_string}:{row}")
260 }
261 } else {
262 path_like_string
263 }
264 }
265}
266
267#[derive(Clone, Debug, Default)]
268pub struct PathMatcher {
269 sources: Vec<String>,
270 glob: GlobSet,
271}
272
273// impl std::fmt::Display for PathMatcher {
274// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
275// self.sources.fmt(f)
276// }
277// }
278
279impl PartialEq for PathMatcher {
280 fn eq(&self, other: &Self) -> bool {
281 self.sources.eq(&other.sources)
282 }
283}
284
285impl Eq for PathMatcher {}
286
287impl PathMatcher {
288 pub fn new(globs: &[String]) -> Result<Self, globset::Error> {
289 let globs = globs
290 .into_iter()
291 .map(|glob| Glob::new(&glob))
292 .collect::<Result<Vec<_>, _>>()?;
293 let sources = globs.iter().map(|glob| glob.glob().to_owned()).collect();
294 let mut glob_builder = GlobSetBuilder::new();
295 for single_glob in globs {
296 glob_builder.add(single_glob);
297 }
298 let glob = glob_builder.build()?;
299 Ok(PathMatcher { glob, sources })
300 }
301
302 pub fn sources(&self) -> &[String] {
303 &self.sources
304 }
305
306 pub fn is_match<P: AsRef<Path>>(&self, other: P) -> bool {
307 let other_path = other.as_ref();
308 self.sources.iter().any(|source| {
309 let as_bytes = other_path.as_os_str().as_encoded_bytes();
310 as_bytes.starts_with(source.as_bytes()) || as_bytes.ends_with(source.as_bytes())
311 }) || self.glob.is_match(other_path)
312 || self.check_with_end_separator(other_path)
313 }
314
315 fn check_with_end_separator(&self, path: &Path) -> bool {
316 let path_str = path.to_string_lossy();
317 let separator = std::path::MAIN_SEPARATOR_STR;
318 if path_str.ends_with(separator) {
319 self.glob.is_match(path)
320 } else {
321 self.glob.is_match(path_str.to_string() + separator)
322 }
323 }
324}
325
326#[cfg(test)]
327mod tests {
328 use super::*;
329
330 type TestPath = PathLikeWithPosition<(String, String)>;
331
332 fn parse_str(s: &str) -> TestPath {
333 TestPath::parse_str(s, |normalized, s| {
334 Ok::<_, std::convert::Infallible>((normalized.to_string(), s.to_string()))
335 })
336 .expect("infallible")
337 }
338
339 #[test]
340 fn path_with_position_parsing_positive() {
341 let input_and_expected = [
342 (
343 "test_file.rs",
344 PathLikeWithPosition {
345 path_like: ("test_file.rs".to_string(), "test_file.rs".to_string()),
346 row: None,
347 column: None,
348 },
349 ),
350 (
351 "test_file.rs:1",
352 PathLikeWithPosition {
353 path_like: ("test_file.rs:1".to_string(), "test_file.rs".to_string()),
354 row: Some(1),
355 column: None,
356 },
357 ),
358 (
359 "test_file.rs:1:2",
360 PathLikeWithPosition {
361 path_like: ("test_file.rs:1:2".to_string(), "test_file.rs".to_string()),
362 row: Some(1),
363 column: Some(2),
364 },
365 ),
366 ];
367
368 for (input, expected) in input_and_expected {
369 let actual = parse_str(input);
370 assert_eq!(
371 actual, expected,
372 "For positive case input str '{input}', got a parse mismatch"
373 );
374 }
375 }
376
377 #[test]
378 fn path_with_position_parsing_negative() {
379 for (input, row, column) in [
380 ("test_file.rs:a", None, None),
381 ("test_file.rs:a:b", None, None),
382 ("test_file.rs::", None, None),
383 ("test_file.rs::1", None, None),
384 ("test_file.rs:1::", Some(1), None),
385 ("test_file.rs::1:2", None, None),
386 ("test_file.rs:1::2", Some(1), None),
387 ("test_file.rs:1:2:3", Some(1), Some(2)),
388 ] {
389 let actual = parse_str(input);
390 assert_eq!(
391 actual,
392 PathLikeWithPosition {
393 path_like: (input.to_string(), "test_file.rs".to_string()),
394 row,
395 column,
396 },
397 "For negative case input str '{input}', got a parse mismatch"
398 );
399 }
400 }
401
402 // Trim off trailing `:`s for otherwise valid input.
403 #[test]
404 fn path_with_position_parsing_special() {
405 #[cfg(not(target_os = "windows"))]
406 let input_and_expected = [
407 (
408 "test_file.rs:",
409 PathLikeWithPosition {
410 path_like: ("test_file.rs:".to_string(), "test_file.rs".to_string()),
411 row: None,
412 column: None,
413 },
414 ),
415 (
416 "test_file.rs:1:",
417 PathLikeWithPosition {
418 path_like: ("test_file.rs:1:".to_string(), "test_file.rs".to_string()),
419 row: Some(1),
420 column: None,
421 },
422 ),
423 (
424 "crates/file_finder/src/file_finder.rs:1902:13:",
425 PathLikeWithPosition {
426 path_like: (
427 "crates/file_finder/src/file_finder.rs:1902:13:".to_string(),
428 "crates/file_finder/src/file_finder.rs".to_string(),
429 ),
430 row: Some(1902),
431 column: Some(13),
432 },
433 ),
434 ];
435
436 #[cfg(target_os = "windows")]
437 let input_and_expected = [
438 (
439 "test_file.rs:",
440 PathLikeWithPosition {
441 path_like: ("test_file.rs:".to_string(), "test_file.rs".to_string()),
442 row: None,
443 column: None,
444 },
445 ),
446 (
447 "test_file.rs:1:",
448 PathLikeWithPosition {
449 path_like: ("test_file.rs:1:".to_string(), "test_file.rs".to_string()),
450 row: Some(1),
451 column: None,
452 },
453 ),
454 (
455 "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:",
456 PathLikeWithPosition {
457 path_like: (
458 "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:".to_string(),
459 "C:\\Users\\someone\\test_file.rs".to_string(),
460 ),
461 row: Some(1902),
462 column: Some(13),
463 },
464 ),
465 (
466 "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:",
467 PathLikeWithPosition {
468 path_like: (
469 "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:".to_string(),
470 "C:\\Users\\someone\\test_file.rs".to_string(),
471 ),
472 row: Some(1902),
473 column: Some(13),
474 },
475 ),
476 (
477 "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:",
478 PathLikeWithPosition {
479 path_like: (
480 "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:".to_string(),
481 "C:\\Users\\someone\\test_file.rs".to_string(),
482 ),
483 row: Some(1902),
484 column: None,
485 },
486 ),
487 (
488 "crates/utils/paths.rs",
489 PathLikeWithPosition {
490 path_like: (
491 "crates\\utils\\paths.rs".to_string(),
492 "crates\\utils\\paths.rs".to_string(),
493 ),
494 row: None,
495 column: None,
496 },
497 ),
498 (
499 "crates/utils/paths.rs:101",
500 PathLikeWithPosition {
501 path_like: (
502 "crates\\utils\\paths.rs:101".to_string(),
503 "crates\\utils\\paths.rs".to_string(),
504 ),
505 row: Some(101),
506 column: None,
507 },
508 ),
509 ];
510
511 for (input, expected) in input_and_expected {
512 let actual = parse_str(input);
513 assert_eq!(
514 actual, expected,
515 "For special case input str '{input}', got a parse mismatch"
516 );
517 }
518 }
519
520 #[test]
521 fn test_path_compact() {
522 let path: PathBuf = [
523 home_dir().to_string_lossy().to_string(),
524 "some_file.txt".to_string(),
525 ]
526 .iter()
527 .collect();
528 if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
529 assert_eq!(path.compact().to_str(), Some("~/some_file.txt"));
530 } else {
531 assert_eq!(path.compact().to_str(), path.to_str());
532 }
533 }
534
535 #[test]
536 fn test_icon_stem_or_suffix() {
537 // No dots in name
538 let path = Path::new("/a/b/c/file_name.rs");
539 assert_eq!(path.icon_stem_or_suffix(), Some("rs"));
540
541 // Single dot in name
542 let path = Path::new("/a/b/c/file.name.rs");
543 assert_eq!(path.icon_stem_or_suffix(), Some("rs"));
544
545 // No suffix
546 let path = Path::new("/a/b/c/file");
547 assert_eq!(path.icon_stem_or_suffix(), Some("file"));
548
549 // Multiple dots in name
550 let path = Path::new("/a/b/c/long.file.name.rs");
551 assert_eq!(path.icon_stem_or_suffix(), Some("rs"));
552
553 // Hidden file, no extension
554 let path = Path::new("/a/b/c/.gitignore");
555 assert_eq!(path.icon_stem_or_suffix(), Some("gitignore"));
556
557 // Hidden file, with extension
558 let path = Path::new("/a/b/c/.eslintrc.js");
559 assert_eq!(path.icon_stem_or_suffix(), Some("eslintrc.js"));
560 }
561
562 #[test]
563 fn test_extension_or_hidden_file_name() {
564 // No dots in name
565 let path = Path::new("/a/b/c/file_name.rs");
566 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
567
568 // Single dot in name
569 let path = Path::new("/a/b/c/file.name.rs");
570 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
571
572 // Multiple dots in name
573 let path = Path::new("/a/b/c/long.file.name.rs");
574 assert_eq!(path.extension_or_hidden_file_name(), Some("rs"));
575
576 // Hidden file, no extension
577 let path = Path::new("/a/b/c/.gitignore");
578 assert_eq!(path.extension_or_hidden_file_name(), Some("gitignore"));
579
580 // Hidden file, with extension
581 let path = Path::new("/a/b/c/.eslintrc.js");
582 assert_eq!(path.extension_or_hidden_file_name(), Some("js"));
583 }
584
585 #[test]
586 fn edge_of_glob() {
587 let path = Path::new("/work/node_modules");
588 let path_matcher = PathMatcher::new(&["**/node_modules/**".to_owned()]).unwrap();
589 assert!(
590 path_matcher.is_match(path),
591 "Path matcher should match {path:?}"
592 );
593 }
594
595 #[test]
596 fn project_search() {
597 let path = Path::new("/Users/someonetoignore/work/zed/zed.dev/node_modules");
598 let path_matcher = PathMatcher::new(&["**/node_modules/**".to_owned()]).unwrap();
599 assert!(
600 path_matcher.is_match(path),
601 "Path matcher should match {path:?}"
602 );
603 }
604}