Skip to content

Commit d173e52

Browse files
authored
Merge pull request zyedidia#3352 from dmaluka/action-return-values
Improve return values of some actions + some improvements
2 parents f8e532b + 0373a63 commit d173e52

File tree

5 files changed

+96
-55
lines changed

5 files changed

+96
-55
lines changed

internal/action/actions.go

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,9 @@ func (h *BufPane) ToggleHighlightSearch() bool {
11221122

11231123
// UnhighlightSearch unhighlights all instances of the last used search term
11241124
func (h *BufPane) UnhighlightSearch() bool {
1125+
if !h.Buf.HighlightSearch {
1126+
return false
1127+
}
11251128
h.Buf.HighlightSearch = false
11261129
return true
11271130
}
@@ -1217,15 +1220,19 @@ func (h *BufPane) DiffPrevious() bool {
12171220

12181221
// Undo undoes the last action
12191222
func (h *BufPane) Undo() bool {
1220-
h.Buf.Undo()
1223+
if !h.Buf.Undo() {
1224+
return false
1225+
}
12211226
InfoBar.Message("Undid action")
12221227
h.Relocate()
12231228
return true
12241229
}
12251230

12261231
// Redo redoes the last action
12271232
func (h *BufPane) Redo() bool {
1228-
h.Buf.Redo()
1233+
if !h.Buf.Redo() {
1234+
return false
1235+
}
12291236
InfoBar.Message("Redid action")
12301237
h.Relocate()
12311238
return true
@@ -1624,10 +1631,9 @@ func (h *BufPane) ToggleRuler() bool {
16241631
return true
16251632
}
16261633

1627-
// ClearStatus clears the messenger bar
1634+
// ClearStatus clears the infobar. It is an alias for ClearInfo.
16281635
func (h *BufPane) ClearStatus() bool {
1629-
InfoBar.Message("")
1630-
return true
1636+
return h.ClearInfo()
16311637
}
16321638

16331639
// ToggleHelp toggles the help screen
@@ -1682,12 +1688,18 @@ func (h *BufPane) Escape() bool {
16821688

16831689
// Deselect deselects on the current cursor
16841690
func (h *BufPane) Deselect() bool {
1691+
if !h.Cursor.HasSelection() {
1692+
return false
1693+
}
16851694
h.Cursor.Deselect(true)
16861695
return true
16871696
}
16881697

16891698
// ClearInfo clears the infobar
16901699
func (h *BufPane) ClearInfo() bool {
1700+
if InfoBar.Msg == "" {
1701+
return false
1702+
}
16911703
InfoBar.Message("")
16921704
return true
16931705
}
@@ -1778,6 +1790,10 @@ func (h *BufPane) AddTab() bool {
17781790
// PreviousTab switches to the previous tab in the tab list
17791791
func (h *BufPane) PreviousTab() bool {
17801792
tabsLen := len(Tabs.List)
1793+
if tabsLen == 1 {
1794+
return false
1795+
}
1796+
17811797
a := Tabs.Active() + tabsLen
17821798
Tabs.SetActive((a - 1) % tabsLen)
17831799

@@ -1786,8 +1802,13 @@ func (h *BufPane) PreviousTab() bool {
17861802

17871803
// NextTab switches to the next tab in the tab list
17881804
func (h *BufPane) NextTab() bool {
1805+
tabsLen := len(Tabs.List)
1806+
if tabsLen == 1 {
1807+
return false
1808+
}
1809+
17891810
a := Tabs.Active()
1790-
Tabs.SetActive((a + 1) % len(Tabs.List))
1811+
Tabs.SetActive((a + 1) % tabsLen)
17911812

17921813
return true
17931814
}
@@ -1823,6 +1844,10 @@ func (h *BufPane) Unsplit() bool {
18231844

18241845
// NextSplit changes the view to the next split
18251846
func (h *BufPane) NextSplit() bool {
1847+
if len(h.tab.Panes) == 1 {
1848+
return false
1849+
}
1850+
18261851
a := h.tab.active
18271852
if a < len(h.tab.Panes)-1 {
18281853
a++
@@ -1837,6 +1862,10 @@ func (h *BufPane) NextSplit() bool {
18371862

18381863
// PreviousSplit changes the view to the previous split
18391864
func (h *BufPane) PreviousSplit() bool {
1865+
if len(h.tab.Panes) == 1 {
1866+
return false
1867+
}
1868+
18401869
a := h.tab.active
18411870
if a > 0 {
18421871
a--
@@ -2038,6 +2067,9 @@ func (h *BufPane) MouseMultiCursor(e *tcell.EventMouse) bool {
20382067
// SkipMultiCursor moves the current multiple cursor to the next available position
20392068
func (h *BufPane) SkipMultiCursor() bool {
20402069
lastC := h.Buf.GetCursor(h.Buf.NumCursors() - 1)
2070+
if !lastC.HasSelection() {
2071+
return false
2072+
}
20412073
sel := lastC.GetSelection()
20422074
searchStart := lastC.CurSelection[1]
20432075

@@ -2073,17 +2105,24 @@ func (h *BufPane) RemoveMultiCursor() bool {
20732105
h.Buf.RemoveCursor(h.Buf.NumCursors() - 1)
20742106
h.Buf.SetCurCursor(h.Buf.NumCursors() - 1)
20752107
h.Buf.UpdateCursors()
2076-
} else {
2108+
} else if h.multiWord {
20772109
h.multiWord = false
2110+
h.Cursor.Deselect(true)
2111+
} else {
2112+
return false
20782113
}
20792114
h.Relocate()
20802115
return true
20812116
}
20822117

20832118
// RemoveAllMultiCursors removes all cursors except the base cursor
20842119
func (h *BufPane) RemoveAllMultiCursors() bool {
2085-
h.Buf.ClearCursors()
2086-
h.multiWord = false
2120+
if h.Buf.NumCursors() > 1 || h.multiWord {
2121+
h.Buf.ClearCursors()
2122+
h.multiWord = false
2123+
} else {
2124+
return false
2125+
}
20872126
h.Relocate()
20882127
return true
20892128
}

internal/action/bufpane.go

Lines changed: 34 additions & 35 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-
cursors := h.Buf.GetCursors()
154-
success := true
155153
for i, a := range actionfns {
156-
innerSuccess := true
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,36 +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
}
592591
}
593592

594-
return false
593+
return success
595594
}
596595

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

internal/buffer/buffer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ func (b *Buffer) ClearCursors() {
10891089
b.cursors = b.cursors[:1]
10901090
b.UpdateCursors()
10911091
b.curCursor = 0
1092-
b.GetActiveCursor().ResetSelection()
1092+
b.GetActiveCursor().Deselect(true)
10931093
}
10941094

10951095
// MoveLinesUp moves the range of lines up one row

internal/buffer/eventhandler.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,11 @@ func (eh *EventHandler) Execute(t *TextEvent) {
253253
ExecuteTextEvent(t, eh.buf)
254254
}
255255

256-
// Undo the first event in the undo stack
257-
func (eh *EventHandler) Undo() {
256+
// Undo the first event in the undo stack. Returns false if the stack is empty.
257+
func (eh *EventHandler) Undo() bool {
258258
t := eh.UndoStack.Peek()
259259
if t == nil {
260-
return
260+
return false
261261
}
262262

263263
startTime := t.Time.UnixNano() / int64(time.Millisecond)
@@ -266,15 +266,16 @@ func (eh *EventHandler) Undo() {
266266
for {
267267
t = eh.UndoStack.Peek()
268268
if t == nil {
269-
return
269+
break
270270
}
271271

272272
if t.Time.UnixNano()/int64(time.Millisecond) < endTime {
273-
return
273+
break
274274
}
275275

276276
eh.UndoOneEvent()
277277
}
278+
return true
278279
}
279280

280281
// UndoOneEvent undoes one event
@@ -303,11 +304,11 @@ func (eh *EventHandler) UndoOneEvent() {
303304
eh.RedoStack.Push(t)
304305
}
305306

306-
// Redo the first event in the redo stack
307-
func (eh *EventHandler) Redo() {
307+
// Redo the first event in the redo stack. Returns false if the stack is empty.
308+
func (eh *EventHandler) Redo() bool {
308309
t := eh.RedoStack.Peek()
309310
if t == nil {
310-
return
311+
return false
311312
}
312313

313314
startTime := t.Time.UnixNano() / int64(time.Millisecond)
@@ -316,15 +317,16 @@ func (eh *EventHandler) Redo() {
316317
for {
317318
t = eh.RedoStack.Peek()
318319
if t == nil {
319-
return
320+
break
320321
}
321322

322323
if t.Time.UnixNano()/int64(time.Millisecond) > endTime {
323-
return
324+
break
324325
}
325326

326327
eh.RedoOneEvent()
327328
}
329+
return true
328330
}
329331

330332
// RedoOneEvent redoes one event

runtime/help/keybindings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ ToggleDiffGutter
244244
ToggleRuler
245245
JumpLine
246246
ResetSearch
247+
ClearInfo
247248
ClearStatus
248249
ShellMode
249250
CommandMode

0 commit comments

Comments
 (0)