Skip to content

Commit

Permalink
add display line offset
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Aug 5, 2024
1 parent 7c988a6 commit 54af91a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub struct Source<I: AsRef<str> = String> {
lines: Vec<Line>,
len: usize,
byte_len: usize,
display_line_offset: usize,
}

impl<I: AsRef<str>> Source<I> {
Expand Down Expand Up @@ -121,6 +122,7 @@ impl<I: AsRef<str>> From<I> for Source<I> {
}],
len: 0,
byte_len: 0,
display_line_offset: 0,
};
}

Expand Down Expand Up @@ -162,11 +164,23 @@ impl<I: AsRef<str>> From<I> for Source<I> {
lines,
len: char_offset,
byte_len: byte_offset,
display_line_offset: 0,
}
}
}

impl<I: AsRef<str>> Source<I> {
/// Add an offset to the printed line numbers
pub fn with_display_line_offset(mut self, offset: usize) -> Self {
self.display_line_offset = offset;
self
}

/// Get the offset added to printed line numbers
pub fn display_line_offset(&self) -> usize {
self.display_line_offset
}

/// Get the length of the total number of characters in the source.
pub fn len(&self) -> usize {
self.len
Expand Down
7 changes: 6 additions & 1 deletion src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,12 @@ impl<S: Span> Report<'_, S> {
}),
};
let (line_no, col_no) = line_and_col
.map(|(_, idx, col)| (format!("{}", idx + 1), format!("{}", col + 1)))
.map(|(_, idx, col)| {
(
format!("{}", idx + 1 + src.display_line_offset()),
format!("{}", col + 1),
)
})
.unwrap_or_else(|| ('?'.to_string(), '?'.to_string()));
let line_ref = format!(":{}:{}", line_no, col_no);
writeln!(
Expand Down

0 comments on commit 54af91a

Please sign in to comment.