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: CodeLabel,
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 /// Whether to replace the all arguments, or whether to treat this as an independent argument.
27 pub replace_previous_arguments: bool,
28}
29
30pub trait SlashCommand: 'static + Send + Sync {
31 fn name(&self) -> String;
32 fn label(&self, _cx: &AppContext) -> CodeLabel {
33 CodeLabel::plain(self.name(), None)
34 }
35 fn description(&self) -> String;
36 fn menu_text(&self) -> String;
37 fn complete_argument(
38 self: Arc<Self>,
39 arguments: &[String],
40 cancel: Arc<AtomicBool>,
41 workspace: Option<WeakView<Workspace>>,
42 cx: &mut WindowContext,
43 ) -> Task<Result<Vec<ArgumentCompletion>>>;
44 fn requires_argument(&self) -> bool;
45 fn run(
46 self: Arc<Self>,
47 arguments: &[String],
48 workspace: WeakView<Workspace>,
49 // TODO: We're just using the `LspAdapterDelegate` here because that is
50 // what the extension API is already expecting.
51 //
52 // It may be that `LspAdapterDelegate` needs a more general name, or
53 // perhaps another kind of delegate is needed here.
54 delegate: Option<Arc<dyn LspAdapterDelegate>>,
55 cx: &mut WindowContext,
56 ) -> Task<Result<SlashCommandOutput>>;
57}
58
59pub type RenderFoldPlaceholder = Arc<
60 dyn Send
61 + Sync
62 + Fn(ElementId, Arc<dyn Fn(&mut WindowContext)>, &mut WindowContext) -> AnyElement,
63>;
64
65#[derive(Debug, Default)]
66pub struct SlashCommandOutput {
67 pub text: String,
68 pub sections: Vec<SlashCommandOutputSection<usize>>,
69 pub run_commands_in_text: bool,
70}
71
72#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
73pub struct SlashCommandOutputSection<T> {
74 pub range: Range<T>,
75 pub icon: IconName,
76 pub label: SharedString,
77}