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