Comments (9)
That's very strange. I literally have tests for this: https://github.com/zkat/miette/blob/main/src/source_impls.rs#L190-L271
Can you tell what corner case there you happen to be running into? Sorry this happened!
from miette.
I have no idea what's going on, but to be clear I was expecting the first line of that report to render as...
1 | imagine this is some code
Maybe I should have posted a failing test rather than trying to explain my expectation in the actual test
from miette.
This is also what I would expect!
from miette.
Hello, I'll be happy to help with this issue !
The issue occurs every time there is a break line ( \n ) and a SourceSpan
on the first line.
The problem comes from the context_info function in source_impls.
When constructing MietteSpanContents
an offset is adding to data ( I am not sure what data property should contain in MietteSpanContents ) . If no offset is added ( &input[0..offset]
) everything is displayed however offset is not taking into account ( (4,2)
gives the same result as (0, 2)
) when there is a breakline and a SourceSpan
on the first line .
from miette.
I think you've almost got it.
I suspect this is the specific bit that's breaking, because there's no before_context_lines in this particular case.
My guess is that the fix will either involve flipping that conditional (oops), or just adding another condition in place.
To explain a bit what data
is: data
is the entire span, including context lines, that is supposed to be highlighted. The current tests are all correct in that context, so I'd like to keep them that way.
from miette.
anyway, PRs welcome if you make a test that minimally reproduces this, and then fix that :)
from miette.
Here's a failing test:
+
+ #[test]
+ fn multiline_with_context_line_start() -> Result<(), MietteError> {
+ let src = String::from("aaa\nxxx\n\nfoo\nbar\nbaz\n\nyyy\nbbb\n");
+ let contents = src.read_span(&(2, 0).into(), 2, 2)?;
+ assert_eq!(
+ "aaa\nxxx\n\n",
+ std::str::from_utf8(contents.data()).unwrap()
+ );
+ assert_eq!(0, contents.line());
+ assert_eq!(0, contents.column());
+ let span: SourceSpan = (0, 9).into();
+ assert_eq!(&span, contents.span());
+ Ok(())
+ }
from miette.
It occurred to me after my last comments that I need to add some context: The whole reason for this weirdness is that I tried to solve the problem that #20 is supposed to generalize: Snippets are meant to be windows into the SourceCode that we're trying to render. Good enough windows to show the user what's wrong.
But we only have so much space to do it!
So this code tries to do two things at once:
- If the text is one long line (for example, a single line with thousands of columns of raw JSON), it "skips forward" to where the span is and renders a window into it.
- If the text is multiple lines, it renders a number of context lines before and after.
If both try to happen at the same time, or if our checks run into those corner cases, everything breaks, and that's what's actually happening here.
So... the Ideal Solution is actually to just solve #20 and add a "bounding box width" option that will give us the width, alongside the height (context lines), for our little bounding box. That's an API breaking change, though, so I'd rather just fix this issue's bug individually if we can, for now. I just thought the context might help make sense of why this code is being so weird.
from miette.
Thank you for all these helpful information :) It's very clear, what data is and how is it used !
Since we don't have a "bounding box width" option yet, I assume that, for now, if there is no context lines before ( context_lines_before is 0 ), the line is "skipped forward" to the span. Otherwise, lines are rendered from the start.
I'll try to make a PR for it !
from miette.
Related Issues (20)
- Allow the inner source type of a `NamedSource` to be borrowed directly HOT 1
- More controlability for the `label` HOT 5
- `Severity` should implement `Ord`
- Allow spans in `help` to provide structured suggestions HOT 3
- Backtrace and Spantrace support HOT 5
- Update miette-derive syn dependency to syn 2 HOT 2
- Still print the name of the SourceCode even if all the labels are invalid
- How to manually collect and print errors HOT 2
- Add source slices HOT 1
- Expose access to line and column info from SourceSpan HOT 3
- Add struct for dynamic creation of diagnostics HOT 2
- Use serde HOT 4
- Constify `SourceOffset` and `SourceSpan` HOT 1
- Add `FixIt` support
- Fix overlapping labels HOT 1
- Make "Error: " prefix in error messages configurable/optional HOT 5
- Do I need to fill the source content of NamedSource by myself? HOT 2
- `#[source]` does not work on `Box<dyn Error + Send + Sync>`
- Unexpected behaviour on empty span HOT 2
- Unable to work with &Result<T, E>
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from miette.