Skip to content

Commit 0814113

Browse files
Fix #52
This fixes wrong line numbers shown when the `fold` is set to `true`
1 parent 448b804 commit 0814113

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

src/display_list/from_snippet.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,9 @@ fn format_slice(
111111
) -> Vec<DisplayLine<'_>> {
112112
let main_range = slice.annotations.get(0).map(|x| x.range.0);
113113
let origin = slice.origin;
114-
let line_start = slice.line_start;
115114
let need_empty_header = origin.is_some() || is_first;
116115
let mut body = format_body(slice, need_empty_header, has_footer, margin);
117-
let header = format_header(origin, main_range, line_start, &body, is_first);
116+
let header = format_header(origin, main_range, &body, is_first);
118117
let mut result = vec![];
119118

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

146144
if let Some((main_range, path)) = zip_opt(main_range, origin) {
147145
let mut col = 1;
146+
let mut line_offset = 1;
148147

149148
for item in body {
150149
if let DisplayLine::Source {
151150
line: DisplaySourceLine::Content { range, .. },
151+
lineno,
152152
..
153153
} = item
154154
{
155155
if main_range >= range.0 && main_range <= range.1 {
156156
col = main_range - range.0 + 1;
157+
line_offset = lineno.unwrap_or(1);
157158
break;
158159
}
159-
row += 1;
160160
}
161161
}
162162

163163
return Some(DisplayLine::Raw(DisplayRawLine::Origin {
164164
path,
165-
pos: Some((row, col)),
165+
pos: Some((line_offset, col)),
166166
header_type: display_header,
167167
}));
168168
}
@@ -307,7 +307,7 @@ fn format_body(
307307
let char_widths = line
308308
.chars()
309309
.map(|c| unicode_width::UnicodeWidthChar::width(c).unwrap_or(0))
310-
.chain(std::iter::once(1)) // treat the end of line as signle-width
310+
.chain(std::iter::once(1)) // treat the end of line as single-width
311311
.collect::<Vec<_>>();
312312
body.push(DisplayLine::Source {
313313
lineno: Some(current_line),

tests/fixtures/no-color/issue_52.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[title]
2+
annotation_type = "Error"
3+
4+
[[slices]]
5+
source = """
6+
7+
8+
invalid syntax
9+
"""
10+
line_start = 1
11+
origin = "path/to/error.rs"
12+
fold = true
13+
[[slices.annotations]]
14+
label = "error here"
15+
annotation_type = "Warning"
16+
range = [2,16]

tests/fixtures/no-color/issue_52.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error
2+
--> path/to/error.rs:3:1
3+
|
4+
...
5+
3 | invalid syntax
6+
| -------------- error here
7+
|

0 commit comments

Comments
 (0)