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