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
use crate::config::FileName;
use crate::formatting::FormattingError;
use crate::{ErrorKind, FormatReport};
use annotate_snippets::display_list::DisplayList;
use annotate_snippets::formatter::DisplayListFormatter;
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
use std::fmt::{self, Display};

/// A builder for [`FormatReportFormatter`].
pub struct FormatReportFormatterBuilder<'a> {
    report: &'a FormatReport,
    enable_colors: bool,
}

impl<'a> FormatReportFormatterBuilder<'a> {
    /// Creates a new [`FormatReportFormatterBuilder`].
    pub fn new(report: &'a FormatReport) -> Self {
        Self {
            report,
            enable_colors: false,
        }
    }

    /// Enables colors and formatting in the output.
    pub fn enable_colors(self, enable_colors: bool) -> Self {
        Self {
            enable_colors,
            ..self
        }
    }

    /// Creates a new [`FormatReportFormatter`] from the settings in this builder.
    pub fn build(self) -> FormatReportFormatter<'a> {
        FormatReportFormatter {
            report: self.report,
            enable_colors: self.enable_colors,
        }
    }
}

/// Formats the warnings/errors in a [`FormatReport`].
///
/// Can be created using a [`FormatReportFormatterBuilder`].
pub struct FormatReportFormatter<'a> {
    report: &'a FormatReport,
    enable_colors: bool,
}

impl<'a> Display for FormatReportFormatter<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let formatter = DisplayListFormatter::new(self.enable_colors, false);
        let errors_by_file = &self.report.internal.borrow().0;

        for (file, errors) in errors_by_file {
            for error in errors {
                let snippet = formatting_error_to_snippet(file, error);
                writeln!(f, "{}\n", formatter.format(&DisplayList::from(snippet)))?;
            }
        }

        if !errors_by_file.is_empty() {
            let snippet = formatting_failure_snippet(self.report.warning_count());
            writeln!(f, "{}", formatter.format(&DisplayList::from(snippet)))?;
        }

        Ok(())
    }
}

fn formatting_failure_snippet(warning_count: usize) -> Snippet {
    Snippet {
        title: Some(Annotation {
            id: None,
            label: Some(format!(
                "rustfmt has failed to format. See previous {} errors.",
                warning_count
            )),
            annotation_type: AnnotationType::Warning,
        }),
        footer: Vec::new(),
        slices: Vec::new(),
    }
}

fn formatting_error_to_snippet(file: &FileName, error: &FormattingError) -> Snippet {
    let slices = vec![snippet_code_slice(file, error)];
    let title = Some(snippet_title(error));
    let footer = snippet_footer(error).into_iter().collect();

    Snippet {
        title,
        footer,
        slices,
    }
}

fn snippet_title(error: &FormattingError) -> Annotation {
    let annotation_type = error_kind_to_snippet_annotation_type(&error.kind);

    Annotation {
        id: title_annotation_id(error),
        label: Some(error.kind.to_string()),
        annotation_type,
    }
}

fn snippet_footer(error: &FormattingError) -> Option<Annotation> {
    let message_suffix = error.msg_suffix();

    if !message_suffix.is_empty() {
        Some(Annotation {
            id: None,
            label: Some(message_suffix.to_string()),
            annotation_type: AnnotationType::Note,
        })
    } else {
        None
    }
}

fn snippet_code_slice(file: &FileName, error: &FormattingError) -> Slice {
    let annotations = slice_annotation(error).into_iter().collect();
    let origin = Some(format!("{}:{}", file, error.line));
    let source = error.line_buffer.clone();

    Slice {
        source,
        line_start: error.line,
        origin,
        fold: false,
        annotations,
    }
}

fn slice_annotation(error: &FormattingError) -> Option<SourceAnnotation> {
    let (range_start, range_length) = error.format_len();
    let range_end = range_start + range_length;

    if range_length > 0 {
        Some(SourceAnnotation {
            annotation_type: AnnotationType::Error,
            range: (range_start, range_end),
            label: String::new(),
        })
    } else {
        None
    }
}

fn title_annotation_id(error: &FormattingError) -> Option<String> {
    const INTERNAL_ERROR_ID: &str = "internal";

    if error.is_internal() {
        Some(INTERNAL_ERROR_ID.to_string())
    } else {
        None
    }
}

fn error_kind_to_snippet_annotation_type(error_kind: &ErrorKind) -> AnnotationType {
    match error_kind {
        ErrorKind::LineOverflow(..)
        | ErrorKind::TrailingWhitespace
        | ErrorKind::IoError(_)
        | ErrorKind::ParseError
        | ErrorKind::LostComment
        | ErrorKind::LicenseCheck
        | ErrorKind::BadAttr
        | ErrorKind::InvalidGlobPattern(_)
        | ErrorKind::VersionMismatch => AnnotationType::Error,
        ErrorKind::BadIssue(_) | ErrorKind::DeprecatedAttr => AnnotationType::Warning,
    }
}