default_command.rs

 1use super::{SlashCommand, SlashCommandOutput};
 2use crate::prompt_library::PromptStore;
 3use anyhow::{anyhow, Result};
 4use assistant_slash_command::{ArgumentCompletion, SlashCommandOutputSection};
 5use gpui::{Task, WeakView};
 6use language::{BufferSnapshot, LspAdapterDelegate};
 7use std::{
 8    fmt::Write,
 9    sync::{atomic::AtomicBool, Arc},
10};
11use ui::prelude::*;
12use workspace::Workspace;
13
14pub(crate) struct DefaultSlashCommand;
15
16impl SlashCommand for DefaultSlashCommand {
17    fn name(&self) -> String {
18        "default".into()
19    }
20
21    fn description(&self) -> String {
22        "insert default prompt".into()
23    }
24
25    fn menu_text(&self) -> String {
26        "Insert Default Prompt".into()
27    }
28
29    fn requires_argument(&self) -> bool {
30        false
31    }
32
33    fn complete_argument(
34        self: Arc<Self>,
35        _arguments: &[String],
36        _cancellation_flag: Arc<AtomicBool>,
37        _workspace: Option<WeakView<Workspace>>,
38        _cx: &mut WindowContext,
39    ) -> Task<Result<Vec<ArgumentCompletion>>> {
40        Task::ready(Err(anyhow!("this command does not require argument")))
41    }
42
43    fn run(
44        self: Arc<Self>,
45        _arguments: &[String],
46        _context_slash_command_output_sections: &[SlashCommandOutputSection<language::Anchor>],
47        _context_buffer: BufferSnapshot,
48        _workspace: WeakView<Workspace>,
49        _delegate: Option<Arc<dyn LspAdapterDelegate>>,
50        cx: &mut WindowContext,
51    ) -> Task<Result<SlashCommandOutput>> {
52        let store = PromptStore::global(cx);
53        cx.background_executor().spawn(async move {
54            let store = store.await?;
55            let prompts = store.default_prompt_metadata();
56
57            let mut text = String::new();
58            text.push('\n');
59            for prompt in prompts {
60                if let Some(title) = prompt.title {
61                    writeln!(text, "/prompt {}", title).unwrap();
62                }
63            }
64            text.pop();
65
66            if text.is_empty() {
67                text.push('\n');
68            }
69
70            Ok(SlashCommandOutput {
71                sections: vec![SlashCommandOutputSection {
72                    range: 0..text.len(),
73                    icon: IconName::Library,
74                    label: "Default".into(),
75                    metadata: None,
76                }],
77                text,
78                run_commands_in_text: true,
79            })
80        })
81    }
82}