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