Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/action/tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ type Tab struct {
*views.Node
*display.UIWindow

// id is this tab's unique id. It is taken from the root node when the
// tab is created and does not change afterwards, unlike the id of the
// embedded root node, which becomes 0 once the node has children.
id uint64

isActive bool

Panes []Pane
Expand All @@ -253,6 +258,7 @@ type Tab struct {
func NewTabFromBuffer(x, y, width, height int, b *buffer.Buffer) *Tab {
t := new(Tab)
t.Node = views.NewRoot(x, y, width, height)
t.id = t.Node.ID()
t.UIWindow = display.NewUIWindow(t.Node)
t.release = true

Expand All @@ -266,6 +272,7 @@ func NewTabFromBuffer(x, y, width, height int, b *buffer.Buffer) *Tab {
func NewTabFromPane(x, y, width, height int, pane Pane) *Tab {
t := new(Tab)
t.Node = views.NewRoot(x, y, width, height)
t.id = t.Node.ID()
t.UIWindow = display.NewUIWindow(t.Node)
t.release = true
pane.SetTab(t)
Expand All @@ -275,6 +282,12 @@ func NewTabFromPane(x, y, width, height int, pane Pane) *Tab {
return t
}

// ID returns this tab's unique id. Unlike the id of the tab's root node, it
// stays the same after the tab has been split.
func (t *Tab) ID() uint64 {
return t.id
}

// HandleEvent takes a tcell event and usually dispatches it to the current
// active pane. However if the event is a resize or a mouse event where the user
// is interacting with the UI (resizing splits) then the event is consumed here
Expand Down
40 changes: 40 additions & 0 deletions internal/action/tab_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package action

import (
"testing"

"github.com/micro-editor/micro/v2/internal/buffer"
"github.com/micro-editor/micro/v2/internal/config"
ulua "github.com/micro-editor/micro/v2/internal/lua"
lua "github.com/yuin/gopher-lua"
)

func init() {
ulua.L = lua.NewState()
config.InitRuntimeFiles(false)
config.InitGlobalSettings()
config.GlobalSettings["backup"] = false
config.GlobalSettings["fastdirty"] = true
}

func newTestTab() *Tab {
return NewTabFromBuffer(0, 0, 80, 24, buffer.NewBufferFromString("", "", buffer.BTDefault))
}

func TestTabIDWithSplits(t *testing.T) {
tab1 := newTestTab()
tab2 := newTestTab()

id := tab1.ID()
if id == 0 {
t.Fatal("tab id is 0")
}
if tab2.ID() == id {
t.Fatalf("tabs share id %d", id)
}

tab1.VSplit(true)
if got := tab1.ID(); got != id {
t.Errorf("tab id changed from %d to %d after split", id, got)
}
}