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