Skip to content

Commit ceaa143

Browse files
authored
highlighter: Fix regions and patterns inside regions (zyedidia#2840)
* highlighter: Fix regions and patterns inside regions * highlighting: Remove 2nd recursive highlightRegion() call ...and add limitGroup checks to pattern search. * yaml: Add TODO type highlighting * highlighting: Don't stop in highlightRegion() at empty lines ...because possible region line end pattern must be detected. * syntax/sh: Correct string handling due to additional pattern handling * syntax/sh: Remove slash in variables * highlighter: Accept nested region only in case it's within the current reagion * highlighter: Accept nested patterns only in case it's within the current reagion * highlighter: Don't search for nesting in case the region end was found at start
1 parent cb260bf commit ceaa143

File tree

3 files changed

+56
-50
lines changed

3 files changed

+56
-50
lines changed

pkg/highlight/highlighter.go

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -134,51 +134,33 @@ func (h *Highlighter) highlightRegion(highlights LineMatch, start int, canMatchE
134134
}
135135
}
136136

137-
loc := findIndex(curRegion.end, curRegion.skip, line)
138-
if loc != nil {
139-
if !statesOnly {
140-
highlights[start+loc[0]] = curRegion.limitGroup
141-
}
142-
if curRegion.parent == nil {
143-
if !statesOnly {
144-
highlights[start+loc[1]] = 0
145-
}
146-
h.highlightEmptyRegion(highlights, start+loc[1], canMatchEnd, lineNum, sliceStart(line, loc[1]), statesOnly)
147-
return highlights
148-
}
149-
if !statesOnly {
150-
highlights[start+loc[1]] = curRegion.parent.group
151-
h.highlightRegion(highlights, start, false, lineNum, sliceEnd(line, loc[0]), curRegion, statesOnly)
152-
}
153-
h.highlightRegion(highlights, start+loc[1], canMatchEnd, lineNum, sliceStart(line, loc[1]), curRegion.parent, statesOnly)
154-
return highlights
155-
}
156-
157-
if lineLen == 0 {
158-
if canMatchEnd {
159-
h.lastRegion = curRegion
137+
var firstRegion *region
138+
firstLoc := []int{lineLen, 0}
139+
searchNesting := true
140+
endLoc := findIndex(curRegion.end, curRegion.skip, line)
141+
if endLoc != nil {
142+
if start == endLoc[0] {
143+
searchNesting = false
144+
} else {
145+
firstLoc = endLoc
160146
}
161-
162-
return highlights
163147
}
164-
165-
firstLoc := []int{lineLen, 0}
166-
167-
var firstRegion *region
168-
for _, r := range curRegion.rules.regions {
169-
loc := findIndex(r.start, r.skip, line)
170-
if loc != nil {
171-
if loc[0] < firstLoc[0] {
172-
firstLoc = loc
173-
firstRegion = r
148+
if searchNesting {
149+
for _, r := range curRegion.rules.regions {
150+
loc := findIndex(r.start, r.skip, line)
151+
if loc != nil {
152+
if loc[0] < firstLoc[0] {
153+
firstLoc = loc
154+
firstRegion = r
155+
}
174156
}
175157
}
176158
}
177-
if firstLoc[0] != lineLen {
159+
if firstRegion != nil && firstLoc[0] != lineLen {
178160
if !statesOnly {
179161
highlights[start+firstLoc[0]] = firstRegion.limitGroup
180162
}
181-
h.highlightRegion(highlights, start, false, lineNum, sliceEnd(line, firstLoc[0]), curRegion, statesOnly)
163+
h.highlightEmptyRegion(highlights, start+firstLoc[1], canMatchEnd, lineNum, sliceStart(line, firstLoc[1]), statesOnly)
182164
h.highlightRegion(highlights, start+firstLoc[1], canMatchEnd, lineNum, sliceStart(line, firstLoc[1]), firstRegion, statesOnly)
183165
return highlights
184166
}
@@ -189,11 +171,17 @@ func (h *Highlighter) highlightRegion(highlights LineMatch, start int, canMatchE
189171
fullHighlights[i] = curRegion.group
190172
}
191173

192-
for _, p := range curRegion.rules.patterns {
193-
matches := findAllIndex(p.regex, line)
194-
for _, m := range matches {
195-
for i := m[0]; i < m[1]; i++ {
196-
fullHighlights[i] = p.group
174+
if searchNesting {
175+
for _, p := range curRegion.rules.patterns {
176+
if curRegion.group == curRegion.limitGroup || p.group == curRegion.limitGroup {
177+
matches := findAllIndex(p.regex, line)
178+
for _, m := range matches {
179+
if ((endLoc == nil) || (m[0] < endLoc[0])) {
180+
for i := m[0]; i < m[1]; i++ {
181+
fullHighlights[i] = p.group
182+
}
183+
}
184+
}
197185
}
198186
}
199187
}
@@ -204,6 +192,25 @@ func (h *Highlighter) highlightRegion(highlights LineMatch, start int, canMatchE
204192
}
205193
}
206194

195+
loc := endLoc
196+
if loc != nil {
197+
if !statesOnly {
198+
highlights[start+loc[0]] = curRegion.limitGroup
199+
}
200+
if curRegion.parent == nil {
201+
if !statesOnly {
202+
highlights[start+loc[1]] = 0
203+
}
204+
h.highlightEmptyRegion(highlights, start+loc[1], canMatchEnd, lineNum, sliceStart(line, loc[1]), statesOnly)
205+
return highlights
206+
}
207+
if !statesOnly {
208+
highlights[start+loc[1]] = curRegion.parent.group
209+
}
210+
h.highlightRegion(highlights, start+loc[1], canMatchEnd, lineNum, sliceStart(line, loc[1]), curRegion.parent, statesOnly)
211+
return highlights
212+
}
213+
207214
if canMatchEnd {
208215
h.lastRegion = curRegion
209216
}
@@ -220,8 +227,8 @@ func (h *Highlighter) highlightEmptyRegion(highlights LineMatch, start int, canM
220227
return highlights
221228
}
222229

223-
firstLoc := []int{lineLen, 0}
224230
var firstRegion *region
231+
firstLoc := []int{lineLen, 0}
225232
for _, r := range h.Def.rules.regions {
226233
loc := findIndex(r.start, r.skip, line)
227234
if loc != nil {
@@ -231,7 +238,7 @@ func (h *Highlighter) highlightEmptyRegion(highlights LineMatch, start int, canM
231238
}
232239
}
233240
}
234-
if firstLoc[0] != lineLen {
241+
if firstRegion != nil && firstLoc[0] != lineLen {
235242
if !statesOnly {
236243
highlights[start+firstLoc[0]] = firstRegion.limitGroup
237244
}

runtime/syntax/sh.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ rules:
4242
- statement: " (-[A-Za-z]+|--[a-z]+)"
4343

4444
- identifier: "\\$\\{[0-9A-Za-z_:!%&=+#~@*^$?, .\\-\\/\\[\\]]+\\}"
45-
- identifier: "\\$[0-9A-Za-z_:!%&=+#~@*^$?,\\-\\/\\[\\]]+"
45+
- identifier: "\\$[0-9A-Za-z_:!%&=+#~@*^$?,\\-\\[\\]]+"
4646

4747
- constant.string:
4848
start: "\""
4949
end: "\""
5050
skip: "\\\\."
51-
rules:
52-
- constant.specialChar: "\\\\."
51+
rules: []
5352

5453
- constant.string:
5554
start: "'"
5655
end: "'"
56+
skip: "\\\\."
5757
rules: []
5858

5959
- comment:

runtime/syntax/yaml.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,5 @@ rules:
3030
- comment:
3131
start: "#"
3232
end: "$"
33-
rules: []
34-
35-
33+
rules:
34+
- todo: "(TODO|XXX|FIXME):?"

0 commit comments

Comments
 (0)