fix(viewport): correct highlight offsets on ANSI-styled content - #1050
Open
rksharma-owg wants to merge 1 commit into
Open
rksharma-owg wants to merge 1 commit into
rksharma-owg wants to merge 1 commit into
Conversation
Use ansi.DecodeSequence to iterate through raw content, counting escape sequence bytes in the raw byte offset while preserving true visual column widths. Fixes charmbracelet#1049
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1049
Problem
viewport.SetHighlightsmisplaced highlight ranges when content contained ANSI escape sequences (e.g., color styling). The ranges would shift to the right by the number of ANSI escape bytes preceding the match, or disappear entirely.Root cause: in
viewport/highlight.go,parseMatcheswalked graphemes onansi.Strip(content), but treatedbyteStartandbyteEnd(which are measured against the rawcontent) as if they were stripped byte offsets. It also performed newline checks usingcontent[bytePos] == '\n'wherebytePoswas indexed into the stripped string, leading to coordinate desynchronization.Fix
contentusingansi.DecodeSequence. This accurately consumes escape sequence bytes inbytePos(tracking the rawcontentbyte coordinates matchingmatches), while only adding visual cell widths tographemePos.unisegimport fromviewport/highlight.go.viewport/viewport_test.gowithTestMatchesToHighlights_ANSIverifying single-line ANSI prefix, multi-line styled content, multi-line spanning, and the exact reproducer from issue viewport: SetHighlights misplaces ranges on ANSI-carrying content聽#1049.Verification
go test -v ./viewportpasses cleanlygo test ./...passes cleanly