restores Helder in all its glory

This commit is contained in:
2026-09-28 11:34:49 +02:00
parent c2b38b853c
commit 1d29d0952d
280 changed files with 29205 additions and 7 deletions
+122
View File
@@ -0,0 +1,122 @@
import Foundation
/// One line of a unified diff: unchanged, removed or added.
public enum DiffSign: String, Sendable, Equatable {
case same = " "
case removed = "-"
case added = "+"
}
public struct DiffRow: Sendable, Equatable {
public var sign: DiffSign
/// 1-based line number in the original; nil for an added line.
public var oldNo: Int?
/// 1-based line number in the updated text; nil for a removed line.
public var newNo: Int?
public var text: String
public init(sign: DiffSign, oldNo: Int?, newNo: Int?, text: String) {
self.sign = sign
self.oldNo = oldNo
self.newNo = newNo
self.text = text
}
}
public enum LineMark: String, Sendable, Equatable {
case added
case removed
}
/// One line of one side of the diff, with its 1-based number in that side.
public struct SideLine: Sendable, Equatable {
public var no: Int
public var text: String
public var mark: LineMark?
public init(no: Int, text: String, mark: LineMark? = nil) {
self.no = no
self.text = text
self.mark = mark
}
}
/// A row of the side-by-side view; a nil side is the filler opposite an unpaired add or remove.
public struct SplitRow: Sendable, Equatable {
public var left: SideLine?
public var right: SideLine?
public init(left: SideLine?, right: SideLine?) {
self.left = left
self.right = right
}
}
/// Everything the Actual view and the Diff need, derived from one (original, updated) pair.
public struct Diff: Sendable, Equatable {
public var rows: [DiffRow]
public var left: [SideLine]
public var right: [SideLine]
public var split: [SplitRow]
public var added: Int
public var removed: Int
public var isDeletedFile: Bool
public var isAddedFile: Bool
public var original: String
public var updated: String
/// Built from a change over the diff cap: the texts are empty, not the file.
public var isTooBig = false
public init(
rows: [DiffRow], left: [SideLine], right: [SideLine], split: [SplitRow],
added: Int, removed: Int, isDeletedFile: Bool, isAddedFile: Bool,
original: String, updated: String
) {
self.rows = rows
self.left = left
self.right = right
self.split = split
self.added = added
self.removed = removed
self.isDeletedFile = isDeletedFile
self.isAddedFile = isAddedFile
self.original = original
self.updated = updated
}
}
/// Which half of a file's changes a Diff shows: HEAD vs index, or index vs disk.
/// Absent (nil) means the whole file, HEAD vs disk, which Actual always shows.
public enum DiffSide: String, Codable, Sendable, Equatable {
case staged
case unstaged
}
/// How a line is painted in the read-only editor panes.
public enum ViewLineStyle: String, Sendable, Equatable {
/// Full tint: a line the change added (Actual view).
case added
}
/// Where a removed run's rule sits, since removed lines own no line of their own in Actual.
public enum GapEdge: String, Sendable, Equatable {
case above
case below
}
/// A line descriptor for the editor panes.
public struct ViewLine: Sendable, Equatable {
public var no: Int?
public var text: String
public var sign: DiffSign?
public var style: ViewLineStyle?
public var gap: GapEdge?
public init(no: Int?, text: String, sign: DiffSign? = nil, style: ViewLineStyle? = nil, gap: GapEdge? = nil) {
self.no = no
self.text = text
self.sign = sign
self.style = style
self.gap = gap
}
}
+40
View File
@@ -0,0 +1,40 @@
import Foundation
public enum DiffViews {
/// The Actual view: the updated file with added lines tinted.
///
/// A run of removed lines with no added line beside it puts its rule on the edge of
/// the next surviving line (`gap: .above`), or under the last line when the run ends
/// the file (`gap: .below`). A run that also added lines gets no gap rule.
public static func actual(_ diff: Diff) -> [ViewLine] {
var above = Set<Int>()
var endsWithRemoval = false
var removed = 0, added = 0
func flush(nextNew: Int?) {
if removed > 0 && added == 0 {
if let nextNew { above.insert(nextNew) } else { endsWithRemoval = true }
}
removed = 0
added = 0
}
for row in diff.rows {
switch row.sign {
case .removed: removed += 1
case .added: added += 1
case .same: flush(nextNew: row.newNo)
}
}
flush(nextNew: nil)
let last = diff.right.last?.no
return diff.right.map { line in
let gap: GapEdge? = above.contains(line.no) ? .above : (endsWithRemoval && line.no == last ? .below : nil)
return ViewLine(no: line.no, text: line.text, style: line.mark == .added ? .added : nil, gap: gap)
}
}
/// A plain file with no diff, numbered from 1.
public static func plain(_ text: String) -> [ViewLine] {
LineDiff.droppingTrailingNewline(text).components(separatedBy: "\n").enumerated().map { ViewLine(no: $0.offset + 1, text: $0.element) }
}
}
+205
View File
@@ -0,0 +1,205 @@
import Foundation
public enum LineDiff {
/// Largest LCS matrix built in one go (4M cells); bigger regions are split on unique anchor lines.
public static let maxCells = 4_000_000
/// "" is zero lines, and one trailing "\n" is dropped.
public static func lines(_ text: String) -> [String] {
if text.isEmpty { return [] }
return droppingTrailingNewline(text).components(separatedBy: "\n")
}
/// Drops one final "\n". `hasSuffix("\n")` misses it after a "\r", where "\r\n" is one Character.
static func droppingTrailingNewline(_ text: String) -> String {
text.utf8.last == 10 ? String(decoding: text.utf8.dropLast(), as: UTF8.self) : text
}
/// Line-based LCS diff of two texts into rows, both sides and aligned split rows.
///
/// On a tie the DP takes the deletion before the addition; flipping that reorders
/// removed and added lines in every view.
public static func build(original: String, updated: String, status: GitStatus? = nil) -> Diff {
let a = lines(original)
let b = lines(updated)
let (ia, ib) = intern(a, b)
var ops: [Op] = []
ops.reserveCapacity(max(a.count, b.count))
region(ia, ib, 0, ia.count, 0, ib.count, &ops)
var rows: [DiffRow] = []
rows.reserveCapacity(ops.count)
var removedLines = Set<Int>(), addedLines = Set<Int>()
for op in ops {
switch op {
case let .same(i, j): rows.append(DiffRow(sign: .same, oldNo: i + 1, newNo: j + 1, text: a[i]))
case let .del(i):
rows.append(DiffRow(sign: .removed, oldNo: i + 1, newNo: nil, text: a[i]))
removedLines.insert(i)
case let .add(j):
rows.append(DiffRow(sign: .added, oldNo: nil, newNo: j + 1, text: b[j]))
addedLines.insert(j)
}
}
let left = a.enumerated().map { SideLine(no: $0.offset + 1, text: $0.element, mark: removedLines.contains($0.offset) ? .removed : nil) }
let right = b.enumerated().map { SideLine(no: $0.offset + 1, text: $0.element, mark: addedLines.contains($0.offset) ? .added : nil) }
return Diff(
rows: rows, left: left, right: right, split: splitRows(ops, a, b),
added: addedLines.count, removed: removedLines.count,
isDeletedFile: status == .deleted, isAddedFile: status == .added,
original: original, updated: updated
)
}
private enum Op {
case same(Int, Int)
case del(Int)
case add(Int)
}
/// Maps every distinct line to an integer so the DP compares ints, not strings.
private static func intern(_ a: [String], _ b: [String]) -> ([Int], [Int]) {
var ids: [String: Int] = [:]
func id(_ line: String) -> Int {
if let known = ids[line] { return known }
let next = ids.count
ids[line] = next
return next
}
return (a.map(id), b.map(id))
}
/// Removed and added runs between two unchanged lines pair up side by side.
private static func splitRows(_ ops: [Op], _ a: [String], _ b: [String]) -> [SplitRow] {
var split: [SplitRow] = []
var removed: [SideLine] = [], added: [SideLine] = []
func flush() {
for k in 0..<max(removed.count, added.count) {
split.append(SplitRow(left: k < removed.count ? removed[k] : nil, right: k < added.count ? added[k] : nil))
}
removed.removeAll(keepingCapacity: true)
added.removeAll(keepingCapacity: true)
}
for op in ops {
switch op {
case let .same(i, j):
flush()
split.append(SplitRow(left: SideLine(no: i + 1, text: a[i]), right: SideLine(no: j + 1, text: b[j])))
case let .del(i): removed.append(SideLine(no: i + 1, text: a[i], mark: .removed))
case let .add(j): added.append(SideLine(no: j + 1, text: b[j], mark: .added))
}
}
flush()
return split
}
private static func region(_ a: [Int], _ b: [Int], _ a0In: Int, _ a1In: Int, _ b0In: Int, _ b1In: Int, _ ops: inout [Op]) {
var a0 = a0In, a1 = a1In, b0 = b0In, b1 = b1In
while a0 < a1 && b0 < b1 && a[a0] == b[b0] {
ops.append(.same(a0, b0))
a0 += 1
b0 += 1
}
var tail: [Op] = []
while a1 > a0 && b1 > b0 && a[a1 - 1] == b[b1 - 1] {
a1 -= 1
b1 -= 1
tail.append(.same(a1, b1))
}
let n = a1 - a0, m = b1 - b0
func replaceAll() {
for i in a0..<a1 { ops.append(.del(i)) }
for j in b0..<b1 { ops.append(.add(j)) }
}
if n == 0 || m == 0 {
replaceAll()
} else if n * m <= maxCells {
dynamicProgram(a, b, a0, a1, b0, b1, &ops)
} else {
let pins = anchors(a, b, a0, a1, b0, b1)
if pins.isEmpty {
replaceAll()
} else {
var ai = a0, bi = b0
for (ax, bx) in pins {
region(a, b, ai, ax, bi, bx, &ops)
ops.append(.same(ax, bx))
ai = ax + 1
bi = bx + 1
}
region(a, b, ai, a1, bi, b1, &ops)
}
}
ops.append(contentsOf: tail.reversed())
}
private static func dynamicProgram(_ a: [Int], _ b: [Int], _ a0: Int, _ a1: Int, _ b0: Int, _ b1: Int, _ ops: inout [Op]) {
let n = a1 - a0, m = b1 - b0
let width = m + 1
var dp = [Int32](repeating: 0, count: (n + 1) * width)
for i in stride(from: n - 1, through: 0, by: -1) {
for j in stride(from: m - 1, through: 0, by: -1) {
dp[i * width + j] = a[a0 + i] == b[b0 + j]
? dp[(i + 1) * width + j + 1] + 1
: max(dp[(i + 1) * width + j], dp[i * width + j + 1])
}
}
var i = 0, j = 0
while i < n && j < m {
if a[a0 + i] == b[b0 + j] {
ops.append(.same(a0 + i, b0 + j))
i += 1
j += 1
} else if dp[(i + 1) * width + j] >= dp[i * width + j + 1] {
ops.append(.del(a0 + i))
i += 1
} else {
ops.append(.add(b0 + j))
j += 1
}
}
while i < n { ops.append(.del(a0 + i)); i += 1 }
while j < m { ops.append(.add(b0 + j)); j += 1 }
}
/// Lines that appear exactly once on each side, in an order both sides share.
private static func anchors(_ a: [Int], _ b: [Int], _ a0: Int, _ a1: Int, _ b0: Int, _ b1: Int) -> [(Int, Int)] {
var countA: [Int: Int] = [:], countB: [Int: Int] = [:], atB: [Int: Int] = [:]
for i in a0..<a1 { countA[a[i], default: 0] += 1 }
for j in b0..<b1 {
countB[b[j], default: 0] += 1
atB[b[j]] = j
}
var pairs: [(Int, Int)] = []
for i in a0..<a1 where countA[a[i]] == 1 && countB[a[i]] == 1 {
if let j = atB[a[i]] { pairs.append((i, j)) }
}
return longestRun(pairs)
}
/// Longest strictly increasing subsequence over the b-index of each pair.
private static func longestRun(_ pairs: [(Int, Int)]) -> [(Int, Int)] {
var tails: [Int] = []
var prev = [Int](repeating: -1, count: pairs.count)
for k in pairs.indices {
let v = pairs[k].1
var lo = 0, hi = tails.count
while lo < hi {
let mid = (lo + hi) / 2
if pairs[tails[mid]].1 < v { lo = mid + 1 } else { hi = mid }
}
prev[k] = lo > 0 ? tails[lo - 1] : -1
if lo == tails.count { tails.append(k) } else { tails[lo] = k }
}
var out: [(Int, Int)] = []
var k = tails.last ?? -1
while k >= 0 {
out.append(pairs[k])
k = prev[k]
}
return out.reversed()
}
}
+21
View File
@@ -0,0 +1,21 @@
import Foundation
/// The view the editor shows for the open file. Raw values are stored in sessions, so `.actual` stays "updated".
///
/// `.diff` is the full-screen side-by-side overlay, not a pane mode.
public enum ViewMode: String, Codable, Sendable, CaseIterable {
/// The live buffer with changed lines marked.
case actual = "updated"
case diff
/// An unchanged file: the plain editable buffer.
case code
/// Rendered Markdown.
case preview
public init(_ mode: DefaultDiffMode) {
switch mode {
case .updated: self = .actual
case .diff: self = .diff
}
}
}