Skip to content

Visual Studio Code, rebuilt in Rust

Rust
22.4 KB
Raw Blame
//! actionWidget.ts and the row renderers of actionList.ts: the code action menu over the window, its keyboard and
//! mouse focus, the action bar under the rows, and the block that closes it on a click elsewhere. The list logic is
//! `action_list.rs`.

use std::cell::Cell;
use std::rc::Rc;

use gpui::{AnyElement, BoxShadow, Context, FocusHandle, FontWeight, Subscription, MouseButton, ScrollHandle, TextRun, Window, div, point, prelude::*, px, rgba};
use serde_json::{Value, json};

use super::Workbench;
use super::code_actions::{CodeActionSet, RowLook};
use crate::action_list::{ActionList, Item, ItemKind, Options};
use crate::commands::Outcome;
use crate::editor::{Position, column_x};
use crate::terminal::quick_fix::QuickFix;

const UI_FONT: &str = ".SystemUIFont";
const FONT_SIZE: f32 = 13.;
/// cornerRadius-large and cornerRadius-medium.
const RADIUS: f32 = 8.;
const ROW_RADIUS: f32 = 6.;
/// `.action-widget` padding and border.
const PADDING: f32 = 4.;
/// `.monaco-list-row` padding `0 12px 0 6px` and the 6px gaps around the 12px icon.
const ROW_PADDING_LEFT: f32 = 6.;
const ROW_PADDING_RIGHT: f32 = 12.;
const ICON_SIZE: f32 = 12.;
const GAP: f32 = 6.;
/// `.action-widget-action-bar`: 4px margin, 1px border, 2px padding and a 22px line.
const ACTION_BAR_HEIGHT: f32 = 4. + 1. + 2. + 22.;
/// contextview.ts `getAnchorRect`: a point anchor is a 1px wide, 2px high box.
const ANCHOR_HEIGHT: f32 = 2.;

/// Where the widget opens: under a model position of the active editor, or at a window point.
#[derive(Clone, Copy)]
pub(super) enum Anchor {
    Position(Position),
    Point(gpui::Point<f32>),
}

#[derive(Default)]
pub(super) struct ActionWidget {
    shown: Option<Shown>,
    /// `_showDisabled`.
    show_disabled: bool,
    scroll: ScrollHandle,
    /// The editor's content left the last render laid out, for anchors at a model position.
    pub(super) content_left: Rc<Cell<f32>>,
    /// The list's focus, which `show` takes at the next render (`contextView.focus`); losing it hides the widget.
    focus: Option<FocusHandle>,
    take_focus: bool,
    blur: Option<Subscription>,
}

struct Shown {
    list: ActionList<usize>,
    looks: Vec<RowLook>,
    anchor: Anchor,
    /// The action bar: documentation commands, then Show or Hide Disabled (no command).
    bar: Vec<(String, Option<Value>)>,
    owner: Owner,
    show_above: Option<bool>,
    /// The pointer moved the focus last (`mouse-navigation`).
    mouse_navigation: bool,
}

/// Who showed the widget and gets the accepted row.
pub(super) enum Owner {
    CodeActions { set: Rc<CodeActionSet>, include_disabled: bool, from_lightbulb: bool },
    /// postEditWidget.ts `showSelector`: the row is the index of a paste edit.
    PasteEdits,
    /// quickFixAddon.ts: a terminal instance's quick fixes, by row.
    TerminalQuickFixes(u32, Rc<Vec<QuickFix>>),
}

impl Workbench {
    pub(super) fn register_action_widget_commands(&mut self) {
        self.commands.register("hideCodeActionWidget", |workbench, _, window, cx| {
            workbench.hide_action_widget_to_owner(window, cx);
            Outcome::done()
        });
        self.commands.register("selectNextCodeAction", |workbench, _, _, cx| {
            workbench.move_action_widget_focus(ActionList::focus_next, cx);
            Outcome::done()
        });
        self.commands.register("selectPrevCodeAction", |workbench, _, _, cx| {
            workbench.move_action_widget_focus(ActionList::focus_previous, cx);
            Outcome::done()
        });
        for (id, preview) in [("acceptSelectedCodeAction", false), ("previewSelectedCodeAction", true)] {
            self.commands.register(id, move |workbench, _, window, cx| {
                workbench.accept_action_widget_row(None, preview, window, cx);
                Outcome::done()
            });
        }
        let sections: [(&str, fn(&mut ActionList<usize>) -> bool); 3] = [
            ("collapseSectionCodeAction", ActionList::collapse_focused_section),
            ("expandSectionCodeAction", ActionList::expand_focused_section),
            ("toggleSectionCodeAction", ActionList::toggle_focused_section),
        ];
        for (id, change) in sections {
            self.commands.register(id, move |workbench, _, _, cx| {
                if let Some(shown) = workbench.action_widget.shown.as_mut() {
                    change(&mut shown.list);
                    cx.notify();
                }
                Outcome::done()
            });
        }
        self.commands.register("clearFilterCodeActionWidget", |workbench, _, _, cx| {
            if let Some(shown) = workbench.action_widget.shown.as_mut()
                && shown.list.clear_filter()
            {
                cx.notify();
            }
            Outcome::done()
        });
    }

    /// `codeActionMenuVisible` and `codeActionMenuFilterFocused`.
    pub(super) fn action_widget_context(&self, context: &mut std::collections::HashMap<String, Value>) {
        context.insert("codeActionMenuVisible".into(), json!(self.action_widget.shown.is_some()));
        context.insert("codeActionMenuFilterFocused".into(), json!(self.action_widget.shown.as_ref().is_some_and(|shown| shown.list.filter_focused)));
    }

    pub(super) fn action_widget_focused(&self, window: &Window) -> bool {
        self.action_widget.focus.as_ref().is_some_and(|focus| focus.is_focused(window))
    }

    /// codeActionController.ts and postEditWidget.ts show the widget in the editor's DOM, quickFixAddon.ts in the terminal's.
    pub(super) fn action_widget_in_editor_focused(&self, window: &Window) -> bool {
        self.action_widget_focused(window) && self.action_widget.shown.as_ref().is_some_and(|shown| !matches!(shown.owner, Owner::TerminalQuickFixes(..)))
    }

    /// The list's type navigation: `word` typed so far, `idle` when it is a new word.
    pub(super) fn action_widget_type_navigation(&mut self, word: &str, idle: bool, cx: &mut Context<Self>) {
        let Some(shown) = self.action_widget.shown.as_mut() else { return };
        if shown.list.focus_by_label(word, idle) {
            shown.mouse_navigation = false;
            self.action_widget.scroll.scroll_to_item(shown.list.focused.expect("focus_by_label set it"));
            cx.notify();
        }
    }

    pub(super) fn action_widget_visible(&self) -> bool {
        self.action_widget.shown.is_some()
    }

    pub(super) fn action_widget_shows_disabled(&self) -> bool {
        self.action_widget.show_disabled
    }

    #[cfg(test)]
    pub(super) fn action_widget_labels(&self) -> Vec<String> {
        let shown = self.action_widget.shown.as_ref().expect("the widget shows");
        shown.list.all.iter().map(|item| item.label.clone().unwrap_or_default()).collect()
    }

    /// The focused row's code action, for its `quickfix-edit-highlight`.
    pub(super) fn action_widget_focused_action(&self) -> Option<&Value> {
        let shown = self.action_widget.shown.as_ref()?;
        let index = shown.list.focused_item()?.item?;
        let Owner::CodeActions { set, .. } = &shown.owner else { return None };
        set.all.iter().chain(&set.valid).nth(index).map(|item| &item.action)
    }

    /// `ActionWidgetService.show`: the rows with the first focusable one focused, the list taking the keyboard focus.
    pub(super) fn show_action_widget(&mut self, items: Vec<Item<usize>>, looks: Vec<RowLook>, anchor: Anchor, bar: Vec<(String, Option<Value>)>, owner: Owner, cx: &mut Context<Self>) {
        self.hide_action_widget(cx);
        let mut list = ActionList::new(items, Options::default());
        list.focus();
        self.action_widget.shown = Some(Shown { list, looks, anchor, bar, owner, show_above: None, mouse_navigation: false });
        self.action_widget.scroll = ScrollHandle::new();
        self.action_widget.take_focus = true;
        cx.notify();
    }

    /// quickFixAddon.ts `showMenu`: the widget over a terminal's quick fixes, without an action bar.
    pub(super) fn show_terminal_quick_fix_widget(&mut self, items: Vec<Item<usize>>, looks: Vec<RowLook>, anchor: Anchor, instance: u32, fixes: Rc<Vec<QuickFix>>, cx: &mut Context<Self>) {
        self.show_action_widget(items, looks, anchor, Vec::new(), Owner::TerminalQuickFixes(instance, fixes), cx);
    }

    /// The widget hides and the focus goes back to what showed it: the editor or the terminal.
    fn hide_action_widget_to_owner(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let terminal = matches!(self.action_widget.shown.as_ref().map(|shown| &shown.owner), Some(Owner::TerminalQuickFixes(..)));
        self.hide_action_widget(cx);
        match terminal {
            true => window.focus(&self.terminal_view.focus, cx),
            false => window.focus(&self.editor_focus, cx),
        }
    }

    /// `hide`: the rows go and the set is released unless the light bulb still shows it.
    pub(super) fn hide_action_widget(&mut self, cx: &mut Context<Self>) {
        if let Some(shown) = self.action_widget.shown.take() {
            if let Owner::CodeActions { set, .. } = shown.owner {
                self.release_code_action_set(set);
            }
            cx.notify();
        }
    }

    fn move_action_widget_focus(&mut self, change: fn(&mut ActionList<usize>), cx: &mut Context<Self>) {
        let Some(shown) = self.action_widget.shown.as_mut() else { return };
        change(&mut shown.list);
        shown.mouse_navigation = false;
        if let Some(row) = shown.list.focused {
            self.action_widget.scroll.scroll_to_item(row);
        }
        cx.notify();
    }

    /// `acceptSelected` or a click on `row`: an action row applies its action, a code action through the Refactor
    /// Preview with `preview`, and the widget hides.
    fn accept_action_widget_row(&mut self, row: Option<usize>, preview: bool, window: &mut Window, cx: &mut Context<Self>) {
        let Some(shown) = self.action_widget.shown.as_mut() else { return };
        let accepted = match row {
            Some(row) => shown.list.accept_row(row, false, false),
            None => shown.list.accept_selected(false, false),
        };
        let crate::action_list::Accepted::Select { item, .. } = accepted else { return cx.notify() };
        match &shown.owner {
            Owner::CodeActions { set, .. } => {
                let set = set.clone();
                self.hide_action_widget(cx);
                self.accept_code_action(item, set, preview, window, cx);
            }
            Owner::PasteEdits => {
                self.hide_action_widget_to_owner(window, cx);
                self.select_paste_edit(item, window, cx);
            }
            Owner::TerminalQuickFixes(instance, fixes) => {
                let (instance, fix) = (*instance, fixes[item].clone());
                self.hide_action_widget_to_owner(window, cx);
                self.run_terminal_quick_fix(instance, &fix, window, cx);
            }
        }
    }

    /// An action bar entry: a documentation command runs, Show or Hide Disabled shows the list again.
    fn run_action_widget_bar(&mut self, index: usize, window: &mut Window, cx: &mut Context<Self>) {
        let Some(shown) = self.action_widget.shown.as_ref() else { return };
        let (_, command) = &shown.bar[index];
        match command.clone() {
            Some(command) => {
                self.hide_action_widget(cx);
                window.focus(&self.editor_focus, cx);
                let args = command["arguments"].as_array().cloned().unwrap_or_default();
                drop(self.execute_command(command["id"].as_str().unwrap_or_default(), args, window, cx));
            }
            None => {
                let Owner::CodeActions { set, include_disabled, from_lightbulb } = &shown.owner else { return };
                let (set, anchor, include_disabled, from_lightbulb) = (set.clone(), shown.anchor, *include_disabled, *from_lightbulb);
                self.action_widget.show_disabled = !self.action_widget.show_disabled;
                self.show_code_action_list(set, anchor, include_disabled, from_lightbulb, cx);
            }
        }
    }

    /// The widget at its anchor, below it when it fits in the window, else above; with the block that closes it on a
    /// press anywhere else.
    // Accepted platform difference: the menu opens without the frosted glass scale animation (GPUI has no element transforms).
    pub(super) fn action_widget(&mut self, window: &mut Window, cx: &mut Context<Self>) -> Option<AnyElement> {
        self.action_widget.shown.as_ref()?;
        let focus = self.action_widget.focus.get_or_insert_with(|| cx.focus_handle()).clone();
        if std::mem::take(&mut self.action_widget.take_focus) {
            window.focus(&focus, cx);
        }
        if self.action_widget.blur.is_none() {
            // actionWidget.ts `focusTracker.onDidBlur`; a deactivated window keeps the DOM focus where it was.
            self.action_widget.blur = Some(cx.on_blur(&focus, window, |workbench, window, cx| {
                if window.is_window_active() {
                    workbench.hide_action_widget(cx);
                }
            }));
        }
        let content_left = self.action_widget.content_left.get();
        let window_size = window.viewport_size().map(f32::from);
        let anchor = match self.action_widget.shown.as_ref()?.anchor {
            Anchor::Point(at) => at,
            Anchor::Position(position) => {
                let editor = self.editors.active()?;
                let viewport = self.editors.viewport;
                let text = editor.document().lines.get(position.line)?.clone();
                let bounds = self.text_area_bounds.get();
                let x = column_x(&text, position.column, editor.document().indentation.tab_size) * viewport.char_width - editor.scroll.left + content_left;
                let y = editor.line_top(editor.view_line(position.line), viewport) - editor.scroll.top + viewport.line_height;
                point(bounds.origin.x + x, bounds.origin.y + y)
            }
        };
        let bar_width: f32 = {
            let shown = self.action_widget.shown.as_ref()?;
            match shown.bar.is_empty() {
                true => 0.,
                false => 26. + 8. + shown.bar.iter().map(|(label, _)| text_width(label, 12., FontWeight::NORMAL, window) + 16.).sum::<f32>(),
            }
        };
        let shown = self.action_widget.shown.as_mut()?;
        let chrome = 2. * PADDING + 2. + if shown.bar.is_empty() { 0. } else { ACTION_BAR_HEIGHT };
        let list_height = shown.list.compute_height(anchor.y, ANCHOR_HEIGHT, window_size.height, chrome, &mut shown.show_above);
        let list_width = shown.list.compute_width(bar_width, |item| row_width(item, &shown.looks, &shown.list, window));
        let size = (list_width + 2. * PADDING + 2., list_height + chrome);
        let shown = self.action_widget.shown.as_ref()?;
        // contextview.ts `layout`: below the anchor's 1x2 box when it fits, else above it, else against the far edge.
        let below = anchor.y + ANCHOR_HEIGHT;
        let top = if below + size.1 <= window_size.height { below } else if anchor.y - size.1 >= 0. { anchor.y - size.1 } else { (window_size.height - size.1).max(0.) };
        let left = if anchor.x + size.0 <= window_size.width { anchor.x } else { (anchor.x + 1. - size.0).max(0.) };
        let high_contrast = self.theme.kind.is_high_contrast();
        let mut rows = Vec::new();
        for row in 0..shown.list.visible.len() {
            rows.push(self.action_widget_row(shown, row, cx));
        }
        let list = self.scrollable("action-widget-list", Some(&self.action_widget.scroll), div().id("action-widget-list").children(rows), cx).h(px(list_height)).w(px(list_width));
        let bar = (!shown.bar.is_empty()).then(|| {
            let link = self.color("textLink.activeForeground");
            div()
                .mt(px(4.))
                .border_t_1()
                .border_color(self.color("editorHoverWidget.border"))
                .pt(px(2.))
                .pl(px(26.))
                .pr(px(8.))
                .flex()
                .children(shown.bar.iter().enumerate().map(|(index, (label, _))| {
                    div()
                        .id(("action-widget-bar", index))
                        .mr(px(16.))
                        .text_size(px(12.))
                        .line_height(px(22.))
                        .text_color(link)
                        .cursor_pointer()
                        .child(label.clone())
                        .on_mouse_down(MouseButton::Left, cx.listener(move |workbench, _, window, cx| {
                            cx.stop_propagation();
                            workbench.run_action_widget_bar(index, window, cx);
                        }))
                }))
        });
        let widget = div()
            .id("action-widget")
            .track_focus(&focus)
            .absolute()
            .left(px(left))
            .top(px(top))
            .p(px(PADDING))
            .border_1()
            .border_color(self.color("editorHoverWidget.border"))
            .rounded(px(RADIUS))
            .font_family(UI_FONT)
            .text_size(px(FONT_SIZE))
            .text_color(self.foreground_or("menu.foreground"))
            .occlude()
            .when(high_contrast, |widget| widget.bg(self.color("menu.background")))
            .when(!high_contrast, |widget| {
                // --vscode-shadow-lg blurs 12px; GPUI's blur_radius is the sigma, half the CSS blur.
                widget.child(self.frosted_glass("menu.background", RADIUS)).shadow(vec![BoxShadow { color: rgba(0x00000024).into(), offset: point(px(0.), px(0.)), blur_radius: px(6.), spread_radius: px(0.), inset: false }])
            })
            .on_any_mouse_down(|_, _, cx| cx.stop_propagation())
            .child(list)
            .children(bar);
        let block = div().id("action-widget-block").absolute().inset_0().occlude().on_any_mouse_down(cx.listener(|workbench, _, window, cx| {
            workbench.hide_action_widget_to_owner(window, cx);
        }));
        Some(div().absolute().inset_0().child(block).child(widget).into_any_element())
    }

    /// `HeaderRenderer` and `ActionItemRenderer`: a header's group title, an action's icon, title and keybinding.
    fn action_widget_row(&self, shown: &Shown, row: usize, cx: &mut Context<Self>) -> AnyElement {
        let index = shown.list.visible[row];
        let item = &shown.list.all[index];
        let look = &shown.looks[index];
        let height = ActionList::<usize>::item_height(item);
        let base = div().id(("action-widget-row", row)).h(px(height)).flex_none().flex().items_center().pl(px(ROW_PADDING_LEFT)).pr(px(ROW_PADDING_RIGHT)).whitespace_nowrap().overflow_hidden();
        if item.kind == ItemKind::Header {
            // `.group-header:not(:first-of-type)` has a 2px top margin; the list places rows absolutely, so it only moves
            // the header itself.
            let header = base.when(row > 0, |header| header.relative().top(px(2.))).text_color(self.color("descriptionForeground")).font_weight(FontWeight::SEMIBOLD).text_size(px(11.)).child(item.label.clone().unwrap_or_default());
            return header.into_any_element();
        }
        let focused = shown.list.focused == Some(row) && !item.disabled;
        let foreground = match (item.disabled, focused) {
            (true, _) => Some(self.color("disabledForeground")),
            (false, true) => self.theme.color("list.hoverForeground"),
            (false, false) => None,
        };
        let icon_color = foreground.or_else(|| look.color.map(|key| self.color(key))).unwrap_or_else(|| self.foreground_or("menu.foreground"));
        let selection_border = self.theme.color("menu.selectionBorder");
        let mut row_element = base
            .gap(px(GAP))
            .rounded(px(ROW_RADIUS))
            .when(!item.disabled, |row| row.cursor_pointer())
            .when_some(foreground, |row, color| row.text_color(color))
            .when(focused, |row| row.bg(self.color("list.hoverBackground")))
            .child(self.icon(crate::symbol_icons::svg_name(look.icon), ICON_SIZE, ICON_SIZE, icon_color))
            .child(div().flex_1().min_w_0().overflow_hidden().text_ellipsis().child(item.label.clone().unwrap_or_default()));
        if focused && let Some(border) = selection_border {
            row_element = row_element.border_1().when(shown.mouse_navigation, |row| row.border_dashed()).border_color(border);
        }
        if let Some(keybinding) = &look.keybinding {
            row_element = row_element.child(div().ml(px(FONT_SIZE / 2.)).text_size(px(12.)).text_color(self.color("descriptionForeground")).child(keybinding.clone()));
        }
        row_element
            .on_mouse_move(cx.listener(move |workbench, _, _, cx| {
                let Some(shown) = workbench.action_widget.shown.as_mut() else { return };
                if shown.list.focused != Some(row) && shown.list.visible_item(row).focusable() {
                    shown.list.focused = Some(row);
                    shown.mouse_navigation = true;
                    cx.notify();
                }
            }))
            .on_mouse_down(MouseButton::Left, cx.listener(move |workbench, _, window, cx| {
                cx.stop_propagation();
                workbench.accept_action_widget_row(Some(row), false, window, cx);
            }))
            .into_any_element()
    }
}

/// A row's width with `width: auto`: its padding, the icon and gaps, the title and the keybinding.
fn row_width(item: &Item<usize>, looks: &[RowLook], list: &ActionList<usize>, window: &Window) -> f32 {
    let label = item.label.as_deref().unwrap_or_default();
    let padding = ROW_PADDING_LEFT + ROW_PADDING_RIGHT;
    if item.kind == ItemKind::Header {
        return padding + text_width(label, 11., FontWeight::SEMIBOLD, window);
    }
    let index = list.all.iter().position(|candidate| std::ptr::eq(candidate, item)).expect("rows come from the list");
    let keybinding = looks[index].keybinding.as_deref().map_or(0., |keybinding| GAP + FONT_SIZE / 2. + text_width(keybinding, 12., FontWeight::NORMAL, window));
    // The empty `.action-item-additional-badges` flex box still takes one more gap (measured 5px wider in 1.147).
    padding + ICON_SIZE + 2. * GAP + text_width(label, FONT_SIZE, FontWeight::NORMAL, window) + keybinding
}

fn text_width(text: &str, size: f32, weight: FontWeight, window: &Window) -> f32 {
    let font = gpui::Font { weight, ..gpui::font(UI_FONT) };
    let run = TextRun { len: text.len(), font, color: gpui::black(), background_color: None, underline: None, strikethrough: None };
    f32::from(window.text_system().shape_line(text.to_string().into(), px(size), &[run], None).width)
}