-
Notifications
You must be signed in to change notification settings - Fork 44
Understanding SourceAnnotation.range #24
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
Comments
Is there a chance that it's due to line endings? Like CLRF? Can you provide an example test.rs? |
Sure thing, this is the file I was using: https://github.com/gleam-lang/gleam/blob/1cfcbdfac2fccd4107c3502cd4878bd4db1bc2ff/test/hello_world/src/other.gleam Line endings should be the linux style |
Can you also provide an |
I'm afraid I don't have time to produce a minimal example, but a patch to my program can be found here https://github.com/gleam-lang/gleam/pull/376/files |
I've got a similar problem: on a file with Unix line endings, the annotation gets shifted by one for each line. If I convert the file to dos line endings, then the annotation is correctly positioned. It seems like annotate-snippets counts the end of line char as two chars maybe ? Unicode in the source file also messes up annotations, so it could be a difference between char-indexing and byte-indexing. |
Experiment confirms that indexing is indeed by char and not by byte index. Furthermore somehow fn char_idx_from_byte_idx(input: &str, idx: usize) -> usize {
let char_idx = input
.char_indices()
.enumerate()
.find(|(_, (i, _))| *i == idx)
.unwrap()
.0;
// Unix-style newlines are counted as two chars (see
// https://github.com/rust-lang/annotate-snippets-rs/issues/24).
let nbr_newlines =
input[..idx].chars().filter(|c| *c == '\n').count();
let nbr_carriage_returns =
input[..idx].chars().filter(|c| *c == '\r').count();
char_idx + nbr_newlines - nbr_carriage_returns
} |
Yeah, I'm actually running into this as well when testing the library out on Pikelet! Weirdly though it doesn't seem to show up in the tests. It appears like this:
The bug seems like it might occur here: https://github.com/rust-lang/annotate-snippets-rs/blob/master/src/display_list/from_snippet.rs#L178-L199 - at least it seems like ugly bugs would occur here wrt. multibyte characters and tab characters. It'd probably be better to try to try to decouple byte position from character index in this library. |
Ah yeah,
current_index (with an additional increment, I don't know why) there
Together, those lines effectively count each end of line as two chars, as I had observed |
thank you for the report! I'm a bit swamped with work lately, but will try to find time to write a PR unless someone beats me to it! |
@lpil I'm actually going to release a new version anyway, but when you have a chance, please check if this bug is fixed for you |
Thank you, though I have not used this library recent and thus do not have any code to test with. |
I was getting this bug with v0.6.1, but not anymore. Unfortunately I got a similar problem when I tried to update to v0.7.0. Here's with v0.6.1:
and with v0.7.0, removing the
|
Can you file it as a new issue? Thank you! |
I can confirm that 0.7.0 fixed this for me ! |
Thank you! Marking as fixed, and let's reopen if needed! |
chore: Ensure pre-commit gets non-system Python
Hello!
I'm testing this library as an alternative to codespan_reporting and I'm having trouble understanding the SourceAnnotation.range value. I thought it was a byte offset (as in codespan_reporting, that I can get from my lalrpop parser) but it seems to have behaviour that suggests it is not.
When the annotation is on the first line of the source the annotation is positioned as I expect:
line 1:
However when the annotation is not on the first line of the source it seems to move back by one character per line.
line 8:
SourceAnnotation.range doesn't seem to be documented- what does it accept?
Thank you
The text was updated successfully, but these errors were encountered: