zkat / miette

Fancy extension for std::error::Error with pretty, detailed diagnostic printing.
https://docs.rs/miette
Apache License 2.0
2.03k stars 116 forks source link

Extend error text span to whole code points #312

Closed MattX closed 1 year ago

MattX commented 1 year ago

This fixes a panic when an error starts inside a Unicode code point. The range is extended to start (or end) at the beginning (or end) of the character inside which the byte offset is located.

Fixes zkat/miette#223. Unit tests provided in that issue by @Benjamin-L (lightly modified).

Benjamin-L commented 1 year ago

There's one more edge case here that might be worth fixing now or might be worth handling in a later PR:

    let src = "source\n  πŸ‘ΌπŸΌtext\n    here".to_string();
    let err = MyBad {
        src: NamedSource::new("bad_file.rs", src),
        highlight: (10, 0).into(),
    };

Produces

---- single_line_with_wide_char_unaligned_span_empty stdout ----
Error: oops::my::bad

  Γ— oops!
   ╭─[bad_file.rs:2:4]
 1 β”‚ source
 2 β”‚   πŸ‘ΌπŸΌtext
   Β·   ─▲
   Β·    ╰── this bit here
 3 β”‚     here
   ╰────
  help: try doing it better next time?

I would probably expect either

---- single_line_with_wide_char_unaligned_span_empty stdout ----
Error: oops::my::bad

  Γ— oops!
   ╭─[bad_file.rs:2:3]
 1 β”‚ source
 2 β”‚   πŸ‘ΌπŸΌtext
   Β·   β–²
   Β·   ╰── this bit here
 3 β”‚     here
   ╰────
  help: try doing it better next time?

or maybe

---- single_line_with_wide_char_unaligned_span_empty stdout ----
Error: oops::my::bad

  Γ— oops!
   ╭─[bad_file.rs:2:3]
 1 β”‚ source
 2 β”‚   πŸ‘ΌπŸΌtext
   Β·    β–²
   Β·    ╰── this bit here
 3 β”‚     here
   ╰────
  help: try doing it better next time?
zkat commented 1 year ago

@Benjamin-L oops, I didn't see your message before merging. Yeah, let's address that, too.