Skip to content

Show correct line numbers when fold ist set to true, fixes #52 #55

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

Merged
merged 1 commit into from
Jun 2, 2023
Merged
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
12 changes: 6 additions & 6 deletions src/display_list/from_snippet.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error
--> path/to/error.rs:3:1
|
...
3 | invalid syntax
| -------------- error here
|