code_context_menus.rs

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