Skip to content

Commit fd7ea33

Browse files
committed
Addressing #157
1 parent a6b4c1c commit fd7ea33

2 files changed

Lines changed: 70 additions & 3 deletions

File tree

tui/v2/tree.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,24 @@ func (t *treeModel) rebuild() {
6969
return
7070
}
7171
t.entries = make([]treeEntry, 0, 64)
72+
rootChanges := getNodeChanges(t.root)
7273
children := t.root.Children
73-
for i, child := range children {
74-
isLast := i == len(children)-1
75-
t.flattenNode(child, 0, isLast, nil)
74+
totalItems := len(rootChanges) + len(children)
75+
idx := 0
76+
77+
for _, change := range rootChanges {
78+
idx++
79+
t.entries = append(t.entries, treeEntry{
80+
node: t.root,
81+
change: change,
82+
depth: 0,
83+
isLast: idx == totalItems,
84+
})
85+
}
86+
87+
for _, child := range children {
88+
idx++
89+
t.flattenNode(child, 0, idx == totalItems, nil)
7690
}
7791
// Clamp cursor
7892
if t.cursor >= len(t.entries) {

tui/v2/tree_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,36 @@ func makeTestTree() *v3.Node {
8989
return root
9090
}
9191

92+
func makeRootChangeTree() *v3.Node {
93+
openapiChange := &whatChangedModel.Change{
94+
ChangeType: whatChangedModel.Modified,
95+
Property: "openapi",
96+
Original: "3.0.0",
97+
New: "3.1.0",
98+
Breaking: true,
99+
}
100+
infoChange := &whatChangedModel.Change{
101+
ChangeType: whatChangedModel.Modified,
102+
Property: "version",
103+
Original: "1.0",
104+
New: "2.0",
105+
}
106+
107+
infoNode := &v3.Node{
108+
Label: "Info",
109+
Type: "Info",
110+
}
111+
infoNode.AppendChange(&mockChanged{changes: []*whatChangedModel.Change{infoChange}})
112+
113+
root := &v3.Node{
114+
Label: "Document",
115+
Type: "Document",
116+
Children: []*v3.Node{infoNode},
117+
}
118+
root.AppendChange(&mockChanged{changes: []*whatChangedModel.Change{openapiChange}})
119+
return root
120+
}
121+
92122
func TestFlattenNodeTree_AllExpanded(t *testing.T) {
93123
root := makeTestTree()
94124
tm := newTreeModel(root, 20)
@@ -130,6 +160,29 @@ func TestCursorStartsOnFirstLeaf(t *testing.T) {
130160
assert.Equal(t, "deprecated", entry.change.Property)
131161
}
132162

163+
func TestFlattenNodeTree_IncludesRootOwnedLeaves(t *testing.T) {
164+
root := makeRootChangeTree()
165+
tm := newTreeModel(root, 20)
166+
167+
require.Len(t, tm.entries, 3)
168+
require.NotNil(t, tm.entries[0].change)
169+
assert.Equal(t, "openapi", tm.entries[0].change.Property)
170+
assert.Equal(t, "Info", tm.entries[1].node.Label)
171+
require.NotNil(t, tm.entries[2].change)
172+
assert.Equal(t, "version", tm.entries[2].change.Property)
173+
}
174+
175+
func TestCursorStartsOnRootOwnedLeaf(t *testing.T) {
176+
root := makeRootChangeTree()
177+
tm := newTreeModel(root, 20)
178+
179+
require.Equal(t, 0, tm.cursor)
180+
entry := tm.selectedEntry()
181+
require.NotNil(t, entry)
182+
require.NotNil(t, entry.change)
183+
assert.Equal(t, "openapi", entry.change.Property)
184+
}
185+
133186
func TestMoveDown_SkipsBranchNodes(t *testing.T) {
134187
root := makeTestTree()
135188
tm := newTreeModel(root, 20)

0 commit comments

Comments
 (0)