Skip to content

Commit fd3a002

Browse files
authored
Add matchbraceleft option (zyedidia#3432)
Add `matchbraceleft` option to allow disabling the default behavior matching not just the brace under cursor but also the brace to the left of it (which is arguably convenient, but also ambiguous and non-intuitive). With `matchbraceleft` disabled, micro will only match the brace character that is precisely under the cursor, and also when jumping to the matching brace, will always move cursor precisely to the matching brace character, not to the character next to it. Nota bene: historical journey: - There was already a `matchbraceleft` option introduced in commit ea6a87d, when this feature (matching brace to the left) was introduced first time. That time it was matching _only_ the brace to the left, _instead_ of the brace under the cursor, and was disabled by default. - Later this feature was removed during the big refactoring of micro. - Then this feature was reintroduced again in commit d1e713c, in its present form (i.e. combined brace matching both under the cursor and to the left, simulating I-beam cursor behavior), and it was introduced unconditionally, without an option to disable it. - Since then, multiple users complained about this feature and asked for an option to disable it, so now we are reintroducing it as an option again (this time enabled by default though).
1 parent f88ac6d commit fd3a002

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

internal/action/actions.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,10 +1472,14 @@ func (h *BufPane) paste(clip string) {
14721472
func (h *BufPane) JumpToMatchingBrace() bool {
14731473
matchingBrace, left, found := h.Buf.FindMatchingBrace(h.Cursor.Loc)
14741474
if found {
1475-
if left {
1476-
h.Cursor.GotoLoc(matchingBrace)
1475+
if h.Buf.Settings["matchbraceleft"].(bool) {
1476+
if left {
1477+
h.Cursor.GotoLoc(matchingBrace)
1478+
} else {
1479+
h.Cursor.GotoLoc(matchingBrace.Move(1, h.Buf))
1480+
}
14771481
} else {
1478-
h.Cursor.GotoLoc(matchingBrace.Move(1, h.Buf))
1482+
h.Cursor.GotoLoc(matchingBrace)
14791483
}
14801484
h.Relocate()
14811485
return true

internal/buffer/buffer.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,17 +1193,19 @@ func (b *Buffer) FindMatchingBrace(start Loc) (Loc, bool, bool) {
11931193
}
11941194
}
11951195

1196-
// failed to find matching brace for the given location, so try to find matching
1197-
// brace for the location one character left of it
1198-
if start.X-1 >= 0 && start.X-1 < len(curLine) {
1199-
leftChar := curLine[start.X-1]
1200-
left := Loc{start.X - 1, start.Y}
1201-
1202-
for _, bp := range BracePairs {
1203-
if leftChar == bp[0] || leftChar == bp[1] {
1204-
mb, found := b.findMatchingBrace(bp, left, leftChar)
1205-
if found {
1206-
return mb, true, true
1196+
if b.Settings["matchbraceleft"].(bool) {
1197+
// failed to find matching brace for the given location, so try to find matching
1198+
// brace for the location one character left of it
1199+
if start.X-1 >= 0 && start.X-1 < len(curLine) {
1200+
leftChar := curLine[start.X-1]
1201+
left := Loc{start.X - 1, start.Y}
1202+
1203+
for _, bp := range BracePairs {
1204+
if leftChar == bp[0] || leftChar == bp[1] {
1205+
mb, found := b.findMatchingBrace(bp, left, leftChar)
1206+
if found {
1207+
return mb, true, true
1208+
}
12071209
}
12081210
}
12091211
}

internal/config/settings.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ var defaultCommonSettings = map[string]interface{}{
7171
"indentchar": " ",
7272
"keepautoindent": false,
7373
"matchbrace": true,
74+
"matchbraceleft": true,
7475
"matchbracestyle": "underline",
7576
"mkparents": false,
7677
"permbackup": false,

runtime/help/options.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,19 @@ Here are the available options:
231231
default value: `false`
232232

233233
* `matchbrace`: show matching braces for '()', '{}', '[]' when the cursor
234-
is on a brace character or next to it.
234+
is on a brace character or (if `matchbraceleft` is enabled) next to it.
235+
236+
default value: `true`
237+
238+
* `matchbraceleft`: simulate I-beam cursor behavior (cursor located not on a
239+
character but "between" characters): when showing matching braces, if there
240+
is no brace character directly under the cursor, match the brace character
241+
to the left of the cursor instead. Also when jumping to the matching brace,
242+
move the cursor either to the matching brace character or to the character
243+
next to it, depending on whether the initial cursor position was on the
244+
brace character or next to it (i.e. "inside" or "outside" the braces).
245+
With `matchbraceleft` disabled, micro will only match the brace directly
246+
under the cursor and will only jump to precisely to the matching brace.
235247

236248
default value: `true`
237249

@@ -526,6 +538,7 @@ so that you can see what the formatting should look like.
526538
"linter": true,
527539
"literate": true,
528540
"matchbrace": true,
541+
"matchbraceleft": true,
529542
"matchbracestyle": "underline",
530543
"mkparents": false,
531544
"mouse": true,

0 commit comments

Comments
 (0)