1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
use std::{
io::{self, Write},
str,
};
use crate::{config::OutFormat, lint::LintResult};
use ariadne::{
CharSet, Color, Config as CliConfig, Fmt, Label, LabelAttach, Report as CliReport,
ReportKind as CliReportKind, Source,
};
use rnix::{TextRange, TextSize};
use vfs::ReadOnlyVfs;
pub trait WriteDiagnostic {
fn write(
&mut self,
report: &LintResult,
vfs: &ReadOnlyVfs,
format: OutFormat,
) -> io::Result<()>;
}
impl<T> WriteDiagnostic for T
where
T: Write,
{
fn write(
&mut self,
lint_result: &LintResult,
vfs: &ReadOnlyVfs,
format: OutFormat,
) -> io::Result<()> {
match format {
#[cfg(feature = "json")]
OutFormat::Json => json::write_json(self, lint_result, vfs),
OutFormat::StdErr => write_stderr(self, lint_result, vfs),
OutFormat::Errfmt => write_errfmt(self, lint_result, vfs),
}
}
}
fn write_stderr<T: Write>(
writer: &mut T,
lint_result: &LintResult,
vfs: &ReadOnlyVfs,
) -> io::Result<()> {
let file_id = lint_result.file_id;
let src = str::from_utf8(vfs.get(file_id)).unwrap();
let path = vfs.file_path(file_id);
let range = |at: TextRange| at.start().into()..at.end().into();
let src_id = path.to_str().unwrap_or("<unknown>");
for report in lint_result.reports.iter() {
let offset = report
.diagnostics
.iter()
.map(|d| d.at.start().into())
.min()
.unwrap_or(0usize);
report
.diagnostics
.iter()
.fold(
CliReport::build(CliReportKind::Warning, src_id, offset)
.with_config(
CliConfig::default()
.with_cross_gap(true)
.with_multiline_arrows(false)
.with_label_attach(LabelAttach::Middle)
.with_char_set(CharSet::Unicode),
)
.with_message(report.note)
.with_code(report.code),
|cli_report, diagnostic| {
cli_report.with_label(
Label::new((src_id, range(diagnostic.at)))
.with_message(&colorize(&diagnostic.message))
.with_color(Color::Magenta),
)
},
)
.finish()
.write((src_id, Source::from(src)), &mut *writer)?;
}
Ok(())
}
fn write_errfmt<T: Write>(
writer: &mut T,
lint_result: &LintResult,
vfs: &ReadOnlyVfs,
) -> io::Result<()> {
let file_id = lint_result.file_id;
let src = str::from_utf8(vfs.get(file_id)).unwrap();
let path = vfs.file_path(file_id);
for report in lint_result.reports.iter() {
for diagnostic in report.diagnostics.iter() {
let line = line(diagnostic.at.start(), &src);
let col = column(diagnostic.at.start(), &src);
writeln!(
writer,
"{filename}>{linenumber}:{columnnumber}:{errortype}:{errornumber}:{errormessage}",
filename = path.to_str().unwrap_or("<unknown>"),
linenumber = line,
columnnumber = col,
errortype = "W",
errornumber = report.code,
errormessage = diagnostic.message
)?;
}
}
Ok(())
}
#[cfg(feature = "json")]
mod json {
use crate::lint::LintResult;
use std::io::{self, Write};
use rnix::TextRange;
use serde::Serialize;
use serde_json;
use vfs::ReadOnlyVfs;
#[derive(Serialize)]
struct Out<'μ> {
#[serde(rename = "file")]
path: &'μ std::path::Path,
report: Vec<JsonReport<'μ>>,
}
#[derive(Serialize)]
struct JsonReport<'μ> {
note: &'static str,
code: u32,
diagnostics: Vec<JsonDiagnostic<'μ>>,
}
#[derive(Serialize)]
struct JsonDiagnostic<'μ> {
at: JsonSpan,
message: &'μ String,
suggestion: Option<JsonSuggestion>,
}
#[derive(Serialize)]
struct JsonSuggestion {
at: JsonSpan,
fix: String,
}
#[derive(Serialize)]
struct JsonSpan {
from: Position,
to: Position,
}
#[derive(Serialize)]
struct Position {
line: usize,
column: usize,
}
impl JsonSpan {
fn from_textrange(at: TextRange, src: &str) -> Self {
let start = at.start();
let end = at.end();
let from = Position {
line: super::line(start, src),
column: super::column(start, src),
};
let to = Position {
line: super::line(end, src),
column: super::column(end, src),
};
Self { from, to }
}
}
pub fn write_json<T: Write>(
writer: &mut T,
lint_result: &LintResult,
vfs: &ReadOnlyVfs,
) -> io::Result<()> {
let file_id = lint_result.file_id;
let path = vfs.file_path(file_id);
let src = vfs.get_str(file_id);
let report = lint_result
.reports
.iter()
.map(|r| {
let note = r.note;
let code = r.code;
let diagnostics = r
.diagnostics
.iter()
.map(|d| JsonDiagnostic {
at: JsonSpan::from_textrange(d.at, src),
message: &d.message,
suggestion: d.suggestion.as_ref().map(|s| JsonSuggestion {
at: JsonSpan::from_textrange(s.at, src),
fix: s.fix.to_string(),
}),
})
.collect::<Vec<_>>();
JsonReport {
note,
code,
diagnostics,
}
})
.collect();
writeln!(
writer,
"{}",
serde_json::to_string_pretty(&Out { path, report }).unwrap()
)?;
Ok(())
}
}
fn line(at: TextSize, src: &str) -> usize {
let at = at.into();
src[..at].chars().filter(|&c| c == '\n').count() + 1
}
fn column(at: TextSize, src: &str) -> usize {
let at = at.into();
src[..at].rfind('\n').map(|c| at - c).unwrap_or(at + 1)
}
// everything within backticks is colorized, backticks are removed
fn colorize(message: &str) -> String {
message
.split('`')
.enumerate()
.map(|(idx, part)| {
if idx % 2 == 1 {
part.fg(Color::Cyan).to_string()
} else {
part.to_string()
}
})
.collect::<Vec<_>>()
.join("")
}
|