Skip to content

Commit

Permalink
Correct FindMatchingBrace() counting
Browse files Browse the repository at this point in the history
Make brace under the cursor have priority over brace to the left in
ambiguous cases when matching braces

Co-authored-by: Dmitry Maluka <dmitrymaluka@gmail.com>
  • Loading branch information
2 people authored and toiletbril committed Nov 20, 2023
1 parent 8ee5534 commit 320e71f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
20 changes: 10 additions & 10 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ func (b *Buffer) FindMatchingBrace(braceType [2]rune, start Loc) (Loc, bool, boo
leftChar = curLine[start.X-1]
}
var i int
if startChar == braceType[0] || leftChar == braceType[0] {
if startChar == braceType[0] || (leftChar == braceType[0] && startChar != braceType[1]) {
for y := start.Y; y < b.LinesNum(); y++ {
l := []rune(string(b.LineBytes(y)))
xInit := 0
Expand Down Expand Up @@ -1039,24 +1039,24 @@ func (b *Buffer) FindMatchingBrace(braceType [2]rune, start Loc) (Loc, bool, boo
l := []rune(string(b.lines[y].data))
xInit := len(l) - 1
if y == start.Y {
if leftChar == braceType[1] {
xInit = start.X - 1
} else {
if startChar == braceType[1] {
xInit = start.X
} else {
xInit = start.X - 1
}
}
for x := xInit; x >= 0; x-- {
r := l[x]
if r == braceType[0] {
if r == braceType[1] {
i++
} else if r == braceType[0] {
i--
if i == 0 {
if leftChar == braceType[1] {
return Loc{x, y}, true, true
if startChar == braceType[1] {
return Loc{x, y}, false, true
}
return Loc{x, y}, false, true
return Loc{x, y}, true, true
}
} else if r == braceType[1] {
i++
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion internal/display/bufwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,9 @@ func (w *BufWindow) displayBuffer() {
if found {
matchingBraces = append(matchingBraces, mb)
if !left {
matchingBraces = append(matchingBraces, curLoc)
if b.Settings["matchbracestyle"].(string) != "highlight" {
matchingBraces = append(matchingBraces, curLoc)
}
} else {
matchingBraces = append(matchingBraces, curLoc.Move(-1, b))
}
Expand Down

0 comments on commit 320e71f

Please # to comment.