-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathdiff.ts
More file actions
157 lines (119 loc) · 4.46 KB
/
diff.ts
File metadata and controls
157 lines (119 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { colorize } from "@herb-tools/highlighter"
export interface DiffLine {
type: "context" | "removed" | "added"
content: string
lineNumber: number
}
export interface DiffHunk {
lines: DiffLine[]
}
export function computeDiff(original: string, fixed: string, contextLines: number = 1): DiffHunk[] {
if (original === fixed) return []
const originalLines = original.split("\n")
const fixedLines = fixed.split("\n")
if (originalLines.length === fixedLines.length) {
return computeParallelDiff(originalLines, fixedLines, contextLines)
}
return computeBlockDiff(originalLines, fixedLines, contextLines)
}
function computeParallelDiff(originalLines: string[], fixedLines: string[], contextLines: number): DiffHunk[] {
const changeIndices: number[] = []
for (let index = 0; index < originalLines.length; index++) {
if (originalLines[index] !== fixedLines[index]) {
changeIndices.push(index)
}
}
if (changeIndices.length === 0) return []
const changeGroups: number[][] = []
let currentGroup: number[] = [changeIndices[0]]
for (let index = 1; index < changeIndices.length; index++) {
const gapBetweenChanges = changeIndices[index] - changeIndices[index - 1] - 1
if (gapBetweenChanges <= contextLines * 2) {
currentGroup.push(changeIndices[index])
} else {
changeGroups.push(currentGroup)
currentGroup = [changeIndices[index]]
}
}
changeGroups.push(currentGroup)
const hunks: DiffHunk[] = []
for (const group of changeGroups) {
const hunk: DiffHunk = { lines: [] }
const firstChange = group[0]
const lastChange = group[group.length - 1]
const contextStart = Math.max(0, firstChange - contextLines)
const contextEnd = Math.min(originalLines.length - 1, lastChange + contextLines)
for (let index = contextStart; index <= contextEnd; index++) {
if (group.includes(index)) {
hunk.lines.push({ type: "removed", content: originalLines[index], lineNumber: index + 1 })
hunk.lines.push({ type: "added", content: fixedLines[index], lineNumber: index + 1 })
} else {
hunk.lines.push({ type: "context", content: originalLines[index], lineNumber: index + 1 })
}
}
hunks.push(hunk)
}
return hunks
}
function computeBlockDiff(originalLines: string[], fixedLines: string[], contextLines: number): DiffHunk[] {
let prefixLength = 0
while (
prefixLength < originalLines.length &&
prefixLength < fixedLines.length &&
originalLines[prefixLength] === fixedLines[prefixLength]
) {
prefixLength++
}
let originalEnd = originalLines.length - 1
let fixedEnd = fixedLines.length - 1
while (
originalEnd > prefixLength &&
fixedEnd > prefixLength &&
originalLines[originalEnd] === fixedLines[fixedEnd]
) {
originalEnd--
fixedEnd--
}
if (prefixLength > originalEnd && prefixLength > fixedEnd) {
return []
}
const hunk: DiffHunk = { lines: [] }
const contextStart = Math.max(0, prefixLength - contextLines)
for (let index = contextStart; index < prefixLength; index++) {
hunk.lines.push({ type: "context", content: originalLines[index], lineNumber: index + 1 })
}
for (let index = prefixLength; index <= originalEnd; index++) {
hunk.lines.push({ type: "removed", content: originalLines[index], lineNumber: index + 1 })
}
for (let index = prefixLength; index <= fixedEnd; index++) {
hunk.lines.push({ type: "added", content: fixedLines[index], lineNumber: index + 1 })
}
const contextEnd = Math.min(originalLines.length - 1, originalEnd + contextLines)
for (let index = originalEnd + 1; index <= contextEnd; index++) {
hunk.lines.push({ type: "context", content: originalLines[index], lineNumber: index + 1 })
}
return hunk.lines.length > 0 ? [hunk] : []
}
export function formatDiff(hunks: DiffHunk[], indent: string = " "): string {
const lines: string[] = []
for (let hunkIndex = 0; hunkIndex < hunks.length; hunkIndex++) {
const hunk = hunks[hunkIndex]
if (hunkIndex > 0) {
lines.push(indent + colorize(" ...", "gray"))
}
for (const line of hunk.lines) {
switch (line.type) {
case "removed":
lines.push(indent + colorize(`- ${line.content}`, "red"))
break
case "added":
lines.push(indent + colorize(`+ ${line.content}`, "green"))
break
case "context":
lines.push(indent + colorize(` ${line.content}`, "gray"))
break
}
}
}
return lines.join("\n")
}