From 5f83661fee40bcc88a5b46a5346c6ac29fc0508a Mon Sep 17 00:00:00 2001 From: Massimo Mund Date: Sat, 17 Aug 2024 13:57:07 +0200 Subject: [PATCH] Fixes a bug where new `BufPanes` are not being inserted into the right array index. When adding a new `BufPane` it is always being inserted last into `MainTab().Panes`. This leads to a confusion when using the actions `PreviousSplit`, `NextSplit` as the previous/next split may not be the expected one. How to reproduce: - Launch micro and insert char "1" - Open a new vsplit via the command `vsplit` and insert "2" - Switch back to the left split (1) by using `PreviousSplit` - Again open a new vsplit via command: `vsplit` and type char "3" - Now switch between the 3 splits using `PreviousSplit`, `NextSplit` Switching from most left split to the most right, the expected order would be 1, 3, 2 but actually is 1, 2, 3. --- internal/action/bufpane.go | 16 ++++++++++++---- internal/action/tab.go | 10 ++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/internal/action/bufpane.go b/internal/action/bufpane.go index 054f3933..b6dbdd97 100644 --- a/internal/action/bufpane.go +++ b/internal/action/bufpane.go @@ -663,9 +663,13 @@ func (h *BufPane) DoRuneInsert(r rune) { func (h *BufPane) VSplitIndex(buf *buffer.Buffer, right bool) *BufPane { e := NewBufPaneFromBuf(buf, h.tab) e.splitID = MainTab().GetNode(h.splitID).VSplit(right) - MainTab().Panes = append(MainTab().Panes, e) + currentPaneIdx := MainTab().GetPane(h.splitID) + if right { + currentPaneIdx++ + } + MainTab().AddPane(e, currentPaneIdx) MainTab().Resize() - MainTab().SetActive(len(MainTab().Panes) - 1) + MainTab().SetActive(currentPaneIdx) return e } @@ -673,9 +677,13 @@ func (h *BufPane) VSplitIndex(buf *buffer.Buffer, right bool) *BufPane { func (h *BufPane) HSplitIndex(buf *buffer.Buffer, bottom bool) *BufPane { e := NewBufPaneFromBuf(buf, h.tab) e.splitID = MainTab().GetNode(h.splitID).HSplit(bottom) - MainTab().Panes = append(MainTab().Panes, e) + currentPaneIdx := MainTab().GetPane(h.splitID) + if bottom { + currentPaneIdx++ + } + MainTab().AddPane(e, currentPaneIdx) MainTab().Resize() - MainTab().SetActive(len(MainTab().Panes) - 1) + MainTab().SetActive(currentPaneIdx) return e } diff --git a/internal/action/tab.go b/internal/action/tab.go index bde667a3..235cb361 100644 --- a/internal/action/tab.go +++ b/internal/action/tab.go @@ -349,6 +349,16 @@ func (t *Tab) SetActive(i int) { } } +// AddPane adds a pane at a given index +func (t *Tab) AddPane(pane Pane, i int) { + if len(t.Panes) == i { + t.Panes = append(t.Panes, pane) + return + } + t.Panes = append(t.Panes[:i+1], t.Panes[i:]...) + t.Panes[i] = pane +} + // GetPane returns the pane with the given split index func (t *Tab) GetPane(splitid uint64) int { for i, p := range t.Panes {