Skip to content

Commit

Permalink
Group diagnostics by file
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Aug 28, 2019
1 parent 53ae03d commit f8ed720
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 237 deletions.
34 changes: 31 additions & 3 deletions codespan-reporting/src/term/views/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,42 @@ impl<'a> RichDiagnostic<'a> {
}

pub fn emit(&self, writer: &mut impl WriteColor, config: &Config) -> io::Result<()> {
use std::collections::BTreeMap;

use super::MarkStyle;

Header::new(self.diagnostic).emit(writer, config)?;
NewLine::new().emit(writer, config)?;

SourceSnippet::new_primary(self.files, &self.diagnostic).emit(writer, config)?;
let primary_label = &self.diagnostic.primary_label;
let primary_file_id = self.diagnostic.primary_label.file_id;
let severity = self.diagnostic.severity;
let notes = &self.diagnostic.notes;

// Group labels by file

let mut label_groups = BTreeMap::new();

label_groups
.entry(primary_file_id)
.or_insert(vec![])
.push((primary_label, MarkStyle::Primary(severity)));

for secondary_label in &self.diagnostic.secondary_labels {
label_groups
.entry(secondary_label.file_id)
.or_insert(vec![])
.push((secondary_label, MarkStyle::Secondary));
}

// Emit the snippets, starting with the one that contains the primary label

let labels = label_groups.remove(&primary_file_id).unwrap_or(vec![]);
SourceSnippet::new(self.files, primary_file_id, labels, notes).emit(writer, config)?;
NewLine::new().emit(writer, config)?;

for label in &self.diagnostic.secondary_labels {
SourceSnippet::new_secondary(self.files, &label).emit(writer, config)?;
for (file_id, labels) in label_groups {
SourceSnippet::new(self.files, file_id, labels, &[]).emit(writer, config)?;
NewLine::new().emit(writer, config)?;
}

Expand Down
Loading

0 comments on commit f8ed720

Please sign in to comment.