-
Notifications
You must be signed in to change notification settings - Fork 48
Description
For additional context: rust-lang/rust#132860.
Given the following setup:
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};
fn main() {
/***** (A): ASCII only *****/
// let source = "/*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/?";
// let highlight = "?";
/***** (B): non-ASCII prefix, ASCII highlight *****/
// let source = "/*这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/?";
// let highlight = "?";
/***** (C): non-ASCII everything *****/
// let source = "/*这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/好";
// let highlight = "好";
let start = source.find(&highlight).unwrap();
let range = start..start + highlight.len();
let group = Group::with_level(Level::ERROR).element(
Snippet::source(source).annotation(AnnotationKind::Primary.span(range).label("label")),
);
eprintln!("{}", Renderer::styled().render(&[group]));
}Case (A): The annotation / highlight / underline / caret is correctly placed below the ?:
|
1 | ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/?
| ^ label
Case (B): The annotation / … / caret is incorrectly offset by +1 (points one past the ?):
|
1 | ... 是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/?
| ^ label
Case (C): Similarly offset by +1 (1st ^ points at the 2nd half of 好, 2nd ^ points into nothingness):
|
1 | ... 是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/好
| ^^ label
Important
Your browser probably renders these labeled annotations "way off". If so, that's only because it renders the Hanzi characters with a "width" of <2 (however, it probably also depends on your system fonts) whereas terminal emulators conventionally render Hanzi characters using two "blocks".
So, here's a screenshot of my terminal output to further clarify that it's a +1 offset only:
Again, if your browser renders is like in the image below, don't be misled, only the terminal emulator's output matters!
Meta
I'm using annotate-snippets v0.11.5, git revision 74964e9 (i.e., latest master at the time of writing).
