package editor import ( "math" "slices" "sort" "github.com/egoist/mygo/internal/rope" ) // diffPrepared is what the blocks and hidden lines were made of. type diffPrepared struct { version uint64 expandV int layout DiffLayout context int anns uint64 // the annotations' sides, lines and heights, hashed wrapsA uint64 wrapsB uint64 lh float32 wrap bool expAll bool inline DiffInline nOld int nNew int editable bool // which shows the line after a final newline (shownLines) } // diffGap is a run of unchanged lines between hunks, from the old line // a and the new line b, n lines; hidden says which of them collapse. type diffGap struct { a, b, n int hideA, hideB int // where the hidden lines start in the old and new text hidden int before bool // before the first hunk after bool // after the last } const diffSeparatorHeight = 32 // prepare makes the hidden lines and the blocks of each side, again only // when what they come of changed. func (d *DiffState) prepare() { d.model.update() d.markEnds() unified := d.Layout == DiffUnified edA, edB := &d.eds[0], &d.eds[1] viewA := &edA.view if unified { viewA = &d.oldView } viewB := &edB.view key := diffPrepared{version: d.model.version, expandV: d.expandV, layout: d.Layout, context: d.context(), anns: annotationsKey(d.Annotations), wrapsA: viewA.heights.wraps.version, wrapsB: viewB.heights.wraps.version, lh: viewB.lineHeight, wrap: d.Wrap, expAll: d.ExpandUnchanged, inline: d.Inline, nOld: viewA.text.LineCount(), nNew: viewB.text.LineCount(), editable: d.Editable} if d.prepared == key { return } // Measure the wraps of the lines of the hunks, which spacers align. if d.Wrap { for _, h := range d.model.hunks { for i := h.A0; i < h.A1; i++ { viewA.wrapLineNow(i) } for i := h.B0; i < h.B1; i++ { viewB.wrapLineNow(i) } } key.wrapsA, key.wrapsB = viewA.heights.wraps.version, viewB.heights.wraps.version } d.prepared = key nA, nB := d.shownLines(viewA.text), d.shownLines(viewB.text) gaps := d.gaps(nA, nB) d.gapList = gaps lh := viewB.lineHeight var hideA, hideB []lineSpan var blkA, blkB []block // Blocks below a line that go after its annotations, as the separator // of the lines after the last change. var lateA, lateB []block for gi, g := range gaps { if g.hidden <= 0 { continue } hideA = append(hideA, lineSpan{g.hideA, g.hideA + g.hidden - 1}) hideB = append(hideB, lineSpan{g.hideB, g.hideB + g.hidden - 1}) sep := func(n, from, total int) block { b := block{kind: blockSeparator, height: diffSeparatorHeight, from: from, to: from + g.hidden, hunk: gi, id: g.a} if from+g.hidden < total { b.line = from + g.hidden } else { b.line, b.below = max(0, from-1), true } return b } for _, side := range []struct { blks, late *[]block from, n int }{{&blkA, &lateA, g.hideA, nA}, {&blkB, &lateB, g.hideB, nB}} { b := sep(gi, side.from, side.n) if b.below { *side.late = append(*side.late, b) } else { *side.blks = append(*side.blks, b) } } } hideA = hideEnd(hideA, nA, viewA.text.LineCount()) hideB = hideEnd(hideB, nB, viewB.text.LineCount()) // A patch's hunks under their headers: above their first line, or, // for a hunk with no lines left on a side, below its last line, after // what the hunk before puts there (late). if pt := d.partial; pt != nil { header := func(blks, late *[]block, start, n, k int, h string) { b := block{kind: blockHeader, height: diffSeparatorHeight, line: start, hunk: k, id: h} if start >= n { b.line, b.below = max(0, n-1), true *late = append(*late, b) return } *blks = append(*blks, b) } for k, h := range pt.headers { header(&blkA, &lateA, pt.oldStarts[k], viewA.text.LineCount(), k, h) header(&blkB, &lateB, pt.newStarts[k], viewB.text.LineCount(), k, h) } } anns := [2]map[int]float32{{}, {}} for _, a := range d.Annotations { anns[a.Side][a.Line] += a.Height } rowsOf := func(v *view, line int) float32 { return float32(v.heights.wraps.rows(line)) * lh } if unified { // The tops of the deleted lines, which painting them starts at the // first in view with, rather than at the first of a run as long as // a file deleted. d.deletedTops = slices.Grow(d.deletedTops[:0], len(d.model.hunks))[:len(d.model.hunks)] for hi, h := range d.model.hunks { tops := d.deletedTops[hi][:0] if h.A1 > h.A0 { var height float32 var top float64 for i := h.A0; i < h.A1; i++ { tops = append(tops, top) dy := rowsOf(viewA, i) + anns[DiffOld][i] height += dy top += float64(dy) } tops = append(tops, top) b := block{kind: blockDeleted, height: height, from: h.A0, to: h.A1, hunk: hi} if h.B0 < viewB.text.LineCount() { b.line = h.B0 } else { b.line, b.below = viewB.text.LineCount()-1, true } blkB = append(blkB, b) } d.deletedTops[hi] = tops } // The old text's annotations on unchanged lines go under the same // lines of the new text, as only those show. for line, height := range anns[DiffOld] { if nl, ok := d.model.newLineOf(line); ok { anns[DiffNew][nl] += height } } for line, height := range anns[DiffNew] { blkB = append(blkB, block{kind: blockAnnotation, line: line, below: true, height: height, id: line}) } d.setSide(1, hideB, sticky(append(blkB, lateB...))) return } // Side by side: walk the rows of each hunk, the shorter side's lines // pushed down by the offset pairing them best, each row as high as // the higher side's line and annotations there, fillers where a side // has no line. type side struct { v *view blks *[]block ann map[int]float32 n int } sides := [2]side{{viewA, &blkA, anns[DiffOld], viewA.text.LineCount()}, {viewB, &blkB, anns[DiffNew], viewB.text.LineCount()}} emitAnn := func(s side, line int, extra float32) { if extra > 0 && line >= 0 { *s.blks = append(*s.blks, block{kind: blockSpacer, line: line, below: true, height: extra}) } } // Sides as wide as each other wrap an unchanged line alike; sides // the line between them made unlike (SplitPercent) wrap it in as many // rows as their widths take, and the side with fewer leaves the room // of the others under it, as under an annotation. Collapsed lines // take no room on either side. alignRows := d.Wrap && viewA.wrapWidth != viewB.wrapWidth hidden := func(line int) bool { i := sort.Search(len(hideA), func(i int) bool { return hideA[i].to >= line }) return i < len(hideA) && hideA[i].from <= line } prevA, prevB := 0, 0 unchanged := func(a0, b0, n int) { // Annotations on unchanged lines leave room on the other side, // those of collapsed lines none: they go with their lines. for i := 0; i < n; i++ { ha, hb := sides[0].ann[a0+i], sides[1].ann[b0+i] var ra, rb float32 if alignRows && !hidden(a0+i) { ra, rb = rowsOf(viewA, a0+i), rowsOf(viewB, b0+i) } if ha == 0 && hb == 0 && ra == rb || hidden(a0+i) { continue } hmax := max(ra+ha, rb+hb) if ha > 0 { *sides[0].blks = append(*sides[0].blks, block{kind: blockAnnotation, line: a0 + i, below: true, height: ha, id: a0 + i}) } if hb > 0 { *sides[1].blks = append(*sides[1].blks, block{kind: blockAnnotation, line: b0 + i, below: true, height: hb, id: b0 + i}) } emitAnn(sides[0], a0+i, hmax-ra-ha) emitAnn(sides[1], b0+i, hmax-rb-hb) } } for hi, h := range d.model.hunks { unchanged(prevA, prevB, h.A0-prevA) prevA, prevB = h.A1, h.B1 nA, nB := h.A1-h.A0, h.B1-h.B0 off := 0 if nA > 0 && nB > 0 && nA != nB { off = d.model.wordsOf(hi, d.Inline).offset } rows := max(nA, nB) lineOf := func(s int, r int) int { n0, n := h.A0, nA if s == 1 { n0, n = h.B0, nB } k := r if n < rows { k = r - off } if k < 0 || k >= n { return -1 } return n0 + k } var filler [2]float32 var lastLine [2]int lastLine[0], lastLine[1] = -1, -1 for r := 0; r < rows; r++ { var hs [2]float32 var ls [2]int for s := 0; s < 2; s++ { ls[s] = lineOf(s, r) if ls[s] >= 0 { hs[s] = rowsOf(sides[s].v, ls[s]) + sides[s].ann[ls[s]] } } hmax := max(hs[0], hs[1]) for s := 0; s < 2; s++ { sd := sides[s] if ls[s] < 0 { filler[s] += hmax continue } if filler[s] > 0 { *sd.blks = append(*sd.blks, block{kind: blockSpacer, line: ls[s], height: filler[s], from: 1, hunk: hi}) filler[s] = 0 } if a := sd.ann[ls[s]]; a > 0 { *sd.blks = append(*sd.blks, block{kind: blockAnnotation, line: ls[s], below: true, height: a, id: ls[s]}) } emitAnn(sd, ls[s], hmax-hs[s]) lastLine[s] = ls[s] } } for s := 0; s < 2; s++ { if filler[s] <= 0 { continue } sd := sides[s] b := block{kind: blockSpacer, height: filler[s], from: 1, hunk: hi} pos := h.A0 if s == 1 { pos = h.B0 } switch { case lastLine[s] >= 0: b.line, b.below = lastLine[s], true case pos < sd.n && !(s == 0 && nA == 0 && pos == sd.n-1 && sides[0].v.text.Len() == 0): b.line = pos default: b.line, b.below = max(0, sd.n-1), true } *sd.blks = append(*sd.blks, b) } } unchanged(prevA, prevB, min(sides[0].n-prevA, sides[1].n-prevB)) d.setSide(0, hideA, sticky(append(blkA, lateA...))) d.setSide(1, hideB, sticky(append(blkB, lateB...))) } // sticky keeps the blocks that stand for lines, as separators, deleted // lines and fillers, where their line is hidden, as with no context // lines; annotations go with the line they are under. func sticky(blks []block) []block { for i := range blks { if blks[i].kind != blockAnnotation { blks[i].sticky = true } } return blks } // setSide gives side i its hidden lines and blocks, keeping those it has // when they are the same, as typing within a line leaves them: the // heights take them without work, and the view, whose separators and // buttons stand on them, is not built again (blocksV). func (d *DiffState) setSide(i int, hide []lineSpan, blks []block) { s := &d.sides[i] if slices.Equal(s.hide, hide) && slices.Equal(s.blks, blks) { return } s.hide, s.blks = hide, blks d.blocksV++ } // wrapLineNow measures the wraps of a line, if not yet, as laying it out // would, with its faces, but without shaping it (wrapBreaks without fit, // which laying it out may find a row more than): a diff measures the lines // of all its changes at each width the window is resized through, most of // them out of view, and shaping them took most of the time. A line laid // out already keeps its rows. func (v *view) wrapLineNow(i int) { w := &v.heights.wraps if !w.on || i < 0 || i >= len(w.extra) || w.extra[i] != wrapUnknown { return } if l, ok := v.lines.m[i]; ok { w.set(i, len(l.rows)) return } l := &lineLayout{n: i, start: v.text.LineStart(i)} l.text = v.text.Slice(l.start, v.text.LineEnd(i)) l.toks, _ = v.tokens(i, l.text, nil) v.display(l) if v.styled || len(l.reps) > 0 { l.faces = v.facesOf(l, nil) } w.set(i, len(v.wrapBreaks(l.disp, l.faces, false))+1) } // shownLines returns how many lines of a side's text the diff shows. A // diff that cannot be edited ends at the text's final newline, as git // diff does, leaving out the empty line the editor starts after it, // which its lines, compared with their line feeds, do not count. An // editable one shows it, as there is where typing adds a line. func (d *DiffState) shownLines(text rope.Text) int { n := text.LineCount() if d.Editable || n < 2 || text.LineStart(n-1) != text.Len() { return n } return n - 1 } // markEnds marks the last line of each side whose text does not end // with a newline, after its text and muted, as git diff's "\ No newline // at end of file" does, where the diff is not editable. func (d *DiffState) markEnds() { d.eds[0].view.markEnd(d.noNewlineAtEnd(d.eds[0].view.text, true), d.endMark) d.oldView.markEnd(d.noNewlineAtEnd(d.oldView.text, true), d.endMark) d.eds[1].view.markEnd(d.noNewlineAtEnd(d.eds[1].view.text, false), d.endMark) } // noNewlineAtEnd reports whether the text of a side ends without a // newline, which a diff that is not editable marks: a patch's own texts // by its "\ No newline at end of file" lines, as they end without one // either way. An empty text has no line to mark. func (d *DiffState) noNewlineAtEnd(text rope.Text, old bool) bool { switch { case d.Editable || text.Len() == 0: return false case d.partial != nil && old: return d.partial.oldNoEOL case d.partial != nil: return d.partial.newNoEOL } return text.LineStart(text.LineCount()-1) != text.Len() } // markEnd shows mark at the end of the text, or takes it away. func (v *view) markEnd(on bool, mark string) { i := slices.IndexFunc(v.inlays, func(in inlay) bool { return in.kind == inlayNoNewline }) if i >= 0 && on && v.inlays[i].at == v.text.Len() && v.inlays[i].text == mark || i < 0 && !on { return } if i >= 0 { v.inlays = slices.Delete(v.inlays, i, i+1) } if on { v.inlays = append(v.inlays, inlay{at: v.text.Len(), text: mark, kind: inlayNoNewline}) sortInlays(v.inlays) } v.lines.m = nil } // hideEnd hides the lines of a side from shown to its last, total - 1, // joining the hidden lines that end right before them. func hideEnd(hide []lineSpan, shown, total int) []lineSpan { if shown >= total { return hide } if k := len(hide) - 1; k >= 0 && hide[k].to == shown-1 { hide[k].to = total - 1 return hide } return append(hide, lineSpan{shown, total - 1}) } // gaps returns the runs of unchanged lines and which of them collapse. func (d *DiffState) gaps(nOld, nNew int) []diffGap { hunks := d.model.hunks if len(hunks) == 0 || d.partial != nil { return nil } ctx := d.context() var out []diffGap pa, pb := 0, 0 for i := 0; i <= len(hunks); i++ { na, nb := nOld, nNew if i < len(hunks) { na, nb = hunks[i].A0, hunks[i].B0 } n := nb - pb if na-pa != n { n = min(n, na-pa) } g := diffGap{a: pa, b: pb, n: n, before: i == 0, after: i == len(hunks)} if !d.ExpandUnchanged && n > 0 { top, bottom := ctx, ctx if g.before { top = 0 } if g.after { bottom = 0 } // And the lines expanded at either edge. if top < n { if end := d.revealedAt(pa + top); end >= 0 { top = min(n, end+1-pa) } } if bottom < n { if first := d.revealedBefore(pa + n - 1 - bottom); first >= 0 { bottom = min(n, pa+n-first) } } hidden := n - top - bottom // One line alone shows rather than collapsing. if hidden > 1 { g.hidden = hidden g.hideA, g.hideB = pa+top, pb+top } } out = append(out, g) if i < len(hunks) { pa, pb = hunks[i].A1, hunks[i].B1 } } return out } func (d *DiffState) annotationHeight(side DiffSide, line int) float32 { var h float32 for _, a := range d.Annotations { if a.Side == side && a.Line == line { h += a.Height } } return h } // annotationsKey hashes what of the annotations places the blocks: their // sides, lines and heights (FNV-1a). func annotationsKey(anns []DiffAnnotation) uint64 { h := uint64(14695981039346656037) mix := func(v uint64) { for range 8 { h ^= v & 0xff h *= 1099511628211 v >>= 8 } } mix(uint64(len(anns))) for _, a := range anns { mix(uint64(a.Side)) mix(uint64(a.Line)) mix(uint64(math.Float32bits(a.Height))) } return h }