Skip to content

Conversation

@Muscraft
Copy link
Member

#330 showed a few cases where annotate-snippets would annotate a line incorrectly when it tried to trim unicode characters. Two things caused this:

  • Not adjusting the left padding if it didn't match skipped
  • Not skipping multi-byte chars when their width + skipped exceeded left.

This PR addresses both of those issues.

Before:
Screenshot From 2025-10-24 11-43-21

After:
Screenshot From 2025-10-24 11-42-53

Test Script
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};

fn main() {
    /***** (A): ASCII only *****/
    let ascii_only_end = (
        "(A): ASCII only",
        "/*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/?",
        "?",
    );

    /***** (B): non-ASCII prefix, ASCII highlight *****/
    let non_ascii_pre_ascii_highlight_end = (
        "(B): non-ASCII prefix, ASCII highlight",
        "/*这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/?",
        "?",
    );

    /***** (C): non-ASCII everything *****/
    let non_ascii_end = (
        "(C): non-ASCII everything",
        "/*这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/好",
        "好",
    );

    /***** (D): non-ASCII everything, annotate middle *****/
    let non_ascii_middle = (
        "(D): non-ASCII everything, annotate middle",
        "/*这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽好。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。这是宽的。*/?",
        "好",
    );
    for (desc, source, highlight) in [
        ascii_only_end,
        non_ascii_pre_ascii_highlight_end,
        non_ascii_end,
        non_ascii_middle,
    ] {
        //println!("\n");
        let renderer = Renderer::styled();

        let start = source.find(highlight).unwrap();
        let range = start..start + highlight.len();
        let annotation = AnnotationKind::Primary.span(range.clone());

        let group_with_label = Group::with_level(Level::ERROR)
            .element(Snippet::source(source).annotation(annotation.clone().label("label")));
        println!("{desc}\n{}\n", renderer.render(&[group_with_label]));

        let group_no_label =
            Group::with_level(Level::ERROR).element(Snippet::source(source).annotation(annotation));

        println!("{}\n", renderer.render(&[group_no_label]));
    }
}

fixes #330


let expected_unicode = str![[r#"
error: title
error: titlecompiler/rustc_errors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this commit pass on its own? I'm not seeing the edit that would cause this output change (and ascii is unchanged)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That change shouldn't have been there. I'm not sure how I missed it.


let expected_unicode = str![[r#"
error: titlecompiler/rustc_errors
error: title
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then reverted here

Copy link
Contributor

@epage epage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to merge once the commits are fixed

@Muscraft Muscraft force-pushed the fix-unicode-trimming branch from c79ab5d to b5eae2a Compare October 24, 2025 17:55
@Muscraft Muscraft merged commit 7e85cbd into rust-lang:master Oct 24, 2025
15 checks passed
@Muscraft Muscraft deleted the fix-unicode-trimming branch October 24, 2025 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Annotation is incorrectly offset when overly long source line that contains non-ASCII characters is elided

2 participants