| 1 |
use helix_core::diagnostic::Severity; |
| 2 |
use helix_core::doc_formatter::{FormattedGrapheme, TextFormat}; |
| 3 |
use helix_core::text_annotations::LineAnnotation; |
| 4 |
use helix_core::{softwrapped_dimensions, Diagnostic, Position}; |
| 5 |
use serde::{Deserialize, Serialize}; |
| 6 |
|
| 7 |
use crate::Document; |
| 8 |
|
| 9 |
|
| 10 |
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord)] |
| 11 |
pub enum DiagnosticFilter { |
| 12 |
Disable, |
| 13 |
Enable(Severity), |
| 14 |
} |
| 15 |
|
| 16 |
impl<'de> Deserialize<'de> for DiagnosticFilter { |
| 17 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 18 |
where |
| 19 |
D: serde::Deserializer<'de>, |
| 20 |
{ |
| 21 |
match &*String::deserialize(deserializer)? { |
| 22 |
"disable" => Ok(DiagnosticFilter::Disable), |
| 23 |
"hint" => Ok(DiagnosticFilter::Enable(Severity::Hint)), |
| 24 |
"info" => Ok(DiagnosticFilter::Enable(Severity::Info)), |
| 25 |
"warning" => Ok(DiagnosticFilter::Enable(Severity::Warning)), |
| 26 |
"error" => Ok(DiagnosticFilter::Enable(Severity::Error)), |
| 27 |
variant => Err(serde::de::Error::unknown_variant( |
| 28 |
variant, |
| 29 |
&["disable", "hint", "info", "warning", "error"], |
| 30 |
)), |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
impl Serialize for DiagnosticFilter { |
| 36 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 37 |
where |
| 38 |
S: serde::Serializer, |
| 39 |
{ |
| 40 |
let filter = match self { |
| 41 |
DiagnosticFilter::Disable => "disable", |
| 42 |
DiagnosticFilter::Enable(Severity::Hint) => "hint", |
| 43 |
DiagnosticFilter::Enable(Severity::Info) => "info", |
| 44 |
DiagnosticFilter::Enable(Severity::Warning) => "warning", |
| 45 |
DiagnosticFilter::Enable(Severity::Error) => "error", |
| 46 |
}; |
| 47 |
filter.serialize(serializer) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 52 |
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)] |
| 53 |
pub struct InlineDiagnosticsConfig { |
| 54 |
pub cursor_line: DiagnosticFilter, |
| 55 |
pub other_lines: DiagnosticFilter, |
| 56 |
pub min_diagnostic_width: u16, |
| 57 |
pub prefix_len: u16, |
| 58 |
pub max_wrap: u16, |
| 59 |
pub max_diagnostics: usize, |
| 60 |
} |
| 61 |
|
| 62 |
impl InlineDiagnosticsConfig { |
| 63 |
pub fn disabled(&self) -> bool { |
| 64 |
matches!( |
| 65 |
self, |
| 66 |
Self { |
| 67 |
cursor_line: DiagnosticFilter::Disable, |
| 68 |
other_lines: DiagnosticFilter::Disable, |
| 69 |
.. |
| 70 |
} |
| 71 |
) |
| 72 |
} |
| 73 |
|
| 74 |
pub fn prepare(&self, width: u16, enable_cursor_line: bool) -> Self { |
| 75 |
let mut config = self.clone(); |
| 76 |
if width < self.min_diagnostic_width + self.prefix_len { |
| 77 |
config.cursor_line = DiagnosticFilter::Disable; |
| 78 |
config.other_lines = DiagnosticFilter::Disable; |
| 79 |
} else if !enable_cursor_line { |
| 80 |
config.cursor_line = self.cursor_line.min(self.other_lines); |
| 81 |
} |
| 82 |
config |
| 83 |
} |
| 84 |
|
| 85 |
pub fn max_diagnostic_start(&self, width: u16) -> u16 { |
| 86 |
width - self.min_diagnostic_width - self.prefix_len |
| 87 |
} |
| 88 |
|
| 89 |
pub fn text_fmt(&self, anchor_col: u16, width: u16) -> TextFormat { |
| 90 |
let width = if anchor_col > self.max_diagnostic_start(width) { |
| 91 |
self.min_diagnostic_width |
| 92 |
} else { |
| 93 |
width - anchor_col - self.prefix_len |
| 94 |
}; |
| 95 |
|
| 96 |
TextFormat { |
| 97 |
soft_wrap: true, |
| 98 |
tab_width: 4, |
| 99 |
max_wrap: self.max_wrap.min(width / 4), |
| 100 |
max_indent_retain: 0, |
| 101 |
wrap_indicator: "".into(), |
| 102 |
wrap_indicator_highlight: None, |
| 103 |
viewport_width: width, |
| 104 |
soft_wrap_at_text_width: true, |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
impl Default for InlineDiagnosticsConfig { |
| 110 |
fn default() -> Self { |
| 111 |
InlineDiagnosticsConfig { |
| 112 |
cursor_line: DiagnosticFilter::Enable(Severity::Warning), |
| 113 |
other_lines: DiagnosticFilter::Disable, |
| 114 |
min_diagnostic_width: 40, |
| 115 |
prefix_len: 1, |
| 116 |
max_wrap: 20, |
| 117 |
max_diagnostics: 10, |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
pub struct InlineDiagnosticAccumulator<'a> { |
| 123 |
idx: usize, |
| 124 |
doc: &'a Document, |
| 125 |
pub stack: Vec<(&'a Diagnostic, u16)>, |
| 126 |
pub config: InlineDiagnosticsConfig, |
| 127 |
cursor: usize, |
| 128 |
cursor_line: bool, |
| 129 |
} |
| 130 |
|
| 131 |
impl<'a> InlineDiagnosticAccumulator<'a> { |
| 132 |
pub fn new(cursor: usize, doc: &'a Document, config: InlineDiagnosticsConfig) -> Self { |
| 133 |
InlineDiagnosticAccumulator { |
| 134 |
idx: 0, |
| 135 |
doc, |
| 136 |
stack: Vec::new(), |
| 137 |
config, |
| 138 |
cursor, |
| 139 |
cursor_line: false, |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
pub fn reset_pos(&mut self, char_idx: usize) -> usize { |
| 144 |
self.idx = 0; |
| 145 |
self.clear(); |
| 146 |
self.skip_concealed(char_idx) |
| 147 |
} |
| 148 |
|
| 149 |
pub fn skip_concealed(&mut self, conceal_end_char_idx: usize) -> usize { |
| 150 |
let diagnostics = &self.doc.diagnostics[self.idx..]; |
| 151 |
let idx = diagnostics.partition_point(|diag| diag.range.start < conceal_end_char_idx); |
| 152 |
self.idx += idx; |
| 153 |
self.next_anchor(conceal_end_char_idx) |
| 154 |
} |
| 155 |
|
| 156 |
pub fn next_anchor(&self, current_char_idx: usize) -> usize { |
| 157 |
let next_diag_start = self |
| 158 |
.doc |
| 159 |
.diagnostics |
| 160 |
.get(self.idx) |
| 161 |
.map_or(usize::MAX, |diag| diag.range.start); |
| 162 |
if (current_char_idx..next_diag_start).contains(&self.cursor) { |
| 163 |
self.cursor |
| 164 |
} else { |
| 165 |
next_diag_start |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
pub fn clear(&mut self) { |
| 170 |
self.cursor_line = false; |
| 171 |
self.stack.clear(); |
| 172 |
} |
| 173 |
|
| 174 |
fn process_anchor_impl( |
| 175 |
&mut self, |
| 176 |
grapheme: &FormattedGrapheme, |
| 177 |
width: u16, |
| 178 |
horizontal_off: usize, |
| 179 |
) -> bool { |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
if grapheme.char_idx == self.cursor { |
| 184 |
self.cursor_line = true; |
| 185 |
if self |
| 186 |
.doc |
| 187 |
.diagnostics |
| 188 |
.get(self.idx) |
| 189 |
.is_none_or(|diag| diag.range.start != grapheme.char_idx) |
| 190 |
{ |
| 191 |
return false; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
let Some(anchor_col) = grapheme.visual_pos.col.checked_sub(horizontal_off) else { |
| 196 |
return true; |
| 197 |
}; |
| 198 |
if anchor_col >= width as usize { |
| 199 |
return true; |
| 200 |
} |
| 201 |
|
| 202 |
for diag in &self.doc.diagnostics[self.idx..] { |
| 203 |
if diag.range.start != grapheme.char_idx { |
| 204 |
break; |
| 205 |
} |
| 206 |
self.stack.push((diag, anchor_col as u16)); |
| 207 |
self.idx += 1; |
| 208 |
} |
| 209 |
false |
| 210 |
} |
| 211 |
|
| 212 |
pub fn proccess_anchor( |
| 213 |
&mut self, |
| 214 |
grapheme: &FormattedGrapheme, |
| 215 |
width: u16, |
| 216 |
horizontal_off: usize, |
| 217 |
) -> usize { |
| 218 |
if self.process_anchor_impl(grapheme, width, horizontal_off) { |
| 219 |
self.idx += self.doc.diagnostics[self.idx..] |
| 220 |
.iter() |
| 221 |
.take_while(|diag| diag.range.start == grapheme.char_idx) |
| 222 |
.count(); |
| 223 |
} |
| 224 |
self.next_anchor(grapheme.char_idx + 1) |
| 225 |
} |
| 226 |
|
| 227 |
pub fn filter(&self) -> DiagnosticFilter { |
| 228 |
if self.cursor_line { |
| 229 |
self.config.cursor_line |
| 230 |
} else { |
| 231 |
self.config.other_lines |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
pub fn compute_line_diagnostics(&mut self) { |
| 236 |
let filter = if self.cursor_line { |
| 237 |
self.cursor_line = false; |
| 238 |
self.config.cursor_line |
| 239 |
} else { |
| 240 |
self.config.other_lines |
| 241 |
}; |
| 242 |
let DiagnosticFilter::Enable(filter) = filter else { |
| 243 |
self.stack.clear(); |
| 244 |
return; |
| 245 |
}; |
| 246 |
self.stack.retain(|(diag, _)| diag.severity() >= filter); |
| 247 |
self.stack.truncate(self.config.max_diagnostics) |
| 248 |
} |
| 249 |
|
| 250 |
pub fn has_multi(&self, width: u16) -> bool { |
| 251 |
self.stack |
| 252 |
.last() |
| 253 |
.is_some_and(|&(_, anchor)| anchor > self.config.max_diagnostic_start(width)) |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
pub(crate) struct InlineDiagnostics<'a> { |
| 258 |
state: InlineDiagnosticAccumulator<'a>, |
| 259 |
width: u16, |
| 260 |
horizontal_off: usize, |
| 261 |
} |
| 262 |
|
| 263 |
impl<'a> InlineDiagnostics<'a> { |
| 264 |
#[allow(clippy::new_ret_no_self)] |
| 265 |
pub(crate) fn new( |
| 266 |
doc: &'a Document, |
| 267 |
cursor: usize, |
| 268 |
width: u16, |
| 269 |
horizontal_off: usize, |
| 270 |
config: InlineDiagnosticsConfig, |
| 271 |
) -> Box<dyn LineAnnotation + 'a> { |
| 272 |
Box::new(InlineDiagnostics { |
| 273 |
state: InlineDiagnosticAccumulator::new(cursor, doc, config), |
| 274 |
width, |
| 275 |
horizontal_off, |
| 276 |
}) |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
impl LineAnnotation for InlineDiagnostics<'_> { |
| 281 |
fn reset_pos(&mut self, char_idx: usize) -> usize { |
| 282 |
self.state.reset_pos(char_idx) |
| 283 |
} |
| 284 |
|
| 285 |
fn skip_concealed_anchors(&mut self, conceal_end_char_idx: usize) -> usize { |
| 286 |
self.state.skip_concealed(conceal_end_char_idx) |
| 287 |
} |
| 288 |
|
| 289 |
fn process_anchor(&mut self, grapheme: &FormattedGrapheme) -> usize { |
| 290 |
self.state |
| 291 |
.proccess_anchor(grapheme, self.width, self.horizontal_off) |
| 292 |
} |
| 293 |
|
| 294 |
fn insert_virtual_lines( |
| 295 |
&mut self, |
| 296 |
_line_end_char_idx: usize, |
| 297 |
_line_end_visual_pos: Position, |
| 298 |
_doc_line: usize, |
| 299 |
) -> Position { |
| 300 |
self.state.compute_line_diagnostics(); |
| 301 |
let multi = self.state.has_multi(self.width); |
| 302 |
let diagostic_height: usize = self |
| 303 |
.state |
| 304 |
.stack |
| 305 |
.drain(..) |
| 306 |
.map(|(diag, anchor)| { |
| 307 |
let text_fmt = self.state.config.text_fmt(anchor, self.width); |
| 308 |
softwrapped_dimensions(diag.message.as_str().trim().into(), &text_fmt).0 |
| 309 |
}) |
| 310 |
.sum(); |
| 311 |
Position::new(multi as usize + diagostic_height, 0) |
| 312 |
} |
| 313 |
} |