use std::rc::Rc; use gpui::{ Action, AnyElement, App, AppContext, Context, DismissEvent, Empty, Entity, EventEmitter, Half as _, HighlightStyle, InteractiveElement as _, IntoElement, ParentElement, Pixels, Point, Render, RenderOnce, SharedString, Styled, StyledText, Subscription, WeakEntity, Window, deferred, div, prelude::FluentBuilder, px, relative, }; use lsp_types::CompletionItem; const MAX_MENU_HEIGHT: Pixels = px(240.); const POPOVER_GAP: Pixels = px(4.); use crate::{ ActiveTheme, IndexPath, Selectable, actions, h_flex, input::{ self, EditorState, popovers::{editor_popover, render_markdown}, }, label::Label, list::{List, ListDelegate, ListEvent, ListState}, }; struct ContextMenuDelegate { query: SharedString, menu: Entity, items: Vec>, selected_ix: usize, } impl ContextMenuDelegate { fn set_items(&mut self, items: Vec) { self.items = items.into_iter().map(Rc::new).collect(); self.selected_ix = 0; } fn selected_item(&self) -> Option<&Rc> { self.items.get(self.selected_ix) } } #[derive(IntoElement)] struct CompletionMenuItem { ix: usize, item: Rc, children: Vec, selected: bool, highlight_prefix: SharedString, } impl CompletionMenuItem { fn new(ix: usize, item: Rc) -> Self { Self { ix, item, children: vec![], selected: false, highlight_prefix: "".into(), } } fn highlight_prefix(mut self, s: impl Into) -> Self { self.highlight_prefix = s.into(); self } } impl Selectable for CompletionMenuItem { fn selected(mut self, selected: bool) -> Self { self.selected = selected; self } fn is_selected(&self) -> bool { self.selected } } impl ParentElement for CompletionMenuItem { fn extend(&mut self, elements: impl IntoIterator) { self.children.extend(elements); } } impl RenderOnce for CompletionMenuItem { fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { let item = self.item; let deprecated = item.deprecated.unwrap_or(false); let matched_len = item .filter_text .as_ref() .map(|s| s.len()) .unwrap_or(self.highlight_prefix.len()) .min(item.label.len()); let highlights = vec![( 0..matched_len, HighlightStyle { color: Some(cx.theme().blue), ..Default::default() }, )]; h_flex() .id(self.ix) .gap_2() .p_1() .text_xs() .line_height(relative(1.)) .rounded(cx.theme().radius.half()) .when(item.deprecated.unwrap_or(false), |this| this.line_through()) .hover(|this| this.bg(cx.theme().accent.opacity(0.8))) .when(self.selected, |this| { this.bg(cx.theme().tokens.accent) .text_color(cx.theme().accent_foreground) }) .child(div().child(StyledText::new(item.label.clone()).with_highlights(highlights))) .when(item.detail.is_some(), |this| { this.child( Label::new(item.detail.as_deref().unwrap_or("").to_string()) .text_color(cx.theme().muted_foreground) .when(deprecated, |this| this.line_through()) .italic(), ) }) .children(self.children) } } impl EventEmitter for ContextMenuDelegate {} impl ListDelegate for ContextMenuDelegate { type Item = CompletionMenuItem; fn items_count(&self, _: usize, _: &gpui::App) -> usize { self.items.len() } fn render_item( &mut self, ix: crate::IndexPath, _: &mut Window, _: &mut Context>, ) -> Option { let item = self.items.get(ix.row)?; Some(CompletionMenuItem::new(ix.row, item.clone()).highlight_prefix(self.query.clone())) } fn set_selected_index( &mut self, ix: Option, _: &mut Window, cx: &mut Context>, ) { self.selected_ix = ix.map(|i| i.row).unwrap_or(0); cx.notify(); } fn confirm(&mut self, _: bool, window: &mut Window, cx: &mut Context>) { let Some(item) = self.selected_item() else { return; }; self.menu.update(cx, |this, cx| { this.select_item(&item, window, cx); }); } } /// A context menu for code completions and code actions. pub struct CompletionMenu { offset: usize, editor: WeakEntity, list: Entity>, open: bool, /// The offset of the first character that triggered the completion. pub(crate) trigger_start_offset: Option, query: SharedString, _subscriptions: Vec, } impl CompletionMenu { /// Creates a new `CompletionMenu` with the given offset and completion items. /// /// NOTE: This element should not call from EditorState::new, unless that will stack overflow. pub(crate) fn new( editor: Entity, window: &mut Window, cx: &mut App, ) -> Entity { cx.new(|cx| { let view = cx.entity(); let menu = ContextMenuDelegate { query: SharedString::default(), menu: view, items: vec![], selected_ix: 0, }; let list = cx.new(|cx| ListState::new(menu, window, cx)); let _subscriptions = vec![ cx.subscribe(&list, |this: &mut Self, _, ev: &ListEvent, cx| { match ev { ListEvent::Confirm(_) => { this.dismiss(cx); } _ => {} } cx.notify(); }), ]; Self { offset: 0, editor: editor.downgrade(), list, open: false, trigger_start_offset: None, query: SharedString::default(), _subscriptions, } }) } fn select_item(&mut self, item: &CompletionItem, window: &mut Window, cx: &mut Context) { let item = item.clone(); let range = self.trigger_start_offset.unwrap_or(self.offset)..self.offset; let editor = self.editor.clone(); cx.spawn_in(window, async move |_, cx| { editor.update_in(cx, |editor, window, cx| { editor.insert_completion(&item, range, window, cx); }) }) .detach(); self.dismiss(cx); } pub(crate) fn handle_action( &mut self, action: Box, window: &mut Window, cx: &mut Context, ) -> bool { if !self.open { return false; } cx.propagate(); if input::Enter::is_primary(&*action) { self.on_action_enter(window, cx); } else if action.partial_eq(&input::Escape) { self.on_action_escape(window, cx); } else if action.partial_eq(&input::MoveUp) { self.on_action_up(window, cx); } else if action.partial_eq(&input::MoveDown) { self.on_action_down(window, cx); } else { return false; } true } fn on_action_enter(&mut self, window: &mut Window, cx: &mut Context) { let Some(item) = self.list.read(cx).delegate().selected_item().cloned() else { return; }; self.select_item(&item, window, cx); } fn on_action_escape(&mut self, _: &mut Window, cx: &mut Context) { self.dismiss(cx); } fn on_action_up(&mut self, window: &mut Window, cx: &mut Context) { self.list.update(cx, |this, cx| { this.on_action_select_prev(&actions::SelectUp, window, cx) }); } fn on_action_down(&mut self, window: &mut Window, cx: &mut Context) { self.list.update(cx, |this, cx| { this.on_action_select_next(&actions::SelectDown, window, cx) }); } /// Synchronize a hidden editor menu without dismissing a newer response. pub(crate) fn hide(&mut self, cx: &mut Context) { self.open = false; self.trigger_start_offset = None; cx.notify(); } /// Dismiss the menu in response to a user action. fn dismiss(&mut self, cx: &mut Context) { self.hide(cx); let editor = self.editor.clone(); cx.spawn(async move |_, cx| { let _ = editor.update(cx, |editor, cx| editor.dismiss_completion_overlay(cx)); }) .detach(); } /// Sets the trigger start offset if it is not already set. pub(crate) fn update_query(&mut self, start_offset: usize, query: impl Into) { if self.trigger_start_offset.is_none() { self.trigger_start_offset = Some(start_offset); } self.query = query.into(); } pub(crate) fn show( &mut self, offset: usize, items: impl Into>, window: &mut Window, cx: &mut Context, ) { let items = items.into(); self.offset = offset; self.open = true; self.list.update(cx, |this, cx| { let longest_ix = items .iter() .enumerate() .max_by_key(|(_, item)| { item.label.len() + item.detail.as_ref().map(|d| d.len()).unwrap_or(0) }) .map(|(ix, _)| ix) .unwrap_or(0); this.delegate_mut().query = self.query.clone(); this.delegate_mut().set_items(items); this.set_selected_index(Some(IndexPath::new(0)), window, cx); this.set_item_to_measure_index(IndexPath::new(longest_ix), window, cx); }); cx.notify(); } fn origin(&self, cx: &App) -> Option> { let editor = self.editor.upgrade()?; let editor = editor.read(cx); let Some((cursor_bounds, line_height)) = editor.cursor_layout() else { return None; }; let cursor_origin = cursor_bounds.origin; let scroll_origin = editor.scroll_offset(); Some( scroll_origin + cursor_origin - editor.input_bounds().origin + Point::new(-px(4.), line_height + px(4.)), ) } } impl Render for CompletionMenu { fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { if !self.open { return Empty.into_any_element(); } if self.list.read(cx).delegate().items.is_empty() { self.open = false; return Empty.into_any_element(); } let Some(pos) = self.origin(cx) else { return Empty.into_any_element(); }; let selected_documentation = self .list .read(cx) .delegate() .selected_item() .and_then(|item| item.documentation.clone()); let Some(editor) = self.editor.upgrade() else { return Empty.into_any_element(); }; let configured_max = editor.read(cx).lsp().completion_menu.max_width; let max_width = configured_max.min(window.bounds().size.width - pos.x); let abs_pos = editor.read(cx).input_bounds().origin + pos; let vertical_layout = abs_pos.x + configured_max + POPOVER_GAP + configured_max + POPOVER_GAP > window.bounds().size.width; deferred( div() .absolute() .left(pos.x) .top(pos.y) .flex() .flex_row() .gap(POPOVER_GAP) .items_start() .when(vertical_layout, |this| this.flex_col()) .child( editor_popover("completion-menu", window, cx) .max_w(max_width) .min_w(px(120.)) .child(List::new(&self.list).max_h(MAX_MENU_HEIGHT)), ) .when_some(selected_documentation, |this, documentation| { let mut doc = match documentation { lsp_types::Documentation::String(s) => s.clone(), lsp_types::Documentation::MarkupContent(mc) => mc.value.clone(), }; if vertical_layout { doc = doc.split("\n").next().unwrap_or_default().to_string(); } this.child( div().child( editor_popover("completion-menu", window, cx) .w(configured_max) .px_2() .child(render_markdown("doc", doc, window, cx)), ), ) }) .on_mouse_down_out(cx.listener(|this, _, _, cx| { this.dismiss(cx); })), ) .into_any_element() } } #[cfg(test)] mod tests { use super::*; #[gpui::test] fn hiding_an_old_menu_does_not_dismiss_a_new_completion_response( cx: &mut gpui::TestAppContext, ) { cx.update(crate::init); let window = cx.add_empty_window(); let (editor, menu) = window.update(|window, cx| { let editor = cx.new(|cx| EditorState::new(window, cx).default_value("co")); let menu = CompletionMenu::new(editor.clone(), window, cx); let old_items = vec![CompletionItem { label: "const".into(), ..Default::default() }]; editor.update(cx, |editor, cx| { editor.present_completion_items(0, "c", old_items.clone(), cx); }); menu.update(cx, |menu, cx| { menu.update_query(0, "c"); menu.show(1, old_items, window, cx); // The overlay observes the old request being hidden while a // replacement request is pending. Its deferred dismissal must // not act on the replacement response that arrives next. menu.hide(cx); }); editor.update(cx, |editor, cx| { editor.present_completion_items( 0, "co", vec![CompletionItem { label: "const".into(), ..Default::default() }], cx, ); }); (editor, menu) }); cx.run_until_parked(); cx.read(|cx| { let completion = editor.read(cx).completion_menu_state(); assert!(completion.open, "an old menu dismissed the new response"); assert_eq!(completion.query, "co"); assert_eq!(completion.items.len(), 1); assert_eq!(completion.items[0].label, "const"); }); drop(menu); } }