Skip to content

Commit 0373a63

Browse files
committed
Refactor action execution code
Instead of calling execAction() and then letting it check whether it should actually execute this action, do this check before calling execAction(), to make the code clear and straightforward. Precisely: for multicursor actions, call execAction() in a loop for every cursor, but for non-multicursor actions, call execAction() just once, without a loop. This, in particular, allows to get rid of the hacky "c == nil" check, since we no longer iterate a slice that may change in the meantime (since SpawnMultiCursor and RemoveMultiCursor are non-multicursor actions and thus are no longer executed while iterating the slice).
1 parent 765889f commit 0373a63

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

internal/action/bufpane.go

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -150,29 +150,31 @@ func BufMapEvent(k Event, action string) {
150150
actionfns = append(actionfns, afn)
151151
}
152152
bufAction := func(h *BufPane, te *tcell.EventMouse) bool {
153-
success := true
154153
for i, a := range actionfns {
155-
innerSuccess := true
156-
cursors := h.Buf.GetCursors()
157-
for j, c := range cursors {
158-
if c == nil {
159-
continue
160-
}
161-
h.Buf.SetCurCursor(c.Num)
162-
h.Cursor = c
163-
if i == 0 || (success && types[i-1] == '&') || (!success && types[i-1] == '|') || (types[i-1] == ',') {
164-
innerSuccess = innerSuccess && h.execAction(a, names[i], j, te)
165-
} else {
166-
break
154+
var success bool
155+
if _, ok := MultiActions[names[i]]; ok {
156+
success = true
157+
for _, c := range h.Buf.GetCursors() {
158+
h.Buf.SetCurCursor(c.Num)
159+
h.Cursor = c
160+
success = success && h.execAction(a, names[i], te)
167161
}
162+
} else {
163+
h.Buf.SetCurCursor(0)
164+
h.Cursor = h.Buf.GetActiveCursor()
165+
success = h.execAction(a, names[i], te)
168166
}
167+
169168
// if the action changed the current pane, update the reference
170169
h = MainTab().CurPane()
171-
success = innerSuccess
172170
if h == nil {
173171
// stop, in case the current pane is not a BufPane
174172
break
175173
}
174+
175+
if (!success && types[i] == '&') || (success && types[i] == '|') {
176+
break
177+
}
176178
}
177179
return true
178180
}
@@ -562,39 +564,33 @@ func (h *BufPane) DoKeyEvent(e Event) bool {
562564
return more
563565
}
564566

565-
func (h *BufPane) execAction(action BufAction, name string, cursor int, te *tcell.EventMouse) bool {
567+
func (h *BufPane) execAction(action BufAction, name string, te *tcell.EventMouse) bool {
566568
if name != "Autocomplete" && name != "CycleAutocompleteBack" {
567569
h.Buf.HasSuggestions = false
568570
}
569571

570-
_, isMulti := MultiActions[name]
571-
if (!isMulti && cursor == 0) || isMulti {
572-
if h.PluginCB("pre" + name) {
573-
var success bool
574-
switch a := action.(type) {
575-
case BufKeyAction:
576-
success = a(h)
577-
case BufMouseAction:
578-
success = a(h, te)
579-
}
580-
success = success && h.PluginCB("on"+name)
572+
if !h.PluginCB("pre" + name) {
573+
return false
574+
}
581575

582-
if isMulti {
583-
if recordingMacro {
584-
if name != "ToggleMacro" && name != "PlayMacro" {
585-
curmacro = append(curmacro, action)
586-
}
587-
}
588-
}
576+
var success bool
577+
switch a := action.(type) {
578+
case BufKeyAction:
579+
success = a(h)
580+
case BufMouseAction:
581+
success = a(h, te)
582+
}
583+
success = success && h.PluginCB("on"+name)
589584

590-
return success
585+
if _, ok := MultiActions[name]; ok {
586+
if recordingMacro {
587+
if name != "ToggleMacro" && name != "PlayMacro" {
588+
curmacro = append(curmacro, action)
589+
}
591590
}
592-
} else {
593-
// do nothing but return true, to not break the chain
594-
return true
595591
}
596592

597-
return false
593+
return success
598594
}
599595

600596
func (h *BufPane) completeAction(action string) {

0 commit comments

Comments
 (0)