Skip to content

Commit

Permalink
Refactor action execution code
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
dmaluka committed Jul 18, 2024
1 parent 765889f commit 0373a63
Showing 1 changed file with 34 additions and 38 deletions.
72 changes: 34 additions & 38 deletions internal/action/bufpane.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0373a63

Please sign in to comment.