feat(xatu): report diagnostic line ranges

This commit is contained in:
2026-09-26 19:59:42 -04:00
parent 41076e5778
commit a295bed5c0
2 changed files with 23 additions and 3 deletions

View File

@@ -27,8 +27,8 @@ impl std::fmt::Display for Diagnostic {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
formatter,
"{}: {}: {}",
self.finding.range, self.finding.rule_id, self.finding.message,
"{}: {}",
self.finding.rule_id, self.finding.message
)
}
}

View File

@@ -25,13 +25,33 @@ impl std::fmt::Display for Report {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for diagnostic in &self.diagnostics {
let file = self.files.get(diagnostic.file_id);
writeln!(formatter, "{}:{diagnostic}", file.path.display())?;
let (start, end) = line_range(&file.source, diagnostic.finding.range);
writeln!(
formatter,
"{}:{start}..{end}:{diagnostic}",
file.path.display()
)?;
}
Ok(())
}
}
fn line_range(source: &str, range: ast::SourceRange) -> (usize, usize) {
let line_at = |offset| {
source
.bytes()
.take(offset)
.filter(|byte| *byte == b'\n')
.count()
+ 1
};
let start = line_at(range.start);
let end = line_at(range.end);
(start, end)
}
/// Analyzes source files and returns the diagnostics Xatu finds.
pub fn future_sight(files: Vec<SourceFile>) -> status::Result<Report> {
let mut store = FileStore::default();