assistant_slash_command.rs

 1mod slash_command_registry;
 2
 3use anyhow::Result;
 4use gpui::{AnyElement, AppContext, ElementId, SharedString, Task, WeakView, WindowContext};
 5use language::{CodeLabel, LspAdapterDelegate};
 6use serde::{Deserialize, Serialize};
 7pub use slash_command_registry::*;
 8use std::{
 9    ops::Range,
10    sync::{atomic::AtomicBool, Arc},
11};
12use workspace::{ui::IconName, Workspace};
13
14pub fn init(cx: &mut AppContext) {
15    SlashCommandRegistry::default_global(cx);
16}
17
18#[derive(Debug)]
19pub struct ArgumentCompletion {
20    /// The label to display for this completion.
21    pub label: String,
22    /// The new text that should be inserted into the command when this completion is accepted.
23    pub new_text: String,
24    /// Whether the command should be run when accepting this completion.
25    pub run_command: bool,
26}
27
28pub trait SlashCommand: 'static + Send + Sync {
29    fn name(&self) -> String;
30    fn label(&self, _cx: &AppContext) -> CodeLabel {
31        CodeLabel::plain(self.name(), None)
32    }
33    fn description(&self) -> String;
34    fn menu_text(&self) -> String;
35    fn complete_argument(
36        self: Arc<Self>,
37        query: String,
38        cancel: Arc<AtomicBool>,
39        workspace: Option<WeakView<Workspace>>,
40        cx: &mut AppContext,
41    ) -> Task<Result<Vec<ArgumentCompletion>>>;
42    fn requires_argument(&self) -> bool;
43    fn run(
44        self: Arc<Self>,
45        argument: Option<&str>,
46        workspace: WeakView<Workspace>,
47        // TODO: We're just using the `LspAdapterDelegate` here because that is
48        // what the extension API is already expecting.
49        //
50        // It may be that `LspAdapterDelegate` needs a more general name, or
51        // perhaps another kind of delegate is needed here.
52        delegate: Arc<dyn LspAdapterDelegate>,
53        cx: &mut WindowContext,
54    ) -> Task<Result<SlashCommandOutput>>;
55}
56
57pub type RenderFoldPlaceholder = Arc<
58    dyn Send
59        + Sync
60        + Fn(ElementId, Arc<dyn Fn(&mut WindowContext)>, &mut WindowContext) -> AnyElement,
61>;
62
63#[derive(Default)]
64pub struct SlashCommandOutput {
65    pub text: String,
66    pub sections: Vec<SlashCommandOutputSection<usize>>,
67    pub run_commands_in_text: bool,
68}
69
70#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
71pub struct SlashCommandOutputSection<T> {
72    pub range: Range<T>,
73    pub icon: IconName,
74    pub label: SharedString,
75}