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                resolved: true,
221            })
222            .collect();
223
224        let match_candidates = choices
225            .iter()
226            .enumerate()
227            .map(|(id, completion)| StringMatchCandidate::new(id, &completion))
228            .collect();
229        let entries = choices
230            .iter()
231            .enumerate()
232            .map(|(id, completion)| {
233                CompletionEntry::Match(StringMatch {
234                    candidate_id: id,
235                    score: 1.,
236                    positions: vec![],
237                    string: completion.clone(),
238                })
239            })
240            .collect();
241        Self {
242            id,
243            sort_completions,
244            initial_position: selection.start,
245            buffer,
246            completions: RefCell::new(completions).into(),
247            match_candidates,
248            entries,
249            selected_item: 0,
250            scroll_handle: UniformListScrollHandle::new(),
251            resolve_completions: false,
252            show_completion_documentation: false,
253        }
254    }
255
256    fn select_first(
257        &mut self,
258        provider: Option<&dyn CompletionProvider>,
259        cx: &mut ViewContext<Editor>,
260    ) {
261        self.selected_item = 0;
262        self.scroll_handle
263            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
264        self.resolve_selected_completion(provider, cx);
265        cx.notify();
266    }
267
268    fn select_prev(
269        &mut self,
270        provider: Option<&dyn CompletionProvider>,
271        cx: &mut ViewContext<Editor>,
272    ) {
273        if self.selected_item > 0 {
274            self.selected_item -= 1;
275        } else {
276            self.selected_item = self.entries.len() - 1;
277        }
278        self.scroll_handle
279            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
280        self.resolve_selected_completion(provider, cx);
281        cx.notify();
282    }
283
284    fn select_next(
285        &mut self,
286        provider: Option<&dyn CompletionProvider>,
287        cx: &mut ViewContext<Editor>,
288    ) {
289        if self.selected_item + 1 < self.entries.len() {
290            self.selected_item += 1;
291        } else {
292            self.selected_item = 0;
293        }
294        self.scroll_handle
295            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
296        self.resolve_selected_completion(provider, cx);
297        cx.notify();
298    }
299
300    fn select_last(
301        &mut self,
302        provider: Option<&dyn CompletionProvider>,
303        cx: &mut ViewContext<Editor>,
304    ) {
305        self.selected_item = self.entries.len() - 1;
306        self.scroll_handle
307            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
308        self.resolve_selected_completion(provider, cx);
309        cx.notify();
310    }
311
312    pub fn show_inline_completion_hint(&mut self, hint: InlineCompletionMenuHint) {
313        let hint = CompletionEntry::InlineCompletionHint(hint);
314
315        self.entries = match self.entries.first() {
316            Some(CompletionEntry::InlineCompletionHint { .. }) => {
317                let mut entries = Vec::from(&*self.entries);
318                entries[0] = hint;
319                entries
320            }
321            _ => {
322                let mut entries = Vec::with_capacity(self.entries.len() + 1);
323                entries.push(hint);
324                entries.extend_from_slice(&self.entries);
325                entries
326            }
327        }
328        .into();
329        self.selected_item = 0;
330    }
331
332    pub fn resolve_selected_completion(
333        &mut self,
334        provider: Option<&dyn CompletionProvider>,
335        cx: &mut ViewContext<Editor>,
336    ) {
337        if !self.resolve_completions {
338            return;
339        }
340        let Some(provider) = provider else {
341            return;
342        };
343
344        match &self.entries[self.selected_item] {
345            CompletionEntry::Match(entry) => {
346                let completion_index = entry.candidate_id;
347                let resolve_task = provider.resolve_completions(
348                    self.buffer.clone(),
349                    vec![completion_index],
350                    self.completions.clone(),
351                    cx,
352                );
353
354                cx.spawn(move |editor, mut cx| async move {
355                    if let Some(true) = resolve_task.await.log_err() {
356                        editor.update(&mut cx, |_, cx| cx.notify()).ok();
357                    }
358                })
359                .detach();
360            }
361            CompletionEntry::InlineCompletionHint { .. } => {}
362        }
363    }
364
365    pub fn visible(&self) -> bool {
366        !self.entries.is_empty()
367    }
368
369    fn origin(&self, cursor_position: DisplayPoint) -> ContextMenuOrigin {
370        ContextMenuOrigin::EditorPoint(cursor_position)
371    }
372
373    fn render(
374        &self,
375        style: &EditorStyle,
376        max_height_in_lines: u32,
377        cx: &mut ViewContext<Editor>,
378    ) -> AnyElement {
379        let completions = self.completions.borrow_mut();
380        let show_completion_documentation = self.show_completion_documentation;
381        let widest_completion_ix = self
382            .entries
383            .iter()
384            .enumerate()
385            .max_by_key(|(_, mat)| match mat {
386                CompletionEntry::Match(mat) => {
387                    let completion = &completions[mat.candidate_id];
388                    let documentation = &completion.documentation;
389
390                    let mut len = completion.label.text.chars().count();
391                    if let Some(Documentation::SingleLine(text)) = documentation {
392                        if show_completion_documentation {
393                            len += text.chars().count();
394                        }
395                    }
396
397                    len
398                }
399                CompletionEntry::InlineCompletionHint(InlineCompletionMenuHint {
400                    provider_name,
401                    ..
402                }) => provider_name.len(),
403            })
404            .map(|(ix, _)| ix);
405        drop(completions);
406
407        let selected_item = self.selected_item;
408        let completions = self.completions.clone();
409        let matches = self.entries.clone();
410        let style = style.clone();
411        let list = uniform_list(
412            cx.view().clone(),
413            "completions",
414            matches.len(),
415            move |_editor, range, cx| {
416                let start_ix = range.start;
417                let completions_guard = completions.borrow_mut();
418
419                matches[range]
420                    .iter()
421                    .enumerate()
422                    .map(|(ix, mat)| {
423                        let item_ix = start_ix + ix;
424                        match mat {
425                            CompletionEntry::Match(mat) => {
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)
444                                        .map(|(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                                            {
451                                                highlight.strikethrough =
452                                                    Some(StrikethroughStyle {
453                                                        thickness: 1.0.into(),
454                                                        ..Default::default()
455                                                    });
456                                                highlight.color =
457                                                    Some(cx.theme().colors().text_muted);
458                                            }
459
460                                            (range, highlight)
461                                        }),
462                                );
463
464                                let completion_label =
465                                    StyledText::new(completion.label.text.clone())
466                                        .with_highlights(&style.text, highlights);
467                                let documentation_label =
468                                    if let Some(Documentation::SingleLine(text)) = documentation {
469                                        if text.trim().is_empty() {
470                                            None
471                                        } else {
472                                            Some(
473                                                Label::new(text.clone())
474                                                    .ml_4()
475                                                    .size(LabelSize::Small)
476                                                    .color(Color::Muted),
477                                            )
478                                        }
479                                    } else {
480                                        None
481                                    };
482
483                                let color_swatch = completion
484                                    .color()
485                                    .map(|color| div().size_4().bg(color).rounded_sm());
486
487                                div().min_w(px(220.)).max_w(px(540.)).child(
488                                    ListItem::new(mat.candidate_id)
489                                        .inset(true)
490                                        .toggle_state(item_ix == selected_item)
491                                        .on_click(cx.listener(move |editor, _event, cx| {
492                                            cx.stop_propagation();
493                                            if let Some(task) = editor.confirm_completion(
494                                                &ConfirmCompletion {
495                                                    item_ix: Some(item_ix),
496                                                },
497                                                cx,
498                                            ) {
499                                                task.detach_and_log_err(cx)
500                                            }
501                                        }))
502                                        .start_slot::<Div>(color_swatch)
503                                        .child(h_flex().overflow_hidden().child(completion_label))
504                                        .end_slot::<Label>(documentation_label),
505                                )
506                            }
507                            CompletionEntry::InlineCompletionHint(InlineCompletionMenuHint {
508                                provider_name,
509                                ..
510                            }) => div().min_w(px(250.)).max_w(px(500.)).child(
511                                ListItem::new("inline-completion")
512                                    .inset(true)
513                                    .toggle_state(item_ix == selected_item)
514                                    .start_slot(Icon::new(IconName::ZedPredict))
515                                    .child(
516                                        StyledText::new(format!(
517                                            "{} Completion",
518                                            SharedString::new_static(provider_name)
519                                        ))
520                                        .with_highlights(&style.text, None),
521                                    )
522                                    .on_click(cx.listener(move |editor, _event, cx| {
523                                        cx.stop_propagation();
524                                        editor.accept_inline_completion(
525                                            &AcceptInlineCompletion {},
526                                            cx,
527                                        );
528                                    })),
529                            ),
530                        }
531                    })
532                    .collect()
533            },
534        )
535        .occlude()
536        .max_h(max_height_in_lines as f32 * cx.line_height())
537        .track_scroll(self.scroll_handle.clone())
538        .with_width_from_item(widest_completion_ix)
539        .with_sizing_behavior(ListSizingBehavior::Infer);
540
541        Popover::new().child(list).into_any_element()
542    }
543
544    fn render_aside(
545        &self,
546        style: &EditorStyle,
547        max_height: Pixels,
548        workspace: Option<WeakView<Workspace>>,
549        cx: &mut ViewContext<Editor>,
550    ) -> Option<AnyElement> {
551        if !self.show_completion_documentation {
552            return None;
553        }
554
555        let multiline_docs = match &self.entries[self.selected_item] {
556            CompletionEntry::Match(mat) => {
557                match self.completions.borrow_mut()[mat.candidate_id]
558                    .documentation
559                    .as_ref()?
560                {
561                    Documentation::MultiLinePlainText(text) => {
562                        div().child(SharedString::from(text.clone()))
563                    }
564                    Documentation::MultiLineMarkdown(parsed) if !parsed.text.is_empty() => div()
565                        .child(render_parsed_markdown(
566                            "completions_markdown",
567                            parsed,
568                            &style,
569                            workspace,
570                            cx,
571                        )),
572                    Documentation::MultiLineMarkdown(_) => return None,
573                    Documentation::SingleLine(_) => return None,
574                    Documentation::Undocumented => return None,
575                }
576            }
577            CompletionEntry::InlineCompletionHint(hint) => match &hint.text {
578                InlineCompletionText::Edit { text, highlights } => div()
579                    .my_1()
580                    .rounded(px(6.))
581                    .bg(cx.theme().colors().editor_background)
582                    .border_1()
583                    .border_color(cx.theme().colors().border_variant)
584                    .child(
585                        gpui::StyledText::new(text.clone())
586                            .with_highlights(&style.text, highlights.clone()),
587                    ),
588                InlineCompletionText::Move(text) => div().child(text.clone()),
589            },
590        };
591
592        Some(
593            Popover::new()
594                .child(
595                    multiline_docs
596                        .id("multiline_docs")
597                        .max_h(max_height)
598                        .px_0p5()
599                        .min_w(px(260.))
600                        .max_w(MAX_COMPLETIONS_ASIDE_WIDTH)
601                        .overflow_y_scroll()
602                        .occlude(),
603                )
604                .into_any_element(),
605        )
606    }
607
608    pub async fn filter(&mut self, query: Option<&str>, executor: BackgroundExecutor) {
609        let mut matches = if let Some(query) = query {
610            fuzzy::match_strings(
611                &self.match_candidates,
612                query,
613                query.chars().any(|c| c.is_uppercase()),
614                100,
615                &Default::default(),
616                executor,
617            )
618            .await
619        } else {
620            self.match_candidates
621                .iter()
622                .enumerate()
623                .map(|(candidate_id, candidate)| StringMatch {
624                    candidate_id,
625                    score: Default::default(),
626                    positions: Default::default(),
627                    string: candidate.string.clone(),
628                })
629                .collect()
630        };
631
632        // Remove all candidates where the query's start does not match the start of any word in the candidate
633        if let Some(query) = query {
634            if let Some(query_start) = query.chars().next() {
635                matches.retain(|string_match| {
636                    split_words(&string_match.string).any(|word| {
637                        // Check that the first codepoint of the word as lowercase matches the first
638                        // codepoint of the query as lowercase
639                        word.chars()
640                            .flat_map(|codepoint| codepoint.to_lowercase())
641                            .zip(query_start.to_lowercase())
642                            .all(|(word_cp, query_cp)| word_cp == query_cp)
643                    })
644                });
645            }
646        }
647
648        let completions = self.completions.borrow_mut();
649        if self.sort_completions {
650            matches.sort_unstable_by_key(|mat| {
651                // We do want to strike a balance here between what the language server tells us
652                // to sort by (the sort_text) and what are "obvious" good matches (i.e. when you type
653                // `Creat` and there is a local variable called `CreateComponent`).
654                // So what we do is: we bucket all matches into two buckets
655                // - Strong matches
656                // - Weak matches
657                // Strong matches are the ones with a high fuzzy-matcher score (the "obvious" matches)
658                // and the Weak matches are the rest.
659                //
660                // For the strong matches, we sort by our fuzzy-finder score first and for the weak
661                // matches, we prefer language-server sort_text first.
662                //
663                // The thinking behind that: we want to show strong matches first in order of relevance(fuzzy score).
664                // Rest of the matches(weak) can be sorted as language-server expects.
665
666                #[derive(PartialEq, Eq, PartialOrd, Ord)]
667                enum MatchScore<'a> {
668                    Strong {
669                        score: Reverse<OrderedFloat<f64>>,
670                        sort_text: Option<&'a str>,
671                        sort_key: (usize, &'a str),
672                    },
673                    Weak {
674                        sort_text: Option<&'a str>,
675                        score: Reverse<OrderedFloat<f64>>,
676                        sort_key: (usize, &'a str),
677                    },
678                }
679
680                let completion = &completions[mat.candidate_id];
681                let sort_key = completion.sort_key();
682                let sort_text = completion.lsp_completion.sort_text.as_deref();
683                let score = Reverse(OrderedFloat(mat.score));
684
685                if mat.score >= 0.2 {
686                    MatchScore::Strong {
687                        score,
688                        sort_text,
689                        sort_key,
690                    }
691                } else {
692                    MatchScore::Weak {
693                        sort_text,
694                        score,
695                        sort_key,
696                    }
697                }
698            });
699        }
700        drop(completions);
701
702        let mut new_entries: Vec<_> = matches.into_iter().map(CompletionEntry::Match).collect();
703        if let Some(CompletionEntry::InlineCompletionHint(hint)) = self.entries.first() {
704            new_entries.insert(0, CompletionEntry::InlineCompletionHint(hint.clone()));
705        }
706
707        self.entries = new_entries.into();
708        self.selected_item = 0;
709    }
710}
711
712#[derive(Clone)]
713pub struct AvailableCodeAction {
714    pub excerpt_id: ExcerptId,
715    pub action: CodeAction,
716    pub provider: Rc<dyn CodeActionProvider>,
717}
718
719#[derive(Clone)]
720pub struct CodeActionContents {
721    pub tasks: Option<Rc<ResolvedTasks>>,
722    pub actions: Option<Rc<[AvailableCodeAction]>>,
723}
724
725impl CodeActionContents {
726    fn len(&self) -> usize {
727        match (&self.tasks, &self.actions) {
728            (Some(tasks), Some(actions)) => actions.len() + tasks.templates.len(),
729            (Some(tasks), None) => tasks.templates.len(),
730            (None, Some(actions)) => actions.len(),
731            (None, None) => 0,
732        }
733    }
734
735    fn is_empty(&self) -> bool {
736        match (&self.tasks, &self.actions) {
737            (Some(tasks), Some(actions)) => actions.is_empty() && tasks.templates.is_empty(),
738            (Some(tasks), None) => tasks.templates.is_empty(),
739            (None, Some(actions)) => actions.is_empty(),
740            (None, None) => true,
741        }
742    }
743
744    fn iter(&self) -> impl Iterator<Item = CodeActionsItem> + '_ {
745        self.tasks
746            .iter()
747            .flat_map(|tasks| {
748                tasks
749                    .templates
750                    .iter()
751                    .map(|(kind, task)| CodeActionsItem::Task(kind.clone(), task.clone()))
752            })
753            .chain(self.actions.iter().flat_map(|actions| {
754                actions.iter().map(|available| CodeActionsItem::CodeAction {
755                    excerpt_id: available.excerpt_id,
756                    action: available.action.clone(),
757                    provider: available.provider.clone(),
758                })
759            }))
760    }
761
762    pub fn get(&self, index: usize) -> Option<CodeActionsItem> {
763        match (&self.tasks, &self.actions) {
764            (Some(tasks), Some(actions)) => {
765                if index < tasks.templates.len() {
766                    tasks
767                        .templates
768                        .get(index)
769                        .cloned()
770                        .map(|(kind, task)| CodeActionsItem::Task(kind, task))
771                } else {
772                    actions.get(index - tasks.templates.len()).map(|available| {
773                        CodeActionsItem::CodeAction {
774                            excerpt_id: available.excerpt_id,
775                            action: available.action.clone(),
776                            provider: available.provider.clone(),
777                        }
778                    })
779                }
780            }
781            (Some(tasks), None) => tasks
782                .templates
783                .get(index)
784                .cloned()
785                .map(|(kind, task)| CodeActionsItem::Task(kind, task)),
786            (None, Some(actions)) => {
787                actions
788                    .get(index)
789                    .map(|available| CodeActionsItem::CodeAction {
790                        excerpt_id: available.excerpt_id,
791                        action: available.action.clone(),
792                        provider: available.provider.clone(),
793                    })
794            }
795            (None, None) => None,
796        }
797    }
798}
799
800#[allow(clippy::large_enum_variant)]
801#[derive(Clone)]
802pub enum CodeActionsItem {
803    Task(TaskSourceKind, ResolvedTask),
804    CodeAction {
805        excerpt_id: ExcerptId,
806        action: CodeAction,
807        provider: Rc<dyn CodeActionProvider>,
808    },
809}
810
811impl CodeActionsItem {
812    fn as_task(&self) -> Option<&ResolvedTask> {
813        let Self::Task(_, task) = self else {
814            return None;
815        };
816        Some(task)
817    }
818
819    fn as_code_action(&self) -> Option<&CodeAction> {
820        let Self::CodeAction { action, .. } = self else {
821            return None;
822        };
823        Some(action)
824    }
825
826    pub fn label(&self) -> String {
827        match self {
828            Self::CodeAction { action, .. } => action.lsp_action.title.clone(),
829            Self::Task(_, task) => task.resolved_label.clone(),
830        }
831    }
832}
833
834pub struct CodeActionsMenu {
835    pub actions: CodeActionContents,
836    pub buffer: Model<Buffer>,
837    pub selected_item: usize,
838    pub scroll_handle: UniformListScrollHandle,
839    pub deployed_from_indicator: Option<DisplayRow>,
840}
841
842impl CodeActionsMenu {
843    fn select_first(&mut self, cx: &mut ViewContext<Editor>) {
844        self.selected_item = 0;
845        self.scroll_handle
846            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
847        cx.notify()
848    }
849
850    fn select_prev(&mut self, cx: &mut ViewContext<Editor>) {
851        if self.selected_item > 0 {
852            self.selected_item -= 1;
853        } else {
854            self.selected_item = self.actions.len() - 1;
855        }
856        self.scroll_handle
857            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
858        cx.notify();
859    }
860
861    fn select_next(&mut self, cx: &mut ViewContext<Editor>) {
862        if self.selected_item + 1 < self.actions.len() {
863            self.selected_item += 1;
864        } else {
865            self.selected_item = 0;
866        }
867        self.scroll_handle
868            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
869        cx.notify();
870    }
871
872    fn select_last(&mut self, cx: &mut ViewContext<Editor>) {
873        self.selected_item = self.actions.len() - 1;
874        self.scroll_handle
875            .scroll_to_item(self.selected_item, ScrollStrategy::Top);
876        cx.notify()
877    }
878
879    fn visible(&self) -> bool {
880        !self.actions.is_empty()
881    }
882
883    fn origin(&self, cursor_position: DisplayPoint) -> ContextMenuOrigin {
884        if let Some(row) = self.deployed_from_indicator {
885            ContextMenuOrigin::GutterIndicator(row)
886        } else {
887            ContextMenuOrigin::EditorPoint(cursor_position)
888        }
889    }
890
891    fn render(
892        &self,
893        _style: &EditorStyle,
894        max_height_in_lines: u32,
895        cx: &mut ViewContext<Editor>,
896    ) -> AnyElement {
897        let actions = self.actions.clone();
898        let selected_item = self.selected_item;
899        let list = uniform_list(
900            cx.view().clone(),
901            "code_actions_menu",
902            self.actions.len(),
903            move |_this, range, cx| {
904                actions
905                    .iter()
906                    .skip(range.start)
907                    .take(range.end - range.start)
908                    .enumerate()
909                    .map(|(ix, action)| {
910                        let item_ix = range.start + ix;
911                        let selected = item_ix == selected_item;
912                        let colors = cx.theme().colors();
913                        div().min_w(px(220.)).max_w(px(540.)).child(
914                            ListItem::new(item_ix)
915                                .inset(true)
916                                .toggle_state(selected)
917                                .when_some(action.as_code_action(), |this, action| {
918                                    this.on_click(cx.listener(move |editor, _, cx| {
919                                        cx.stop_propagation();
920                                        if let Some(task) = editor.confirm_code_action(
921                                            &ConfirmCodeAction {
922                                                item_ix: Some(item_ix),
923                                            },
924                                            cx,
925                                        ) {
926                                            task.detach_and_log_err(cx)
927                                        }
928                                    }))
929                                    .child(
930                                        h_flex()
931                                            .overflow_hidden()
932                                            .child(
933                                                // TASK: It would be good to make lsp_action.title a SharedString to avoid allocating here.
934                                                action.lsp_action.title.replace("\n", ""),
935                                            )
936                                            .when(selected, |this| {
937                                                this.text_color(colors.text_accent)
938                                            }),
939                                    )
940                                })
941                                .when_some(action.as_task(), |this, task| {
942                                    this.on_click(cx.listener(move |editor, _, cx| {
943                                        cx.stop_propagation();
944                                        if let Some(task) = editor.confirm_code_action(
945                                            &ConfirmCodeAction {
946                                                item_ix: Some(item_ix),
947                                            },
948                                            cx,
949                                        ) {
950                                            task.detach_and_log_err(cx)
951                                        }
952                                    }))
953                                    .child(
954                                        h_flex()
955                                            .overflow_hidden()
956                                            .child(task.resolved_label.replace("\n", ""))
957                                            .when(selected, |this| {
958                                                this.text_color(colors.text_accent)
959                                            }),
960                                    )
961                                }),
962                        )
963                    })
964                    .collect()
965            },
966        )
967        .occlude()
968        .max_h(max_height_in_lines as f32 * cx.line_height())
969        .track_scroll(self.scroll_handle.clone())
970        .with_width_from_item(
971            self.actions
972                .iter()
973                .enumerate()
974                .max_by_key(|(_, action)| match action {
975                    CodeActionsItem::Task(_, task) => task.resolved_label.chars().count(),
976                    CodeActionsItem::CodeAction { action, .. } => {
977                        action.lsp_action.title.chars().count()
978                    }
979                })
980                .map(|(ix, _)| ix),
981        )
982        .with_sizing_behavior(ListSizingBehavior::Infer);
983
984        Popover::new().child(list).into_any_element()
985    }
986}