Add JumpToMatchingBrace action

This commit adds the JumpToMatchingBrace action which lets the cursor
jump to a matching brace if it is on one.

Closes #853
This commit is contained in:
Zachary Yedidia
2018-01-07 16:17:22 -05:00
parent 86c08bd747
commit fc2566a0de
7 changed files with 39 additions and 6 deletions

View File

@@ -1357,6 +1357,27 @@ func (v *View) PastePrimary(usePlugin bool) bool {
return true
}
// JumpToMatchingBrace moves the cursor to the matching brace if it is
// currently on a brace
func (v *View) JumpToMatchingBrace(usePlugin bool) bool {
if usePlugin && !PreActionCall("JumpToMatchingBrace", v) {
return false
}
for _, bp := range bracePairs {
r := v.Cursor.RuneUnder(v.Cursor.X)
if r == bp[0] || r == bp[1] {
matchingBrace := v.Buf.FindMatchingBrace(bp, v.Cursor.Loc)
v.Cursor.GotoLoc(matchingBrace)
}
}
if usePlugin {
return PostActionCall("JumpToMatchingBrace", v)
}
return true
}
// SelectAll selects the entire buffer
func (v *View) SelectAll(usePlugin bool) bool {
if usePlugin && !PreActionCall("SelectAll", v) {

View File

@@ -111,6 +111,7 @@ var bindingActions = map[string]func(*View, bool) bool{
"RemoveMultiCursor": (*View).RemoveMultiCursor,
"RemoveAllMultiCursors": (*View).RemoveAllMultiCursors,
"SkipMultiCursor": (*View).SkipMultiCursor,
"JumpToMatchingBrace": (*View).JumpToMatchingBrace,
// This was changed to InsertNewline but I don't want to break backwards compatibility
"InsertEnter": (*View).InsertNewline,

View File

@@ -150,7 +150,6 @@ func (c *CellView) Draw(buf *Buffer, top, height, left, width int) {
if viewCol >= 0 {
st := curStyle
if colN == matchingBrace.X && lineN == matchingBrace.Y {
messenger.Message(matchingBrace)
st = curStyle.Reverse(true)
}
c.lines[viewLine][viewCol] = &Char{Loc{viewCol, viewLine}, Loc{colN, lineN}, char, char, st, 1}

View File

@@ -35,6 +35,13 @@ func (c *Cursor) Goto(b Cursor) {
c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
}
// GotoLoc puts the cursor at the given cursor's location and gives
// the current cursor its selection too
func (c *Cursor) GotoLoc(l Loc) {
c.X, c.Y = l.X, l.Y
c.LastVisualX = c.GetVisualX()
}
// CopySelection copies the user's selection to either "primary"
// or "clipboard"
func (c *Cursor) CopySelection(target string) {

File diff suppressed because one or more lines are too long

View File

@@ -218,6 +218,7 @@ RemoveMultiCursor
RemoveAllMultiCursors
SkipMultiCursor
UnbindKey
JumpToMatchingBrace
```
You can also bind some mouse actions (these must be bound to mouse buttons)

View File

@@ -182,6 +182,10 @@ Here are the options that you can set:
default value: `on`
* `matchbrace`: highlight matching braces for '()', '{}', '[]'
default value: `off`
* `syntax`: turns syntax on or off.
default value: `on`