zesterer / ariadne

A fancy diagnostics & error reporting crate
https://crates.io/crates/ariadne
MIT License
1.69k stars 74 forks source link

Support CRLF Newlines #19

Open Measter opened 2 years ago

Measter commented 2 years ago

Right now the implementation of creating a Source from a string iterates over lines by using str::lines which hides whether a line ended with \n or \r\n. This causes it to put labels in the wrong place, or even lose them if they're near the end of the file, as demonstrated here:

Untitled-1

Measter commented 2 years ago

My suggested fix, which I think should work (at least, it did for the above image), would be to replace lines() with split_terminator('\n'), and then manually check for and trim \r for the line:

let (line, newline_len) = if let Some(trimmed_line) = line.strip_suffix('\r') {
    (trimmed_line, 2)
} else {
    (line, 1)
};

That removes both kinds of newline leaving the line in the same state the current code does, and also gets you the correct newline length.

zesterer commented 2 years ago

Temporarily patched in c2b5e34, but needs a better solution.

oscaroox commented 2 years ago

Ah, I was wondering why the labels were disappearing when I have errors near the end of the source file. Fixed it by replacing CRLF with LF newlines.

zesterer commented 2 years ago

This should be fixed in 0.1.5, which has just been released.