Skip to content

Commit 0870a07

Browse files
Extracting brace matching logic into its own public function
1 parent b0baa98 commit 0870a07

File tree

1 file changed

+87
-64
lines changed

1 file changed

+87
-64
lines changed

internal/buffer/buffer.go

Lines changed: 87 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,81 +1177,104 @@ func (b *Buffer) isLocInStringOrComment(loc Loc, sortedIndices []int) bool {
11771177
return false
11781178
}
11791179

1180-
func (b *Buffer) findMatchingBrace(braceType [2]rune, start Loc, char rune) (Loc, bool) {
1181-
if b.isLocInStringOrComment(start, nil) {
1182-
return start, false
1183-
}
1180+
func (b *Buffer) FindOpeningBrace(braceType [2]rune, start Loc) (Loc, bool) {
1181+
startChar := []rune(string(b.LineBytes(start.Y)))[start.X]
11841182
var i int
1185-
if char == braceType[0] {
1186-
for y := start.Y; y < b.LinesNum(); y++ {
1187-
var sortedGroups []int
1188-
sortedGroupsPopulated := false
1189-
l := []rune(string(b.LineBytes(y)))
1190-
xInit := 0
1191-
if y == start.Y {
1192-
xInit = start.X
1193-
}
1194-
for x := xInit; x < len(l); x++ {
1195-
r := l[x]
1196-
if r == braceType[0] {
1197-
if !sortedGroupsPopulated {
1198-
sortedGroups = b.GetSortedSyntaxGroupIndices(y)
1199-
sortedGroupsPopulated = true
1200-
}
1201-
if !b.isLocInStringOrComment(Loc{x, y}, sortedGroups) {
1202-
i++
1203-
}
1204-
} else if r == braceType[1] {
1205-
if !sortedGroupsPopulated {
1206-
sortedGroups = b.GetSortedSyntaxGroupIndices(y)
1207-
sortedGroupsPopulated = true
1208-
}
1209-
if !b.isLocInStringOrComment(Loc{x, y}, sortedGroups) {
1210-
i--
1211-
}
1212-
if i == 0 {
1213-
return Loc{x, y}, true
1214-
}
1183+
if startChar == braceType[1] {
1184+
i = 0
1185+
} else {
1186+
i = 1
1187+
}
1188+
for y := start.Y; y >= 0; y-- {
1189+
var sortedGroups []int
1190+
sortedGroupsPopulated := false
1191+
l := []rune(string(b.lines[y].data))
1192+
xInit := len(l) - 1
1193+
if y == start.Y {
1194+
xInit = start.X
1195+
}
1196+
for x := xInit; x >= 0; x-- {
1197+
r := l[x]
1198+
if r == braceType[1] {
1199+
if !sortedGroupsPopulated {
1200+
sortedGroups = b.GetSortedSyntaxGroupIndices(y)
1201+
sortedGroupsPopulated = true
1202+
}
1203+
if !b.isLocInStringOrComment(Loc{x, y}, sortedGroups) {
1204+
i++
1205+
}
1206+
} else if r == braceType[0]{
1207+
if !sortedGroupsPopulated {
1208+
sortedGroups = b.GetSortedSyntaxGroupIndices(y)
1209+
sortedGroupsPopulated = true
1210+
}
1211+
if !b.isLocInStringOrComment(Loc{x, y}, sortedGroups) {
1212+
i--
1213+
}
1214+
if i == 0 {
1215+
return Loc{x, y}, true
12151216
}
12161217
}
12171218
}
1218-
} else if char == braceType[1] {
1219-
for y := start.Y; y >= 0; y-- {
1220-
var sortedGroups []int
1221-
sortedGroupsPopulated := false
1222-
l := []rune(string(b.lines[y].data))
1223-
xInit := len(l) - 1
1224-
if y == start.Y {
1225-
xInit = start.X
1226-
}
1227-
for x := xInit; x >= 0; x-- {
1228-
r := l[x]
1229-
if r == braceType[1] {
1230-
if !sortedGroupsPopulated {
1231-
sortedGroups = b.GetSortedSyntaxGroupIndices(y)
1232-
sortedGroupsPopulated = true
1233-
}
1234-
if !b.isLocInStringOrComment(Loc{x, y}, sortedGroups) {
1235-
i++
1236-
}
1237-
} else if r == braceType[0]{
1238-
if !sortedGroupsPopulated {
1239-
sortedGroups = b.GetSortedSyntaxGroupIndices(y)
1240-
sortedGroupsPopulated = true
1241-
}
1242-
if !b.isLocInStringOrComment(Loc{x, y}, sortedGroups) {
1243-
i--
1244-
}
1245-
if i == 0 {
1246-
return Loc{x, y}, true
1247-
}
1219+
}
1220+
return start, false
1221+
}
1222+
1223+
func (b *Buffer) FindClosingBrace(braceType [2]rune, start Loc) (Loc, bool) {
1224+
startChar := []rune(string(b.LineBytes(start.Y)))[start.X]
1225+
var i int
1226+
if startChar == braceType[0] {
1227+
i = 0
1228+
} else {
1229+
i = 1
1230+
}
1231+
for y := start.Y; y < b.LinesNum(); y++ {
1232+
var sortedGroups []int
1233+
sortedGroupsPopulated := false
1234+
l := []rune(string(b.LineBytes(y)))
1235+
xInit := 0
1236+
if y == start.Y {
1237+
xInit = start.X
1238+
}
1239+
for x := xInit; x < len(l); x++ {
1240+
r := l[x]
1241+
if r == braceType[0] {
1242+
if !sortedGroupsPopulated {
1243+
sortedGroups = b.GetSortedSyntaxGroupIndices(y)
1244+
sortedGroupsPopulated = true
1245+
}
1246+
if !b.isLocInStringOrComment(Loc{x, y}, sortedGroups) {
1247+
i++
1248+
}
1249+
} else if r == braceType[1] {
1250+
if !sortedGroupsPopulated {
1251+
sortedGroups = b.GetSortedSyntaxGroupIndices(y)
1252+
sortedGroupsPopulated = true
1253+
}
1254+
if !b.isLocInStringOrComment(Loc{x, y}, sortedGroups) {
1255+
i--
1256+
}
1257+
if i == 0 {
1258+
return Loc{x, y}, true
12481259
}
12491260
}
12501261
}
12511262
}
12521263
return start, false
12531264
}
12541265

1266+
func (b *Buffer) findMatchingBrace(braceType [2]rune, start Loc, char rune) (Loc, bool) {
1267+
if b.isLocInStringOrComment(start, nil) {
1268+
return start, false
1269+
}
1270+
if char == braceType[0] {
1271+
return b.FindClosingBrace(braceType, start)
1272+
} else if char == braceType[1] {
1273+
return b.FindOpeningBrace(braceType, start)
1274+
}
1275+
return start, false
1276+
}
1277+
12551278
// If there is a brace character (for example '{' or ']') at the given start location,
12561279
// FindMatchingBrace returns the location of the matching brace for it (for example '}'
12571280
// or '['). The second returned value is true if there was no matching brace found

0 commit comments

Comments
 (0)