forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
300 lines (276 loc) · 7.86 KB
/
Copy pathdiff.go
File metadata and controls
300 lines (276 loc) · 7.86 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package diff
import (
"fmt"
"sort"
"strings"
"github.com/fatih/color"
"github.com/sourcegraph/sourcegraph/lib/codeintel/precise"
)
func Diff(old, new *precise.GroupedBundleDataMaps) string {
builder := strings.Builder{}
allPaths := make(map[string]struct{})
for path := range old.Documents {
if _, exists := new.Documents[path]; !exists {
removed(&builder, fmt.Sprintf("Document: %v", path))
} else {
allPaths[path] = struct{}{}
}
}
for path := range new.Documents {
if _, exists := old.Documents[path]; !exists {
added(&builder, fmt.Sprintf("Document: %v", path))
}
}
for path := range allPaths {
oldDocument := old.Documents[path]
newDocument := new.Documents[path]
locationSet := make(map[precise.LocationData]struct{})
oldResults := make(map[precise.LocationData]precise.QueryResult)
newResults := make(map[precise.LocationData]precise.QueryResult)
for _, rng := range oldDocument.Ranges {
loc := precise.LocationData{
DocumentPath: path,
StartLine: rng.StartLine,
StartCharacter: rng.StartCharacter,
EndLine: rng.EndLine,
EndCharacter: rng.EndCharacter,
}
locationSet[loc] = struct{}{}
oldResults[loc] = precise.Resolve(old, oldDocument, rng)
}
for _, rng := range newDocument.Ranges {
location := precise.LocationData{
DocumentPath: path,
StartLine: rng.StartLine,
StartCharacter: rng.StartCharacter,
EndLine: rng.EndLine,
EndCharacter: rng.EndCharacter,
}
locationSet[location] = struct{}{}
newResults[location] = precise.Resolve(new, newDocument, rng)
}
var sortedLocations []precise.LocationData
for location := range locationSet {
sortedLocations = append(sortedLocations, location)
}
sort.Slice(sortedLocations, func(i, j int) bool {
return precise.CompareLocations(sortedLocations[i], sortedLocations[j]) < 0
})
for _, location := range sortedLocations {
oldResult, oldExists := oldResults[location]
newResult, newExists := newResults[location]
if oldExists && !newExists {
removed(&builder, fmt.Sprintf("Range: %v", locationString(location)))
continue
}
if newExists && !oldExists {
added(&builder, fmt.Sprintf("Range: %v", locationString(location)))
continue
}
if oldResult.Hover != newResult.Hover {
if oldResult.Hover != "" {
removed(
&builder,
fmt.Sprintf("Hover: %v", locationString(location)),
)
for _, line := range strings.Split(oldResult.Hover, "\n") {
builder.WriteString(
color.RedString(
fmt.Sprintf(" %v\n", line),
),
)
}
}
if newResult.Hover != "" {
added(
&builder,
fmt.Sprintf("Hover: %v", locationString(location)),
)
for _, line := range strings.Split(newResult.Hover, "\n") {
builder.WriteString(
color.GreenString(
fmt.Sprintf(" %v\n", line),
),
)
}
}
}
diffLocations(
&builder,
oldResult.Definitions,
newResult.Definitions,
fmt.Sprintf("Definition: %v -> ", locationString(location)),
)
diffLocations(
&builder,
oldResult.References,
newResult.References,
fmt.Sprintf("Reference: %v -> ", locationString(location)),
)
diffQualifiedMonikers(
&builder,
oldResult.Monikers,
newResult.Monikers,
fmt.Sprintf("Moniker: %v -> ", locationString(location)),
)
}
}
diffMonikers(&builder, old.Definitions, new.Definitions, "MonikerDefinition: ")
diffMonikers(&builder, old.References, new.References, "MonikerReference: ")
return builder.String()
}
func diffQualifiedMonikers(builder *strings.Builder, old, new []precise.QualifiedMonikerData, prefix string) {
type noIDMonikerData struct {
Kind string
Scheme string
Identifier string
Name string
Version string
}
oldSet := make(map[noIDMonikerData]struct{})
newSet := make(map[noIDMonikerData]struct{})
for _, moniker := range old {
oldSet[noIDMonikerData{
Kind: moniker.Kind,
Scheme: moniker.Scheme,
Identifier: moniker.Identifier,
Name: moniker.Name,
Version: moniker.Version,
}] = struct{}{}
}
for _, moniker := range new {
newSet[noIDMonikerData{
Kind: moniker.Kind,
Scheme: moniker.Scheme,
Identifier: moniker.Identifier,
Name: moniker.Name,
Version: moniker.Version,
}] = struct{}{}
}
for moniker := range oldSet {
if _, exists := newSet[moniker]; !exists {
removed(builder, fmt.Sprintf(
"%v%v:%v:%v@%v:%v",
prefix,
moniker.Kind,
moniker.Scheme,
moniker.Name,
moniker.Version,
moniker.Identifier,
))
}
}
for moniker := range newSet {
if _, exists := oldSet[moniker]; !exists {
added(builder, fmt.Sprintf(
"%v%v:%v:%v@%v:%v",
prefix,
moniker.Kind,
moniker.Scheme,
moniker.Name,
moniker.Version,
moniker.Identifier,
))
}
}
}
func diffLocations(builder *strings.Builder, old, new []precise.LocationData, prefix string) {
oldSet := make(map[precise.LocationData]struct{})
var oldSlice []precise.LocationData
newSet := make(map[precise.LocationData]struct{})
var newSlice []precise.LocationData
for _, location := range old {
oldSet[location] = struct{}{}
oldSlice = append(oldSlice, location)
}
for _, location := range new {
newSet[location] = struct{}{}
newSlice = append(newSlice, location)
}
sortLocations(oldSlice)
sortLocations(newSlice)
for _, location := range oldSlice {
if _, exists := newSet[location]; !exists {
removed(builder, fmt.Sprintf("%v%v", prefix, locationString(location)))
}
}
for _, location := range newSlice {
if _, exists := oldSet[location]; !exists {
added(builder, fmt.Sprintf("%v%v", prefix, locationString(location)))
}
}
}
func diffMonikers(
builder *strings.Builder,
old, new map[string]map[string]map[string][]precise.LocationData,
prefix string,
) {
type kindSchemeID struct {
kind string
scheme string
id string
}
kindSchemeIDSet := make(map[kindSchemeID]struct{})
for kind, schemeIds := range old {
for scheme, ids := range schemeIds {
for id := range ids {
kindSchemeIDSet[kindSchemeID{kind: kind, scheme: scheme, id: id}] = struct{}{}
}
}
}
for kind, schemeIds := range new {
for scheme, ids := range schemeIds {
for id := range ids {
kindSchemeIDSet[kindSchemeID{kind: kind, scheme: scheme, id: id}] = struct{}{}
}
}
}
var sortedKindSchemeIDs []kindSchemeID
for schemeID := range kindSchemeIDSet {
sortedKindSchemeIDs = append(sortedKindSchemeIDs, schemeID)
}
sort.Slice(sortedKindSchemeIDs, func(i, j int) bool {
if sortedKindSchemeIDs[i].kind < sortedKindSchemeIDs[j].kind {
return true
}
if sortedKindSchemeIDs[i].kind > sortedKindSchemeIDs[j].kind {
return false
}
if sortedKindSchemeIDs[i].scheme < sortedKindSchemeIDs[j].scheme {
return true
}
if sortedKindSchemeIDs[i].scheme > sortedKindSchemeIDs[j].scheme {
return false
}
return sortedKindSchemeIDs[i].id < sortedKindSchemeIDs[j].id
})
for _, kindSchemeID := range sortedKindSchemeIDs {
diffLocations(
builder,
old[kindSchemeID.kind][kindSchemeID.scheme][kindSchemeID.id],
new[kindSchemeID.kind][kindSchemeID.scheme][kindSchemeID.id],
fmt.Sprintf("%v%v:%v:%v -> ", prefix, kindSchemeID.kind, kindSchemeID.scheme, kindSchemeID.id),
)
}
}
func locationString(location precise.LocationData) string {
return fmt.Sprintf(
"%v:(%v:%v)-(%v:%v)",
location.DocumentPath,
location.StartLine,
location.StartCharacter,
location.EndLine,
location.EndCharacter,
)
}
func removed(builder *strings.Builder, value string) {
builder.WriteString(color.RedString(fmt.Sprintf("- %v\n", value)))
}
func added(builder *strings.Builder, value string) {
builder.WriteString(color.GreenString(fmt.Sprintf("+ %v\n", value)))
}
func sortLocations(locations []precise.LocationData) {
sort.Slice(locations, func(i, j int) bool {
return precise.CompareLocations(locations[i], locations[j]) < 0
})
}