Unverified Commit 3e50b822 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[lts-active] fix(next-code-frame): snap visible window to char boundaries (#100212)

Backports [fix(next-code-frame): snap visible window to char boundaries
(#92646)](https://github.com/vercel/next.js/pull/92646) to
`releases/lts/active`. The cherry-pick applied cleanly.

## Verification

- `cargo test -p next-code-frame`, using this branch's pinned toolchain:
53/53 tests passed.

<!-- NEXT_JS_LLM -->
Co-authored-by: 's avatarmugi-uno <mugi.uno@gmail.com>
Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent 9458fb99
......@@ -563,9 +563,13 @@ pub fn extract_highlights(
.copied()
.unwrap_or(source.len());
let (rs, re) = if let Some((trunc_offset, avail_width)) = visible_window {
// Snap to char boundaries — trunc_offset/avail_width are
// display-column counts, not byte counts.
let raw_start = (ls + trunc_offset).min(line_end);
let raw_end = (ls + trunc_offset + avail_width).min(line_end);
(
(ls + trunc_offset).min(line_end),
(ls + trunc_offset + avail_width).min(line_end),
source.floor_char_boundary(raw_start),
source.ceil_char_boundary(raw_end),
)
} else {
(ls, line_end)
......@@ -1593,4 +1597,20 @@ pub mod tests {
starts after the `/*` opener"
);
}
#[test]
fn test_extract_highlights_multibyte_visible_window_panic() {
// Regression test for #92641: visible_window with a width that
// lands inside a multi-byte character must not panic.
let source = "const x = `あ`;";
// byte layout: const x = `(10) あ(11,12,13) `(14) ;(15)
// avail_width=12 → re = 0+0+12 = byte 12, inside 'あ' (bytes 11..14)
let lines = Lines::new(source);
let color_scheme = ColorScheme::colored(ANSI_CODE_RED_BOLD);
let highlights = extract_highlights(&lines, 0..1, JS, Some((0, 12)));
assert_eq!(highlights.len(), 1);
let _result = apply_line_highlights(source, &highlights[0], &color_scheme, 0, 0);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment