Skip to content

Commit 5b25105

Browse files
committed
highlighter: Add capability to add already removed groups again
1 parent f0711b9 commit 5b25105

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

pkg/highlight/highlighter.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type highlightStorage struct {
2929
group Group
3030
region *region
3131
children []*highlightStorage
32+
pattern bool
3233
}
3334

3435
// A Highlighter contains the information needed to highlight a string
@@ -38,6 +39,7 @@ type Highlighter struct {
3839
lastEnd int
3940
Def *Def
4041
storage []highlightStorage
42+
removed []highlightStorage
4143
}
4244

4345
// NewHighlighter returns a new highlighter from the given syntax definition
@@ -97,6 +99,7 @@ func (h *Highlighter) removeRange(start int, end int, removeStart int) {
9799
if start < e.start && e.start < end {
98100
// log.Println("remove: start:", e.start, "end:", e.end, "group:", e.group)
99101
removeEnd++
102+
h.removed = append(h.removed, e)
100103
for childIdx, _ := range h.storage[i].children {
101104
// log.Println("attached child: start:", h.storage[i].children[childIdx].start, "end:", h.storage[i].children[childIdx].end, "group:", h.storage[i].children[childIdx].group)
102105
children = append(children, *(h.storage[i].children[childIdx]))
@@ -146,7 +149,7 @@ func (h *Highlighter) storeRange(start int, end int, group Group, r *region, isP
146149
if r != e.region {
147150
// sibling regions, search for overlaps ...
148151
if start < e.start && end > e.start {
149-
// overlap
152+
// overlap from left
150153
} else if start == e.start && end == e.end {
151154
// same match
152155
continue
@@ -155,13 +158,16 @@ func (h *Highlighter) storeRange(start int, end int, group Group, r *region, isP
155158
} else if start >= e.start && end <= e.end {
156159
// smaller match
157160
return
161+
} else if start > e.start && start < e.end && end > e.end {
162+
// overlap from right
163+
return
158164
} else {
159165
continue
160166
}
161167

162168
if !updated {
163169
// log.Println("exchanged from: start:", e.start, "end:", e.end, "group:", e.group)
164-
h.storage[k] = highlightStorage{start, end, group, r, nil}
170+
h.storage[k] = highlightStorage{start, end, group, r, nil, isPattern}
165171

166172
// check and remove follow-ups matching the same
167173
h.removeRange(start, end, k+1)
@@ -179,7 +185,7 @@ func (h *Highlighter) storeRange(start int, end int, group Group, r *region, isP
179185
}
180186

181187
if !updated {
182-
h.storage = append(h.storage, highlightStorage{start, end, group, r, nil})
188+
h.storage = append(h.storage, highlightStorage{start, end, group, r, nil, isPattern})
183189
}
184190

185191
// add possible child entry
@@ -382,9 +388,15 @@ func (h *Highlighter) highlight(highlights LineMatch, start int, lineNum int, li
382388
h.lastStart = -1
383389
h.lastEnd = -1
384390
h.storage = h.storage[:0]
391+
h.removed = h.removed[:0]
385392

386393
h.highlightRegions(start, lineNum, line, curRegion, h.Def.rules.regions, false)
387394

395+
// check if entries have been removed by invalid region
396+
for _, e := range h.removed {
397+
h.storeRange(e.start, e.end, e.group, e.region, e.pattern)
398+
}
399+
388400
fullHighlights := make([]Group, lineLen)
389401

390402
for _, e := range h.storage {

0 commit comments

Comments
 (0)