Skip to content

Commit

Permalink
Fix #52
Browse files Browse the repository at this point in the history
This fixes wrong line numbers shown when the `fold` is set to `true`
  • Loading branch information
Inky-developer committed Jan 3, 2022
1 parent 448b804 commit 0814113
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/display_list/from_snippet.rs
Expand Up @@ -111,10 +111,9 @@ fn format_slice(
) -> Vec<DisplayLine<'_>> {
let main_range = slice.annotations.get(0).map(|x| x.range.0);
let origin = slice.origin;
let line_start = slice.line_start;
let need_empty_header = origin.is_some() || is_first;
let mut body = format_body(slice, need_empty_header, has_footer, margin);
let header = format_header(origin, main_range, line_start, &body, is_first);
let header = format_header(origin, main_range, &body, is_first);
let mut result = vec![];

if let Some(header) = header {
Expand All @@ -133,7 +132,6 @@ fn zip_opt<A, B>(a: Option<A>, b: Option<B>) -> Option<(A, B)> {
fn format_header<'a>(
origin: Option<&'a str>,
main_range: Option<usize>,
mut row: usize,
body: &[DisplayLine<'_>],
is_first: bool,
) -> Option<DisplayLine<'a>> {
Expand All @@ -145,24 +143,26 @@ fn format_header<'a>(

if let Some((main_range, path)) = zip_opt(main_range, origin) {
let mut col = 1;
let mut line_offset = 1;

for item in body {
if let DisplayLine::Source {
line: DisplaySourceLine::Content { range, .. },
lineno,
..
} = item
{
if main_range >= range.0 && main_range <= range.1 {
col = main_range - range.0 + 1;
line_offset = lineno.unwrap_or(1);
break;
}
row += 1;
}
}

return Some(DisplayLine::Raw(DisplayRawLine::Origin {
path,
pos: Some((row, col)),
pos: Some((line_offset, col)),
header_type: display_header,
}));
}
Expand Down Expand Up @@ -307,7 +307,7 @@ fn format_body(
let char_widths = line
.chars()
.map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0))
.chain(std::iter::once(1)) // treat the end of line as signle-width
.chain(std::iter::once(1)) // treat the end of line as single-width
.collect::<Vec<_>>();
body.push(DisplayLine::Source {
lineno: Some(current_line),
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/no-color/issue_52.toml
@@ -0,0 +1,16 @@
[title]
annotation_type = "Error"

[[slices]]
source = """
invalid syntax
"""
line_start = 1
origin = "path/to/error.rs"
fold = true
[[slices.annotations]]
label = "error here"
annotation_type = "Warning"
range = [2,16]
7 changes: 7 additions & 0 deletions tests/fixtures/no-color/issue_52.txt
@@ -0,0 +1,7 @@
error
--> path/to/error.rs:3:1
|
...
3 | invalid syntax
| -------------- error here
|

0 comments on commit 0814113

Please sign in to comment.