|
1 | 1 | //! `render_diagnostics`: diagnostics as rustc-style text, the source line underlined at the span. |
2 | 2 |
|
3 | | -use std::fmt::Write; |
| 3 | +use crate::diagnostics::constants::PLAIN_PALETTE; |
| 4 | +use crate::diagnostics::main::render_styled_diagnostics::render_styled_diagnostics; |
| 5 | +use crate::diagnostics::models::Diagnostic; |
4 | 6 |
|
5 | | -use crate::diagnostics::_helpers::line_index::LineIndex; |
6 | | -use crate::diagnostics::constants::{MAX_SHOWN_LINE, TAB_WIDTH}; |
7 | | -use crate::diagnostics::models::{Diagnostic, DiagnosticSpan}; |
8 | | - |
9 | | -/// Every diagnostic of `source`, read from `path`, as text separated by blank lines. |
| 7 | +/// Every diagnostic of `source`, read from `path`, as plain text separated by blank lines. |
10 | 8 | pub fn render_diagnostics(diagnostics: &[Diagnostic], source: &str, path: &str) -> String { |
11 | | - let lines = LineIndex::new(source); |
12 | | - diagnostics |
13 | | - .iter() |
14 | | - .map(|diagnostic| render_one(diagnostic, &lines, path)) |
15 | | - .collect::<Vec<String>>() |
16 | | - .join("\n") |
17 | | -} |
18 | | - |
19 | | -fn render_one(diagnostic: &Diagnostic, lines: &LineIndex<'_>, path: &str) -> String { |
20 | | - let mut text = format!( |
21 | | - "{}[{}]: {}\n", |
22 | | - diagnostic.severity.label(), |
23 | | - diagnostic.code, |
24 | | - diagnostic.message |
25 | | - ); |
26 | | - let gutter = " ".repeat( |
27 | | - diagnostic |
28 | | - .span |
29 | | - .map_or(1, |span| span.line.to_string().len()), |
30 | | - ); |
31 | | - match diagnostic.span { |
32 | | - Some(span) => { |
33 | | - let _ = writeln!(text, "{gutter}--> {path}:{}:{}", span.line, span.column); |
34 | | - let (shown, underline) = excerpt(lines.text(span.line), span); |
35 | | - let _ = writeln!(text, "{gutter} |"); |
36 | | - let _ = writeln!(text, "{} | {shown}", span.line); |
37 | | - let _ = writeln!(text, "{gutter} | {underline} {}", diagnostic.label); |
38 | | - if !diagnostic.notes.is_empty() || diagnostic.help.is_some() { |
39 | | - let _ = writeln!(text, "{gutter} |"); |
40 | | - } |
41 | | - } |
42 | | - None => { |
43 | | - let _ = writeln!(text, "{gutter}--> {path}"); |
44 | | - let _ = writeln!(text, "{gutter} |"); |
45 | | - let _ = writeln!(text, "{gutter} = note: {}", diagnostic.label); |
46 | | - } |
47 | | - } |
48 | | - for note in &diagnostic.notes { |
49 | | - let _ = writeln!(text, "{gutter} = note: {note}"); |
50 | | - } |
51 | | - if let Some(help) = &diagnostic.help { |
52 | | - let _ = writeln!(text, "{gutter} = help: {help}"); |
53 | | - } |
54 | | - text |
55 | | -} |
56 | | - |
57 | | -/// The line as shown, windowed when long and with tabs expanded, and its underline. |
58 | | -fn excerpt(line: &str, span: DiagnosticSpan) -> (String, String) { |
59 | | - let characters: Vec<char> = line.chars().collect(); |
60 | | - let first = span.column.saturating_sub(1).min(characters.len()); |
61 | | - let last = match span.end_line == span.line { |
62 | | - true => span |
63 | | - .end_column |
64 | | - .saturating_sub(1) |
65 | | - .clamp(first, characters.len()), |
66 | | - false => characters.len(), |
67 | | - } |
68 | | - .max(first + 1); |
69 | | - let window_start = match characters.len() > MAX_SHOWN_LINE { |
70 | | - true => first.saturating_sub(MAX_SHOWN_LINE / 2), |
71 | | - false => 0, |
72 | | - }; |
73 | | - let window_end = (window_start + MAX_SHOWN_LINE).min(characters.len()); |
74 | | - let mut shown = String::new(); |
75 | | - let mut underline = String::new(); |
76 | | - if window_start > 0 { |
77 | | - shown.push('…'); |
78 | | - underline.push(' '); |
79 | | - } |
80 | | - for (index, character) in characters[window_start..window_end].iter().enumerate() { |
81 | | - let offset = window_start + index; |
82 | | - let width = match character { |
83 | | - '\t' => TAB_WIDTH, |
84 | | - _ => 1, |
85 | | - }; |
86 | | - match character { |
87 | | - '\t' => shown.push_str(&" ".repeat(TAB_WIDTH)), |
88 | | - other => shown.push(*other), |
89 | | - } |
90 | | - let mark = match (first..last).contains(&offset) { |
91 | | - true => '^', |
92 | | - false => ' ', |
93 | | - }; |
94 | | - underline.push_str(&mark.to_string().repeat(width)); |
95 | | - } |
96 | | - if window_end < characters.len() { |
97 | | - shown.push('…'); |
98 | | - } |
99 | | - let marked = last.min(window_end.max(first + 1)); |
100 | | - for _ in window_end.max(first)..marked { |
101 | | - underline.push('^'); |
102 | | - } |
103 | | - (shown, underline.trim_end().to_owned()) |
| 9 | + render_styled_diagnostics(diagnostics, source, path, &PLAIN_PALETTE) |
104 | 10 | } |
0 commit comments