1use collections::HashSet;
2use editor::{Editor, GoToDiagnostic};
3use gpui::{
4 elements::*,
5 platform::{CursorStyle, MouseButton},
6 serde_json, AppContext, Entity, Subscription, View, ViewContext, ViewHandle, WeakViewHandle,
7};
8use language::Diagnostic;
9use lsp::LanguageServerId;
10use workspace::{item::ItemHandle, StatusItemView, Workspace};
11
12use crate::ProjectDiagnosticsEditor;
13
14pub struct DiagnosticIndicator {
15 summary: project::DiagnosticSummary,
16 active_editor: Option<WeakViewHandle<Editor>>,
17 workspace: WeakViewHandle<Workspace>,
18 current_diagnostic: Option<Diagnostic>,
19 in_progress_checks: HashSet<LanguageServerId>,
20 _observe_active_editor: Option<Subscription>,
21}
22
23pub fn init(cx: &mut AppContext) {
24 cx.add_action(DiagnosticIndicator::go_to_next_diagnostic);
25}
26
27impl DiagnosticIndicator {
28 pub fn new(workspace: &Workspace, cx: &mut ViewContext<Self>) -> Self {
29 let project = workspace.project();
30 cx.subscribe(project, |this, project, event, cx| match event {
31 project::Event::DiskBasedDiagnosticsStarted { language_server_id } => {
32 this.in_progress_checks.insert(*language_server_id);
33 cx.notify();
34 }
35 project::Event::DiskBasedDiagnosticsFinished { language_server_id } => {
36 this.summary = project.read(cx).diagnostic_summary(cx);
37 this.in_progress_checks.remove(language_server_id);
38 cx.notify();
39 }
40 _ => {}
41 })
42 .detach();
43 Self {
44 summary: project.read(cx).diagnostic_summary(cx),
45 in_progress_checks: project
46 .read(cx)
47 .language_servers_running_disk_based_diagnostics()
48 .collect(),
49 active_editor: None,
50 workspace: workspace.weak_handle(),
51 current_diagnostic: None,
52 _observe_active_editor: None,
53 }
54 }
55
56 fn go_to_next_diagnostic(&mut self, _: &GoToDiagnostic, cx: &mut ViewContext<Self>) {
57 if let Some(editor) = self.active_editor.as_ref().and_then(|e| e.upgrade(cx)) {
58 editor.update(cx, |editor, cx| {
59 editor.go_to_diagnostic_impl(editor::Direction::Next, cx);
60 })
61 }
62 }
63
64 fn update(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
65 let editor = editor.read(cx);
66 let buffer = editor.buffer().read(cx);
67 let cursor_position = editor.selections.newest::<usize>(cx).head();
68 let new_diagnostic = buffer
69 .snapshot(cx)
70 .diagnostics_in_range::<_, usize>(cursor_position..cursor_position, false)
71 .filter(|entry| !entry.range.is_empty())
72 .min_by_key(|entry| (entry.diagnostic.severity, entry.range.len()))
73 .map(|entry| entry.diagnostic);
74 if new_diagnostic != self.current_diagnostic {
75 self.current_diagnostic = new_diagnostic;
76 cx.notify();
77 }
78 }
79}
80
81impl Entity for DiagnosticIndicator {
82 type Event = ();
83}
84
85impl View for DiagnosticIndicator {
86 fn ui_name() -> &'static str {
87 "DiagnosticIndicator"
88 }
89
90 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
91 enum Summary {}
92 enum Message {}
93
94 let tooltip_style = theme::current(cx).tooltip.clone();
95 let in_progress = !self.in_progress_checks.is_empty();
96 let mut element = Flex::row().with_child(
97 MouseEventHandler::new::<Summary, _>(0, cx, |state, cx| {
98 let theme = theme::current(cx);
99 let style = theme
100 .workspace
101 .status_bar
102 .diagnostic_summary
103 .style_for(state);
104
105 let mut summary_row = Flex::row();
106 if self.summary.error_count > 0 {
107 summary_row.add_child(
108 Svg::new("icons/circle_x_mark_16.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 );
116 summary_row.add_child(
117 Label::new(self.summary.error_count.to_string(), style.text.clone())
118 .aligned(),
119 );
120 }
121
122 if self.summary.warning_count > 0 {
123 summary_row.add_child(
124 Svg::new("icons/triangle_exclamation_16.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 );
137 summary_row.add_child(
138 Label::new(self.summary.warning_count.to_string(), style.text.clone())
139 .aligned(),
140 );
141 }
142
143 if self.summary.error_count == 0 && self.summary.warning_count == 0 {
144 summary_row.add_child(
145 Svg::new("icons/circle_check_16.svg")
146 .with_color(style.icon_color_ok)
147 .constrained()
148 .with_width(style.icon_width)
149 .aligned()
150 .into_any_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 })
166 .with_cursor_style(CursorStyle::PointingHand)
167 .on_click(MouseButton::Left, |_, this, cx| {
168 if let Some(workspace) = this.workspace.upgrade(cx) {
169 workspace.update(cx, |workspace, cx| {
170 ProjectDiagnosticsEditor::deploy(workspace, &Default::default(), cx)
171 })
172 }
173 })
174 .with_tooltip::<Summary>(
175 0,
176 "Project Diagnostics",
177 Some(Box::new(crate::Deploy)),
178 tooltip_style,
179 cx,
180 )
181 .aligned()
182 .into_any(),
183 );
184
185 let style = &theme::current(cx).workspace.status_bar;
186 let item_spacing = style.item_spacing;
187
188 if in_progress {
189 element.add_child(
190 Label::new("Checking…", style.diagnostic_message.default.text.clone())
191 .aligned()
192 .contained()
193 .with_margin_left(item_spacing),
194 );
195 } else if let Some(diagnostic) = &self.current_diagnostic {
196 let message_style = style.diagnostic_message.clone();
197 element.add_child(
198 MouseEventHandler::new::<Message, _>(1, cx, |state, _| {
199 Label::new(
200 diagnostic.message.split('\n').next().unwrap().to_string(),
201 message_style.style_for(state).text.clone(),
202 )
203 .aligned()
204 .contained()
205 .with_margin_left(item_spacing)
206 })
207 .with_cursor_style(CursorStyle::PointingHand)
208 .on_click(MouseButton::Left, |_, this, cx| {
209 this.go_to_next_diagnostic(&Default::default(), cx)
210 }),
211 );
212 }
213
214 element.into_any_named("diagnostic indicator")
215 }
216
217 fn debug_json(&self, _: &gpui::AppContext) -> serde_json::Value {
218 serde_json::json!({ "summary": self.summary })
219 }
220}
221
222impl StatusItemView for DiagnosticIndicator {
223 fn set_active_pane_item(
224 &mut self,
225 active_pane_item: Option<&dyn ItemHandle>,
226 cx: &mut ViewContext<Self>,
227 ) {
228 if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
229 self.active_editor = Some(editor.downgrade());
230 self._observe_active_editor = Some(cx.observe(&editor, Self::update));
231 self.update(editor, cx);
232 } else {
233 self.active_editor = None;
234 self.current_diagnostic = None;
235 self._observe_active_editor = None;
236 }
237 cx.notify();
238 }
239}