Improve the git diff preview from the file viewer
CI / check (push) Waiting to run

This commit is contained in:
2026-09-21 06:47:46 +02:00
parent a2d1c5df83
commit dbb7e40c6c
10 changed files with 98 additions and 221 deletions
+9 -14
View File
@@ -2,44 +2,39 @@ import { describe, it, expect } from 'vitest'
import { buildDiffView } from '../src/renderer/src/editor'
import { makeDiff } from '../src/renderer/src/diff'
/** The current-side view of one text pair, plus the map the overlay reads. */
/** The current-side view of one text pair: the lines Actual marks. */
function view(original: string, updated: string): ReturnType<typeof buildDiffView> {
return buildDiffView(makeDiff('M', original, updated))
}
describe('buildDiffView', () => {
it('maps an added line to the line it replaced', () => {
const { lines, peek } = view('a\nold\nb', 'a\nnew\nb')
it('marks a rewritten line in place', () => {
const lines = view('a\nold\nb', 'a\nnew\nb')
expect(lines.map((l) => l.text)).toEqual(['a', 'new', 'b'])
expect(lines[1].row).toBe('add')
expect(peek.get(2)).toEqual({ anchor: 2, from: 2, to: 2 })
expect(lines.every((l) => !l.gap)).toBe(true)
})
it('puts a pure deletion on the line that follows it', () => {
const { lines, peek } = view('a\ngone\nb', 'a\nb')
const lines = view('a\ngone\nb', 'a\nb')
expect(lines[1].text).toBe('b')
expect(lines[1].gap).toBe('above')
expect(lines[1].row).toBeNull()
expect(peek.get(2)).toEqual({ anchor: 2, from: 2, to: 2 })
})
it('puts a deletion at end of file under the last line', () => {
const { lines, peek } = view('a\nb\ntail', 'a\nb')
const lines = view('a\nb\ntail', 'a\nb')
expect(lines[1].gap).toBe('below')
expect(peek.get(2)).toEqual({ anchor: 3, from: 3, to: 3 })
})
it('anchors an insertion but marks nothing as removed', () => {
const { lines, peek } = view('a\nb', 'a\nNEW\nb')
it('marks an insertion and leaves the neighbours alone', () => {
const lines = view('a\nb', 'a\nNEW\nb')
expect(lines[1].row).toBe('add')
expect(peek.get(2)).toEqual({ anchor: 1, from: null, to: null })
expect(lines.every((l) => !l.gap)).toBe(true)
})
it('leaves an unchanged file without marks or peek targets', () => {
const { lines, peek } = view('a\nb\n', 'a\nb\n')
expect(peek.size).toBe(0)
it('leaves an unchanged file without marks', () => {
const lines = view('a\nb\n', 'a\nb\n')
expect(lines.every((l) => !l.row && !l.gap)).toBe(true)
})
})