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