code_context_menus.rs

  1use std::cell::RefCell;
  2use std::{cell::Cell, cmp::Reverse, ops::Range, rc::Rc};
  3
  4use fuzzy::{StringMatch, StringMatchCandidate};
  5use gpui::{
  6    div, px, uniform_list, AnyElement, BackgroundExecutor, Div, FontWeight, ListSizingBehavior,
  7    Model, ScrollStrategy, SharedString, StrikethroughStyle, StyledText, UniformListScrollHandle,
  8    ViewContext, WeakView,
  9};
 10use language::Buffer;
 11use language::{CodeLabel, Documentation};
 12use lsp::LanguageServerId;
 13use multi_buffer::{Anchor, ExcerptId};
 14use ordered_float::OrderedFloat;
 15use project::{CodeAction, Completion, TaskSourceKind};
 16use task::ResolvedTask;
 17use ui::{
 18    h_flex, ActiveTheme as _, Color, FluentBuilder as _, InteractiveElement as _, IntoElement,
 19    Label, LabelCommon as _, LabelSize, ListItem, ParentElement as _, Popover,
 20    StatefulInteractiveElement as _, Styled, Toggleable as _,
 21};
 22use util::ResultExt as _;
 23use workspace::Workspace;
 24
 25use crate::{
 26    actions::{ConfirmCodeAction, ConfirmCompletion},
 27    display_map::DisplayPoint,
 28    render_parsed_markdown, split_words, styled_runs_for_code_label, CodeActionProvider,
 29    CompletionId, CompletionProvider, DisplayRow, Editor, EditorStyle, ResolvedTasks,
 30};
 31
 32pub enum CodeContextMenu {
 33    Completions(CompletionsMenu),
 34    CodeActions(CodeActionsMenu),
 35}
 36
 37impl CodeContextMenu {
 38    pub fn select_first(
 39        &mut self,
 40        provider: Option<&dyn CompletionProvider>,
 41        cx: &mut ViewContext<Editor>,
 42    ) -> bool {
 43        if self.visible() {
 44            match self {
 45                CodeContextMenu::Completions(menu) => menu.select_first(provider, cx),
 46                CodeContextMenu::CodeActions(menu) => menu.select_first(cx),
 47            }
 48            true
 49        } else {
 50            false
 51        }
 52    }
 53
 54    pub fn select_prev(
 55        &mut self,
 56        provider: Option<&dyn CompletionProvider>,
 57        cx: &mut ViewContext<Editor>,
 58    ) -> bool {
 59        if self.visible() {
 60            match self {
 61                CodeContextMenu::Completions(menu) => menu.select_prev(provider, cx),
 62                CodeContextMenu::CodeActions(menu) => menu.select_prev(cx),
 63            }
 64            true
 65        } else {
 66            false
 67        }
 68    }
 69
 70    pub fn select_next(
 71        &mut self,
 72        provider: Option<&dyn CompletionProvider>,
 73        cx: &mut ViewContext<Editor>,
 74    ) -> bool {
 75        if self.visible() {
 76            match self {
 77                CodeContextMenu::Completions(menu) => menu.select_next(provider, cx),
 78                CodeContextMenu::CodeActions(menu) => menu.select_next(cx),
 79            }
 80            true
 81        } else {
 82            false
 83        }
 84    }
 85
 86    pub fn select_last(
 87        &mut self,
 88        provider: Option<&dyn CompletionProvider>,
 89        cx: &mut ViewContext<Editor>,
 90    ) -> bool {
 91        if self.visible() {
 92            match self {
 93                CodeContextMenu::Completions(menu) => menu.select_last(provider, cx),
 94                CodeContextMenu::CodeActions(menu) => menu.select_last(cx),
 95            }
 96            true
 97        } else {
 98            false
 99        }
100    }
101
102    pub fn visible(&self) -> bool {
103        match self {
104            CodeContextMenu::Completions(menu) => menu.visible(),
105            CodeContextMenu::CodeActions(menu) => menu.visible(),
106        }
107    }
108
109    pub fn origin(&self, cursor_position: DisplayPoint) -> ContextMenuOrigin {
110        match self {
111            CodeContextMenu::Completions(menu) => menu.origin(cursor_position),
112            CodeContextMenu::CodeActions(menu) => menu.origin(cursor_position),
113        }
114    }
115    pub fn render(
116        &self,
117        style: &EditorStyle,
118        max_height_in_lines: u32,
119        workspace: Option<WeakView<Workspace>>,
120        cx: &mut ViewContext<Editor>,
121    ) -> AnyElement {
122        match self {
123            CodeContextMenu::Completions(menu) => {
124                menu.render(style, max_height_in_lines, workspace, cx)
125            }
126            CodeContextMenu::CodeActions(menu) => menu.render(style, max_height_in_lines, cx),
127        }
128    }
129}
130
131pub enum ContextMenuOrigin {
132    EditorPoint(DisplayPoint),
133    GutterIndicator(DisplayRow),
134}
135
136#[derive(Clone, Debug)]
137pub struct CompletionsMenu {
138    pub id: CompletionId,
139    sort_completions: bool,
140    pub initial_position: Anchor,
141    pub buffer: Model<Buffer>,
142    pub completions: Rc<RefCell<Box<[Completion]>>>,
143    match_candidates: Rc<[StringMatchCandidate]>,
144    pub matches: Rc<[StringMatch]>,
145    pub selected_item: usize,
146    scroll_handle: UniformListScrollHandle,
147    resolve_completions: bool,
148    pub aside_was_displayed: Cell<bool>,
149    show_completion_documentation: bool,
150}
151
152impl CompletionsMenu {
153    pub fn new(
154        id: CompletionId,
155        sort_completions: bool,
156        show_completion_documentation: bool,
157        initial_position: Anchor,
158        buffer: Model<Buffer>,
159        completions: Box<[Completion]>,
160        aside_was_displayed: bool,
161    ) -> Self {
162        let match_candidates = completions
163            .iter()
164            .enumerate()
165            .map(|(id, completion)| StringMatchCandidate::new(id, &completion.label.filter_text()))
166            .collect();
167
168        Self {
169            id,
170            sort_completions,
171            initial_position,
172            buffer,
173            show_completion_documentation,
174            completions: RefCell::new(completions).into(),
175            match_candidates,
176            matches: Vec::new().into(),
177            selected_item: 0,
178            scroll_handle: UniformListScrollHandle::new(),
179            resolve_completions: true,
180            aside_was_displayed: Cell::new(aside_was_displayed),
181        }
182    }
183
184    pub fn new_snippet_choices(
185        id: CompletionId,
186        sort_completions: bool,
187        choices: &Vec<String>,
188        selection: Range<Anchor>,
189        buffer: Model<Buffer>,
190    ) -> Self {
191        let completions = choices
192            .iter()
193            .map(|choice| Completion {
194                old_range: selection.start.text_anchor..selection.end.text_anchor,
195                new_text: choice.to_string(),
196                label: CodeLabel {
197                    text: choice.to_string(),
198                    runs: Default::default(),
199                    filter_range: Default::default(),
200                },
201                server_id: LanguageServerId(usize::MAX),
202                documentation: None,
203                lsp_completion: Default::default(),
204                confirm: None,
205            })
206            .collect();
207
208        let match_candidates = choices
209            .iter()
210            .enumerate()
211            .map(|(id, completion)| StringMatchCandidate::new(id, &completion))
212            .collect();
213        let matches = choices
214            .iter()
215            .enumerate()
216            .map(|(id, completion)| StringMatch {
217                candidate_id: id,
218                score: 1.,
219                positions: vec![],
220                string: completion.clone(),
221            })
222            .collect();
223        Self {
224            id,
225            sort_completions,
226            initial_position: selection.start,
227            buffer,
228            completions: RefCell::new(completions).into(),
229            match_candidates,
230            matches,
231            selected_item: 0,
232            scroll_handle: UniformListScrollHandle::new(),
233            resolve_completions: false,
234            aside_was_displayed: Cell::new(false),
235            show_completion_documentation: false,
236        }
237    }
238
239    fn select_first(
240        &mut self,
241        provider: Option<&dyn CompletionProvider>,
242        cx: &mut ViewContext<Editor>,
243    ) {
244        self.selected_item = 0;
245        self.scroll_handle
246            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
247        self.resolve_selected_completion(provider, cx);
248        cx.notify();
249    }
250
251    fn select_prev(
252        &mut self,
253        provider: Option<&dyn CompletionProvider>,
254        cx: &mut ViewContext<Editor>,
255    ) {
256        if self.selected_item > 0 {
257            self.selected_item -= 1;
258        } else {
259            self.selected_item = self.matches.len() - 1;
260        }
261        self.scroll_handle
262            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
263        self.resolve_selected_completion(provider, cx);
264        cx.notify();
265    }
266
267    fn select_next(
268        &mut self,
269        provider: Option<&dyn CompletionProvider>,
270        cx: &mut ViewContext<Editor>,
271    ) {
272        if self.selected_item + 1 < self.matches.len() {
273            self.selected_item += 1;
274        } else {
275            self.selected_item = 0;
276        }
277        self.scroll_handle
278            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
279        self.resolve_selected_completion(provider, cx);
280        cx.notify();
281    }
282
283    fn select_last(
284        &mut self,
285        provider: Option<&dyn CompletionProvider>,
286        cx: &mut ViewContext<Editor>,
287    ) {
288        self.selected_item = self.matches.len() - 1;
289        self.scroll_handle
290            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
291        self.resolve_selected_completion(provider, cx);
292        cx.notify();
293    }
294
295    pub fn resolve_selected_completion(
296        &mut self,
297        provider: Option<&dyn CompletionProvider>,
298        cx: &mut ViewContext<Editor>,
299    ) {
300        if !self.resolve_completions {
301            return;
302        }
303        let Some(provider) = provider else {
304            return;
305        };
306
307        let completion_index = self.matches[self.selected_item].candidate_id;
308        let resolve_task = provider.resolve_completions(
309            self.buffer.clone(),
310            vec![completion_index],
311            self.completions.clone(),
312            cx,
313        );
314
315        cx.spawn(move |editor, mut cx| async move {
316            if let Some(true) = resolve_task.await.log_err() {
317                editor.update(&mut cx, |_, cx| cx.notify()).ok();
318            }
319        })
320        .detach();
321    }
322
323    fn visible(&self) -> bool {
324        !self.matches.is_empty()
325    }
326
327    fn origin(&self, cursor_position: DisplayPoint) -> ContextMenuOrigin {
328        ContextMenuOrigin::EditorPoint(cursor_position)
329    }
330
331    fn render(
332        &self,
333        style: &EditorStyle,
334        max_height_in_lines: u32,
335        workspace: Option<WeakView<Workspace>>,
336        cx: &mut ViewContext<Editor>,
337    ) -> AnyElement {
338        let max_height = max_height_in_lines as f32 * cx.line_height();
339
340        let completions = self.completions.borrow_mut();
341        let show_completion_documentation = self.show_completion_documentation;
342        let widest_completion_ix = self
343            .matches
344            .iter()
345            .enumerate()
346            .max_by_key(|(_, mat)| {
347                let completion = &completions[mat.candidate_id];
348                let documentation = &completion.documentation;
349
350                let mut len = completion.label.text.chars().count();
351                if let Some(Documentation::SingleLine(text)) = documentation {
352                    if show_completion_documentation {
353                        len += text.chars().count();
354                    }
355                }
356
357                len
358            })
359            .map(|(ix, _)| ix);
360
361        let selected_item = self.selected_item;
362        let style = style.clone();
363
364        let multiline_docs = if show_completion_documentation {
365            let mat = &self.matches[selected_item];
366            match &completions[mat.candidate_id].documentation {
367                Some(Documentation::MultiLinePlainText(text)) => {
368                    Some(div().child(SharedString::from(text.clone())))
369                }
370                Some(Documentation::MultiLineMarkdown(parsed)) if !parsed.text.is_empty() => {
371                    Some(div().child(render_parsed_markdown(
372                        "completions_markdown",
373                        parsed,
374                        &style,
375                        workspace,
376                        cx,
377                    )))
378                }
379                Some(Documentation::Undocumented) if self.aside_was_displayed.get() => {
380                    Some(div().child("No documentation"))
381                }
382                _ => None,
383            }
384        } else {
385            None
386        };
387
388        let aside_contents = if let Some(multiline_docs) = multiline_docs {
389            Some(multiline_docs)
390        } else if self.aside_was_displayed.get() {
391            Some(div().child("Fetching documentation..."))
392        } else {
393            None
394        };
395        self.aside_was_displayed.set(aside_contents.is_some());
396
397        let aside_contents = aside_contents.map(|div| {
398            div.id("multiline_docs")
399                .max_h(max_height)
400                .flex_1()
401                .px_1p5()
402                .py_1()
403                .min_w(px(260.))
404                .max_w(px(640.))
405                .w(px(500.))
406                .overflow_y_scroll()
407                .occlude()
408        });
409
410        drop(completions);
411        let completions = self.completions.clone();
412        let matches = self.matches.clone();
413        let list = uniform_list(
414            cx.view().clone(),
415            "completions",
416            matches.len(),
417            move |_editor, range, cx| {
418                let start_ix = range.start;
419                let completions_guard = completions.borrow_mut();
420
421                matches[range]
422                    .iter()
423                    .enumerate()
424                    .map(|(ix, mat)| {
425                        let item_ix = start_ix + ix;
426                        let candidate_id = mat.candidate_id;
427                        let completion = &completions_guard[candidate_id];
428
429                        let documentation = if show_completion_documentation {
430                            &completion.documentation
431                        } else {
432                            &None
433                        };
434
435                        let filter_start = completion.label.filter_range.start;
436                        let highlights = gpui::combine_highlights(
437                            mat.ranges().map(|range| {
438                                (
439                                    filter_start + range.start..filter_start + range.end,
440                                    FontWeight::BOLD.into(),
441                                )
442                            }),
443                            styled_runs_for_code_label(&completion.label, &style.syntax).map(
444                                |(range, mut highlight)| {
445                                    // Ignore font weight for syntax highlighting, as we'll use it
446                                    // for fuzzy matches.
447                                    highlight.font_weight = None;
448
449                                    if completion.lsp_completion.deprecated.unwrap_or(false) {
450                                        highlight.strikethrough = Some(StrikethroughStyle {
451                                            thickness: 1.0.into(),
452                                            ..Default::default()
453                                        });
454                                        highlight.color = Some(cx.theme().colors().text_muted);
455                                    }
456
457                                    (range, highlight)
458                                },
459                            ),
460                        );
461                        let completion_label = StyledText::new(completion.label.text.clone())
462                            .with_highlights(&style.text, highlights);
463                        let documentation_label =
464                            if let Some(Documentation::SingleLine(text)) = documentation {
465                                if text.trim().is_empty() {
466                                    None
467                                } else {
468                                    Some(
469                                        Label::new(text.clone())
470                                            .ml_4()
471                                            .size(LabelSize::Small)
472                                            .color(Color::Muted),
473                                    )
474                                }
475                            } else {
476                                None
477                            };
478
479                        let color_swatch = completion
480                            .color()
481                            .map(|color| div().size_4().bg(color).rounded_sm());
482
483                        div().min_w(px(220.)).max_w(px(540.)).child(
484                            ListItem::new(mat.candidate_id)
485                                .inset(true)
486                                .toggle_state(item_ix == selected_item)
487                                .on_click(cx.listener(move |editor, _event, cx| {
488                                    cx.stop_propagation();
489                                    if let Some(task) = editor.confirm_completion(
490                                        &ConfirmCompletion {
491                                            item_ix: Some(item_ix),
492                                        },
493                                        cx,
494                                    ) {
495                                        task.detach_and_log_err(cx)
496                                    }
497                                }))
498                                .start_slot::<Div>(color_swatch)
499                                .child(h_flex().overflow_hidden().child(completion_label))
500                                .end_slot::<Label>(documentation_label),
501                        )
502                    })
503                    .collect()
504            },
505        )
506        .occlude()
507        .max_h(max_height_in_lines as f32 * cx.line_height())
508        .track_scroll(self.scroll_handle.clone())
509        .with_width_from_item(widest_completion_ix)
510        .with_sizing_behavior(ListSizingBehavior::Infer);
511
512        Popover::new()
513            .child(list)
514            .when_some(aside_contents, |popover, aside_contents| {
515                popover.aside(aside_contents)
516            })
517            .into_any_element()
518    }
519
520    pub async fn filter(&mut self, query: Option<&str>, executor: BackgroundExecutor) {
521        let mut matches = if let Some(query) = query {
522            fuzzy::match_strings(
523                &self.match_candidates,
524                query,
525                query.chars().any(|c| c.is_uppercase()),
526                100,
527                &Default::default(),
528                executor,
529            )
530            .await
531        } else {
532            self.match_candidates
533                .iter()
534                .enumerate()
535                .map(|(candidate_id, candidate)| StringMatch {
536                    candidate_id,
537                    score: Default::default(),
538                    positions: Default::default(),
539                    string: candidate.string.clone(),
540                })
541                .collect()
542        };
543
544        // Remove all candidates where the query's start does not match the start of any word in the candidate
545        if let Some(query) = query {
546            if let Some(query_start) = query.chars().next() {
547                matches.retain(|string_match| {
548                    split_words(&string_match.string).any(|word| {
549                        // Check that the first codepoint of the word as lowercase matches the first
550                        // codepoint of the query as lowercase
551                        word.chars()
552                            .flat_map(|codepoint| codepoint.to_lowercase())
553                            .zip(query_start.to_lowercase())
554                            .all(|(word_cp, query_cp)| word_cp == query_cp)
555                    })
556                });
557            }
558        }
559
560        let completions = self.completions.borrow_mut();
561        if self.sort_completions {
562            matches.sort_unstable_by_key(|mat| {
563                // We do want to strike a balance here between what the language server tells us
564                // to sort by (the sort_text) and what are "obvious" good matches (i.e. when you type
565                // `Creat` and there is a local variable called `CreateComponent`).
566                // So what we do is: we bucket all matches into two buckets
567                // - Strong matches
568                // - Weak matches
569                // Strong matches are the ones with a high fuzzy-matcher score (the "obvious" matches)
570                // and the Weak matches are the rest.
571                //
572                // For the strong matches, we sort by our fuzzy-finder score first and for the weak
573                // matches, we prefer language-server sort_text first.
574                //
575                // The thinking behind that: we want to show strong matches first in order of relevance(fuzzy score).
576                // Rest of the matches(weak) can be sorted as language-server expects.
577
578                #[derive(PartialEq, Eq, PartialOrd, Ord)]
579                enum MatchScore<'a> {
580                    Strong {
581                        score: Reverse<OrderedFloat<f64>>,
582                        sort_text: Option<&'a str>,
583                        sort_key: (usize, &'a str),
584                    },
585                    Weak {
586                        sort_text: Option<&'a str>,
587                        score: Reverse<OrderedFloat<f64>>,
588                        sort_key: (usize, &'a str),
589                    },
590                }
591
592                let completion = &completions[mat.candidate_id];
593                let sort_key = completion.sort_key();
594                let sort_text = completion.lsp_completion.sort_text.as_deref();
595                let score = Reverse(OrderedFloat(mat.score));
596
597                if mat.score >= 0.2 {
598                    MatchScore::Strong {
599                        score,
600                        sort_text,
601                        sort_key,
602                    }
603                } else {
604                    MatchScore::Weak {
605                        sort_text,
606                        score,
607                        sort_key,
608                    }
609                }
610            });
611        }
612        drop(completions);
613
614        self.matches = matches.into();
615        self.selected_item = 0;
616    }
617}
618
619#[derive(Clone)]
620pub struct AvailableCodeAction {
621    pub excerpt_id: ExcerptId,
622    pub action: CodeAction,
623    pub provider: Rc<dyn CodeActionProvider>,
624}
625
626#[derive(Clone)]
627pub struct CodeActionContents {
628    pub tasks: Option<Rc<ResolvedTasks>>,
629    pub actions: Option<Rc<[AvailableCodeAction]>>,
630}
631
632impl CodeActionContents {
633    fn len(&self) -> usize {
634        match (&self.tasks, &self.actions) {
635            (Some(tasks), Some(actions)) => actions.len() + tasks.templates.len(),
636            (Some(tasks), None) => tasks.templates.len(),
637            (None, Some(actions)) => actions.len(),
638            (None, None) => 0,
639        }
640    }
641
642    fn is_empty(&self) -> bool {
643        match (&self.tasks, &self.actions) {
644            (Some(tasks), Some(actions)) => actions.is_empty() && tasks.templates.is_empty(),
645            (Some(tasks), None) => tasks.templates.is_empty(),
646            (None, Some(actions)) => actions.is_empty(),
647            (None, None) => true,
648        }
649    }
650
651    fn iter(&self) -> impl Iterator<Item = CodeActionsItem> + '_ {
652        self.tasks
653            .iter()
654            .flat_map(|tasks| {
655                tasks
656                    .templates
657                    .iter()
658                    .map(|(kind, task)| CodeActionsItem::Task(kind.clone(), task.clone()))
659            })
660            .chain(self.actions.iter().flat_map(|actions| {
661                actions.iter().map(|available| CodeActionsItem::CodeAction {
662                    excerpt_id: available.excerpt_id,
663                    action: available.action.clone(),
664                    provider: available.provider.clone(),
665                })
666            }))
667    }
668
669    pub fn get(&self, index: usize) -> Option<CodeActionsItem> {
670        match (&self.tasks, &self.actions) {
671            (Some(tasks), Some(actions)) => {
672                if index < tasks.templates.len() {
673                    tasks
674                        .templates
675                        .get(index)
676                        .cloned()
677                        .map(|(kind, task)| CodeActionsItem::Task(kind, task))
678                } else {
679                    actions.get(index - tasks.templates.len()).map(|available| {
680                        CodeActionsItem::CodeAction {
681                            excerpt_id: available.excerpt_id,
682                            action: available.action.clone(),
683                            provider: available.provider.clone(),
684                        }
685                    })
686                }
687            }
688            (Some(tasks), None) => tasks
689                .templates
690                .get(index)
691                .cloned()
692                .map(|(kind, task)| CodeActionsItem::Task(kind, task)),
693            (None, Some(actions)) => {
694                actions
695                    .get(index)
696                    .map(|available| CodeActionsItem::CodeAction {
697                        excerpt_id: available.excerpt_id,
698                        action: available.action.clone(),
699                        provider: available.provider.clone(),
700                    })
701            }
702            (None, None) => None,
703        }
704    }
705}
706
707#[allow(clippy::large_enum_variant)]
708#[derive(Clone)]
709pub enum CodeActionsItem {
710    Task(TaskSourceKind, ResolvedTask),
711    CodeAction {
712        excerpt_id: ExcerptId,
713        action: CodeAction,
714        provider: Rc<dyn CodeActionProvider>,
715    },
716}
717
718impl CodeActionsItem {
719    fn as_task(&self) -> Option<&ResolvedTask> {
720        let Self::Task(_, task) = self else {
721            return None;
722        };
723        Some(task)
724    }
725
726    fn as_code_action(&self) -> Option<&CodeAction> {
727        let Self::CodeAction { action, .. } = self else {
728            return None;
729        };
730        Some(action)
731    }
732
733    pub fn label(&self) -> String {
734        match self {
735            Self::CodeAction { action, .. } => action.lsp_action.title.clone(),
736            Self::Task(_, task) => task.resolved_label.clone(),
737        }
738    }
739}
740
741pub struct CodeActionsMenu {
742    pub actions: CodeActionContents,
743    pub buffer: Model<Buffer>,
744    pub selected_item: usize,
745    pub scroll_handle: UniformListScrollHandle,
746    pub deployed_from_indicator: Option<DisplayRow>,
747}
748
749impl CodeActionsMenu {
750    fn select_first(&mut self, cx: &mut ViewContext<Editor>) {
751        self.selected_item = 0;
752        self.scroll_handle
753            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
754        cx.notify()
755    }
756
757    fn select_prev(&mut self, cx: &mut ViewContext<Editor>) {
758        if self.selected_item > 0 {
759            self.selected_item -= 1;
760        } else {
761            self.selected_item = self.actions.len() - 1;
762        }
763        self.scroll_handle
764            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
765        cx.notify();
766    }
767
768    fn select_next(&mut self, cx: &mut ViewContext<Editor>) {
769        if self.selected_item + 1 < self.actions.len() {
770            self.selected_item += 1;
771        } else {
772            self.selected_item = 0;
773        }
774        self.scroll_handle
775            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
776        cx.notify();
777    }
778
779    fn select_last(&mut self, cx: &mut ViewContext<Editor>) {
780        self.selected_item = self.actions.len() - 1;
781        self.scroll_handle
782            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
783        cx.notify()
784    }
785
786    fn visible(&self) -> bool {
787        !self.actions.is_empty()
788    }
789
790    fn origin(&self, cursor_position: DisplayPoint) -> ContextMenuOrigin {
791        if let Some(row) = self.deployed_from_indicator {
792            ContextMenuOrigin::GutterIndicator(row)
793        } else {
794            ContextMenuOrigin::EditorPoint(cursor_position)
795        }
796    }
797
798    fn render(
799        &self,
800        _style: &EditorStyle,
801        max_height_in_lines: u32,
802        cx: &mut ViewContext<Editor>,
803    ) -> AnyElement {
804        let actions = self.actions.clone();
805        let selected_item = self.selected_item;
806        let list = uniform_list(
807            cx.view().clone(),
808            "code_actions_menu",
809            self.actions.len(),
810            move |_this, range, cx| {
811                actions
812                    .iter()
813                    .skip(range.start)
814                    .take(range.end - range.start)
815                    .enumerate()
816                    .map(|(ix, action)| {
817                        let item_ix = range.start + ix;
818                        let selected = item_ix == selected_item;
819                        let colors = cx.theme().colors();
820                        div().min_w(px(220.)).max_w(px(540.)).child(
821                            ListItem::new(item_ix)
822                                .inset(true)
823                                .toggle_state(selected)
824                                .when_some(action.as_code_action(), |this, action| {
825                                    this.on_click(cx.listener(move |editor, _, cx| {
826                                        cx.stop_propagation();
827                                        if let Some(task) = editor.confirm_code_action(
828                                            &ConfirmCodeAction {
829                                                item_ix: Some(item_ix),
830                                            },
831                                            cx,
832                                        ) {
833                                            task.detach_and_log_err(cx)
834                                        }
835                                    }))
836                                    .child(
837                                        h_flex()
838                                            .overflow_hidden()
839                                            .child(
840                                                // TASK: It would be good to make lsp_action.title a SharedString to avoid allocating here.
841                                                action.lsp_action.title.replace("\n", ""),
842                                            )
843                                            .when(selected, |this| {
844                                                this.text_color(colors.text_accent)
845                                            }),
846                                    )
847                                })
848                                .when_some(action.as_task(), |this, task| {
849                                    this.on_click(cx.listener(move |editor, _, cx| {
850                                        cx.stop_propagation();
851                                        if let Some(task) = editor.confirm_code_action(
852                                            &ConfirmCodeAction {
853                                                item_ix: Some(item_ix),
854                                            },
855                                            cx,
856                                        ) {
857                                            task.detach_and_log_err(cx)
858                                        }
859                                    }))
860                                    .child(
861                                        h_flex()
862                                            .overflow_hidden()
863                                            .child(task.resolved_label.replace("\n", ""))
864                                            .when(selected, |this| {
865                                                this.text_color(colors.text_accent)
866                                            }),
867                                    )
868                                }),
869                        )
870                    })
871                    .collect()
872            },
873        )
874        .occlude()
875        .max_h(max_height_in_lines as f32 * cx.line_height())
876        .track_scroll(self.scroll_handle.clone())
877        .with_width_from_item(
878            self.actions
879                .iter()
880                .enumerate()
881                .max_by_key(|(_, action)| match action {
882                    CodeActionsItem::Task(_, task) => task.resolved_label.chars().count(),
883                    CodeActionsItem::CodeAction { action, .. } => {
884                        action.lsp_action.title.chars().count()
885                    }
886                })
887                .map(|(ix, _)| ix),
888        )
889        .with_sizing_behavior(ListSizingBehavior::Infer);
890
891        Popover::new().child(list).into_any_element()
892    }
893}