提交 6f302826 编写于 作者: Z Zachary Yedidia

Add bracket surround

上级 612658d9
...@@ -1852,6 +1852,68 @@ func (v *View) PreviousSplit(usePlugin bool) bool { ...@@ -1852,6 +1852,68 @@ func (v *View) PreviousSplit(usePlugin bool) bool {
return false 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 curMacro []interface{}
var recordingMacro bool var recordingMacro bool
......
...@@ -69,6 +69,7 @@ var bindingActions = map[string]func(*View, bool) bool{ ...@@ -69,6 +69,7 @@ var bindingActions = map[string]func(*View, bool) bool{
"PastePrimary": (*View).PastePrimary, "PastePrimary": (*View).PastePrimary,
"SelectAll": (*View).SelectAll, "SelectAll": (*View).SelectAll,
"OpenFile": (*View).OpenFile, "OpenFile": (*View).OpenFile,
"WrapBracket": (*View).WrapBracket,
"Start": (*View).Start, "Start": (*View).Start,
"End": (*View).End, "End": (*View).End,
"PageUp": (*View).PageUp, "PageUp": (*View).PageUp,
...@@ -438,6 +439,7 @@ func DefaultBindings() map[string]string { ...@@ -438,6 +439,7 @@ func DefaultBindings() map[string]string {
"Down": "CursorDown", "Down": "CursorDown",
"Right": "CursorRight", "Right": "CursorRight",
"Left": "CursorLeft", "Left": "CursorLeft",
"Alt-j": "WrapBracket",
"ShiftUp": "SelectUp", "ShiftUp": "SelectUp",
"ShiftDown": "SelectDown", "ShiftDown": "SelectDown",
"ShiftLeft": "SelectLeft", "ShiftLeft": "SelectLeft",
......
...@@ -59,6 +59,9 @@ var ( ...@@ -59,6 +59,9 @@ var (
// Event channel // Event channel
events chan tcell.Event events chan tcell.Event
autosave chan bool 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 // LoadInput determines which files should be loaded into buffers
...@@ -419,8 +422,9 @@ func main() { ...@@ -419,8 +422,9 @@ func main() {
// Here is the event loop which runs in a separate thread // Here is the event loop which runs in a separate thread
go func() { go func() {
for { for {
if screen != nil { if screen != nil && !lockPollEvent {
events <- screen.PollEvent() events <- screen.PollEvent()
time.Sleep(1 * time.Millisecond)
} }
} }
}() }()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册