Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
57dfac8
feat: parse delta's OSC 1717 line metadata
pablospe Nov 14, 2026
b6f55b3
fix: keep the first row that carries the OSC 1717 handshake
pablospe Nov 14, 2026
45d9dac
feat: keep the scroll position when toggling side-by-side or resizing
pablospe Nov 14, 2026
17254ce
fix: take the scroll anchor before the viewport is resized
pablospe Nov 14, 2026
3c5533e
fix: keep rows without a record in place when restoring the scroll
pablospe Nov 14, 2026
f25396b
refactor: drop the nearest-line fallback and skip rows without records
pablospe Nov 14, 2026
fe2ec61
test: run the real delta without the user's git config
pablospe Nov 14, 2026
0f2c9b6
fix: keep a wrapped-row offset within the anchor line's rows
pablospe Nov 14, 2026
71eb9b4
style: wrap a long test line for golines
pablospe Nov 14, 2026
f0e7568
fix: don't restore the scroll position after the user moved
pablospe Nov 15, 2026
654d4f5
fix: keep the scroll anchor when a scroll call doesn't move
pablospe Nov 15, 2026
67221b4
fix: follow a move made while the re-render is pending
pablospe Nov 15, 2026
6d05cce
docs: say how rows without a record and wrapped rows are anchored
pablospe Nov 15, 2026
18286d9
fix: unquote octal-escaped file names in OSC 1717 records
pablospe Nov 15, 2026
a98623c
fix: don't read records of the previous node while a new one renders
pablospe Nov 25, 2026
f552006
fix: unquote file names that keep git's quotes in OSC 1717 records
pablospe Nov 25, 2026
51b689b
refactor: compute the restored row in a pure anchorRow
pablospe Nov 26, 2026
ed42303
refactor: track view moves in one place
pablospe Nov 26, 2026
e674a5e
refactor: cut OSC 1717 sequences with strings.Cut and unquote names i…
pablospe Nov 26, 2026
74166ba
test: share the line and model fixtures of the scroll tests
pablospe Nov 26, 2026
e2250d9
docs: leave the offset rules to anchorRow's comments
pablospe Nov 26, 2026
771a561
refactor: reanchor after moves without a closure, and jump to edges w…
pablospe Nov 26, 2026
1cf712e
fix: keep g/G pressed at the edge while a re-render is pending
pablospe Nov 26, 2026
1921186
fix: don't overwrite a pending anchor when another re-render starts
pablospe Nov 26, 2026
b95383b
refactor: match records with slices helpers and check the file in sam…
pablospe Nov 26, 2026
45ab706
test: test anchorRow, and sharpen and share the scroll test fixtures
pablospe Nov 26, 2026
e1bda73
test: keep the line scrolled to with the wheel during a pending render
pablospe Nov 26, 2026
0778ecf
test: share the delivery of the pending render in a helper
pablospe Nov 26, 2026
d1f2aba
test: keep G when the wheel doesn't move at the bottom
pablospe Nov 26, 2026
3ca4c08
chore: slight renaming and docs
dlvhdr Nov 30, 2026
e3adc17
Merge branch 'main' into osc1717
dlvhdr Dec 4, 2026
57e99f1
refactor: put things in consts
dlvhdr Dec 4, 2026
ecc637c
chore: lint
dlvhdr Dec 4, 2026
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
8 changes: 6 additions & 2 deletions pkg/ui/panes/diffviewer/delta_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ func TestMakeDeltaArgs(t *testing.T) {
func TestMakeDeltaEnv(t *testing.T) {
t.Setenv("GIT_CONFIG_PARAMETERS", "'delta.navigate'='true'")

if got := makeDeltaEnv(deltaOpts{SideBySide: true}); !slices.Equal(got, os.Environ()) {
t.Fatalf("themed: expected the plain environment, got %q", got)
wantEnv := append(os.Environ(), "OSC1717=V1")
if got := makeDeltaEnv(deltaOpts{SideBySide: true}); !slices.Equal(got, wantEnv) {
t.Fatalf("themed: expected %q, got %q", wantEnv, got)
}

env := makeDeltaEnv(deltaOpts{UseUserDeltaTheme: true})
if !slices.Contains(env, "OSC1717=V1") {
t.Errorf("user delta theme: expected OSC1717=V1 in %q", env)
}
got := env[len(env)-1]
want := "GIT_CONFIG_PARAMETERS='delta.navigate'='true' 'delta.side-by-side'='false'"
if got != want {
Expand Down
171 changes: 158 additions & 13 deletions pkg/ui/panes/diffviewer/diffviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func cacheKey(path string, sideBySide bool) string {
}

type diffLine struct {
item item.Item
item item.Item
records []oscRecord
}

func (o diffLine) GetItem() item.Item {
Expand Down Expand Up @@ -95,8 +96,31 @@ type Model struct {
useUserDeltaTheme bool
preamble string
sb common.Scrollbar
// shown are the lines on screen: the last render while a new one is pending.
shown []diffLine
// anchor is where to scroll back to once the re-rendered diff is shown.
anchor *scrollAnchor
}

// scrollAnchor is where to scroll back to after a re-render
type scrollAnchor struct {
// whether the user explicitly navigated to the top or bottom
edge anchorEdge

// otherwise - the position the view was in: the line's osc record and the view's offset from its first row.
oscRec oscRecord
offset int
}

// anchorEdge is the edge of the diff an anchor sticks to, if any.
type anchorEdge int

const (
edgeNone anchorEdge = iota
edgeTop
edgeBottom
)

// SetPreamble stores the preamble text (e.g. commit metadata from git show).
func (m *Model) SetPreamble(preamble string) {
m.preamble = preamble
Expand Down Expand Up @@ -228,13 +252,23 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
}
node.diff = msg.lines
if node == m.file || node == m.dir {
m.fvp.SetObjects(msg.lines)
m.setLines(msg.lines)
}
}

edge := edgeNone
if k, ok := msg.(tea.KeyPressMsg); ok && !m.fvp.FilterFocused() {
if key.Matches(k, ViewportKeyMap.Top) {
edge = edgeTop
} else if key.Matches(k, ViewportKeyMap.Bottom) {
edge = edgeBottom
}
}
row := m.currentRow()
fvp, fvpCmd := m.fvp.Update(msg)
cmds = append(cmds, fvpCmd)
m.fvp = fvp
m.reanchor(row, edge)

return m, tea.Batch(cmds...)
}
Expand All @@ -257,6 +291,8 @@ func (m Model) View() string {
}

func (m *Model) SetSize(width, height int) tea.Cmd {
// Before resizing: the viewport clamps its top row to the new size.
m.saveAnchor()
m.Width = width
m.Height = height
m.fvp.SetWidth(m.contentWidth())
Expand All @@ -266,10 +302,110 @@ func (m *Model) SetSize(width, height int) tea.Cmd {
}

func (m *Model) UpdateTheme() tea.Cmd {
m.saveAnchor()
m.ClearCache()
return m.diff()
}

// saveAnchor remembers the diff osc record at the top of the view (or the selected one) and
// the view's offset from its first row, so the next render scrolls back to it (anchorRow).
func (m *Model) saveAnchor() {
// there's already a valid anchor
if m.anchor != nil {
return
}
if rec, ok := m.currentOSCRecord(); ok {
m.anchor = &scrollAnchor{oscRec: rec, offset: m.currentRow() - findOSCRecord(m.shown, rec)}
}
}

// currentOSCRecord returns the first record at or below the current row of the lines on
// screen, so it is correct even while a new render is pending.
func (m *Model) currentOSCRecord() (oscRecord, bool) {
row := m.currentRow()
if row < 0 || m.fvp.GetMatchingItemsOnly() {
return oscRecord{}, false
}
return firstOSCRecord(m.shown, row)
}

// currentRow is the selected row, or the top visible one without a selection.
func (m *Model) currentRow() int {
if m.SelectionEnabled() {
return m.fvp.GetSelectedItemIdx()
}
return m.fvp.GetItemMetrics().FirstVisibleItemIdx
}

// reanchor keeps a pending render from undoing a move the user made from row: a jump to
// the top or bottom (edge) stays there, other moves keep the line they land on.
func (m *Model) reanchor(row int, edge anchorEdge) {
if m.anchor == nil {
return
}
if edge != edgeNone {
m.anchor = &scrollAnchor{edge: edge}
return
}
if m.currentRow() == row {
return
}
m.anchor = nil
m.saveAnchor()
}

// setLines shows lines and scrolls to the pending anchor, if any.
func (m *Model) setLines(lines []diffLine) {
m.shown = lines
m.fvp.SetObjects(lines)
if m.anchor == nil {
return
}
a := *m.anchor
m.anchor = nil
switch a.edge {
case edgeTop:
m.fvp.GoToTop()
return
case edgeBottom:
m.fvp.GoToBottom()
return
}
if m.fvp.GetMatchingItemsOnly() {
return
}
row, ok := anchorRow(lines, a.oscRec, a.offset)
if !ok {
return
}
if m.SelectionEnabled() {
m.fvp.SetSelectedItemIdx(row)
return
}
m.fvp.GoToTop()
m.fvp.ScrollDown(row)
}

// anchorRow returns the row offset rows from rec's first row, or false when rec is gone.
// Rows above it (offset < 0) have no record, like blank or decoration rows. Rows below it
// are its wrapped rows, so it stops at the line's last row.
func anchorRow(lines []diffLine, rec oscRecord, offset int) (int, bool) {
row := findOSCRecord(lines, rec)
if row < 0 {
return 0, false
}
if offset < 0 {
return max(row+offset, 0), true
}
for range offset {
if row+1 == len(lines) || !lines[row+1].showsOSCRecord(rec) {
break
}
row++
}
return row, true
}

func (m Model) contentWidth() int {
return m.Width - scrollbarWidth
}
Expand All @@ -281,7 +417,7 @@ func (m *Model) diff() tea.Cmd {
key := cacheKey(m.file.path, sideBySide)
if cached, ok := m.cache[key]; ok && len(cached.diff) != 0 {
m.file = cached
m.fvp.SetObjects(cached.diff)
m.setLines(cached.diff)
return nil
}
node := &cachedNode{
Expand All @@ -297,7 +433,7 @@ func (m *Model) diff() tea.Cmd {
key := cacheKey(m.dir.path, m.sideBySide)
if cached, ok := m.cache[key]; ok && len(cached.diff) != 0 {
m.dir = cached
m.fvp.SetObjects(cached.diff)
m.setLines(cached.diff)
return nil
}
node := &cachedNode{
Expand Down Expand Up @@ -363,14 +499,16 @@ func (m Model) dirHeaderView() string {

func (m Model) SetFilePatch(file *gitdiff.File) (Model, tea.Cmd) {
m.dir = nil
// The rows on screen are another node's until this one is shown.
m.anchor, m.shown = nil, nil

fname := filenode.GetFileName(file)
sideBySide := m.sideBySide && !file.IsNew && !file.IsDelete
key := cacheKey(fname, sideBySide)
if cached, ok := m.cache[key]; ok {
m.file = cached
m.updateHeader()
m.fvp.SetObjects(cached.diff)
m.setLines(cached.diff)
return m, nil
}

Expand All @@ -391,12 +529,13 @@ func (m Model) SetFilePatch(file *gitdiff.File) (Model, tea.Cmd) {

func (m Model) SetDirPatch(dirPath string, files []*gitdiff.File) (Model, tea.Cmd) {
m.file = nil
m.anchor, m.shown = nil, nil

key := cacheKey(dirPath, m.sideBySide)
if cached, ok := m.cache[key]; ok {
m.dir = cached
m.updateHeader()
m.fvp.SetObjects(cached.diff)
m.setLines(cached.diff)
return m, nil
}

Expand Down Expand Up @@ -428,17 +567,22 @@ func (m *Model) updateHeader() {
// SetSideBySide updates the diff view mode and re-renders.
func (m *Model) SetSideBySide(sideBySide bool) tea.Cmd {
m.sideBySide = sideBySide
m.saveAnchor()
return m.diff()
}

// ScrollUp scrolls the viewport up by the given number of lines.
func (m *Model) ScrollUp(lines int) {
row := m.currentRow()
m.fvp.ScrollUp(lines)
m.reanchor(row, edgeNone)
}

// ScrollDown scrolls the viewport down by the given number of lines.
func (m *Model) ScrollDown(lines int) {
row := m.currentRow()
m.fvp.ScrollDown(lines)
m.reanchor(row, edgeNone)
}

// ScrollBottom scrolls the viewport to the bottom.
Expand Down Expand Up @@ -488,7 +632,7 @@ func (m *Model) diffFile(node *cachedNode, width int) tea.Cmd {
return common.ErrMsg{Err: err}
}

return diffContentMsg{cacheKey: key, node: node, lines: stringToDiffLines(string(out))}
return diffContentMsg{cacheKey: key, node: node, lines: parseDeltaOutput(string(out))}
}
}

Expand Down Expand Up @@ -522,11 +666,11 @@ func (m *Model) diffDir(dir *cachedNode, width int, sideBySide bool, preamble st
return common.ErrMsg{Err: err}
}

text := string(out)
lines := parseDeltaOutput(string(out))
if preamble != "" {
text = m.renderPreamble(preamble) + "\n" + text
lines = append(stringToDiffLines(m.renderPreamble(preamble)), lines...)
}
return diffContentMsg{cacheKey: key, node: dir, lines: stringToDiffLines(text)}
return diffContentMsg{cacheKey: key, node: dir, lines: lines}
}
}

Expand Down Expand Up @@ -623,10 +767,11 @@ type deltaOpts struct {
UseUserDeltaTheme bool
}

// makeDeltaEnv sets side-by-side like `git -c delta.side-by-side=...` would.
// The --side-by-side flag would override the user's line-number format and can't turn it off.
// makeDeltaEnv asks for OSC 1717 line metadata and, with the user's theme, sets side-by-side
// like `git -c delta.side-by-side=...` would. The --side-by-side flag would override the
// user's line-number format and can't turn it off.
func makeDeltaEnv(opts deltaOpts) []string {
env := os.Environ()
env := append(os.Environ(), "OSC1717=V1")
if !opts.UseUserDeltaTheme {
return env
}
Expand Down
Loading
Loading