1use editor::{Editor, GoToNextDiagnostic};
2use gpui::{
3 elements::*, platform::CursorStyle, serde_json, Entity, ModelHandle, MutableAppContext,
4 RenderContext, Subscription, View, ViewContext, ViewHandle, WeakViewHandle,
5};
6use language::Diagnostic;
7use project::Project;
8use settings::Settings;
9use workspace::StatusItemView;
10
11pub struct DiagnosticIndicator {
12 summary: project::DiagnosticSummary,
13 active_editor: Option<WeakViewHandle<Editor>>,
14 current_diagnostic: Option<Diagnostic>,
15 check_in_progress: bool,
16 _observe_active_editor: Option<Subscription>,
17}
18
19pub fn init(cx: &mut MutableAppContext) {
20 cx.add_action(DiagnosticIndicator::go_to_next_diagnostic);
21}
22
23impl DiagnosticIndicator {
24 pub fn new(project: &ModelHandle<Project>, cx: &mut ViewContext<Self>) -> Self {
25 cx.subscribe(project, |this, project, event, cx| match event {
26 project::Event::DiskBasedDiagnosticsUpdated => {
27 cx.notify();
28 }
29 project::Event::DiskBasedDiagnosticsStarted => {
30 this.check_in_progress = true;
31 cx.notify();
32 }
33 project::Event::DiskBasedDiagnosticsFinished => {
34 this.summary = project.read(cx).diagnostic_summary(cx);
35 this.check_in_progress = false;
36 cx.notify();
37 }
38 _ => {}
39 })
40 .detach();
41 Self {
42 summary: project.read(cx).diagnostic_summary(cx),
43 check_in_progress: project.read(cx).is_running_disk_based_diagnostics(),
44 active_editor: None,
45 current_diagnostic: None,
46 _observe_active_editor: None,
47 }
48 }
49
50 fn go_to_next_diagnostic(&mut self, _: &GoToNextDiagnostic, cx: &mut ViewContext<Self>) {
51 if let Some(editor) = self.active_editor.as_ref().and_then(|e| e.upgrade(cx)) {
52 editor.update(cx, |editor, cx| {
53 editor.go_to_diagnostic(editor::Direction::Next, cx);
54 })
55 }
56 }
57
58 fn update(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
59 let editor = editor.read(cx);
60 let buffer = editor.buffer().read(cx);
61 let cursor_position = editor
62 .newest_selection_with_snapshot::<usize>(&buffer.read(cx))
63 .head();
64 let new_diagnostic = buffer
65 .read(cx)
66 .diagnostics_in_range::<_, usize>(cursor_position..cursor_position, false)
67 .filter(|entry| !entry.range.is_empty())
68 .min_by_key(|entry| (entry.diagnostic.severity, entry.range.len()))
69 .map(|entry| entry.diagnostic);
70 if new_diagnostic != self.current_diagnostic {
71 self.current_diagnostic = new_diagnostic;
72 cx.notify();
73 }
74 }
75}
76
77impl Entity for DiagnosticIndicator {
78 type Event = ();
79}
80
81impl View for DiagnosticIndicator {
82 fn ui_name() -> &'static str {
83 "DiagnosticIndicator"
84 }
85
86 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
87 enum Summary {}
88 enum Message {}
89
90 let in_progress = self.check_in_progress;
91 let mut element = Flex::row().with_child(
92 MouseEventHandler::new::<Summary, _, _>(0, cx, |state, cx| {
93 let style = &cx
94 .global::<Settings>()
95 .theme
96 .workspace
97 .status_bar
98 .diagnostic_summary;
99 let style = if state.hovered {
100 style.hover()
101 } else {
102 &style.default
103 };
104
105 let mut summary_row = Flex::row();
106 if self.summary.error_count > 0 {
107 summary_row.add_children([
108 Svg::new("icons/error-solid-14.svg")
109 .with_color(style.icon_color_error)
110 .constrained()
111 .with_width(style.icon_width)
112 .aligned()
113 .contained()
114 .with_margin_right(style.icon_spacing)
115 .named("error-icon"),
116 Label::new(self.summary.error_count.to_string(), style.text.clone())
117 .aligned()
118 .boxed(),
119 ]);
120 }
121
122 if self.summary.warning_count > 0 {
123 summary_row.add_children([
124 Svg::new("icons/warning-solid-14.svg")
125 .with_color(style.icon_color_warning)
126 .constrained()
127 .with_width(style.icon_width)
128 .aligned()
129 .contained()
130 .with_margin_right(style.icon_spacing)
131 .with_margin_left(if self.summary.error_count > 0 {
132 style.summary_spacing
133 } else {
134 0.
135 })
136 .named("warning-icon"),
137 Label::new(self.summary.warning_count.to_string(), style.text.clone())
138 .aligned()
139 .boxed(),
140 ]);
141 }
142
143 if self.summary.error_count == 0 && self.summary.warning_count == 0 {
144 summary_row.add_child(
145 Svg::new("icons/no-error-solid-14.svg")
146 .with_color(style.icon_color_ok)
147 .constrained()
148 .with_width(style.icon_width)
149 .aligned()
150 .named("ok-icon"),
151 );
152 }
153
154 summary_row
155 .constrained()
156 .with_height(style.height)
157 .contained()
158 .with_style(if self.summary.error_count > 0 {
159 style.container_error
160 } else if self.summary.warning_count > 0 {
161 style.container_warning
162 } else {
163 style.container_ok
164 })
165 .boxed()
166 })
167 .with_cursor_style(CursorStyle::PointingHand)
168 .on_click(|cx| cx.dispatch_action(crate::Deploy))
169 .aligned()
170 .boxed(),
171 );
172
173 let style = &cx.global::<Settings>().theme.workspace.status_bar;
174 let item_spacing = style.item_spacing;
175
176 if in_progress {
177 element.add_child(
178 Label::new(
179 "checking…".into(),
180 style.diagnostic_message.default.text.clone(),
181 )
182 .aligned()
183 .contained()
184 .with_margin_left(item_spacing)
185 .boxed(),
186 );
187 } else if let Some(diagnostic) = &self.current_diagnostic {
188 let message_style = style.diagnostic_message.clone();
189 element.add_child(
190 MouseEventHandler::new::<Message, _, _>(1, cx, |state, _| {
191 Label::new(
192 diagnostic.message.split('\n').next().unwrap().to_string(),
193 if state.hovered {
194 message_style.hover().text.clone()
195 } else {
196 message_style.default.text.clone()
197 },
198 )
199 .aligned()
200 .contained()
201 .with_margin_left(item_spacing)
202 .boxed()
203 })
204 .with_cursor_style(CursorStyle::PointingHand)
205 .on_click(|cx| cx.dispatch_action(GoToNextDiagnostic))
206 .boxed(),
207 );
208 }
209
210 element.named("diagnostic indicator")
211 }
212
213 fn debug_json(&self, _: &gpui::AppContext) -> serde_json::Value {
214 serde_json::json!({ "summary": self.summary })
215 }
216}
217
218impl StatusItemView for DiagnosticIndicator {
219 fn set_active_pane_item(
220 &mut self,
221 active_pane_item: Option<&dyn workspace::ItemHandle>,
222 cx: &mut ViewContext<Self>,
223 ) {
224 if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
225 self.active_editor = Some(editor.downgrade());
226 self._observe_active_editor = Some(cx.observe(&editor, Self::update));
227 self.update(editor, cx);
228 } else {
229 self.active_editor = None;
230 self.current_diagnostic = None;
231 self._observe_active_editor = None;
232 }
233 cx.notify();
234 }
235}