-
Notifications
You must be signed in to change notification settings - Fork 9
/
deltas.go
222 lines (193 loc) · 5.17 KB
/
deltas.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package diff
import (
"bytes"
"fmt"
"github.com/j-keck/go-diff/diffmatchpatch"
)
// DeltaType classifies a patch action like 'delete', 'equal' or 'insert'
type DeltaType int
const (
// Del represents a deleted patch chunk
Del DeltaType = (iota - 1)
// Eq represents a not changed patch chunk
Eq
// Ins represents a added patch chunk
Ins
)
// Delta represents a patch chunk
type Delta struct {
Type DeltaType `json:"kind"`
LineNrFrom int `json:"lineNrFrom"`
LineNrTarget int `json:"lineNrTarget"`
StartPosFrom int64 `json:"startPosFrom"`
StartPosTarget int64 `json:"startPosTarget"`
Text string `json:"text"`
}
func (d *Delta) String() string {
var t string
switch d.Type {
case Ins:
t = "+"
case Del:
t = "-"
case Eq:
t = "="
default:
panic("Unexpected DeltaType")
}
return fmt.Sprintf("{%s:%d,%d:%d,%d:%s}", t, d.LineNrFrom, d.LineNrTarget,
d.StartPosFrom, d.StartPosTarget, d.Text)
}
// Deltas represents all patch chunks of a file
type Deltas []Delta
func (deltas Deltas) String() string {
var buffer bytes.Buffer
for idx, delta := range deltas {
if idx > 0 {
buffer.WriteString(",")
}
buffer.WriteString(delta.String())
}
return buffer.String()
}
func createDeltasFromDiffs(diffs []diffmatchpatch.Diff, contextSize int) Deltas {
var deltas Deltas
var lineNrFrom, lineNrTarget int = 1, 1 // first line is line nr. 1
var startPosFrom, startPosTarget int64 = 1, 1 // first char ist at pos 1
idx := 0
nextDiffIfTypeIs := func(diffType diffmatchpatch.Operation) (diffmatchpatch.Diff, bool) {
if idx < len(diffs) && diffs[idx].Type == diffType {
next := diffs[idx]
idx++
return next, true
}
return diffmatchpatch.Diff{}, false
}
createPrevEqDelta := func(diff diffmatchpatch.Diff) Delta {
lineCount := countNewLines(diff.Text)
lines := splitText(diff.Text)
var text string
if lineCount > contextSize {
fromIndex := lineCount - contextSize
text = joinLines(lines[fromIndex:])
} else {
text = diff.Text
}
count := countNewLines(text)
length := int64(len(text))
return Delta{
Eq,
lineNrFrom - count,
lineNrTarget - count,
startPosFrom - length,
startPosTarget - length,
text,
}
}
createAfterEqDelta := func(diff diffmatchpatch.Diff) (Delta, bool) {
lineCount := countNewLines(diff.Text)
lines := splitText(diff.Text)
var text string
nextPrevEqMerged := false
if idx < len(diffs) && lineCount < contextSize*2 {
// merge - but not the last element
text = diff.Text
nextPrevEqMerged = true
} else if lineCount < contextSize {
text = diff.Text
} else {
text = joinLines(lines[:contextSize])
}
return Delta{
Eq,
lineNrFrom,
lineNrTarget,
startPosFrom,
startPosTarget,
text,
}, nextPrevEqMerged
}
// first equal block if there is one
if diff, ok := nextDiffIfTypeIs(diffmatchpatch.DiffEqual); ok {
lineCount := countNewLines(diff.Text)
textLength := int64(len(diff.Text))
lineNrFrom += lineCount
lineNrTarget += lineCount
startPosFrom += textLength
startPosTarget += textLength
if contextSize > 0 {
deltas = append(deltas, createPrevEqDelta(diff))
}
}
for idx < len(diffs) {
var delDiff, insDiff diffmatchpatch.Diff
var hasDel, hasIns bool
// add del-delta if there is a delete
delDiff, hasDel = nextDiffIfTypeIs(diffmatchpatch.DiffDelete)
if hasDel {
deltas = append(deltas, Delta{
Del,
lineNrFrom,
lineNrTarget,
startPosFrom,
startPosTarget,
delDiff.Text,
})
}
// add ins-delta if there is a insert
insDiff, hasIns = nextDiffIfTypeIs(diffmatchpatch.DiffInsert)
if hasIns {
deltas = append(deltas, Delta{
Ins,
lineNrFrom,
lineNrTarget,
startPosFrom,
startPosTarget,
insDiff.Text,
})
}
// update lineNr / startPos
// * after the delta additions!
// * if not after, a text replace has the wrong lineNr / startPos
// in the ins-delta
if hasDel {
lineCount := countNewLines(delDiff.Text)
textLength := int64(len(delDiff.Text))
lineNrFrom += lineCount
startPosFrom += textLength
}
if hasIns {
lineCount := countNewLines(insDiff.Text)
textLength := int64(len(insDiff.Text))
lineNrTarget += lineCount
startPosTarget += textLength
}
// handle equal
// * add after context
// * update line / pos
// * add prev context for the next
if diff, ok := nextDiffIfTypeIs(diffmatchpatch.DiffEqual); ok {
lineCount := countNewLines(diff.Text)
textLength := int64(len(diff.Text))
var afterEqDelta Delta
var nextPrevEqMerged bool
if contextSize > 0 {
afterEqDelta, nextPrevEqMerged = createAfterEqDelta(diff)
deltas = append(deltas, afterEqDelta)
}
lineNrFrom += lineCount
lineNrTarget += lineCount
startPosFrom += textLength
startPosTarget += textLength
// don't add prevEq if
// * no context requested
// * we are at the end
// * the afterEqDelta has merged the next (this) prevEqDelta, because the context overlap
// * the afterEqDelta contains the whole text
if contextSize > 0 && idx < len(diffs) && !nextPrevEqMerged && afterEqDelta.Text != diff.Text {
deltas = append(deltas, createPrevEqDelta(diff))
}
}
}
return deltas
}