Skip to content

Conversation

@Muscraft
Copy link
Member

annotate-snippets currently tries to minify replacements by comparing the replacement to the original and "removing" any common prefix, then checks if the original's remaining char match the end of the replacement. This works for most simple cases, but can produce odd results when the end of the replacement matches all of the original, and the replacement also shares some matching prefix with the original. This PR tries to address that edge case by first trying to strip all of the original from the replacement's prefix or suffix and returning the stripped string if it can.

Code Example

use annotate_snippets::{Group, Level, Patch, Renderer, Snippet};

fn main() {
    let source = r#"use sync"#;

    let report = &[Group::with_level(Level::HELP)
        .element(Snippet::source(source).patch(Patch::new(4..8, "std::sync")))];

    let renderer = Renderer::plain();
    println!("{}", renderer.render(report));
}

Before:

  |
1 | use std::sync;
  |      +++++

After:

  |
1 | use std::sync
  |     +++++

Written

original: sync
replacement: std::sync
before: td::s
after: std::

Original rust-lang/rust issue

Closes #343

@epage epage merged commit bf0ce66 into rust-lang:master Nov 15, 2025
14 of 15 checks passed
@Muscraft Muscraft deleted the fix-replacement-trimming branch November 15, 2025 17:11
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.

2 participants