Compare commits

...

1 commit

Author SHA1 Message Date
Zachary Yedidia
6f302826c8 Add bracket surround 2017-09-13 10:48:53 -04:00
3 changed files with 69 additions and 1 deletions

View file

@ -1852,6 +1852,68 @@ func (v *View) PreviousSplit(usePlugin bool) bool {
return false
}
func (v *View) WrapBracket(usePlugin bool) bool {
if v.Cursor.HasSelection() {
lockPollEvent = true
event := screen.PollEvent()
lockPollEvent = false
ev, ok := event.(*tcell.EventKey)
if !ok {
return false
}
if !strings.Contains("()[]{}\"'", string(ev.Rune())) {
return false
}
start := v.Cursor.CurSelection[0]
end := v.Cursor.CurSelection[1]
if start.Y != end.Y {
return false
}
close := map[string]string{
")": ")",
"]": "]",
"}": "}",
"(": ")",
"[": "]",
"{": "}",
"\"": "\"",
"'": "'",
}
open := map[string]string{
")": "(",
"]": "[",
"}": "{",
"(": "(",
"[": "[",
"{": "{",
"\"": "\"",
"'": "'",
}
if usePlugin && !PreActionCall("WrapBracket", v) {
return false
}
r := string(ev.Rune())
if start.GreaterThan(end) {
start, end = end, start
v.Buf.Insert(start, open[r])
v.Buf.Insert(end.Move(1, v.Buf), close[r])
v.Cursor.CurSelection[1] = start
} else {
v.Buf.Insert(start, open[r])
v.Buf.Insert(end.Move(1, v.Buf), close[r])
v.Cursor.CurSelection[0] = start
}
if usePlugin {
return PostActionCall("WrapBracket", v)
}
}
return false
}
var curMacro []interface{}
var recordingMacro bool

View file

@ -69,6 +69,7 @@ var bindingActions = map[string]func(*View, bool) bool{
"PastePrimary": (*View).PastePrimary,
"SelectAll": (*View).SelectAll,
"OpenFile": (*View).OpenFile,
"WrapBracket": (*View).WrapBracket,
"Start": (*View).Start,
"End": (*View).End,
"PageUp": (*View).PageUp,
@ -438,6 +439,7 @@ func DefaultBindings() map[string]string {
"Down": "CursorDown",
"Right": "CursorRight",
"Left": "CursorLeft",
"Alt-j": "WrapBracket",
"ShiftUp": "SelectUp",
"ShiftDown": "SelectDown",
"ShiftLeft": "SelectLeft",

View file

@ -59,6 +59,9 @@ var (
// Event channel
events chan tcell.Event
autosave chan bool
// Read events on another thread or wait for a temporary read
lockPollEvent bool
)
// LoadInput determines which files should be loaded into buffers
@ -419,8 +422,9 @@ func main() {
// Here is the event loop which runs in a separate thread
go func() {
for {
if screen != nil {
if screen != nil && !lockPollEvent {
events <- screen.PollEvent()
time.Sleep(1 * time.Millisecond)
}
}
}()