diff --git a/vendor/texlang/src/error/display.rs b/vendor/texlang/src/error/display.rs index 1661e7a..45bf6b8 100644 --- a/vendor/texlang/src/error/display.rs +++ b/vendor/texlang/src/error/display.rs @@ -251,11 +251,22 @@ fn highlight_substring(line: &str, start: usize, length: usize) -> String { if line.len() < start + length { return line.into(); } + // Source spans are byte offsets, which may fall inside a multi-byte + // character (CJK templates are a common case). Snap outwards to the + // nearest char boundaries before slicing. + let mut start = start.min(line.len()); + while start > 0 && !line.is_char_boundary(start) { + start -= 1; + } + let mut end = (start + length).min(line.len()); + while end < line.len() && !line.is_char_boundary(end) { + end += 1; + } format![ "{}{}{}", &line[..start], - (&line[start..start + length]).bold(), - line[start + length..].trim_end(), + (&line[start..end]).bold(), + line[end..].trim_end(), ] }