Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add support for disabling the primary label line/col information #419

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions src/handlers/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct GraphicalReportHandler {
pub(crate) with_cause_chain: bool,
pub(crate) wrap_lines: bool,
pub(crate) break_words: bool,
pub(crate) with_primary_span_start: bool,
pub(crate) word_separator: Option<textwrap::WordSeparator>,
pub(crate) word_splitter: Option<textwrap::WordSplitter>,
pub(crate) highlighter: MietteHighlighter,
Expand Down Expand Up @@ -61,6 +62,7 @@ impl GraphicalReportHandler {
with_cause_chain: true,
wrap_lines: true,
break_words: true,
with_primary_span_start: true,
word_separator: None,
word_splitter: None,
highlighter: MietteHighlighter::default(),
Expand All @@ -80,6 +82,7 @@ impl GraphicalReportHandler {
tab_width: 4,
wrap_lines: true,
with_cause_chain: true,
with_primary_span_start: true,
break_words: true,
word_separator: None,
word_splitter: None,
Expand Down Expand Up @@ -119,6 +122,20 @@ impl GraphicalReportHandler {
self
}

/// Include the line and column for the the start of the primary span when the
/// snippet extends multiple lines
pub fn with_primary_span_start(mut self) -> Self {
self.with_primary_span_start = true;
self
}

/// Do not include the line and column for the the start of the primary span
/// when the snippet extends multiple lines
pub fn without_primary_span_start(mut self) -> Self {
self.with_primary_span_start = false;
self
}

/// Whether to include [`Diagnostic::url()`] in the output.
///
/// Disabling this is not recommended, but can be useful for more easily
Expand Down Expand Up @@ -654,26 +671,34 @@ impl GraphicalReportHandler {
};

if let Some(source_name) = primary_contents.name() {
writeln!(
f,
"[{}]",
format_args!(
"{}:{}:{}",
source_name,
primary_contents.line() + 1,
primary_contents.column() + 1
)
.style(self.theme.styles.link)
)?;
} else if lines.len() <= 1 {
writeln!(f, "{}", self.theme.characters.hbar.to_string().repeat(3))?;
} else {
if self.with_primary_span_start {
writeln!(
f,
"[{}]",
format_args!(
"{}:{}:{}",
source_name,
primary_contents.line() + 1,
primary_contents.column() + 1
)
.style(self.theme.styles.link)
)?;
} else {
writeln!(
f,
"[{}]",
format_args!("{}", source_name,).style(self.theme.styles.link)
)?;
}
} else if self.with_primary_span_start && lines.len() > 1 {
writeln!(
f,
"[{}:{}]",
primary_contents.line() + 1,
primary_contents.column() + 1
)?;
} else {
writeln!(f, "{}", self.theme.characters.hbar.to_string().repeat(3))?;
}

// Now it's time for the fun part--actually rendering everything!
Expand Down
60 changes: 60 additions & 0 deletions tests/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2510,3 +2510,63 @@ fn after_invalid_unicode() -> Result<(), MietteError> {

Ok(())
}

#[test]
fn option_include_primary_span_start() {
#[derive(Debug, Clone, Diagnostic, Error)]
#[error("decoding error")]
#[diagnostic(code(decode_err))]
struct E {
#[label("valid data here")]
src: SourceSpan,
}

let invalid_source: &[u8] = b"malformed\nh\xf0\x93\x8aXYZ";

let (x_index, _) = invalid_source
.iter()
.enumerate()
.find(|&(_, &x)| x == b'X')
.unwrap();

// make err pointing at the X
let err = E {
src: SourceSpan::from((x_index, 1)),
};

let result = fmt_report_with_settings(
Report::new(err.clone()).with_source_code(invalid_source),
|handler| handler,
);

let expected = "decode_err

× decoding error
╭─[2:5]
1 │ malformed
2 │ h�XYZ
· ┬
· ╰── valid data here
╰────
";

assert_eq!(expected, result);

let result = fmt_report_with_settings(
Report::new(err).with_source_code(invalid_source),
|handler| handler.without_primary_span_start(),
);

let expected = "decode_err

× decoding error
╭────
1 │ malformed
2 │ h�XYZ
· ┬
· ╰── valid data here
╰────
";

assert_eq!(expected, result);
}