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

Add Source::with_line_offset function #7

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 12 additions & 5 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl Line {
pub struct Source {
lines: Vec<Line>,
len: usize,
line_offset: usize,
}

impl<S: AsRef<str>> From<S> for Source {
Expand All @@ -79,11 +80,17 @@ impl<S: AsRef<str>> From<S> for Source {
})
.collect(),
len: offset,
line_offset: 0,
}
}
}

impl Source {
/// Add an offset to the line number displayed in the message.
pub fn with_line_offset<S: AsRef<str>>(source: S, line_offset: usize) -> Source {
Source { line_offset, ..Source::from(source) }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with_x might make more sense as a builder pattern (i.e: takes self, returns Self). This would be strictly more powerful.

}

/// Get the length of the total number of characters in the source.
pub fn len(&self) -> usize { self.len }

Expand All @@ -98,17 +105,17 @@ impl Source {
/// Return an iterator over the [`Line`]s in this source.
pub fn lines(&self) -> impl ExactSizeIterator<Item = &Line> + '_ { self.lines.iter() }

/// Get the line that the given offset appears on, and the line/column numbers of the offset.
/// Get the line that the given offset appears on, the line/column numbers of the offset, and any offset added to the line number.
///
/// Note that the line/column numbers are zero-indexed.
pub fn get_offset_line(&self, offset: usize) -> Option<(&Line, usize, usize)> {
pub fn get_offset_line(&self, offset: usize) -> Option<(&Line, usize, usize, usize)> {
if offset <= self.len {
let idx = self.lines
.binary_search_by_key(&offset, |line| line.offset)
.unwrap_or_else(|idx| idx.saturating_sub(1));
let line = &self.lines[idx];
assert!(offset >= line.offset, "offset = {}, line.offset = {}", offset, line.offset);
Some((line, idx, offset - line.offset))
Some((line, idx, offset - line.offset, self.line_offset))
} else {
None
}
Expand All @@ -119,8 +126,8 @@ impl Source {
/// The resulting range is guaranteed to contain valid line indices (i.e: those that can be used for
/// [`Source::line`]).
pub fn get_line_range<S: Span>(&self, span: &S) -> Range<usize> {
let start = self.get_offset_line(span.start()).map_or(0, |(_, l, _)| l);
let end = self.get_offset_line(span.end().saturating_sub(1).max(span.start())).map_or(self.lines.len(), |(_, l, _)| l + 1);
let start = self.get_offset_line(span.start()).map_or(0, |(_, l, _, _)| l);
let end = self.get_offset_line(span.end().saturating_sub(1).max(span.start())).map_or(self.lines.len(), |(_, l, _, _)| l + 1);
start..end
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ impl<S: Span> Report<S> {
},
};

let start_line = src.get_offset_line(label.span.start()).map(|(_, l, _)| l);
let end_line = src.get_offset_line(label.span.end().saturating_sub(1).max(label.span.start())).map(|(_, l, _)| l);
let start_line = src.get_offset_line(label.span.start()).map(|(_, l, _, _)| l);
let end_line = src.get_offset_line(label.span.end().saturating_sub(1).max(label.span.start())).map(|(_, l, _, _)| l);

let label_info = LabelInfo {
kind: if start_line == end_line {
Expand Down Expand Up @@ -141,7 +141,7 @@ impl<S: Span> Report<S> {
};
let (line_no, col_no) = src
.get_offset_line(location)
.map(|(_, idx, col)| (format!("{}", idx + 1), format!("{}", col + 1)))
.map(|(_, idx, col, offs)| (format!("{}", idx + 1 + offs), format!("{}", col + 1)))
.unwrap_or_else(|| ('?'.to_string(), '?'.to_string()));
let line_ref = format!(":{}:{}", line_no, col_no);
writeln!(
Expand Down