Debian Codegraph @

helix-view/src/handlers/diagnostics.rs

raw symbols SCIP
1 use std::cell::Cell;
2 use std::num::NonZeroUsize;
3 use std::sync::atomic::{self, AtomicUsize};
4 use std::sync::Arc;
5 use std::time::Duration;
6
7 use helix_event::{request_redraw, send_blocking, AsyncHook};
8 use tokio::sync::mpsc::Sender;
9 use tokio::time::Instant;
10
11 use crate::{Document, DocumentId, ViewId};
12
13 #[derive(Debug)]
14 pub enum DiagnosticEvent {
15 CursorLineChanged { generation: usize },
16 Refresh,
17 }
18
19 struct DiagnosticTimeout {
20 active_generation: Arc<AtomicUsize>,
21 generation: usize,
22 }
23
24 const TIMEOUT: Duration = Duration::from_millis(350);
25
26 impl AsyncHook for DiagnosticTimeout {
27 type Event = DiagnosticEvent;
28
29 fn handle_event(
30 &mut self,
31 event: DiagnosticEvent,
32 timeout: Option<Instant>,
33 ) -> Option<Instant> {
34 match event {
35 DiagnosticEvent::CursorLineChanged { generation } => {
36 if generation > self.generation {
37 self.generation = generation;
38 Some(Instant::now() + TIMEOUT)
39 } else {
40 timeout
41 }
42 }
43 DiagnosticEvent::Refresh if timeout.is_some() => Some(Instant::now() + TIMEOUT),
44 DiagnosticEvent::Refresh => None,
45 }
46 }
47
48 fn finish_debounce(&mut self) {
49 if self.active_generation.load(atomic::Ordering::Relaxed) < self.generation {
50 self.active_generation
51 .store(self.generation, atomic::Ordering::Relaxed);
52 request_redraw();
53 }
54 }
55 }
56
57 pub struct DiagnosticsHandler {
58 active_generation: Arc<AtomicUsize>,
59 generation: Cell<usize>,
60 last_doc: Cell<DocumentId>,
61 last_cursor_line: Cell<usize>,
62 pub active: bool,
63 pub events: Sender<DiagnosticEvent>,
64 }
65
66 // make sure we never share handlers across multiple views this is a stop
67 // gap solution. We just shouldn't be cloneing a view to begin with (we do
68 // for :hsplit/vsplit) and really this should not be view specific to begin with
69 // but to fix that larger architecutre changes are needed
70 impl Clone for DiagnosticsHandler {
71 fn clone(&self) -> Self {
72 Self::new()
73 }
74 }
75
76 impl DiagnosticsHandler {
77 #[allow(clippy::new_without_default)]
78 pub fn new() -> Self {
79 let active_generation = Arc::new(AtomicUsize::new(0));
80 let events = DiagnosticTimeout {
81 active_generation: active_generation.clone(),
82 generation: 0,
83 }
84 .spawn();
85 Self {
86 active_generation,
87 generation: Cell::new(0),
88 events,
89 last_doc: Cell::new(DocumentId(NonZeroUsize::new(usize::MAX).unwrap())),
90 last_cursor_line: Cell::new(usize::MAX),
91 active: true,
92 }
93 }
94 }
95
96 impl DiagnosticsHandler {
97 pub fn immediately_show_diagnostic(&self, doc: &Document, view: ViewId) {
98 self.last_doc.set(doc.id());
99 let cursor_line = doc
100 .selection(view)
101 .primary()
102 .cursor_line(doc.text().slice(..));
103 self.last_cursor_line.set(cursor_line);
104 self.active_generation
105 .store(self.generation.get(), atomic::Ordering::Relaxed);
106 }
107 pub fn show_cursorline_diagnostics(&self, doc: &Document, view: ViewId) -> bool {
108 if !self.active {
109 return false;
110 }
111 let cursor_line = doc
112 .selection(view)
113 .primary()
114 .cursor_line(doc.text().slice(..));
115 if self.last_cursor_line.get() == cursor_line && self.last_doc.get() == doc.id() {
116 let active_generation = self.active_generation.load(atomic::Ordering::Relaxed);
117 self.generation.get() == active_generation
118 } else {
119 self.last_doc.set(doc.id());
120 self.last_cursor_line.set(cursor_line);
121 self.generation.set(self.generation.get() + 1);
122 send_blocking(
123 &self.events,
124 DiagnosticEvent::CursorLineChanged {
125 generation: self.generation.get(),
126 },
127 );
128 false
129 }
130 }
131 }