zesterer / ariadne

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

Support for line offset #6

Closed olson-dan closed 2 years ago

olson-dan commented 2 years ago

I have a perhaps odd situation where I'm dealing with chunks of text out of a larger file, so to get the correct line number I need to add an offset value. I implemented this here https://github.com/olson-dan/ariadne/commit/3b66508bafd6de14f8c1c57546e9a5136d1cb861 but it seems like rustfmt made a mess of this commit. Are you interested in the change at all? Maybe adding a rustfmt config to the main branch could help with this type of situation.

zesterer commented 2 years ago

Could you explain more about what information you have available in your case? I'm not sure I understand this problem sufficiently to come to a conclusion about it.

olson-dan commented 2 years ago

In my program I have several chunks of the file that need to be paid attention to. I pull these out separately and process them individually, but I save the line offset in the file for the start of each chunk of text. When I emit error messages I need to apply the line offset to the message to output the correct line of the larger file.

The changes are not large but they are hidden by the rustfmt that my editor did automatically.

I added fn Source::with_line_offset(source: &str, line_offset: usize) and stored the offset in the Source structure. I then made Source::get_offset_line return a four-tuple instead of a three-tuple with the fourth value being the offset, which I then add when the error message is written out.

Basically, if you're interested in this I'll clean it up and make a PR. If you're not interested in it I will not worry about it.

zesterer commented 2 years ago

That seems like a reasonable API to add. I'd be interested in a PR for this!

tertsdiepraam commented 2 weeks ago

Hi there! Even though this is marked as completed I don't think it is. I was just looking for exactly this functionality. My use-case is that I'm writing a programming language but I have tests where I parse strings containing source code. I'm trying to make Ariadne display the correct position of an error within that string in the larger Rust source file.

Is there anything holding the linked PR back? If something needs to be changed there I'd be happy to adopt it and make a new PR.