1use super::{diagnostics_command::write_single_file_diagnostics, SlashCommand, SlashCommandOutput};
2use anyhow::{anyhow, Context as _, Result};
3use assistant_slash_command::{AfterCompletion, ArgumentCompletion, SlashCommandOutputSection};
4use fuzzy::PathMatch;
5use gpui::{AppContext, Model, Task, View, WeakView};
6use language::{BufferSnapshot, CodeLabel, HighlightId, LineEnding, LspAdapterDelegate};
7use project::{PathMatchCandidateSet, Project};
8use std::{
9 fmt::Write,
10 ops::Range,
11 path::{Path, PathBuf},
12 sync::{atomic::AtomicBool, Arc},
13};
14use ui::prelude::*;
15use util::ResultExt;
16use workspace::Workspace;
17
18pub(crate) struct FileSlashCommand;
19
20impl FileSlashCommand {
21 fn search_paths(
22 &self,
23 query: String,
24 cancellation_flag: Arc<AtomicBool>,
25 workspace: &View<Workspace>,
26 cx: &mut AppContext,
27 ) -> Task<Vec<PathMatch>> {
28 if query.is_empty() {
29 let workspace = workspace.read(cx);
30 let project = workspace.project().read(cx);
31 let entries = workspace.recent_navigation_history(Some(10), cx);
32
33 let entries = entries
34 .into_iter()
35 .map(|entries| (entries.0, false))
36 .chain(project.worktrees(cx).flat_map(|worktree| {
37 let worktree = worktree.read(cx);
38 let id = worktree.id();
39 worktree.child_entries(Path::new("")).map(move |entry| {
40 (
41 project::ProjectPath {
42 worktree_id: id,
43 path: entry.path.clone(),
44 },
45 entry.kind.is_dir(),
46 )
47 })
48 }))
49 .collect::<Vec<_>>();
50
51 let path_prefix: Arc<str> = Arc::default();
52 Task::ready(
53 entries
54 .into_iter()
55 .filter_map(|(entry, is_dir)| {
56 let worktree = project.worktree_for_id(entry.worktree_id, cx)?;
57 let mut full_path = PathBuf::from(worktree.read(cx).root_name());
58 full_path.push(&entry.path);
59 Some(PathMatch {
60 score: 0.,
61 positions: Vec::new(),
62 worktree_id: entry.worktree_id.to_usize(),
63 path: full_path.into(),
64 path_prefix: path_prefix.clone(),
65 distance_to_relative_ancestor: 0,
66 is_dir,
67 })
68 })
69 .collect(),
70 )
71 } else {
72 let worktrees = workspace.read(cx).visible_worktrees(cx).collect::<Vec<_>>();
73 let candidate_sets = worktrees
74 .into_iter()
75 .map(|worktree| {
76 let worktree = worktree.read(cx);
77
78 PathMatchCandidateSet {
79 snapshot: worktree.snapshot(),
80 include_ignored: worktree
81 .root_entry()
82 .map_or(false, |entry| entry.is_ignored),
83 include_root_name: true,
84 candidates: project::Candidates::Entries,
85 }
86 })
87 .collect::<Vec<_>>();
88
89 let executor = cx.background_executor().clone();
90 cx.foreground_executor().spawn(async move {
91 fuzzy::match_path_sets(
92 candidate_sets.as_slice(),
93 query.as_str(),
94 None,
95 false,
96 100,
97 &cancellation_flag,
98 executor,
99 )
100 .await
101 })
102 }
103 }
104}
105
106impl SlashCommand for FileSlashCommand {
107 fn name(&self) -> String {
108 "file".into()
109 }
110
111 fn description(&self) -> String {
112 "insert file".into()
113 }
114
115 fn menu_text(&self) -> String {
116 "Insert File".into()
117 }
118
119 fn requires_argument(&self) -> bool {
120 true
121 }
122
123 fn complete_argument(
124 self: Arc<Self>,
125 arguments: &[String],
126 cancellation_flag: Arc<AtomicBool>,
127 workspace: Option<WeakView<Workspace>>,
128 cx: &mut WindowContext,
129 ) -> Task<Result<Vec<ArgumentCompletion>>> {
130 let Some(workspace) = workspace.and_then(|workspace| workspace.upgrade()) else {
131 return Task::ready(Err(anyhow!("workspace was dropped")));
132 };
133
134 let paths = self.search_paths(
135 arguments.last().cloned().unwrap_or_default(),
136 cancellation_flag,
137 &workspace,
138 cx,
139 );
140 let comment_id = cx.theme().syntax().highlight_id("comment").map(HighlightId);
141 cx.background_executor().spawn(async move {
142 Ok(paths
143 .await
144 .into_iter()
145 .filter_map(|path_match| {
146 let text = format!(
147 "{}{}",
148 path_match.path_prefix,
149 path_match.path.to_string_lossy()
150 );
151
152 let mut label = CodeLabel::default();
153 let file_name = path_match.path.file_name()?.to_string_lossy();
154 let label_text = if path_match.is_dir {
155 format!("{}/ ", file_name)
156 } else {
157 format!("{} ", file_name)
158 };
159
160 label.push_str(label_text.as_str(), None);
161 label.push_str(&text, comment_id);
162 label.filter_range = 0..file_name.len();
163
164 Some(ArgumentCompletion {
165 label,
166 new_text: text,
167 after_completion: AfterCompletion::Compose,
168 replace_previous_arguments: false,
169 })
170 })
171 .collect())
172 })
173 }
174
175 fn run(
176 self: Arc<Self>,
177 arguments: &[String],
178 workspace: WeakView<Workspace>,
179 _delegate: Option<Arc<dyn LspAdapterDelegate>>,
180 cx: &mut WindowContext,
181 ) -> Task<Result<SlashCommandOutput>> {
182 let Some(workspace) = workspace.upgrade() else {
183 return Task::ready(Err(anyhow!("workspace was dropped")));
184 };
185
186 if arguments.is_empty() {
187 return Task::ready(Err(anyhow!("missing path")));
188 };
189
190 let task = collect_files(workspace.read(cx).project().clone(), arguments, cx);
191
192 cx.foreground_executor().spawn(async move {
193 let output = task.await?;
194 Ok(SlashCommandOutput {
195 text: output.completion_text,
196 sections: output
197 .files
198 .into_iter()
199 .map(|file| {
200 build_entry_output_section(
201 file.range_in_text,
202 Some(&file.path),
203 file.entry_type == EntryType::Directory,
204 None,
205 )
206 })
207 .collect(),
208 run_commands_in_text: true,
209 })
210 })
211 }
212}
213
214#[derive(Clone, Copy, PartialEq, Debug)]
215enum EntryType {
216 File,
217 Directory,
218}
219
220#[derive(Clone, PartialEq, Debug)]
221struct FileCommandOutput {
222 completion_text: String,
223 files: Vec<OutputFile>,
224}
225
226#[derive(Clone, PartialEq, Debug)]
227struct OutputFile {
228 range_in_text: Range<usize>,
229 path: PathBuf,
230 entry_type: EntryType,
231}
232
233fn collect_files(
234 project: Model<Project>,
235 glob_inputs: &[String],
236 cx: &mut AppContext,
237) -> Task<Result<FileCommandOutput>> {
238 let Ok(matchers) = glob_inputs
239 .into_iter()
240 .map(|glob_input| {
241 custom_path_matcher::PathMatcher::new(&[glob_input.to_owned()])
242 .with_context(|| format!("invalid path {glob_input}"))
243 })
244 .collect::<anyhow::Result<Vec<custom_path_matcher::PathMatcher>>>()
245 else {
246 return Task::ready(Err(anyhow!("invalid path")));
247 };
248
249 let project_handle = project.downgrade();
250 let snapshots = project
251 .read(cx)
252 .worktrees(cx)
253 .map(|worktree| worktree.read(cx).snapshot())
254 .collect::<Vec<_>>();
255
256 cx.spawn(|mut cx| async move {
257 let mut text = String::new();
258 let mut ranges = Vec::new();
259 for snapshot in snapshots {
260 let worktree_id = snapshot.id();
261 let mut directory_stack: Vec<(Arc<Path>, String, usize)> = Vec::new();
262 let mut folded_directory_names_stack = Vec::new();
263 let mut is_top_level_directory = true;
264
265 for entry in snapshot.entries(false, 0) {
266 let mut path_including_worktree_name = PathBuf::new();
267 path_including_worktree_name.push(snapshot.root_name());
268 path_including_worktree_name.push(&entry.path);
269
270 if !matchers
271 .iter()
272 .any(|matcher| matcher.is_match(&path_including_worktree_name))
273 {
274 continue;
275 }
276
277 while let Some((dir, _, _)) = directory_stack.last() {
278 if entry.path.starts_with(dir) {
279 break;
280 }
281 let (_, entry_name, start) = directory_stack.pop().unwrap();
282 ranges.push(OutputFile {
283 range_in_text: start..text.len().saturating_sub(1),
284 path: PathBuf::from(entry_name),
285 entry_type: EntryType::Directory,
286 });
287 }
288
289 let filename = entry
290 .path
291 .file_name()
292 .unwrap_or_default()
293 .to_str()
294 .unwrap_or_default()
295 .to_string();
296
297 if entry.is_dir() {
298 // Auto-fold directories that contain no files
299 let mut child_entries = snapshot.child_entries(&entry.path);
300 if let Some(child) = child_entries.next() {
301 if child_entries.next().is_none() && child.kind.is_dir() {
302 if is_top_level_directory {
303 is_top_level_directory = false;
304 folded_directory_names_stack.push(
305 path_including_worktree_name.to_string_lossy().to_string(),
306 );
307 } else {
308 folded_directory_names_stack.push(filename.to_string());
309 }
310 continue;
311 }
312 } else {
313 // Skip empty directories
314 folded_directory_names_stack.clear();
315 continue;
316 }
317 let prefix_paths = folded_directory_names_stack.drain(..).as_slice().join("/");
318 let entry_start = text.len();
319 if prefix_paths.is_empty() {
320 if is_top_level_directory {
321 text.push_str(&path_including_worktree_name.to_string_lossy());
322 is_top_level_directory = false;
323 } else {
324 text.push_str(&filename);
325 }
326 directory_stack.push((entry.path.clone(), filename, entry_start));
327 } else {
328 let entry_name = format!("{}/{}", prefix_paths, &filename);
329 text.push_str(&entry_name);
330 directory_stack.push((entry.path.clone(), entry_name, entry_start));
331 }
332 text.push('\n');
333 } else if entry.is_file() {
334 let Some(open_buffer_task) = project_handle
335 .update(&mut cx, |project, cx| {
336 project.open_buffer((worktree_id, &entry.path), cx)
337 })
338 .ok()
339 else {
340 continue;
341 };
342 if let Some(buffer) = open_buffer_task.await.log_err() {
343 let buffer_snapshot =
344 cx.read_model(&buffer, |buffer, _| buffer.snapshot())?;
345 let prev_len = text.len();
346 collect_file_content(
347 &mut text,
348 &buffer_snapshot,
349 path_including_worktree_name.to_string_lossy().to_string(),
350 );
351 text.push('\n');
352 if !write_single_file_diagnostics(
353 &mut text,
354 Some(&path_including_worktree_name),
355 &buffer_snapshot,
356 ) {
357 text.pop();
358 }
359 ranges.push(OutputFile {
360 range_in_text: prev_len..text.len(),
361 path: path_including_worktree_name,
362 entry_type: EntryType::File,
363 });
364 text.push('\n');
365 }
366 }
367 }
368
369 while let Some((dir, entry, start)) = directory_stack.pop() {
370 if directory_stack.is_empty() {
371 let mut root_path = PathBuf::new();
372 root_path.push(snapshot.root_name());
373 root_path.push(&dir);
374 ranges.push(OutputFile {
375 range_in_text: start..text.len(),
376 path: root_path,
377 entry_type: EntryType::Directory,
378 });
379 } else {
380 ranges.push(OutputFile {
381 range_in_text: start..text.len(),
382 path: PathBuf::from(entry.as_str()),
383 entry_type: EntryType::Directory,
384 });
385 }
386 }
387 }
388 Ok(FileCommandOutput {
389 completion_text: text,
390 files: ranges,
391 })
392 })
393}
394
395fn collect_file_content(buffer: &mut String, snapshot: &BufferSnapshot, filename: String) {
396 let mut content = snapshot.text();
397 LineEnding::normalize(&mut content);
398 buffer.reserve(filename.len() + content.len() + 9);
399 buffer.push_str(&codeblock_fence_for_path(
400 Some(&PathBuf::from(filename)),
401 None,
402 ));
403 buffer.push_str(&content);
404 if !buffer.ends_with('\n') {
405 buffer.push('\n');
406 }
407 buffer.push_str("```");
408}
409
410pub fn codeblock_fence_for_path(path: Option<&Path>, row_range: Option<Range<u32>>) -> String {
411 let mut text = String::new();
412 write!(text, "```").unwrap();
413
414 if let Some(path) = path {
415 if let Some(extension) = path.extension().and_then(|ext| ext.to_str()) {
416 write!(text, "{} ", extension).unwrap();
417 }
418
419 write!(text, "{}", path.display()).unwrap();
420 } else {
421 write!(text, "untitled").unwrap();
422 }
423
424 if let Some(row_range) = row_range {
425 write!(text, ":{}-{}", row_range.start + 1, row_range.end + 1).unwrap();
426 }
427
428 text.push('\n');
429 text
430}
431
432pub fn build_entry_output_section(
433 range: Range<usize>,
434 path: Option<&Path>,
435 is_directory: bool,
436 line_range: Option<Range<u32>>,
437) -> SlashCommandOutputSection<usize> {
438 let mut label = if let Some(path) = path {
439 path.to_string_lossy().to_string()
440 } else {
441 "untitled".to_string()
442 };
443 if let Some(line_range) = line_range {
444 write!(label, ":{}-{}", line_range.start, line_range.end).unwrap();
445 }
446
447 let icon = if is_directory {
448 IconName::Folder
449 } else {
450 IconName::File
451 };
452
453 SlashCommandOutputSection {
454 range,
455 icon,
456 label: label.into(),
457 }
458}
459
460/// This contains a small fork of the util::paths::PathMatcher, that is stricter about the prefix
461/// check. Only subpaths pass the prefix check, rather than any prefix.
462mod custom_path_matcher {
463 use std::{fmt::Debug as _, path::Path};
464
465 use globset::{Glob, GlobSet, GlobSetBuilder};
466
467 #[derive(Clone, Debug, Default)]
468 pub struct PathMatcher {
469 sources: Vec<String>,
470 sources_with_trailing_slash: Vec<String>,
471 glob: GlobSet,
472 }
473
474 impl std::fmt::Display for PathMatcher {
475 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
476 self.sources.fmt(f)
477 }
478 }
479
480 impl PartialEq for PathMatcher {
481 fn eq(&self, other: &Self) -> bool {
482 self.sources.eq(&other.sources)
483 }
484 }
485
486 impl Eq for PathMatcher {}
487
488 impl PathMatcher {
489 pub fn new(globs: &[String]) -> Result<Self, globset::Error> {
490 let globs = globs
491 .into_iter()
492 .map(|glob| Glob::new(&glob))
493 .collect::<Result<Vec<_>, _>>()?;
494 let sources = globs.iter().map(|glob| glob.glob().to_owned()).collect();
495 let sources_with_trailing_slash = globs
496 .iter()
497 .map(|glob| glob.glob().to_string() + std::path::MAIN_SEPARATOR_STR)
498 .collect();
499 let mut glob_builder = GlobSetBuilder::new();
500 for single_glob in globs {
501 glob_builder.add(single_glob);
502 }
503 let glob = glob_builder.build()?;
504 Ok(PathMatcher {
505 glob,
506 sources,
507 sources_with_trailing_slash,
508 })
509 }
510
511 pub fn is_match<P: AsRef<Path>>(&self, other: P) -> bool {
512 let other_path = other.as_ref();
513 self.sources
514 .iter()
515 .zip(self.sources_with_trailing_slash.iter())
516 .any(|(source, with_slash)| {
517 let as_bytes = other_path.as_os_str().as_encoded_bytes();
518 let with_slash = if source.ends_with("/") {
519 source.as_bytes()
520 } else {
521 with_slash.as_bytes()
522 };
523
524 as_bytes.starts_with(with_slash) || as_bytes.ends_with(source.as_bytes())
525 })
526 || self.glob.is_match(other_path)
527 || self.check_with_end_separator(other_path)
528 }
529
530 fn check_with_end_separator(&self, path: &Path) -> bool {
531 let path_str = path.to_string_lossy();
532 let separator = std::path::MAIN_SEPARATOR_STR;
533 if path_str.ends_with(separator) {
534 return false;
535 } else {
536 self.glob.is_match(path_str.to_string() + separator)
537 }
538 }
539 }
540}
541
542#[cfg(test)]
543mod test {
544 use fs::FakeFs;
545 use gpui::TestAppContext;
546 use project::Project;
547 use serde_json::json;
548 use settings::SettingsStore;
549
550 use crate::slash_command::file_command::collect_files;
551
552 pub fn init_test(cx: &mut gpui::TestAppContext) {
553 if std::env::var("RUST_LOG").is_ok() {
554 env_logger::try_init().ok();
555 }
556
557 cx.update(|cx| {
558 let settings_store = SettingsStore::test(cx);
559 cx.set_global(settings_store);
560 // release_channel::init(SemanticVersion::default(), cx);
561 language::init(cx);
562 Project::init_settings(cx);
563 });
564 }
565
566 #[gpui::test]
567 async fn test_file_exact_matching(cx: &mut TestAppContext) {
568 init_test(cx);
569 let fs = FakeFs::new(cx.executor());
570
571 fs.insert_tree(
572 "/root",
573 json!({
574 "dir": {
575 "subdir": {
576 "file_0": "0"
577 },
578 "file_1": "1",
579 "file_2": "2",
580 "file_3": "3",
581 },
582 "dir.rs": "4"
583 }),
584 )
585 .await;
586
587 let project = Project::test(fs, ["/root".as_ref()], cx).await;
588
589 let result_1 = cx
590 .update(|cx| collect_files(project.clone(), &["root/dir".to_string()], cx))
591 .await
592 .unwrap();
593
594 assert!(result_1.completion_text.starts_with("root/dir"));
595 // 4 files + 2 directories
596 assert_eq!(6, result_1.files.len());
597
598 let result_2 = cx
599 .update(|cx| collect_files(project.clone(), &["root/dir/".to_string()], cx))
600 .await
601 .unwrap();
602
603 assert_eq!(result_1, result_2);
604
605 let result = cx
606 .update(|cx| collect_files(project.clone(), &["root/dir*".to_string()], cx))
607 .await
608 .unwrap();
609
610 assert!(result.completion_text.starts_with("root/dir"));
611 // 5 files + 2 directories
612 assert_eq!(7, result.files.len());
613
614 // Ensure that the project lasts until after the last await
615 drop(project);
616 }
617
618 #[gpui::test]
619 async fn test_file_sub_directory_rendering(cx: &mut TestAppContext) {
620 init_test(cx);
621 let fs = FakeFs::new(cx.executor());
622
623 fs.insert_tree(
624 "/zed",
625 json!({
626 "assets": {
627 "dir1": {
628 ".gitkeep": ""
629 },
630 "dir2": {
631 ".gitkeep": ""
632 },
633 "themes": {
634 "ayu": {
635 "LICENSE": "1",
636 },
637 "andromeda": {
638 "LICENSE": "2",
639 },
640 "summercamp": {
641 "LICENSE": "3",
642 },
643 },
644 },
645 }),
646 )
647 .await;
648
649 let project = Project::test(fs, ["/zed".as_ref()], cx).await;
650
651 let result = cx
652 .update(|cx| collect_files(project.clone(), &["zed/assets/themes".to_string()], cx))
653 .await
654 .unwrap();
655
656 // Sanity check
657 assert!(result.completion_text.starts_with("zed/assets/themes\n"));
658 assert_eq!(7, result.files.len());
659
660 // Ensure that full file paths are included in the real output
661 assert!(result
662 .completion_text
663 .contains("zed/assets/themes/andromeda/LICENSE"));
664 assert!(result
665 .completion_text
666 .contains("zed/assets/themes/ayu/LICENSE"));
667 assert!(result
668 .completion_text
669 .contains("zed/assets/themes/summercamp/LICENSE"));
670
671 assert_eq!("summercamp", result.files[5].path.to_string_lossy());
672
673 // Ensure that things are in descending order, with properly relativized paths
674 assert_eq!(
675 "zed/assets/themes/andromeda/LICENSE",
676 result.files[0].path.to_string_lossy()
677 );
678 assert_eq!("andromeda", result.files[1].path.to_string_lossy());
679 assert_eq!(
680 "zed/assets/themes/ayu/LICENSE",
681 result.files[2].path.to_string_lossy()
682 );
683 assert_eq!("ayu", result.files[3].path.to_string_lossy());
684 assert_eq!(
685 "zed/assets/themes/summercamp/LICENSE",
686 result.files[4].path.to_string_lossy()
687 );
688
689 // Ensure that the project lasts until after the last await
690 drop(project);
691 }
692
693 #[gpui::test]
694 async fn test_file_deep_sub_directory_rendering(cx: &mut TestAppContext) {
695 init_test(cx);
696 let fs = FakeFs::new(cx.executor());
697
698 fs.insert_tree(
699 "/zed",
700 json!({
701 "assets": {
702 "themes": {
703 "LICENSE": "1",
704 "summercamp": {
705 "LICENSE": "1",
706 "subdir": {
707 "LICENSE": "1",
708 "subsubdir": {
709 "LICENSE": "3",
710 }
711 }
712 },
713 },
714 },
715 }),
716 )
717 .await;
718
719 let project = Project::test(fs, ["/zed".as_ref()], cx).await;
720
721 let result = cx
722 .update(|cx| collect_files(project.clone(), &["zed/assets/themes".to_string()], cx))
723 .await
724 .unwrap();
725
726 assert!(result.completion_text.starts_with("zed/assets/themes\n"));
727 assert_eq!(
728 "zed/assets/themes/LICENSE",
729 result.files[0].path.to_string_lossy()
730 );
731 assert_eq!(
732 "zed/assets/themes/summercamp/LICENSE",
733 result.files[1].path.to_string_lossy()
734 );
735 assert_eq!(
736 "zed/assets/themes/summercamp/subdir/LICENSE",
737 result.files[2].path.to_string_lossy()
738 );
739 assert_eq!(
740 "zed/assets/themes/summercamp/subdir/subsubdir/LICENSE",
741 result.files[3].path.to_string_lossy()
742 );
743 assert_eq!("subsubdir", result.files[4].path.to_string_lossy());
744 assert_eq!("subdir", result.files[5].path.to_string_lossy());
745 assert_eq!("summercamp", result.files[6].path.to_string_lossy());
746 assert_eq!("zed/assets/themes", result.files[7].path.to_string_lossy());
747
748 // Ensure that the project lasts until after the last await
749 drop(project);
750 }
751}