Skip to content

Commit 4ddbe99

Browse files
committed
refactor: extract isRootHidden() to reduce duplication
Consolidate duplicate root-hidden checks into a single reusable method. Also fixes inconsistency where one check used '.' and another used dirIcon+'.' - now all use the correct dirIcon+'.' check.
1 parent 8d08f31 commit 4ddbe99

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

pkg/ui/panes/filetree/filetree.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ type Model struct {
2525
selectedFile *string
2626
}
2727

28+
// isRootHidden returns true if the tree root is hidden (not displayed).
29+
func (m Model) isRootHidden() bool {
30+
return m.tree != nil && m.tree.Value() == dirIcon+"."
31+
}
32+
2833
func (m Model) SetFiles(files []*gitdiff.File) Model {
2934
m.files = files
3035
t := buildFullFileTree(files)
@@ -87,8 +92,7 @@ func (m *Model) scrollSelectedFileIntoView(t *tree.Tree) {
8792
if child.Path() == *m.selectedFile {
8893
// offset is 1-based, so we need to subtract 1
8994
offset := child.YOffset - 1 - contextLines
90-
// we also need to subtract 1 if the root is not shown
91-
if m.tree.Value() == "." {
95+
if m.isRootHidden() {
9296
offset = offset - 1
9397
}
9498
m.vp.SetYOffset(offset)
@@ -153,12 +157,11 @@ func (m Model) GetFileAtY(y int) string {
153157
if m.tree == nil {
154158
return ""
155159
}
156-
// Convert visual line (0-indexed) to YOffset (1-indexed from tree traversal)
160+
// Convert visual line (0-indexed) to YOffset (1-indexed from tree traversal).
157161
// YOffset starts at 1 for root, 2 for first child, etc.
158-
// If root is hidden (Value == dirIcon+"."), first visible is at YOffset 2
159-
yOffset := y + 1 // Convert from 0-indexed to 1-indexed
160-
if m.tree.Value() == dirIcon+"." {
161-
yOffset++ // Root is hidden, so visual line 0 = YOffset 2
162+
yOffset := y + 1
163+
if m.isRootHidden() {
164+
yOffset++ // Root is hidden, so visual line 0 = YOffset 2.
162165
}
163166
return m.findFileAtY(m.tree, yOffset)
164167
}
@@ -193,7 +196,7 @@ func (m *Model) ScrollDown(lines int) {
193196
}
194197

195198
func (m Model) printWithoutRoot() string {
196-
if m.tree.Value() != dirIcon+"." {
199+
if !m.isRootHidden() {
197200
return m.tree.String()
198201
}
199202

0 commit comments

Comments
 (0)