Skip to content

Commit 20f4124

Browse files
committed
fix: prefer highlighted path for live follow
1 parent 47a0ced commit 20f4124

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

internal/ui/model.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ func (m Model) applyFilesUpdate(msg FilesUpdatedMsg) Model {
209209
}
210210

211211
if m.FollowOn {
212-
if idx := fileIndexByPath(m.Files, m.lastChangedPath); idx >= 0 {
212+
if idx := fileIndexByPath(m.Files, m.lastHighlightedPath); idx >= 0 {
213+
m.setFollowTarget(idx, currentPath)
214+
} else if idx := fileIndexByPath(m.Files, m.lastChangedPath); idx >= 0 {
213215
m.setFollowTarget(idx, currentPath)
214216
} else {
215217
m.anchorCurrentPath(currentPath)

internal/ui/model_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,38 @@ func TestModelFollowUsesMostRecentChangedPath(t *testing.T) {
8282
}
8383
}
8484

85+
// TestModelFollowPrefersLatestHighlightedPath verifies active follow mode uses
86+
// the last file with a newly highlighted hunk over a later changed path that
87+
// produced no new diff hunk.
88+
func TestModelFollowPrefersLatestHighlightedPath(t *testing.T) {
89+
model := NewModel("repo", "/tmp/repo", "sha", []internal.FileDiff{
90+
fileWithHunks("a.txt", addHunk(10, "stable")),
91+
fileWithHunks("b.txt", addHunk(10, "old")),
92+
})
93+
model.Width = 80
94+
model.Height = 5
95+
96+
updated, _ := model.Update(FilesUpdatedMsg{
97+
BaselineSHA: "sha",
98+
Files: []internal.FileDiff{
99+
fileWithHunks("a.txt", addHunk(10, "stable")),
100+
fileWithHunks("b.txt",
101+
addHunk(10, "old"),
102+
addHunk(20, "latest"),
103+
),
104+
},
105+
ChangedPaths: []string{"b.txt", "a.txt"},
106+
})
107+
108+
got := updated.(Model)
109+
if got.CurrentIdx != 1 {
110+
t.Fatalf("CurrentIdx = %d, want 1 for highlighted path b.txt", got.CurrentIdx)
111+
}
112+
if got.ScrollOffset == 0 {
113+
t.Fatalf("ScrollOffset = %d, want non-zero for latest highlighted hunk", got.ScrollOffset)
114+
}
115+
}
116+
85117
// TestModelPausedFollowTracksLastChangedPath verifies paused follow keeps the
86118
// current file selected and records the latest changed path without +N state.
87119
func TestModelPausedFollowTracksLastChangedPath(t *testing.T) {

0 commit comments

Comments
 (0)