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