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