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.selections.newest::<usize>(cx).head();
62 let new_diagnostic = buffer
63 .snapshot(cx)
64 .diagnostics_in_range::<_, usize>(cursor_position..cursor_position, false)
65 .filter(|entry| !entry.range.is_empty())
66 .min_by_key(|entry| (entry.diagnostic.severity, entry.range.len()))
67 .map(|entry| entry.diagnostic);
68 if new_diagnostic != self.current_diagnostic {
69 self.current_diagnostic = new_diagnostic;
70 cx.notify();
71 }
72 }
73}
74
75impl Entity for DiagnosticIndicator {
76 type Event = ();
77}
78
79impl View for DiagnosticIndicator {
80 fn ui_name() -> &'static str {
81 "DiagnosticIndicator"
82 }
83
84 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
85 enum Summary {}
86 enum Message {}
87
88 let in_progress = self.check_in_progress;
89 let mut element = Flex::row().with_child(
90 MouseEventHandler::new::<Summary, _, _>(0, cx, |state, cx| {
91 let style = &cx
92 .global::<Settings>()
93 .theme
94 .workspace
95 .status_bar
96 .diagnostic_summary
97 .style_for(state, false);
98
99 let mut summary_row = Flex::row();
100 if self.summary.error_count > 0 {
101 summary_row.add_children([
102 Svg::new("icons/error-solid-14.svg")
103 .with_color(style.icon_color_error)
104 .constrained()
105 .with_width(style.icon_width)
106 .aligned()
107 .contained()
108 .with_margin_right(style.icon_spacing)
109 .named("error-icon"),
110 Label::new(self.summary.error_count.to_string(), style.text.clone())
111 .aligned()
112 .boxed(),
113 ]);
114 }
115
116 if self.summary.warning_count > 0 {
117 summary_row.add_children([
118 Svg::new("icons/warning-solid-14.svg")
119 .with_color(style.icon_color_warning)
120 .constrained()
121 .with_width(style.icon_width)
122 .aligned()
123 .contained()
124 .with_margin_right(style.icon_spacing)
125 .with_margin_left(if self.summary.error_count > 0 {
126 style.summary_spacing
127 } else {
128 0.
129 })
130 .named("warning-icon"),
131 Label::new(self.summary.warning_count.to_string(), style.text.clone())
132 .aligned()
133 .boxed(),
134 ]);
135 }
136
137 if self.summary.error_count == 0 && self.summary.warning_count == 0 {
138 summary_row.add_child(
139 Svg::new("icons/no-error-solid-14.svg")
140 .with_color(style.icon_color_ok)
141 .constrained()
142 .with_width(style.icon_width)
143 .aligned()
144 .named("ok-icon"),
145 );
146 }
147
148 summary_row
149 .constrained()
150 .with_height(style.height)
151 .contained()
152 .with_style(if self.summary.error_count > 0 {
153 style.container_error
154 } else if self.summary.warning_count > 0 {
155 style.container_warning
156 } else {
157 style.container_ok
158 })
159 .boxed()
160 })
161 .with_cursor_style(CursorStyle::PointingHand)
162 .on_click(|_, _, cx| cx.dispatch_action(crate::Deploy))
163 .aligned()
164 .boxed(),
165 );
166
167 let style = &cx.global::<Settings>().theme.workspace.status_bar;
168 let item_spacing = style.item_spacing;
169
170 if in_progress {
171 element.add_child(
172 Label::new(
173 "checking…".into(),
174 style.diagnostic_message.default.text.clone(),
175 )
176 .aligned()
177 .contained()
178 .with_margin_left(item_spacing)
179 .boxed(),
180 );
181 } else if let Some(diagnostic) = &self.current_diagnostic {
182 let message_style = style.diagnostic_message.clone();
183 element.add_child(
184 MouseEventHandler::new::<Message, _, _>(1, cx, |state, _| {
185 Label::new(
186 diagnostic.message.split('\n').next().unwrap().to_string(),
187 message_style.style_for(state, false).text.clone(),
188 )
189 .aligned()
190 .contained()
191 .with_margin_left(item_spacing)
192 .boxed()
193 })
194 .with_cursor_style(CursorStyle::PointingHand)
195 .on_click(|_, _, cx| cx.dispatch_action(GoToNextDiagnostic))
196 .boxed(),
197 );
198 }
199
200 element.named("diagnostic indicator")
201 }
202
203 fn debug_json(&self, _: &gpui::AppContext) -> serde_json::Value {
204 serde_json::json!({ "summary": self.summary })
205 }
206}
207
208impl StatusItemView for DiagnosticIndicator {
209 fn set_active_pane_item(
210 &mut self,
211 active_pane_item: Option<&dyn workspace::ItemHandle>,
212 cx: &mut ViewContext<Self>,
213 ) {
214 if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
215 self.active_editor = Some(editor.downgrade());
216 self._observe_active_editor = Some(cx.observe(&editor, Self::update));
217 self.update(editor, cx);
218 } else {
219 self.active_editor = None;
220 self.current_diagnostic = None;
221 self._observe_active_editor = None;
222 }
223 cx.notify();
224 }
225}