Skip to content

highlight_source(true) misaligned when highlighting wide characters #396

Description

@tonywu6

With highlight_source(true), when the highlighted span contains wider characters (like emojis and CJK characters), the resulting highlight goes past the intended range (although the underline is correct):

use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet, renderer::DecorStyle};

fn main() {
    let text1 = "[lorem ipsum](";
    let text2 = "表意文字";
    let text3 = ")";

    let text = format!("{text1}{text2}{text3}");
    // highlight `text2`
    let span = text1.len()..text1.len() + text2.len();

    let report = &[Level::WARNING
        .primary_title("this is a test message")
        .element(
            Snippet::source(&text)
                .path("<anon>")
                .annotation(AnnotationKind::Primary.span(span).highlight_source(true)),
        )];

    let renderer = Renderer::styled().decor_style(DecorStyle::Unicode);
    println!("{}", renderer.render(report));
}
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions