From 0373a63b31e666773b7fb2555dfe85d833597ac9 Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Sun, 16 Jun 2024 00:05:12 +0200 Subject: [PATCH] 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). --- internal/action/bufpane.go | 72 ++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/internal/action/bufpane.go b/internal/action/bufpane.go index 40f4379c2e..4c5b885804 100644 --- a/internal/action/bufpane.go +++ b/internal/action/bufpane.go @@ -150,29 +150,31 @@ func BufMapEvent(k Event, action string) { actionfns = append(actionfns, afn) } bufAction := func(h *BufPane, te *tcell.EventMouse) bool { - success := true for i, a := range actionfns { - innerSuccess := true - cursors := h.Buf.GetCursors() - for j, c := range cursors { - if c == nil { - continue - } - h.Buf.SetCurCursor(c.Num) - h.Cursor = c - if i == 0 || (success && types[i-1] == '&') || (!success && types[i-1] == '|') || (types[i-1] == ',') { - innerSuccess = innerSuccess && h.execAction(a, names[i], j, te) - } else { - break + var success bool + if _, ok := MultiActions[names[i]]; ok { + success = true + for _, c := range h.Buf.GetCursors() { + h.Buf.SetCurCursor(c.Num) + h.Cursor = c + success = success && h.execAction(a, names[i], te) } + } else { + h.Buf.SetCurCursor(0) + h.Cursor = h.Buf.GetActiveCursor() + success = h.execAction(a, names[i], te) } + // if the action changed the current pane, update the reference h = MainTab().CurPane() - success = innerSuccess if h == nil { // stop, in case the current pane is not a BufPane break } + + if (!success && types[i] == '&') || (success && types[i] == '|') { + break + } } return true } @@ -562,39 +564,33 @@ func (h *BufPane) DoKeyEvent(e Event) bool { return more } -func (h *BufPane) execAction(action BufAction, name string, cursor int, te *tcell.EventMouse) bool { +func (h *BufPane) execAction(action BufAction, name string, te *tcell.EventMouse) bool { if name != "Autocomplete" && name != "CycleAutocompleteBack" { h.Buf.HasSuggestions = false } - _, isMulti := MultiActions[name] - if (!isMulti && cursor == 0) || isMulti { - if h.PluginCB("pre" + name) { - var success bool - switch a := action.(type) { - case BufKeyAction: - success = a(h) - case BufMouseAction: - success = a(h, te) - } - success = success && h.PluginCB("on"+name) + if !h.PluginCB("pre" + name) { + return false + } - if isMulti { - if recordingMacro { - if name != "ToggleMacro" && name != "PlayMacro" { - curmacro = append(curmacro, action) - } - } - } + var success bool + switch a := action.(type) { + case BufKeyAction: + success = a(h) + case BufMouseAction: + success = a(h, te) + } + success = success && h.PluginCB("on"+name) - return success + if _, ok := MultiActions[name]; ok { + if recordingMacro { + if name != "ToggleMacro" && name != "PlayMacro" { + curmacro = append(curmacro, action) + } } - } else { - // do nothing but return true, to not break the chain - return true } - return false + return success } func (h *BufPane) completeAction(action string) {