Skip to content

Commit

Permalink
refactor(error): Clean up highlight code
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Mar 11, 2024
1 parent 01d2f4a commit 31457b3
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion crates/toml_edit/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ impl Display for TomlError {
let col_num = column + 1;
let gutter = line_num.to_string().len();
let content = raw.split('\n').nth(line).expect("valid line number");
let highlight_len = span.end - span.start;
// Allow highlight to go one past the line
let highlight_len = highlight_len.min(content.len().saturating_sub(column));

writeln!(
f,
Expand Down Expand Up @@ -125,7 +128,7 @@ impl Display for TomlError {
// The span will be empty at eof, so we need to make sure we always print at least
// one `^`
write!(f, "^")?;
for _ in (span.start + 1)..(span.end.min(span.start + content.len() - column)) {
for _ in 1..highlight_len {
write!(f, "^")?;
}
writeln!(f)?;
Expand Down

0 comments on commit 31457b3

Please sign in to comment.