Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

View File

@@ -0,0 +1,321 @@
use termcolor::{Color, ColorSpec};
use crate::diagnostic::{LabelStyle, Severity};
/// Configures how a diagnostic is rendered.
#[derive(Clone, Debug)]
pub struct Config {
/// The display style to use when rendering diagnostics.
/// Defaults to: [`DisplayStyle::Rich`].
///
/// [`DisplayStyle::Rich`]: DisplayStyle::Rich
pub display_style: DisplayStyle,
/// Column width of tabs.
/// Defaults to: `4`.
pub tab_width: usize,
/// Styles to use when rendering the diagnostic.
pub styles: Styles,
/// Characters to use when rendering the diagnostic.
pub chars: Chars,
/// The minimum number of lines to be shown after the line on which a multiline [`Label`] begins.
///
/// Defaults to: `3`.
///
/// [`Label`]: crate::diagnostic::Label
pub start_context_lines: usize,
/// The minimum number of lines to be shown before the line on which a multiline [`Label`] ends.
///
/// Defaults to: `1`.
///
/// [`Label`]: crate::diagnostic::Label
pub end_context_lines: usize,
}
impl Default for Config {
fn default() -> Config {
Config {
display_style: DisplayStyle::Rich,
tab_width: 4,
styles: Styles::default(),
chars: Chars::default(),
start_context_lines: 3,
end_context_lines: 1,
}
}
}
/// The display style to use when rendering diagnostics.
#[derive(Clone, Debug)]
pub enum DisplayStyle {
/// Output a richly formatted diagnostic, with source code previews.
///
/// ```text
/// error[E0001]: unexpected type in `+` application
/// ┌─ test:2:9
/// │
/// 2 │ (+ test "")
/// │ ^^ expected `Int` but found `String`
/// │
/// = expected type `Int`
/// found type `String`
///
/// error[E0002]: Bad config found
///
/// ```
Rich,
/// Output a condensed diagnostic, with a line number, severity, message and notes (if any).
///
/// ```text
/// test:2:9: error[E0001]: unexpected type in `+` application
/// = expected type `Int`
/// found type `String`
///
/// error[E0002]: Bad config found
/// ```
Medium,
/// Output a short diagnostic, with a line number, severity, and message.
///
/// ```text
/// test:2:9: error[E0001]: unexpected type in `+` application
/// error[E0002]: Bad config found
/// ```
Short,
}
/// Styles to use when rendering the diagnostic.
#[derive(Clone, Debug)]
pub struct Styles {
/// The style to use when rendering bug headers.
/// Defaults to `fg:red bold intense`.
pub header_bug: ColorSpec,
/// The style to use when rendering error headers.
/// Defaults to `fg:red bold intense`.
pub header_error: ColorSpec,
/// The style to use when rendering warning headers.
/// Defaults to `fg:yellow bold intense`.
pub header_warning: ColorSpec,
/// The style to use when rendering note headers.
/// Defaults to `fg:green bold intense`.
pub header_note: ColorSpec,
/// The style to use when rendering help headers.
/// Defaults to `fg:cyan bold intense`.
pub header_help: ColorSpec,
/// The style to use when the main diagnostic message.
/// Defaults to `bold intense`.
pub header_message: ColorSpec,
/// The style to use when rendering bug labels.
/// Defaults to `fg:red`.
pub primary_label_bug: ColorSpec,
/// The style to use when rendering error labels.
/// Defaults to `fg:red`.
pub primary_label_error: ColorSpec,
/// The style to use when rendering warning labels.
/// Defaults to `fg:yellow`.
pub primary_label_warning: ColorSpec,
/// The style to use when rendering note labels.
/// Defaults to `fg:green`.
pub primary_label_note: ColorSpec,
/// The style to use when rendering help labels.
/// Defaults to `fg:cyan`.
pub primary_label_help: ColorSpec,
/// The style to use when rendering secondary labels.
/// Defaults `fg:blue` (or `fg:cyan` on windows).
pub secondary_label: ColorSpec,
/// The style to use when rendering the line numbers.
/// Defaults `fg:blue` (or `fg:cyan` on windows).
pub line_number: ColorSpec,
/// The style to use when rendering the source code borders.
/// Defaults `fg:blue` (or `fg:cyan` on windows).
pub source_border: ColorSpec,
/// The style to use when rendering the note bullets.
/// Defaults `fg:blue` (or `fg:cyan` on windows).
pub note_bullet: ColorSpec,
}
impl Styles {
/// The style used to mark a header at a given severity.
pub fn header(&self, severity: Severity) -> &ColorSpec {
match severity {
Severity::Bug => &self.header_bug,
Severity::Error => &self.header_error,
Severity::Warning => &self.header_warning,
Severity::Note => &self.header_note,
Severity::Help => &self.header_help,
}
}
/// The style used to mark a primary or secondary label at a given severity.
pub fn label(&self, severity: Severity, label_style: LabelStyle) -> &ColorSpec {
match (label_style, severity) {
(LabelStyle::Primary, Severity::Bug) => &self.primary_label_bug,
(LabelStyle::Primary, Severity::Error) => &self.primary_label_error,
(LabelStyle::Primary, Severity::Warning) => &self.primary_label_warning,
(LabelStyle::Primary, Severity::Note) => &self.primary_label_note,
(LabelStyle::Primary, Severity::Help) => &self.primary_label_help,
(LabelStyle::Secondary, _) => &self.secondary_label,
}
}
#[doc(hidden)]
pub fn with_blue(blue: Color) -> Styles {
let header = ColorSpec::new().set_bold(true).set_intense(true).clone();
Styles {
header_bug: header.clone().set_fg(Some(Color::Red)).clone(),
header_error: header.clone().set_fg(Some(Color::Red)).clone(),
header_warning: header.clone().set_fg(Some(Color::Yellow)).clone(),
header_note: header.clone().set_fg(Some(Color::Green)).clone(),
header_help: header.clone().set_fg(Some(Color::Cyan)).clone(),
header_message: header,
primary_label_bug: ColorSpec::new().set_fg(Some(Color::Red)).clone(),
primary_label_error: ColorSpec::new().set_fg(Some(Color::Red)).clone(),
primary_label_warning: ColorSpec::new().set_fg(Some(Color::Yellow)).clone(),
primary_label_note: ColorSpec::new().set_fg(Some(Color::Green)).clone(),
primary_label_help: ColorSpec::new().set_fg(Some(Color::Cyan)).clone(),
secondary_label: ColorSpec::new().set_fg(Some(blue)).clone(),
line_number: ColorSpec::new().set_fg(Some(blue)).clone(),
source_border: ColorSpec::new().set_fg(Some(blue)).clone(),
note_bullet: ColorSpec::new().set_fg(Some(blue)).clone(),
}
}
}
impl Default for Styles {
fn default() -> Styles {
// Blue is really difficult to see on the standard windows command line
#[cfg(windows)]
const BLUE: Color = Color::Cyan;
#[cfg(not(windows))]
const BLUE: Color = Color::Blue;
Self::with_blue(BLUE)
}
}
/// Characters to use when rendering the diagnostic.
///
/// By using [`Chars::ascii()`] you can switch to an ASCII-only format suitable
/// for rendering on terminals that do not support box drawing characters.
#[derive(Clone, Debug)]
pub struct Chars {
/// The characters to use for the top-left border of the snippet.
/// Defaults to: `"┌─"` or `"-->"` with [`Chars::ascii()`].
pub snippet_start: String,
/// The character to use for the left border of the source.
/// Defaults to: `'│'` or `'|'` with [`Chars::ascii()`].
pub source_border_left: char,
/// The character to use for the left border break of the source.
/// Defaults to: `'·'` or `'.'` with [`Chars::ascii()`].
pub source_border_left_break: char,
/// The character to use for the note bullet.
/// Defaults to: `'='`.
pub note_bullet: char,
/// The character to use for marking a single-line primary label.
/// Defaults to: `'^'`.
pub single_primary_caret: char,
/// The character to use for marking a single-line secondary label.
/// Defaults to: `'-'`.
pub single_secondary_caret: char,
/// The character to use for marking the start of a multi-line primary label.
/// Defaults to: `'^'`.
pub multi_primary_caret_start: char,
/// The character to use for marking the end of a multi-line primary label.
/// Defaults to: `'^'`.
pub multi_primary_caret_end: char,
/// The character to use for marking the start of a multi-line secondary label.
/// Defaults to: `'\''`.
pub multi_secondary_caret_start: char,
/// The character to use for marking the end of a multi-line secondary label.
/// Defaults to: `'\''`.
pub multi_secondary_caret_end: char,
/// The character to use for the top-left corner of a multi-line label.
/// Defaults to: `'╭'` or `'/'` with [`Chars::ascii()`].
pub multi_top_left: char,
/// The character to use for the top of a multi-line label.
/// Defaults to: `'─'` or `'-'` with [`Chars::ascii()`].
pub multi_top: char,
/// The character to use for the bottom-left corner of a multi-line label.
/// Defaults to: `'╰'` or `'\'` with [`Chars::ascii()`].
pub multi_bottom_left: char,
/// The character to use when marking the bottom of a multi-line label.
/// Defaults to: `'─'` or `'-'` with [`Chars::ascii()`].
pub multi_bottom: char,
/// The character to use for the left of a multi-line label.
/// Defaults to: `'│'` or `'|'` with [`Chars::ascii()`].
pub multi_left: char,
/// The character to use for the left of a pointer underneath a caret.
/// Defaults to: `'│'` or `'|'` with [`Chars::ascii()`].
pub pointer_left: char,
}
impl Default for Chars {
fn default() -> Chars {
Chars::box_drawing()
}
}
impl Chars {
/// A character set that uses Unicode box drawing characters.
pub fn box_drawing() -> Chars {
Chars {
snippet_start: "┌─".into(),
source_border_left: '',
source_border_left_break: '·',
note_bullet: '=',
single_primary_caret: '^',
single_secondary_caret: '-',
multi_primary_caret_start: '^',
multi_primary_caret_end: '^',
multi_secondary_caret_start: '\'',
multi_secondary_caret_end: '\'',
multi_top_left: '',
multi_top: '',
multi_bottom_left: '',
multi_bottom: '',
multi_left: '',
pointer_left: '',
}
}
/// A character set that only uses ASCII characters.
///
/// This is useful if your terminal's font does not support box drawing
/// characters well and results in output that looks similar to rustc's
/// diagnostic output.
pub fn ascii() -> Chars {
Chars {
snippet_start: "-->".into(),
source_border_left: '|',
source_border_left_break: '.',
note_bullet: '=',
single_primary_caret: '^',
single_secondary_caret: '-',
multi_primary_caret_start: '^',
multi_primary_caret_end: '^',
multi_secondary_caret_start: '\'',
multi_secondary_caret_end: '\'',
multi_top_left: '/',
multi_top: '-',
multi_bottom_left: '\\',
multi_bottom: '-',
multi_left: '|',
pointer_left: '|',
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,478 @@
use std::ops::Range;
use crate::diagnostic::{Diagnostic, LabelStyle};
use crate::files::{Error, Files, Location};
use crate::term::renderer::{Locus, MultiLabel, Renderer, SingleLabel};
use crate::term::Config;
/// Count the number of decimal digits in `n`.
fn count_digits(mut n: usize) -> usize {
let mut count = 0;
while n != 0 {
count += 1;
n /= 10; // remove last digit
}
count
}
/// Output a richly formatted diagnostic, with source code previews.
pub struct RichDiagnostic<'diagnostic, 'config, FileId> {
diagnostic: &'diagnostic Diagnostic<FileId>,
config: &'config Config,
}
impl<'diagnostic, 'config, FileId> RichDiagnostic<'diagnostic, 'config, FileId>
where
FileId: Copy + PartialEq,
{
pub fn new(
diagnostic: &'diagnostic Diagnostic<FileId>,
config: &'config Config,
) -> RichDiagnostic<'diagnostic, 'config, FileId> {
RichDiagnostic { diagnostic, config }
}
pub fn render<'files>(
&self,
files: &'files impl Files<'files, FileId = FileId>,
renderer: &mut Renderer<'_, '_>,
) -> Result<(), Error>
where
FileId: 'files,
{
use std::collections::BTreeMap;
struct LabeledFile<'diagnostic, FileId> {
file_id: FileId,
start: usize,
name: String,
location: Location,
num_multi_labels: usize,
lines: BTreeMap<usize, Line<'diagnostic>>,
max_label_style: LabelStyle,
}
impl<'diagnostic, FileId> LabeledFile<'diagnostic, FileId> {
fn get_or_insert_line(
&mut self,
line_index: usize,
line_range: Range<usize>,
line_number: usize,
) -> &mut Line<'diagnostic> {
self.lines.entry(line_index).or_insert_with(|| Line {
range: line_range,
number: line_number,
single_labels: vec![],
multi_labels: vec![],
// This has to be false by default so we know if it must be rendered by another condition already.
must_render: false,
})
}
}
struct Line<'diagnostic> {
number: usize,
range: std::ops::Range<usize>,
// TODO: How do we reuse these allocations?
single_labels: Vec<SingleLabel<'diagnostic>>,
multi_labels: Vec<(usize, LabelStyle, MultiLabel<'diagnostic>)>,
must_render: bool,
}
// TODO: Make this data structure external, to allow for allocation reuse
let mut labeled_files = Vec::<LabeledFile<'_, _>>::new();
// Keep track of the outer padding to use when rendering the
// snippets of source code.
let mut outer_padding = 0;
// Group labels by file
for label in &self.diagnostic.labels {
let start_line_index = files.line_index(label.file_id, label.range.start)?;
let start_line_number = files.line_number(label.file_id, start_line_index)?;
let start_line_range = files.line_range(label.file_id, start_line_index)?;
let end_line_index = files.line_index(label.file_id, label.range.end)?;
let end_line_number = files.line_number(label.file_id, end_line_index)?;
let end_line_range = files.line_range(label.file_id, end_line_index)?;
outer_padding = std::cmp::max(outer_padding, count_digits(start_line_number));
outer_padding = std::cmp::max(outer_padding, count_digits(end_line_number));
// NOTE: This could be made more efficient by using an associative
// data structure like a hashmap or B-tree, but we use a vector to
// preserve the order that unique files appear in the list of labels.
let labeled_file = match labeled_files
.iter_mut()
.find(|labeled_file| label.file_id == labeled_file.file_id)
{
Some(labeled_file) => {
// another diagnostic also referenced this file
if labeled_file.max_label_style > label.style
|| (labeled_file.max_label_style == label.style
&& labeled_file.start > label.range.start)
{
// this label has a higher style or has the same style but starts earlier
labeled_file.start = label.range.start;
labeled_file.location = files.location(label.file_id, label.range.start)?;
labeled_file.max_label_style = label.style;
}
labeled_file
}
None => {
// no other diagnostic referenced this file yet
labeled_files.push(LabeledFile {
file_id: label.file_id,
start: label.range.start,
name: files.name(label.file_id)?.to_string(),
location: files.location(label.file_id, label.range.start)?,
num_multi_labels: 0,
lines: BTreeMap::new(),
max_label_style: label.style,
});
// this unwrap should never fail because we just pushed an element
labeled_files
.last_mut()
.expect("just pushed an element that disappeared")
}
};
if start_line_index == end_line_index {
// Single line
//
// ```text
// 2 │ (+ test "")
// │ ^^ expected `Int` but found `String`
// ```
let label_start = label.range.start - start_line_range.start;
// Ensure that we print at least one caret, even when we
// have a zero-length source range.
let label_end =
usize::max(label.range.end - start_line_range.start, label_start + 1);
let line = labeled_file.get_or_insert_line(
start_line_index,
start_line_range,
start_line_number,
);
// Ensure that the single line labels are lexicographically
// sorted by the range of source code that they cover.
let index = match line.single_labels.binary_search_by(|(_, range, _)| {
// `Range<usize>` doesn't implement `Ord`, so convert to `(usize, usize)`
// to piggyback off its lexicographic comparison implementation.
(range.start, range.end).cmp(&(label_start, label_end))
}) {
// If the ranges are the same, order the labels in reverse
// to how they were originally specified in the diagnostic.
// This helps with printing in the renderer.
Ok(index) | Err(index) => index,
};
line.single_labels
.insert(index, (label.style, label_start..label_end, &label.message));
// If this line is not rendered, the SingleLabel is not visible.
line.must_render = true;
} else {
// Multiple lines
//
// ```text
// 4 │ fizz₁ num = case (mod num 5) (mod num 3) of
// │ ╭─────────────^
// 5 │ │ 0 0 => "FizzBuzz"
// 6 │ │ 0 _ => "Fizz"
// 7 │ │ _ 0 => "Buzz"
// 8 │ │ _ _ => num
// │ ╰──────────────^ `case` clauses have incompatible types
// ```
let label_index = labeled_file.num_multi_labels;
labeled_file.num_multi_labels += 1;
// First labeled line
let label_start = label.range.start - start_line_range.start;
let start_line = labeled_file.get_or_insert_line(
start_line_index,
start_line_range.clone(),
start_line_number,
);
start_line.multi_labels.push((
label_index,
label.style,
MultiLabel::Top(label_start),
));
// The first line has to be rendered so the start of the label is visible.
start_line.must_render = true;
// Marked lines
//
// ```text
// 5 │ │ 0 0 => "FizzBuzz"
// 6 │ │ 0 _ => "Fizz"
// 7 │ │ _ 0 => "Buzz"
// ```
for line_index in (start_line_index + 1)..end_line_index {
let line_range = files.line_range(label.file_id, line_index)?;
let line_number = files.line_number(label.file_id, line_index)?;
outer_padding = std::cmp::max(outer_padding, count_digits(line_number));
let line = labeled_file.get_or_insert_line(line_index, line_range, line_number);
line.multi_labels
.push((label_index, label.style, MultiLabel::Left));
// The line should be rendered to match the configuration of how much context to show.
line.must_render |=
// Is this line part of the context after the start of the label?
line_index - start_line_index <= self.config.start_context_lines
||
// Is this line part of the context before the end of the label?
end_line_index - line_index <= self.config.end_context_lines;
}
// Last labeled line
//
// ```text
// 8 │ │ _ _ => num
// │ ╰──────────────^ `case` clauses have incompatible types
// ```
let label_end = label.range.end - end_line_range.start;
let end_line = labeled_file.get_or_insert_line(
end_line_index,
end_line_range,
end_line_number,
);
end_line.multi_labels.push((
label_index,
label.style,
MultiLabel::Bottom(label_end, &label.message),
));
// The last line has to be rendered so the end of the label is visible.
end_line.must_render = true;
}
}
// Header and message
//
// ```text
// error[E0001]: unexpected type in `+` application
// ```
renderer.render_header(
None,
self.diagnostic.severity,
self.diagnostic.code.as_deref(),
self.diagnostic.message.as_str(),
)?;
// Source snippets
//
// ```text
// ┌─ test:2:9
// │
// 2 │ (+ test "")
// │ ^^ expected `Int` but found `String`
// │
// ```
let mut labeled_files = labeled_files.into_iter().peekable();
while let Some(labeled_file) = labeled_files.next() {
let source = files.source(labeled_file.file_id)?;
let source = source.as_ref();
// Top left border and locus.
//
// ```text
// ┌─ test:2:9
// ```
if !labeled_file.lines.is_empty() {
renderer.render_snippet_start(
outer_padding,
&Locus {
name: labeled_file.name,
location: labeled_file.location,
},
)?;
renderer.render_snippet_empty(
outer_padding,
self.diagnostic.severity,
labeled_file.num_multi_labels,
&[],
)?;
}
let mut lines = labeled_file
.lines
.iter()
.filter(|(_, line)| line.must_render)
.peekable();
while let Some((line_index, line)) = lines.next() {
renderer.render_snippet_source(
outer_padding,
line.number,
&source[line.range.clone()],
self.diagnostic.severity,
&line.single_labels,
labeled_file.num_multi_labels,
&line.multi_labels,
)?;
// Check to see if we need to render any intermediate stuff
// before rendering the next line.
if let Some((next_line_index, _)) = lines.peek() {
match next_line_index.checked_sub(*line_index) {
// Consecutive lines
Some(1) => {}
// One line between the current line and the next line
Some(2) => {
// Write a source line
let file_id = labeled_file.file_id;
// This line was not intended to be rendered initially.
// To render the line right, we have to get back the original labels.
let labels = labeled_file
.lines
.get(&(line_index + 1))
.map_or(&[][..], |line| &line.multi_labels[..]);
renderer.render_snippet_source(
outer_padding,
files.line_number(file_id, line_index + 1)?,
&source[files.line_range(file_id, line_index + 1)?],
self.diagnostic.severity,
&[],
labeled_file.num_multi_labels,
labels,
)?;
}
// More than one line between the current line and the next line.
Some(_) | None => {
// Source break
//
// ```text
// ·
// ```
renderer.render_snippet_break(
outer_padding,
self.diagnostic.severity,
labeled_file.num_multi_labels,
&line.multi_labels,
)?;
}
}
}
}
// Check to see if we should render a trailing border after the
// final line of the snippet.
if labeled_files.peek().is_none() && self.diagnostic.notes.is_empty() {
// We don't render a border if we are at the final newline
// without trailing notes, because it would end up looking too
// spaced-out in combination with the final new line.
} else {
// Render the trailing snippet border.
renderer.render_snippet_empty(
outer_padding,
self.diagnostic.severity,
labeled_file.num_multi_labels,
&[],
)?;
}
}
// Additional notes
//
// ```text
// = expected type `Int`
// found type `String`
// ```
for note in &self.diagnostic.notes {
renderer.render_snippet_note(outer_padding, note)?;
}
renderer.render_empty()
}
}
/// Output a short diagnostic, with a line number, severity, and message.
pub struct ShortDiagnostic<'diagnostic, FileId> {
diagnostic: &'diagnostic Diagnostic<FileId>,
show_notes: bool,
}
impl<'diagnostic, FileId> ShortDiagnostic<'diagnostic, FileId>
where
FileId: Copy + PartialEq,
{
pub fn new(
diagnostic: &'diagnostic Diagnostic<FileId>,
show_notes: bool,
) -> ShortDiagnostic<'diagnostic, FileId> {
ShortDiagnostic {
diagnostic,
show_notes,
}
}
pub fn render<'files>(
&self,
files: &'files impl Files<'files, FileId = FileId>,
renderer: &mut Renderer<'_, '_>,
) -> Result<(), Error>
where
FileId: 'files,
{
// Located headers
//
// ```text
// test:2:9: error[E0001]: unexpected type in `+` application
// ```
let mut primary_labels_encountered = 0;
let labels = self.diagnostic.labels.iter();
for label in labels.filter(|label| label.style == LabelStyle::Primary) {
primary_labels_encountered += 1;
renderer.render_header(
Some(&Locus {
name: files.name(label.file_id)?.to_string(),
location: files.location(label.file_id, label.range.start)?,
}),
self.diagnostic.severity,
self.diagnostic.code.as_deref(),
self.diagnostic.message.as_str(),
)?;
}
// Fallback to printing a non-located header if no primary labels were encountered
//
// ```text
// error[E0002]: Bad config found
// ```
if primary_labels_encountered == 0 {
renderer.render_header(
None,
self.diagnostic.severity,
self.diagnostic.code.as_deref(),
self.diagnostic.message.as_str(),
)?;
}
if self.show_notes {
// Additional notes
//
// ```text
// = expected type `Int`
// found type `String`
// ```
for note in &self.diagnostic.notes {
renderer.render_snippet_note(0, note)?;
}
}
Ok(())
}
}