From d3aebefdd24ec7a1edca05b0884fa721ce23467a Mon Sep 17 00:00:00 2001 From: Oliver <480930+rivo@users.noreply.github.com> Date: Thu, 18 Jan 2024 09:59:32 +0100 Subject: [PATCH] Ignoring unknown regions when highlighting. Fixes #939 --- textview.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/textview.go b/textview.go index b4e0a76..6c914d8 100644 --- a/textview.go +++ b/textview.go @@ -590,7 +590,7 @@ func (t *TextView) clear() { // may also provide nil to turn off all highlights). // // For more information on regions, see class description. Empty region strings -// are ignored. +// or regions not contained in the text are ignored. // // Text in highlighted regions will be drawn inverted, i.e. with their // background and foreground colors swapped. @@ -611,6 +611,15 @@ func (t *TextView) Highlight(regionIDs ...string) *TextView { return true }) + // Remove unknown regions. + newRegions := make([]string, 0, len(regionIDs)) + for _, regionID := range regionIDs { + if _, ok := t.regions[regionID]; ok { + newRegions = append(newRegions, regionID) + } + } + regionIDs = newRegions + // Toggle highlights. if t.toggleHighlights { var newIDs []string @@ -1038,6 +1047,16 @@ func (t *TextView) Draw(screen tcell.Screen) { // Scroll to highlighted regions. if t.regionTags && t.scrollToHighlights { + // Make sure we know all highlighted regions. + t.parseAhead(width, func(lineNumber int, line *textViewLine) bool { + for regionID := range t.highlights { + if _, ok := t.regions[regionID]; !ok { + return false + } + } + return true + }) + // What is the line range for all highlighted regions? var ( firstRegion string