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
18pub trait SlashCommand: 'static + Send + Sync {
19    fn name(&self) -> String;
20    fn label(&self, _cx: &AppContext) -> CodeLabel {
21        CodeLabel::plain(self.name(), None)
22    }
23    fn description(&self) -> String;
24    fn menu_text(&self) -> String;
25    fn complete_argument(
26        self: Arc<Self>,
27        query: String,
28        cancel: Arc<AtomicBool>,
29        workspace: Option<WeakView<Workspace>>,
30        cx: &mut AppContext,
31    ) -> Task<Result<Vec<String>>>;
32    fn requires_argument(&self) -> bool;
33    fn run(
34        self: Arc<Self>,
35        argument: Option<&str>,
36        workspace: WeakView<Workspace>,
37        // TODO: We're just using the `LspAdapterDelegate` here because that is
38        // what the extension API is already expecting.
39        //
40        // It may be that `LspAdapterDelegate` needs a more general name, or
41        // perhaps another kind of delegate is needed here.
42        delegate: Arc<dyn LspAdapterDelegate>,
43        cx: &mut WindowContext,
44    ) -> Task<Result<SlashCommandOutput>>;
45}
46
47pub type RenderFoldPlaceholder = Arc<
48    dyn Send
49        + Sync
50        + Fn(ElementId, Arc<dyn Fn(&mut WindowContext)>, &mut WindowContext) -> AnyElement,
51>;
52
53pub struct SlashCommandOutput {
54    pub text: String,
55    pub sections: Vec<SlashCommandOutputSection<usize>>,
56    pub run_commands_in_text: bool,
57}
58
59#[derive(Clone, Serialize, Deserialize)]
60pub struct SlashCommandOutputSection<T> {
61    pub range: Range<T>,
62    pub icon: IconName,
63    pub label: SharedString,
64}