Make Disagnostice::line_colunm 1 based

Commit 893983e8d3 "fixed" it to be 0 based as it was
documented. But that's a change of behavior so restore the previous behavior and
document it properly
This commit is contained in:
Olivier Goffart 2023-04-19 08:39:28 +02:00 committed by Olivier Goffart
parent c11415dbbb
commit cbc0c790a3
3 changed files with 8 additions and 8 deletions

View file

@ -91,18 +91,18 @@ impl SourceFileInner {
Rc::new(Self { path, ..Default::default() })
}
/// Returns a tuple with the line (starting at 1) and column number (starting at 0)
/// Returns a tuple with the line (starting at 1) and column number (starting at 1)
pub fn line_column(&self, offset: usize) -> (usize, usize) {
let line_offsets = self.line_offsets();
line_offsets.binary_search(&offset).map_or_else(
|line| {
if line == 0 {
(1, offset)
(1, offset + 1)
} else {
(line + 1, line_offsets.get(line - 1).map_or(0, |x| offset - x))
(line + 1, line_offsets.get(line - 1).map_or(0, |x| offset - x + 1))
}
},
|line| (line + 1, 0),
|line| (line + 1, 1),
)
}
@ -219,7 +219,7 @@ impl Diagnostic {
&self.message
}
/// Returns a tuple with the line (starting at 1) and column number (starting at 0)
/// Returns a tuple with the line (starting at 1) and column number (starting at 1)
pub fn line_column(&self) -> (usize, usize) {
let offset = self.span.span.offset;

View file

@ -97,7 +97,7 @@ fn process_diagnostics(
let lines = source
.bytes()
.enumerate()
.filter_map(|(i, c)| if c == b'\n' { Some(i + 1) } else { None })
.filter_map(|(i, c)| if c == b'\n' { Some(i) } else { None })
.collect::<Vec<usize>>();
// Find expected errors in the file. The first caret (^) points to the expected column. The number of