mirror of
https://github.com/zyedidia/micro
synced 2024-09-29 20:54:50 +00:00
Add bracket surround
This commit is contained in:
parent
612658d9c4
commit
6f302826c8
|
@ -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
|
||||
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
|
Loading…
Reference in a new issue