slash_command_picker.rs

  1use std::sync::Arc;
  2
  3use assistant_slash_command::SlashCommandWorkingSet;
  4use gpui::{AnyElement, AnyView, DismissEvent, SharedString, Task, WeakEntity};
  5use picker::{Picker, PickerDelegate, PickerEditorPosition};
  6use ui::{prelude::*, ListItem, ListItemSpacing, PopoverMenu, PopoverTrigger, Tooltip};
  7
  8use crate::context_editor::ContextEditor;
  9
 10#[derive(IntoElement)]
 11pub(super) struct SlashCommandSelector<T, TT>
 12where
 13    T: PopoverTrigger + ButtonCommon,
 14    TT: Fn(&mut Window, &mut App) -> AnyView + 'static,
 15{
 16    working_set: Arc<SlashCommandWorkingSet>,
 17    active_context_editor: WeakEntity<ContextEditor>,
 18    trigger: T,
 19    tooltip: TT,
 20}
 21
 22#[derive(Clone)]
 23struct SlashCommandInfo {
 24    name: SharedString,
 25    description: SharedString,
 26    args: Option<SharedString>,
 27    icon: IconName,
 28}
 29
 30#[derive(Clone)]
 31enum SlashCommandEntry {
 32    Info(SlashCommandInfo),
 33    Advert {
 34        name: SharedString,
 35        renderer: fn(&mut Window, &mut App) -> AnyElement,
 36        on_confirm: fn(&mut Window, &mut App),
 37    },
 38}
 39
 40impl AsRef<str> for SlashCommandEntry {
 41    fn as_ref(&self) -> &str {
 42        match self {
 43            SlashCommandEntry::Info(SlashCommandInfo { name, .. })
 44            | SlashCommandEntry::Advert { name, .. } => name,
 45        }
 46    }
 47}
 48
 49pub(crate) struct SlashCommandDelegate {
 50    all_commands: Vec<SlashCommandEntry>,
 51    filtered_commands: Vec<SlashCommandEntry>,
 52    active_context_editor: WeakEntity<ContextEditor>,
 53    selected_index: usize,
 54}
 55
 56impl<T, TT> SlashCommandSelector<T, TT>
 57where
 58    T: PopoverTrigger + ButtonCommon,
 59    TT: Fn(&mut Window, &mut App) -> AnyView + 'static,
 60{
 61    pub(crate) fn new(
 62        working_set: Arc<SlashCommandWorkingSet>,
 63        active_context_editor: WeakEntity<ContextEditor>,
 64        trigger: T,
 65        tooltip: TT,
 66    ) -> Self {
 67        SlashCommandSelector {
 68            working_set,
 69            active_context_editor,
 70            trigger,
 71            tooltip,
 72        }
 73    }
 74}
 75
 76impl PickerDelegate for SlashCommandDelegate {
 77    type ListItem = ListItem;
 78
 79    fn match_count(&self) -> usize {
 80        self.filtered_commands.len()
 81    }
 82
 83    fn selected_index(&self) -> usize {
 84        self.selected_index
 85    }
 86
 87    fn set_selected_index(&mut self, ix: usize, _: &mut Window, cx: &mut Context<Picker<Self>>) {
 88        self.selected_index = ix.min(self.filtered_commands.len().saturating_sub(1));
 89        cx.notify();
 90    }
 91
 92    fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
 93        "Select a command...".into()
 94    }
 95
 96    fn update_matches(
 97        &mut self,
 98        query: String,
 99        window: &mut Window,
100        cx: &mut Context<Picker<Self>>,
101    ) -> Task<()> {
102        let all_commands = self.all_commands.clone();
103        cx.spawn_in(window, |this, mut cx| async move {
104            let filtered_commands = cx
105                .background_executor()
106                .spawn(async move {
107                    if query.is_empty() {
108                        all_commands
109                    } else {
110                        all_commands
111                            .into_iter()
112                            .filter(|model_info| {
113                                model_info
114                                    .as_ref()
115                                    .to_lowercase()
116                                    .contains(&query.to_lowercase())
117                            })
118                            .collect()
119                    }
120                })
121                .await;
122
123            this.update_in(&mut cx, |this, window, cx| {
124                this.delegate.filtered_commands = filtered_commands;
125                this.delegate.set_selected_index(0, window, cx);
126                cx.notify();
127            })
128            .ok();
129        })
130    }
131
132    fn separators_after_indices(&self) -> Vec<usize> {
133        let mut ret = vec![];
134        let mut previous_is_advert = false;
135
136        for (index, command) in self.filtered_commands.iter().enumerate() {
137            if previous_is_advert {
138                if let SlashCommandEntry::Info(_) = command {
139                    previous_is_advert = false;
140                    debug_assert_ne!(
141                        index, 0,
142                        "index cannot be zero, as we can never have a separator at 0th position"
143                    );
144                    ret.push(index - 1);
145                }
146            } else {
147                if let SlashCommandEntry::Advert { .. } = command {
148                    previous_is_advert = true;
149                    if index != 0 {
150                        ret.push(index - 1);
151                    }
152                }
153            }
154        }
155        ret
156    }
157
158    fn confirm(&mut self, _secondary: bool, window: &mut Window, cx: &mut Context<Picker<Self>>) {
159        if let Some(command) = self.filtered_commands.get(self.selected_index) {
160            match command {
161                SlashCommandEntry::Info(info) => {
162                    self.active_context_editor
163                        .update(cx, |context_editor, cx| {
164                            context_editor.insert_command(&info.name, window, cx)
165                        })
166                        .ok();
167                }
168                SlashCommandEntry::Advert { on_confirm, .. } => {
169                    on_confirm(window, cx);
170                }
171            }
172            cx.emit(DismissEvent);
173        }
174    }
175
176    fn dismissed(&mut self, _window: &mut Window, _cx: &mut Context<Picker<Self>>) {}
177
178    fn editor_position(&self) -> PickerEditorPosition {
179        PickerEditorPosition::End
180    }
181
182    fn render_match(
183        &self,
184        ix: usize,
185        selected: bool,
186        window: &mut Window,
187        cx: &mut Context<Picker<Self>>,
188    ) -> Option<Self::ListItem> {
189        let command_info = self.filtered_commands.get(ix)?;
190
191        match command_info {
192            SlashCommandEntry::Info(info) => Some(
193                ListItem::new(ix)
194                    .inset(true)
195                    .spacing(ListItemSpacing::Dense)
196                    .toggle_state(selected)
197                    .tooltip({
198                        let description = info.description.clone();
199                        move |_, cx| cx.new(|_| Tooltip::new(description.clone())).into()
200                    })
201                    .child(
202                        v_flex()
203                            .group(format!("command-entry-label-{ix}"))
204                            .w_full()
205                            .py_0p5()
206                            .min_w(px(250.))
207                            .max_w(px(400.))
208                            .child(
209                                h_flex()
210                                    .gap_1p5()
211                                    .child(Icon::new(info.icon).size(IconSize::XSmall))
212                                    .child(div().font_buffer(cx).child({
213                                        let mut label = format!("{}", info.name);
214                                        if let Some(args) = info.args.as_ref().filter(|_| selected)
215                                        {
216                                            label.push_str(&args);
217                                        }
218                                        Label::new(label).single_line().size(LabelSize::Small)
219                                    }))
220                                    .children(info.args.clone().filter(|_| !selected).map(
221                                        |args| {
222                                            div()
223                                                .font_buffer(cx)
224                                                .child(
225                                                    Label::new(args)
226                                                        .single_line()
227                                                        .size(LabelSize::Small)
228                                                        .color(Color::Muted),
229                                                )
230                                                .visible_on_hover(format!(
231                                                    "command-entry-label-{ix}"
232                                                ))
233                                        },
234                                    )),
235                            )
236                            .child(
237                                Label::new(info.description.clone())
238                                    .size(LabelSize::Small)
239                                    .color(Color::Muted)
240                                    .text_ellipsis(),
241                            ),
242                    ),
243            ),
244            SlashCommandEntry::Advert { renderer, .. } => Some(
245                ListItem::new(ix)
246                    .inset(true)
247                    .spacing(ListItemSpacing::Dense)
248                    .toggle_state(selected)
249                    .child(renderer(window, cx)),
250            ),
251        }
252    }
253}
254
255impl<T, TT> RenderOnce for SlashCommandSelector<T, TT>
256where
257    T: PopoverTrigger + ButtonCommon,
258    TT: Fn(&mut Window, &mut App) -> AnyView + 'static,
259{
260    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
261        let all_models = self
262            .working_set
263            .featured_command_names(cx)
264            .into_iter()
265            .filter_map(|command_name| {
266                let command = self.working_set.command(&command_name, cx)?;
267                let menu_text = SharedString::from(Arc::from(command.menu_text()));
268                let label = command.label(cx);
269                let args = label.filter_range.end.ne(&label.text.len()).then(|| {
270                    SharedString::from(
271                        label.text[label.filter_range.end..label.text.len()].to_owned(),
272                    )
273                });
274                Some(SlashCommandEntry::Info(SlashCommandInfo {
275                    name: command_name.into(),
276                    description: menu_text,
277                    args,
278                    icon: command.icon(),
279                }))
280            })
281            .chain([SlashCommandEntry::Advert {
282                name: "create-your-command".into(),
283                renderer: |_, cx| {
284                    v_flex()
285                        .w_full()
286                        .child(
287                            h_flex()
288                                .w_full()
289                                .font_buffer(cx)
290                                .items_center()
291                                .justify_between()
292                                .child(
293                                    h_flex()
294                                        .items_center()
295                                        .gap_1p5()
296                                        .child(Icon::new(IconName::Plus).size(IconSize::XSmall))
297                                        .child(
298                                            div().font_buffer(cx).child(
299                                                Label::new("create-your-command")
300                                                    .size(LabelSize::Small),
301                                            ),
302                                        ),
303                                )
304                                .child(
305                                    Icon::new(IconName::ArrowUpRight)
306                                        .size(IconSize::XSmall)
307                                        .color(Color::Muted),
308                                ),
309                        )
310                        .child(
311                            Label::new("Create your custom command")
312                                .size(LabelSize::Small)
313                                .color(Color::Muted),
314                        )
315                        .into_any_element()
316                },
317                on_confirm: |_, cx| cx.open_url("https://zed.dev/docs/extensions/slash-commands"),
318            }])
319            .collect::<Vec<_>>();
320
321        let delegate = SlashCommandDelegate {
322            all_commands: all_models.clone(),
323            active_context_editor: self.active_context_editor.clone(),
324            filtered_commands: all_models,
325            selected_index: 0,
326        };
327
328        let picker_view = cx.new(|cx| {
329            let picker =
330                Picker::uniform_list(delegate, window, cx).max_height(Some(rems(20.).into()));
331            picker
332        });
333
334        let handle = self
335            .active_context_editor
336            .update(cx, |this, _| this.slash_menu_handle.clone())
337            .ok();
338        PopoverMenu::new("model-switcher")
339            .menu(move |_window, _cx| Some(picker_view.clone()))
340            .trigger_with_tooltip(self.trigger, self.tooltip)
341            .attach(gpui::Corner::TopLeft)
342            .anchor(gpui::Corner::BottomLeft)
343            .offset(gpui::Point {
344                x: px(0.0),
345                y: px(-16.0),
346            })
347            .when_some(handle, |this, handle| this.with_handle(handle))
348    }
349}