Skip to content

Commit

Permalink
WIP drawing diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
vcfxb committed May 23, 2024
1 parent f10f356 commit 90922c6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
44 changes: 44 additions & 0 deletions wright/src/reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! [codespan-reporting]: https://crates.io/crates/codespan-reporting
//! [ariadne]: https://crates.io/crates/ariadne
use crate::source_tracking::fragment::Fragment;
use self::{owned_string::OwnedString, severity::Severity};
use std::io;
use termcolor::{ColorChoice, StandardStream, StandardStreamLock, WriteColor};
Expand All @@ -25,6 +26,28 @@ pub struct Diagnostic {

/// The main message of the diagnostic. This should be short enough to display on one terminal line in most cases.
pub message: OwnedString,

/// The primary [Highlight] of this diagnostic, which contains the section of source code where the
/// error/warning lies.
pub primary_highlight: Option<Highlight>,

/// Any secondary [Highlight]s that help the reader understand this diagnostic.
pub secondary_highlights: Vec<Highlight>,

/// Optionally a note giving extra context or re-stating this diagnostic.
pub note: Option<OwnedString>,
}

/// Some highlighted source code that can be printed with a [Diagnostic], usually with its own message(s).
#[derive(Debug)]
pub struct Highlight {
/// A valid [Fragment] representing the source where the error occurred.
/// The surrounding source will be printed to the best of the ability of this
/// implementation, which may be modified or updated to better draw [Diagnostic]s.
pub fragment: Fragment,

/// Optionally a message to display with the highlighted region.
pub message: Option<OwnedString>,
}

impl Diagnostic {
Expand All @@ -37,6 +60,9 @@ impl Diagnostic {
severity,
code: None,
message: message.into(),
primary_highlight: None,
secondary_highlights: Vec::new(),
note: None
}
}

Expand Down Expand Up @@ -70,6 +96,24 @@ impl Diagnostic {
self
}

/// Add a [Diagnostic::note] to this [Diagnostic].
pub fn with_note(mut self, n: impl Into<OwnedString>) -> Self {
self.note = Some(n.into());
self
}

/// Add a [Diagnostic::primary_highlight] to this [Diagnostic].
pub fn with_primary_highlight(mut self, h: Highlight) -> Self {
self.primary_highlight = Some(h);
self
}

/// Add a secondary [Highlight] to this [Diagnostic]'s list of [Diagnostic::secondary_highlights].
pub fn add_secondary_highlight(mut self, h: Highlight) -> Self {
self.secondary_highlights.push(h);
self
}

/// Print this diagnostic to the standard output.
///
/// Uses [supports_unicode] to determine whether to print unicode characters.
Expand Down
40 changes: 39 additions & 1 deletion wright/src/reporting/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,30 @@
use std::io;
use termcolor::{ColorSpec, WriteColor};
use super::Diagnostic;
use super::{Diagnostic, Highlight};

/// Prefix added to the beginning of every line in a section (such as a [Highlight] or [Diagnostic::note])
/// when unicode is not available.
const ASCII_SECTION_PREFIX: char = '|';

/// Prefix added to the beginning of every line in a section (such as a [Highlight] or [Diagnostic::note])
/// when unicode is available.
const UNICODE_SECTION_PREFIX: char = '\u{2503}';

/// Character to print on the first/header line of a section that connects to the prefix characters on following lines.
const UNICODE_SECTION_OPENER: char = '\u{250F}';

/// Character to print on the first/header line of a section that connects to the prefix characters on following lines.
const ASCII_SECTION_OPENER: char = '=';

/// Character to print after the last line of a section that connects to the prefix characters on previous lines.
const UNICODE_SECTION_CLOSER: char = '\u{2579}';

/// Character to print after the last line of a section that connects to the prefix characters on previous lines.
///
/// This is just a space for ASCII as there is not a great way to close sections otherwise.
const ASCII_SECTION_CLOSER: char = ' ';


/// Draw a [Diagnostic] to a [WriteColor] reciever, optionally using unicode.
pub fn draw<W: WriteColor>(diagnostic: &Diagnostic, w: &mut W, write_unicode: bool) -> io::Result<()> {
Expand Down Expand Up @@ -78,5 +101,20 @@ mod tests {
assert_eq!(without_unicode, "error [TEST_001]: test error\n");
assert_eq!(without_unicode, with_unicode);
}

// Run this with `cargo test test_unicode_vs_ascii -- --include-ignored --nocapture` to print the constants \
// used in this module.
#[test]
#[ignore = "print-debugging tests ignored by default"]
fn test_unicode_vs_ascii() {
use super::{UNICODE_SECTION_CLOSER, UNICODE_SECTION_OPENER, UNICODE_SECTION_PREFIX};

// Print unicode section:
println!("{UNICODE_SECTION_OPENER} Note: \
\n{UNICODE_SECTION_PREFIX} Test note \
\n{UNICODE_SECTION_PREFIX} \
\n{UNICODE_SECTION_CLOSER}");

}
}

0 comments on commit 90922c6

Please sign in to comment.