assistant_slash_command.rs

 1mod slash_command_registry;
 2
 3use anyhow::Result;
 4use gpui::{AnyElement, AppContext, ElementId, Task, WeakView, WindowContext};
 5use language::LspAdapterDelegate;
 6pub use slash_command_registry::*;
 7use std::{
 8    ops::Range,
 9    sync::{atomic::AtomicBool, Arc},
10};
11use workspace::Workspace;
12
13pub fn init(cx: &mut AppContext) {
14    SlashCommandRegistry::default_global(cx);
15}
16
17pub trait SlashCommand: 'static + Send + Sync {
18    fn name(&self) -> String;
19    fn description(&self) -> String;
20    fn tooltip_text(&self) -> String;
21    fn complete_argument(
22        &self,
23        query: String,
24        cancel: Arc<AtomicBool>,
25        cx: &mut AppContext,
26    ) -> Task<Result<Vec<String>>>;
27    fn requires_argument(&self) -> bool;
28    fn run(
29        self: Arc<Self>,
30        argument: Option<&str>,
31        workspace: WeakView<Workspace>,
32        // TODO: We're just using the `LspAdapterDelegate` here because that is
33        // what the extension API is already expecting.
34        //
35        // It may be that `LspAdapterDelegate` needs a more general name, or
36        // perhaps another kind of delegate is needed here.
37        delegate: Arc<dyn LspAdapterDelegate>,
38        cx: &mut WindowContext,
39    ) -> Task<Result<SlashCommandOutput>>;
40}
41
42pub type RenderFoldPlaceholder = Arc<
43    dyn Send
44        + Sync
45        + Fn(ElementId, Arc<dyn Fn(&mut WindowContext)>, &mut WindowContext) -> AnyElement,
46>;
47
48pub struct SlashCommandOutput {
49    pub text: String,
50    pub sections: Vec<SlashCommandOutputSection<usize>>,
51}
52
53#[derive(Clone)]
54pub struct SlashCommandOutputSection<T> {
55    pub range: Range<T>,
56    pub render_placeholder: RenderFoldPlaceholder,
57}