1pub mod items;
2mod toolbar_controls;
3
4mod diagnostic_renderer;
5
6#[cfg(test)]
7mod diagnostics_tests;
8
9use anyhow::Result;
10use collections::{BTreeSet, HashMap};
11use diagnostic_renderer::DiagnosticBlock;
12use editor::{
13 DEFAULT_MULTIBUFFER_CONTEXT, Editor, EditorEvent, ExcerptRange, MultiBuffer, PathKey,
14 display_map::{BlockPlacement, BlockProperties, BlockStyle, CustomBlockId},
15 scroll::Autoscroll,
16};
17use gpui::{
18 AnyElement, AnyView, App, AsyncApp, Context, Entity, EventEmitter, FocusHandle, Focusable,
19 Global, InteractiveElement, IntoElement, ParentElement, Render, SharedString, Styled,
20 Subscription, Task, WeakEntity, Window, actions, div,
21};
22use language::{
23 Bias, Buffer, BufferRow, BufferSnapshot, DiagnosticEntry, Point, ToTreeSitterPoint,
24};
25use lsp::DiagnosticSeverity;
26use project::{DiagnosticSummary, Project, ProjectPath, project_settings::ProjectSettings};
27use settings::Settings;
28use std::{
29 any::{Any, TypeId},
30 cmp,
31 cmp::Ordering,
32 ops::{Range, RangeInclusive},
33 sync::Arc,
34 time::Duration,
35};
36use text::{BufferId, OffsetRangeExt};
37use theme::ActiveTheme;
38pub use toolbar_controls::ToolbarControls;
39use ui::{Icon, IconName, Label, h_flex, prelude::*};
40use util::ResultExt;
41use workspace::{
42 ItemNavHistory, ToolbarItemLocation, Workspace,
43 item::{BreadcrumbText, Item, ItemEvent, ItemHandle, TabContentParams},
44 searchable::SearchableItemHandle,
45};
46
47actions!(diagnostics, [Deploy, ToggleWarnings]);
48
49struct IncludeWarnings(bool);
50impl Global for IncludeWarnings {}
51
52pub fn init(cx: &mut App) {
53 editor::set_diagnostic_renderer(diagnostic_renderer::DiagnosticRenderer {}, cx);
54 cx.observe_new(ProjectDiagnosticsEditor::register).detach();
55}
56
57pub(crate) struct ProjectDiagnosticsEditor {
58 project: Entity<Project>,
59 workspace: WeakEntity<Workspace>,
60 focus_handle: FocusHandle,
61 editor: Entity<Editor>,
62 diagnostics: HashMap<BufferId, Vec<DiagnosticEntry<text::Anchor>>>,
63 blocks: HashMap<BufferId, Vec<CustomBlockId>>,
64 summary: DiagnosticSummary,
65 multibuffer: Entity<MultiBuffer>,
66 paths_to_update: BTreeSet<ProjectPath>,
67 include_warnings: bool,
68 update_excerpts_task: Option<Task<Result<()>>>,
69 _subscription: Subscription,
70}
71
72impl EventEmitter<EditorEvent> for ProjectDiagnosticsEditor {}
73
74const DIAGNOSTICS_UPDATE_DELAY: Duration = Duration::from_millis(50);
75
76impl Render for ProjectDiagnosticsEditor {
77 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
78 let warning_count = if self.include_warnings {
79 self.summary.warning_count
80 } else {
81 0
82 };
83
84 let child = if warning_count + self.summary.error_count == 0 {
85 let label = if self.summary.warning_count == 0 {
86 SharedString::new_static("No problems in workspace")
87 } else {
88 SharedString::new_static("No errors in workspace")
89 };
90 v_flex()
91 .key_context("EmptyPane")
92 .size_full()
93 .gap_1()
94 .justify_center()
95 .items_center()
96 .text_center()
97 .bg(cx.theme().colors().editor_background)
98 .child(Label::new(label).color(Color::Muted))
99 .when(self.summary.warning_count > 0, |this| {
100 let plural_suffix = if self.summary.warning_count > 1 {
101 "s"
102 } else {
103 ""
104 };
105 let label = format!(
106 "Show {} warning{}",
107 self.summary.warning_count, plural_suffix
108 );
109 this.child(
110 Button::new("diagnostics-show-warning-label", label).on_click(cx.listener(
111 |this, _, window, cx| {
112 this.toggle_warnings(&Default::default(), window, cx);
113 cx.notify();
114 },
115 )),
116 )
117 })
118 } else {
119 div().size_full().child(self.editor.clone())
120 };
121
122 div()
123 .key_context("Diagnostics")
124 .track_focus(&self.focus_handle(cx))
125 .size_full()
126 .on_action(cx.listener(Self::toggle_warnings))
127 .child(child)
128 }
129}
130
131impl ProjectDiagnosticsEditor {
132 fn register(
133 workspace: &mut Workspace,
134 _window: Option<&mut Window>,
135 _: &mut Context<Workspace>,
136 ) {
137 workspace.register_action(Self::deploy);
138 }
139
140 fn new(
141 include_warnings: bool,
142 project_handle: Entity<Project>,
143 workspace: WeakEntity<Workspace>,
144 window: &mut Window,
145 cx: &mut Context<Self>,
146 ) -> Self {
147 let project_event_subscription =
148 cx.subscribe_in(&project_handle, window, |this, project, event, window, cx| match event {
149 project::Event::DiskBasedDiagnosticsStarted { .. } => {
150 cx.notify();
151 }
152 project::Event::DiskBasedDiagnosticsFinished { language_server_id } => {
153 log::debug!("disk based diagnostics finished for server {language_server_id}");
154 this.update_stale_excerpts(window, cx);
155 }
156 project::Event::DiagnosticsUpdated {
157 language_server_id,
158 path,
159 } => {
160 this.paths_to_update.insert(path.clone());
161 this.summary = project.read(cx).diagnostic_summary(false, cx);
162 cx.emit(EditorEvent::TitleChanged);
163
164 if this.editor.focus_handle(cx).contains_focused(window, cx) || this.focus_handle.contains_focused(window, cx) {
165 log::debug!("diagnostics updated for server {language_server_id}, path {path:?}. recording change");
166 } else {
167 log::debug!("diagnostics updated for server {language_server_id}, path {path:?}. updating excerpts");
168 this.update_stale_excerpts(window, cx);
169 }
170 }
171 _ => {}
172 });
173
174 let focus_handle = cx.focus_handle();
175 cx.on_focus_in(&focus_handle, window, |this, window, cx| {
176 this.focus_in(window, cx)
177 })
178 .detach();
179 cx.on_focus_out(&focus_handle, window, |this, _event, window, cx| {
180 this.focus_out(window, cx)
181 })
182 .detach();
183
184 let excerpts = cx.new(|cx| MultiBuffer::new(project_handle.read(cx).capability()));
185 let editor = cx.new(|cx| {
186 let mut editor =
187 Editor::for_multibuffer(excerpts.clone(), Some(project_handle.clone()), window, cx);
188 editor.set_vertical_scroll_margin(5, cx);
189 editor.disable_inline_diagnostics();
190 editor.set_all_diagnostics_active(cx);
191 editor
192 });
193 cx.subscribe_in(
194 &editor,
195 window,
196 |this, _editor, event: &EditorEvent, window, cx| {
197 cx.emit(event.clone());
198 match event {
199 EditorEvent::Focused => {
200 if this.multibuffer.read(cx).is_empty() {
201 window.focus(&this.focus_handle);
202 }
203 }
204 EditorEvent::Blurred => this.update_stale_excerpts(window, cx),
205 _ => {}
206 }
207 },
208 )
209 .detach();
210 cx.observe_global_in::<IncludeWarnings>(window, |this, window, cx| {
211 this.include_warnings = cx.global::<IncludeWarnings>().0;
212 this.diagnostics.clear();
213 this.update_all_excerpts(window, cx);
214 })
215 .detach();
216
217 let project = project_handle.read(cx);
218 let mut this = Self {
219 project: project_handle.clone(),
220 summary: project.diagnostic_summary(false, cx),
221 diagnostics: Default::default(),
222 blocks: Default::default(),
223 include_warnings,
224 workspace,
225 multibuffer: excerpts,
226 focus_handle,
227 editor,
228 paths_to_update: Default::default(),
229 update_excerpts_task: None,
230 _subscription: project_event_subscription,
231 };
232 this.update_all_excerpts(window, cx);
233 this
234 }
235
236 fn update_stale_excerpts(&mut self, window: &mut Window, cx: &mut Context<Self>) {
237 if self.update_excerpts_task.is_some() {
238 return;
239 }
240 let project_handle = self.project.clone();
241 self.update_excerpts_task = Some(cx.spawn_in(window, async move |this, cx| {
242 cx.background_executor()
243 .timer(DIAGNOSTICS_UPDATE_DELAY)
244 .await;
245 loop {
246 let Some(path) = this.update(cx, |this, _| {
247 let Some(path) = this.paths_to_update.pop_first() else {
248 this.update_excerpts_task.take();
249 return None;
250 };
251 Some(path)
252 })?
253 else {
254 break;
255 };
256
257 if let Some(buffer) = project_handle
258 .update(cx, |project, cx| project.open_buffer(path.clone(), cx))?
259 .await
260 .log_err()
261 {
262 this.update_in(cx, |this, window, cx| {
263 this.update_excerpts(buffer, window, cx)
264 })?
265 .await?;
266 }
267 }
268 Ok(())
269 }));
270 }
271
272 fn deploy(
273 workspace: &mut Workspace,
274 _: &Deploy,
275 window: &mut Window,
276 cx: &mut Context<Workspace>,
277 ) {
278 if let Some(existing) = workspace.item_of_type::<ProjectDiagnosticsEditor>(cx) {
279 let is_active = workspace
280 .active_item(cx)
281 .is_some_and(|item| item.item_id() == existing.item_id());
282 workspace.activate_item(&existing, true, !is_active, window, cx);
283 } else {
284 let workspace_handle = cx.entity().downgrade();
285
286 let include_warnings = match cx.try_global::<IncludeWarnings>() {
287 Some(include_warnings) => include_warnings.0,
288 None => ProjectSettings::get_global(cx).diagnostics.include_warnings,
289 };
290
291 let diagnostics = cx.new(|cx| {
292 ProjectDiagnosticsEditor::new(
293 include_warnings,
294 workspace.project().clone(),
295 workspace_handle,
296 window,
297 cx,
298 )
299 });
300 workspace.add_item_to_active_pane(Box::new(diagnostics), None, true, window, cx);
301 }
302 }
303
304 fn toggle_warnings(&mut self, _: &ToggleWarnings, _: &mut Window, cx: &mut Context<Self>) {
305 cx.set_global(IncludeWarnings(!self.include_warnings));
306 }
307
308 fn focus_in(&mut self, window: &mut Window, cx: &mut Context<Self>) {
309 if self.focus_handle.is_focused(window) && !self.multibuffer.read(cx).is_empty() {
310 self.editor.focus_handle(cx).focus(window)
311 }
312 }
313
314 fn focus_out(&mut self, window: &mut Window, cx: &mut Context<Self>) {
315 if !self.focus_handle.is_focused(window) && !self.editor.focus_handle(cx).is_focused(window)
316 {
317 self.update_stale_excerpts(window, cx);
318 }
319 }
320
321 /// Enqueue an update of all excerpts. Updates all paths that either
322 /// currently have diagnostics or are currently present in this view.
323 fn update_all_excerpts(&mut self, window: &mut Window, cx: &mut Context<Self>) {
324 self.project.update(cx, |project, cx| {
325 let mut paths = project
326 .diagnostic_summaries(false, cx)
327 .map(|(path, _, _)| path)
328 .collect::<BTreeSet<_>>();
329 self.multibuffer.update(cx, |multibuffer, cx| {
330 for buffer in multibuffer.all_buffers() {
331 if let Some(file) = buffer.read(cx).file() {
332 paths.insert(ProjectPath {
333 path: file.path().clone(),
334 worktree_id: file.worktree_id(cx),
335 });
336 }
337 }
338 });
339 self.paths_to_update = paths;
340 });
341 self.update_stale_excerpts(window, cx);
342 }
343
344 fn diagnostics_are_unchanged(
345 &self,
346 existing: &Vec<DiagnosticEntry<text::Anchor>>,
347 new: &Vec<DiagnosticEntry<text::Anchor>>,
348 snapshot: &BufferSnapshot,
349 ) -> bool {
350 if existing.len() != new.len() {
351 return false;
352 }
353 existing.iter().zip(new.iter()).all(|(existing, new)| {
354 existing.diagnostic.message == new.diagnostic.message
355 && existing.diagnostic.severity == new.diagnostic.severity
356 && existing.diagnostic.is_primary == new.diagnostic.is_primary
357 && existing.range.to_offset(snapshot) == new.range.to_offset(snapshot)
358 })
359 }
360
361 fn update_excerpts(
362 &mut self,
363 buffer: Entity<Buffer>,
364 window: &mut Window,
365 cx: &mut Context<Self>,
366 ) -> Task<Result<()>> {
367 let was_empty = self.multibuffer.read(cx).is_empty();
368 let buffer_snapshot = buffer.read(cx).snapshot();
369 let buffer_id = buffer_snapshot.remote_id();
370 let max_severity = if self.include_warnings {
371 DiagnosticSeverity::WARNING
372 } else {
373 DiagnosticSeverity::ERROR
374 };
375
376 cx.spawn_in(window, async move |this, mut cx| {
377 let diagnostics = buffer_snapshot
378 .diagnostics_in_range::<_, text::Anchor>(
379 Point::zero()..buffer_snapshot.max_point(),
380 false,
381 )
382 .filter(|d| !(d.diagnostic.is_primary && d.diagnostic.is_unnecessary))
383 .collect::<Vec<_>>();
384 let unchanged = this.update(cx, |this, _| {
385 if this.diagnostics.get(&buffer_id).is_some_and(|existing| {
386 this.diagnostics_are_unchanged(existing, &diagnostics, &buffer_snapshot)
387 }) {
388 return true;
389 }
390 this.diagnostics.insert(buffer_id, diagnostics.clone());
391 return false;
392 })?;
393 if unchanged {
394 return Ok(());
395 }
396
397 let mut grouped: HashMap<usize, Vec<_>> = HashMap::default();
398 for entry in diagnostics {
399 grouped
400 .entry(entry.diagnostic.group_id)
401 .or_default()
402 .push(DiagnosticEntry {
403 range: entry.range.to_point(&buffer_snapshot),
404 diagnostic: entry.diagnostic,
405 })
406 }
407 let mut blocks: Vec<DiagnosticBlock> = Vec::new();
408
409 for (_, group) in grouped {
410 let group_severity = group.iter().map(|d| d.diagnostic.severity).min();
411 if group_severity.is_none_or(|s| s > max_severity) {
412 continue;
413 }
414 let more = cx.update(|_, cx| {
415 crate::diagnostic_renderer::DiagnosticRenderer::diagnostic_blocks_for_group(
416 group,
417 buffer_snapshot.remote_id(),
418 Some(this.clone()),
419 cx,
420 )
421 })?;
422
423 for item in more {
424 let insert_pos = blocks
425 .binary_search_by(|existing| {
426 match existing.initial_range.start.cmp(&item.initial_range.start) {
427 Ordering::Equal => item
428 .initial_range
429 .end
430 .cmp(&existing.initial_range.end)
431 .reverse(),
432 other => other,
433 }
434 })
435 .unwrap_or_else(|pos| pos);
436
437 blocks.insert(insert_pos, item);
438 }
439 }
440
441 let mut excerpt_ranges: Vec<ExcerptRange<Point>> = Vec::new();
442 for b in blocks.iter() {
443 let excerpt_range = context_range_for_entry(
444 b.initial_range.clone(),
445 DEFAULT_MULTIBUFFER_CONTEXT,
446 buffer_snapshot.clone(),
447 &mut cx,
448 )
449 .await;
450 excerpt_ranges.push(ExcerptRange {
451 context: excerpt_range,
452 primary: b.initial_range.clone(),
453 })
454 }
455
456 this.update_in(cx, |this, window, cx| {
457 if let Some(block_ids) = this.blocks.remove(&buffer_id) {
458 this.editor.update(cx, |editor, cx| {
459 editor.display_map.update(cx, |display_map, cx| {
460 display_map.remove_blocks(block_ids.into_iter().collect(), cx)
461 });
462 })
463 }
464 let (anchor_ranges, _) = this.multibuffer.update(cx, |multi_buffer, cx| {
465 multi_buffer.set_excerpt_ranges_for_path(
466 PathKey::for_buffer(&buffer, cx),
467 buffer.clone(),
468 &buffer_snapshot,
469 excerpt_ranges,
470 cx,
471 )
472 });
473 #[cfg(test)]
474 let cloned_blocks = blocks.clone();
475
476 if was_empty {
477 if let Some(anchor_range) = anchor_ranges.first() {
478 let range_to_select = anchor_range.start..anchor_range.start;
479 this.editor.update(cx, |editor, cx| {
480 editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
481 s.select_anchor_ranges([range_to_select]);
482 })
483 });
484 if this.focus_handle.is_focused(window) {
485 this.editor.read(cx).focus_handle(cx).focus(window);
486 }
487 }
488 }
489
490 let editor_blocks =
491 anchor_ranges
492 .into_iter()
493 .zip(blocks.into_iter())
494 .map(|(anchor, block)| {
495 let editor = this.editor.downgrade();
496 BlockProperties {
497 placement: BlockPlacement::Near(anchor.start),
498 height: Some(1),
499 style: BlockStyle::Flex,
500 render: Arc::new(move |bcx| {
501 block.render_block(editor.clone(), bcx)
502 }),
503 priority: 1,
504 }
505 });
506 let block_ids = this.editor.update(cx, |editor, cx| {
507 editor.display_map.update(cx, |display_map, cx| {
508 display_map.insert_blocks(editor_blocks, cx)
509 })
510 });
511
512 #[cfg(test)]
513 {
514 for (block_id, block) in block_ids.iter().zip(cloned_blocks.iter()) {
515 let markdown = block.markdown.clone();
516 editor::test::set_block_content_for_tests(
517 &this.editor,
518 *block_id,
519 cx,
520 move |cx| {
521 markdown::MarkdownElement::rendered_text(
522 markdown.clone(),
523 cx,
524 editor::hover_markdown_style,
525 )
526 },
527 );
528 }
529 }
530
531 this.blocks.insert(buffer_id, block_ids);
532 cx.notify()
533 })
534 })
535 }
536}
537
538impl Focusable for ProjectDiagnosticsEditor {
539 fn focus_handle(&self, _: &App) -> FocusHandle {
540 self.focus_handle.clone()
541 }
542}
543
544impl Item for ProjectDiagnosticsEditor {
545 type Event = EditorEvent;
546
547 fn to_item_events(event: &EditorEvent, f: impl FnMut(ItemEvent)) {
548 Editor::to_item_events(event, f)
549 }
550
551 fn deactivated(&mut self, window: &mut Window, cx: &mut Context<Self>) {
552 self.editor
553 .update(cx, |editor, cx| editor.deactivated(window, cx));
554 }
555
556 fn navigate(
557 &mut self,
558 data: Box<dyn Any>,
559 window: &mut Window,
560 cx: &mut Context<Self>,
561 ) -> bool {
562 self.editor
563 .update(cx, |editor, cx| editor.navigate(data, window, cx))
564 }
565
566 fn tab_tooltip_text(&self, _: &App) -> Option<SharedString> {
567 Some("Project Diagnostics".into())
568 }
569
570 fn tab_content(&self, params: TabContentParams, _window: &Window, _: &App) -> AnyElement {
571 h_flex()
572 .gap_1()
573 .when(
574 self.summary.error_count == 0 && self.summary.warning_count == 0,
575 |then| {
576 then.child(
577 h_flex()
578 .gap_1()
579 .child(Icon::new(IconName::Check).color(Color::Success))
580 .child(Label::new("No problems").color(params.text_color())),
581 )
582 },
583 )
584 .when(self.summary.error_count > 0, |then| {
585 then.child(
586 h_flex()
587 .gap_1()
588 .child(Icon::new(IconName::XCircle).color(Color::Error))
589 .child(
590 Label::new(self.summary.error_count.to_string())
591 .color(params.text_color()),
592 ),
593 )
594 })
595 .when(self.summary.warning_count > 0, |then| {
596 then.child(
597 h_flex()
598 .gap_1()
599 .child(Icon::new(IconName::Warning).color(Color::Warning))
600 .child(
601 Label::new(self.summary.warning_count.to_string())
602 .color(params.text_color()),
603 ),
604 )
605 })
606 .into_any_element()
607 }
608
609 fn telemetry_event_text(&self) -> Option<&'static str> {
610 Some("Project Diagnostics Opened")
611 }
612
613 fn for_each_project_item(
614 &self,
615 cx: &App,
616 f: &mut dyn FnMut(gpui::EntityId, &dyn project::ProjectItem),
617 ) {
618 self.editor.for_each_project_item(cx, f)
619 }
620
621 fn is_singleton(&self, _: &App) -> bool {
622 false
623 }
624
625 fn set_nav_history(
626 &mut self,
627 nav_history: ItemNavHistory,
628 _: &mut Window,
629 cx: &mut Context<Self>,
630 ) {
631 self.editor.update(cx, |editor, _| {
632 editor.set_nav_history(Some(nav_history));
633 });
634 }
635
636 fn clone_on_split(
637 &self,
638 _workspace_id: Option<workspace::WorkspaceId>,
639 window: &mut Window,
640 cx: &mut Context<Self>,
641 ) -> Option<Entity<Self>>
642 where
643 Self: Sized,
644 {
645 Some(cx.new(|cx| {
646 ProjectDiagnosticsEditor::new(
647 self.include_warnings,
648 self.project.clone(),
649 self.workspace.clone(),
650 window,
651 cx,
652 )
653 }))
654 }
655
656 fn is_dirty(&self, cx: &App) -> bool {
657 self.multibuffer.read(cx).is_dirty(cx)
658 }
659
660 fn has_deleted_file(&self, cx: &App) -> bool {
661 self.multibuffer.read(cx).has_deleted_file(cx)
662 }
663
664 fn has_conflict(&self, cx: &App) -> bool {
665 self.multibuffer.read(cx).has_conflict(cx)
666 }
667
668 fn can_save(&self, _: &App) -> bool {
669 true
670 }
671
672 fn save(
673 &mut self,
674 format: bool,
675 project: Entity<Project>,
676 window: &mut Window,
677 cx: &mut Context<Self>,
678 ) -> Task<Result<()>> {
679 self.editor.save(format, project, window, cx)
680 }
681
682 fn save_as(
683 &mut self,
684 _: Entity<Project>,
685 _: ProjectPath,
686 _window: &mut Window,
687 _: &mut Context<Self>,
688 ) -> Task<Result<()>> {
689 unreachable!()
690 }
691
692 fn reload(
693 &mut self,
694 project: Entity<Project>,
695 window: &mut Window,
696 cx: &mut Context<Self>,
697 ) -> Task<Result<()>> {
698 self.editor.reload(project, window, cx)
699 }
700
701 fn act_as_type<'a>(
702 &'a self,
703 type_id: TypeId,
704 self_handle: &'a Entity<Self>,
705 _: &'a App,
706 ) -> Option<AnyView> {
707 if type_id == TypeId::of::<Self>() {
708 Some(self_handle.to_any())
709 } else if type_id == TypeId::of::<Editor>() {
710 Some(self.editor.to_any())
711 } else {
712 None
713 }
714 }
715
716 fn as_searchable(&self, _: &Entity<Self>) -> Option<Box<dyn SearchableItemHandle>> {
717 Some(Box::new(self.editor.clone()))
718 }
719
720 fn breadcrumb_location(&self, _: &App) -> ToolbarItemLocation {
721 ToolbarItemLocation::PrimaryLeft
722 }
723
724 fn breadcrumbs(&self, theme: &theme::Theme, cx: &App) -> Option<Vec<BreadcrumbText>> {
725 self.editor.breadcrumbs(theme, cx)
726 }
727
728 fn added_to_workspace(
729 &mut self,
730 workspace: &mut Workspace,
731 window: &mut Window,
732 cx: &mut Context<Self>,
733 ) {
734 self.editor.update(cx, |editor, cx| {
735 editor.added_to_workspace(workspace, window, cx)
736 });
737 }
738}
739
740const DIAGNOSTIC_EXPANSION_ROW_LIMIT: u32 = 32;
741
742async fn context_range_for_entry(
743 range: Range<Point>,
744 context: u32,
745 snapshot: BufferSnapshot,
746 cx: &mut AsyncApp,
747) -> Range<Point> {
748 if let Some(rows) = heuristic_syntactic_expand(
749 range.clone(),
750 DIAGNOSTIC_EXPANSION_ROW_LIMIT,
751 snapshot.clone(),
752 cx,
753 )
754 .await
755 {
756 return Range {
757 start: Point::new(*rows.start(), 0),
758 end: snapshot.clip_point(Point::new(*rows.end(), u32::MAX), Bias::Left),
759 };
760 }
761 Range {
762 start: Point::new(range.start.row.saturating_sub(context), 0),
763 end: snapshot.clip_point(Point::new(range.end.row + context, u32::MAX), Bias::Left),
764 }
765}
766
767/// Expands the input range using syntax information from TreeSitter. This expansion will be limited
768/// to the specified `max_row_count`.
769///
770/// If there is a containing outline item that is less than `max_row_count`, it will be returned.
771/// Otherwise fairly arbitrary heuristics are applied to attempt to return a logical block of code.
772async fn heuristic_syntactic_expand(
773 input_range: Range<Point>,
774 max_row_count: u32,
775 snapshot: BufferSnapshot,
776 cx: &mut AsyncApp,
777) -> Option<RangeInclusive<BufferRow>> {
778 let input_row_count = input_range.end.row - input_range.start.row;
779 if input_row_count > max_row_count {
780 return None;
781 }
782
783 // If the outline node contains the diagnostic and is small enough, just use that.
784 let outline_range = snapshot.outline_range_containing(input_range.clone());
785 if let Some(outline_range) = outline_range.clone() {
786 // Remove blank lines from start and end
787 if let Some(start_row) = (outline_range.start.row..outline_range.end.row)
788 .find(|row| !snapshot.line_indent_for_row(*row).is_line_blank())
789 {
790 if let Some(end_row) = (outline_range.start.row..outline_range.end.row + 1)
791 .rev()
792 .find(|row| !snapshot.line_indent_for_row(*row).is_line_blank())
793 {
794 let row_count = end_row.saturating_sub(start_row);
795 if row_count <= max_row_count {
796 return Some(RangeInclusive::new(
797 outline_range.start.row,
798 outline_range.end.row,
799 ));
800 }
801 }
802 }
803 }
804
805 let mut node = snapshot.syntax_ancestor(input_range.clone())?;
806
807 loop {
808 let node_start = Point::from_ts_point(node.start_position());
809 let node_end = Point::from_ts_point(node.end_position());
810 let node_range = node_start..node_end;
811 let row_count = node_end.row - node_start.row + 1;
812 let mut ancestor_range = None;
813 let reached_outline_node = cx.background_executor().scoped({
814 let node_range = node_range.clone();
815 let outline_range = outline_range.clone();
816 let ancestor_range = &mut ancestor_range;
817 |scope| {scope.spawn(async move {
818 // Stop if we've exceeded the row count or reached an outline node. Then, find the interval
819 // of node children which contains the query range. For example, this allows just returning
820 // the header of a declaration rather than the entire declaration.
821 if row_count > max_row_count || outline_range == Some(node_range.clone()) {
822 let mut cursor = node.walk();
823 let mut included_child_start = None;
824 let mut included_child_end = None;
825 let mut previous_end = node_start;
826 if cursor.goto_first_child() {
827 loop {
828 let child_node = cursor.node();
829 let child_range = previous_end..Point::from_ts_point(child_node.end_position());
830 if included_child_start.is_none() && child_range.contains(&input_range.start) {
831 included_child_start = Some(child_range.start);
832 }
833 if child_range.contains(&input_range.end) {
834 included_child_end = Some(child_range.end);
835 }
836 previous_end = child_range.end;
837 if !cursor.goto_next_sibling() {
838 break;
839 }
840 }
841 }
842 let end = included_child_end.unwrap_or(node_range.end);
843 if let Some(start) = included_child_start {
844 let row_count = end.row - start.row;
845 if row_count < max_row_count {
846 *ancestor_range = Some(Some(RangeInclusive::new(start.row, end.row)));
847 return;
848 }
849 }
850
851 log::info!(
852 "Expanding to ancestor started on {} node exceeding row limit of {max_row_count}.",
853 node.grammar_name()
854 );
855 *ancestor_range = Some(None);
856 }
857 })
858 }});
859 reached_outline_node.await;
860 if let Some(node) = ancestor_range {
861 return node;
862 }
863
864 let node_name = node.grammar_name();
865 let node_row_range = RangeInclusive::new(node_range.start.row, node_range.end.row);
866 if node_name.ends_with("block") {
867 return Some(node_row_range);
868 } else if node_name.ends_with("statement") || node_name.ends_with("declaration") {
869 // Expand to the nearest dedent or blank line for statements and declarations.
870 let tab_size = cx
871 .update(|cx| snapshot.settings_at(node_range.start, cx).tab_size.get())
872 .ok()?;
873 let indent_level = snapshot
874 .line_indent_for_row(node_range.start.row)
875 .len(tab_size);
876 let rows_remaining = max_row_count.saturating_sub(row_count);
877 let Some(start_row) = (node_range.start.row.saturating_sub(rows_remaining)
878 ..node_range.start.row)
879 .rev()
880 .find(|row| {
881 is_line_blank_or_indented_less(indent_level, *row, tab_size, &snapshot.clone())
882 })
883 else {
884 return Some(node_row_range);
885 };
886 let rows_remaining = max_row_count.saturating_sub(node_range.end.row - start_row);
887 let Some(end_row) = (node_range.end.row + 1
888 ..cmp::min(
889 node_range.end.row + rows_remaining + 1,
890 snapshot.row_count(),
891 ))
892 .find(|row| {
893 is_line_blank_or_indented_less(indent_level, *row, tab_size, &snapshot.clone())
894 })
895 else {
896 return Some(node_row_range);
897 };
898 return Some(RangeInclusive::new(start_row, end_row));
899 }
900
901 // TODO: doing this instead of walking a cursor as that doesn't work - why?
902 let Some(parent) = node.parent() else {
903 log::info!(
904 "Expanding to ancestor reached the top node, so using default context line count.",
905 );
906 return None;
907 };
908 node = parent;
909 }
910}
911
912fn is_line_blank_or_indented_less(
913 indent_level: u32,
914 row: u32,
915 tab_size: u32,
916 snapshot: &BufferSnapshot,
917) -> bool {
918 let line_indent = snapshot.line_indent_for_row(row);
919 line_indent.is_line_blank() || line_indent.len(tab_size) < indent_level
920}