feat(cabana): mail layouts and partials screens with reset, drift and custom layouts
- Layouts and partials controllers with catalog scoping, reset action and File version diff - Custom layout create/delete with in-use refusal; meta.deletable and customized/outdated row states - List schema golden fixtures updated for the two new row-state message keys Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
124
modules/cabana/mail_diff.go
Normal file
124
modules/cabana/mail_diff.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package cabana
|
||||
|
||||
// diffKind is what one line of a line diff is.
|
||||
type diffKind int
|
||||
|
||||
const (
|
||||
// diffCtx is a line both sides have.
|
||||
diffCtx diffKind = iota
|
||||
// diffAdd is a line only the new side has.
|
||||
diffAdd
|
||||
// diffDel is a line only the old side has.
|
||||
diffDel
|
||||
// diffGap stands for unchanged lines left out between two hunks.
|
||||
diffGap
|
||||
)
|
||||
|
||||
// diffLine is one line of a line diff.
|
||||
type diffLine struct {
|
||||
Kind diffKind
|
||||
Text string
|
||||
}
|
||||
|
||||
// diffMaxLines caps each side of a line diff: above it no diff is computed
|
||||
// (the LCS table grows with the product of the sides), and the File version
|
||||
// tab shows the file content alone.
|
||||
const diffMaxLines = 2000
|
||||
|
||||
// lineDiff is the unified line diff from a (old) to b (new) with context
|
||||
// unchanged lines around each change, computed with a longest common
|
||||
// subsequence over the lines after trimming the common prefix and suffix.
|
||||
// Unchanged runs longer than twice the context are cut to a diffGap line. It
|
||||
// returns nil when the sides are equal and when either side has more than
|
||||
// diffMaxLines lines.
|
||||
func lineDiff(a, b []string, context int) []diffLine {
|
||||
if len(a) > diffMaxLines || len(b) > diffMaxLines {
|
||||
return nil
|
||||
}
|
||||
if context < 0 {
|
||||
context = 0
|
||||
}
|
||||
prefix := 0
|
||||
for prefix < len(a) && prefix < len(b) && a[prefix] == b[prefix] {
|
||||
prefix++
|
||||
}
|
||||
suffix := 0
|
||||
for suffix < len(a)-prefix && suffix < len(b)-prefix && a[len(a)-1-suffix] == b[len(b)-1-suffix] {
|
||||
suffix++
|
||||
}
|
||||
if prefix == len(a) && prefix == len(b) {
|
||||
return nil
|
||||
}
|
||||
ma, mb := a[prefix:len(a)-suffix], b[prefix:len(b)-suffix]
|
||||
n, m := len(ma), len(mb)
|
||||
// lcs[i*(m+1)+j] is the LCS length of ma[i:] and mb[j:]; both sides are
|
||||
// capped at diffMaxLines, so the value fits in 16 bits.
|
||||
lcs := make([]uint16, (n+1)*(m+1))
|
||||
at := func(i, j int) uint16 { return lcs[i*(m+1)+j] }
|
||||
for i := n - 1; i >= 0; i-- {
|
||||
for j := m - 1; j >= 0; j-- {
|
||||
switch {
|
||||
case ma[i] == mb[j]:
|
||||
lcs[i*(m+1)+j] = at(i+1, j+1) + 1
|
||||
case at(i+1, j) >= at(i, j+1):
|
||||
lcs[i*(m+1)+j] = at(i+1, j)
|
||||
default:
|
||||
lcs[i*(m+1)+j] = at(i, j+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
full := make([]diffLine, 0, len(a)+len(b))
|
||||
for _, line := range a[:prefix] {
|
||||
full = append(full, diffLine{Kind: diffCtx, Text: line})
|
||||
}
|
||||
i, j := 0, 0
|
||||
for i < n || j < m {
|
||||
switch {
|
||||
case i < n && j < m && ma[i] == mb[j]:
|
||||
full = append(full, diffLine{Kind: diffCtx, Text: ma[i]})
|
||||
i++
|
||||
j++
|
||||
case j < m && (i == n || at(i, j+1) > at(i+1, j)):
|
||||
full = append(full, diffLine{Kind: diffAdd, Text: mb[j]})
|
||||
j++
|
||||
default:
|
||||
full = append(full, diffLine{Kind: diffDel, Text: ma[i]})
|
||||
i++
|
||||
}
|
||||
}
|
||||
for _, line := range a[len(a)-suffix:] {
|
||||
full = append(full, diffLine{Kind: diffCtx, Text: line})
|
||||
}
|
||||
return hunks(full, context)
|
||||
}
|
||||
|
||||
// hunks keeps the changed lines of a full diff and up to context unchanged
|
||||
// lines on each side of them; every other run of unchanged lines becomes
|
||||
// one diffGap line, except at the start and the end, where it is dropped.
|
||||
func hunks(full []diffLine, context int) []diffLine {
|
||||
keep := make([]bool, len(full))
|
||||
for idx, line := range full {
|
||||
if line.Kind == diffCtx {
|
||||
continue
|
||||
}
|
||||
for k := idx - context; k <= idx+context; k++ {
|
||||
if k >= 0 && k < len(full) {
|
||||
keep[k] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
out := make([]diffLine, 0, len(full))
|
||||
gap := false
|
||||
for idx, line := range full {
|
||||
if keep[idx] {
|
||||
if gap && len(out) > 0 {
|
||||
out = append(out, diffLine{Kind: diffGap})
|
||||
}
|
||||
gap = false
|
||||
out = append(out, line)
|
||||
continue
|
||||
}
|
||||
gap = true
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user