| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use gpui::{ |
| 4 | App, AppContext as _, Context, Empty, Entity, IntoElement, Render, Styled, WeakEntity, Window, |
| 5 | }; |
| 6 | |
| 7 | use crate::{ |
| 8 | highlighter::DiagnosticEntry, |
| 9 | input::{ |
| 10 | EditorState, |
| 11 | popovers::{Popover, render_markdown}, |
| 12 | }, |
| 13 | }; |
| 14 | |
| 15 | pub struct DiagnosticPopover { |
| 16 | state: WeakEntity<EditorState>, |
| 17 | pub(crate) diagnostic: Rc<DiagnosticEntry>, |
| 18 | } |
| 19 | |
| 20 | impl DiagnosticPopover { |
| 21 | pub fn new( |
| 22 | diagnostic: &DiagnosticEntry, |
| 23 | state: Entity<EditorState>, |
| 24 | cx: &mut App, |
| 25 | ) -> Entity<Self> { |
| 26 | let diagnostic = Rc::new(diagnostic.clone()); |
| 27 | |
| 28 | cx.new(|_| Self { |
| 29 | diagnostic, |
| 30 | state: state.downgrade(), |
| 31 | }) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl Render for DiagnosticPopover { |
| 36 | fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 37 | let Some(state) = self.state.upgrade() else { |
| 38 | return Empty.into_any_element(); |
| 39 | }; |
| 40 | let message = self.diagnostic.message.clone(); |
| 41 | |
| 42 | let (border, bg, fg) = ( |
| 43 | crate::highlighter::diagnostic_border(self.diagnostic.severity, cx), |
| 44 | crate::highlighter::diagnostic_background(self.diagnostic.severity, cx), |
| 45 | crate::highlighter::diagnostic_foreground(self.diagnostic.severity, cx), |
| 46 | ); |
| 47 | |
| 48 | Popover::new( |
| 49 | "diagnostic-popover", |
| 50 | state, |
| 51 | self.diagnostic.range.clone(), |
| 52 | move |window, cx| render_markdown("message", message.clone(), window, cx), |
| 53 | ) |
| 54 | .px_1() |
| 55 | .py_0p5() |
| 56 | .bg(bg) |
| 57 | .text_color(fg) |
| 58 | .border_1() |
| 59 | .border_color(border) |
| 60 | .into_any_element() |
| 61 | } |
| 62 | } |