-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.go
More file actions
331 lines (305 loc) · 9.53 KB
/
Copy pathstate.go
File metadata and controls
331 lines (305 loc) · 9.53 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package hpatch
import (
"fmt"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
type renderedCoordinate struct {
line int
column int
}
type renderedSpan struct {
start int
end int
}
type renderedDocument struct {
content string
lines []logicalLine
}
type reportedEdit struct {
file *fileState
command int
operation string
target targetSpec
spans []renderedSpan
}
func (w *workspace) finalStateReport(changes []change) (string, []TargetAlias) {
var report strings.Builder
if w.active == nil {
report.WriteString("no active file\n")
} else {
fmt.Fprintf(&report, "in %s\n", escapeReportControls(w.active.path))
}
last := w.lastReportedEdit()
if last == nil {
report.WriteString("last none\n")
} else {
last.writeSummary(&report)
}
w.writeFileSummary(&report, changes)
activeReferences := false
if len(w.reportedEdits) != 0 {
activeReferences = w.writeFinalReferences(&report)
}
if w.active != nil && !activeReferences {
w.writeFallbackPreview(&report)
}
return report.String(), w.targetAliases()
}
func (w *workspace) lastReportedEdit() *reportedEdit {
if len(w.reportedEdits) == 0 {
return nil
}
return w.reportedEdits[len(w.reportedEdits)-1]
}
func (e *editor) reportedEdit(origin editOrigin) *reportedEdit {
var spans []renderedSpan
for _, edit := range e.edits {
if edit.command == origin.command {
spans = append(spans, renderedSpan{start: edit.targetStart, end: edit.targetEnd})
}
}
if len(spans) == 0 {
return nil
}
return &reportedEdit{command: origin.command, operation: origin.operation, target: origin.targetSpec, spans: spans}
}
func (w *workspace) targetAliases() []TargetAlias {
documents := make(map[*fileState]renderedDocument)
extents := make(map[*fileState]map[int]renderedSpan)
var aliases []TargetAlias
for _, reported := range w.reportedEdits {
if reported.operation != "type" ||
(reported.target.kind != targetLine && reported.target.kind != targetRange) {
continue
}
document, ok := documents[reported.file]
if !ok {
content := reported.file.editor.content()
document = renderedDocument{content: content, lines: previewLines(content)}
documents[reported.file] = document
extents[reported.file] = reported.file.editor.renderedEditExtents()
}
extent, ok := extents[reported.file][reported.command]
if !ok || extent.start == extent.end {
continue
}
startOffset := reported.file.editor.finalOffsets.mapOffset(extent.start)
endOffset := reported.file.editor.finalOffsets.mapOffset(extent.end)
startLine := renderedCoordinateAt(document.content, document.lines, startOffset).line
endLine := renderedCoordinateAt(document.content, document.lines, max(startOffset, endOffset-1)).line
before := renderRowTarget(reported.target)
after := renderDocumentRange(document, startLine, endLine)
aliases = append(aliases, TargetAlias{Path: reported.file.path, Before: before, After: after})
}
return aliases
}
func renderRowTarget(target targetSpec) string {
start := fmt.Sprintf("%d:%s", target.start.line, target.start.hash)
if target.kind == targetRange {
return fmt.Sprintf("%s..%d:%s", start, target.end.line, target.end.hash)
}
return start
}
func renderDocumentRange(document renderedDocument, startLine, endLine int) string {
start := document.lines[startLine-1]
first := fmt.Sprintf("%d:%s", startLine, hashLine(lineContent(document.content, start)))
if startLine == endLine {
return first
}
end := document.lines[endLine-1]
return fmt.Sprintf("%s..%d:%s", first, endLine, hashLine(lineContent(document.content, end)))
}
func (e *reportedEdit) writeSummary(report *strings.Builder) {
document := renderedDocument{content: e.file.editor.baseline, lines: renderedLines(e.file.editor.baseline)}
fmt.Fprintf(
report,
"last %s %s %d ranges ",
e.operation,
escapeReportControls(e.file.path),
len(e.spans),
)
writeSpanLocations(report, document, e.spans)
}
func writeSpanLocations(report *strings.Builder, document renderedDocument, spans []renderedSpan) {
const locationLimit = 3
for index, span := range spans[:min(len(spans), locationLimit)] {
if index != 0 {
report.WriteString(", ")
}
start := renderedCoordinateAt(document.content, document.lines, span.start)
end := renderedCoordinateAt(document.content, document.lines, span.end)
fmt.Fprintf(report, "%d:%d-%d:%d", start.line, start.column, end.line, end.column)
}
if omitted := len(spans) - locationLimit; omitted > 0 {
fmt.Fprintf(report, " +%d more", omitted)
}
report.WriteByte('\n')
}
func (w *workspace) writeFinalReferences(report *strings.Builder) bool {
documents := make(map[*fileState]renderedDocument)
extents := make(map[*fileState]map[int]renderedSpan)
activeReferences := false
for _, reported := range w.reportedEdits {
if reported.file == w.active {
activeReferences = true
}
document, ok := documents[reported.file]
if !ok {
content := reported.file.editor.content()
document = renderedDocument{content: content, lines: previewLines(content)}
documents[reported.file] = document
extents[reported.file] = reported.file.editor.renderedEditExtents()
}
extent, ok := extents[reported.file][reported.command]
if !ok {
panic("reported edit has no effective editor splice")
}
startOffset := reported.file.editor.finalOffsets.mapOffset(extent.start)
endOffset := reported.file.editor.finalOffsets.mapOffset(extent.end)
startLine := renderedCoordinateAt(document.content, document.lines, startOffset).line
endLine := renderedCoordinateAt(document.content, document.lines, endOffset).line
firstLine, lastLine := min(startLine, endLine), max(startLine, endLine)
fmt.Fprintf(
report,
"refs %d %s %s\n",
reported.command,
reported.operation,
escapeReportControls(reported.file.path),
)
indexes := []int{firstLine - 2, firstLine - 1, lastLine - 1, lastLine}
previous := -1
for _, index := range indexes {
if index < 0 || index >= len(document.lines) || index == previous {
continue
}
writePreviewLine(report, document, index)
previous = index
}
}
return activeReferences
}
func (e *editor) renderedEditExtents() map[int]renderedSpan {
result := make(map[int]renderedSpan)
renderedOffset := 0
baselineOffset := 0
for _, edit := range e.orderedEdits() {
renderedOffset += edit.start - baselineOffset
start := renderedOffset
end := start + len(edit.replacement)
if extent, ok := result[edit.command]; ok {
extent.start = min(extent.start, start)
extent.end = max(extent.end, end)
result[edit.command] = extent
} else {
result[edit.command] = renderedSpan{start: start, end: end}
}
renderedOffset = end
baselineOffset = max(baselineOffset, edit.end)
}
return result
}
func (w *workspace) writeFallbackPreview(report *strings.Builder) {
content := w.active.editor.content()
document := renderedDocument{content: content, lines: previewLines(content)}
for index := range min(3, len(document.lines)) {
writePreviewLine(report, document, index)
}
}
func previewLines(text string) []logicalLine {
if text == "" {
return []logicalLine{{}}
}
return logicalLines(text)
}
func writePreviewLine(report *strings.Builder, document renderedDocument, index int) {
line := document.lines[index]
content := lineContent(document.content, line)
writeHashLine(report, index+1, content, previewText(content))
}
func (w *workspace) writeFileSummary(report *strings.Builder, changes []change) {
added, updated, moved, deleted := 0, 0, 0, 0
for _, change := range changes {
switch change.kind {
case changeAdd:
added++
case changeDelete:
deleted++
case changeUpdate:
if change.originalPath != change.path {
moved++
}
if change.original != change.content {
updated++
}
}
}
fmt.Fprintf(report, "files add=%d update=%d move=%d delete=%d\n", added, updated, moved, deleted)
}
func renderedLines(text string) []logicalLine {
lines := logicalLines(text)
if text == "" || endsWithLineTerminator(text) {
lines = append(lines, logicalLine{start: len(text), contentEnd: len(text), fullEnd: len(text)})
}
return lines
}
func renderedCoordinateAt(text string, lines []logicalLine, offset int) renderedCoordinate {
offset = min(max(offset, 0), len(text))
for index, line := range lines {
if offset <= line.contentEnd {
return renderedCoordinate{line: index + 1, column: utf8.RuneCountInString(text[line.start:offset]) + 1}
}
if offset < line.fullEnd {
return renderedCoordinate{line: index + 1, column: utf8.RuneCountInString(text[line.start:line.contentEnd]) + 1}
}
}
last := lines[len(lines)-1]
return renderedCoordinate{line: len(lines), column: utf8.RuneCountInString(text[last.start:last.contentEnd]) + 1}
}
func previewText(text string) string {
return previewTextLimit(text, 64)
}
func previewTextLimit(text string, limit int) string {
var preview strings.Builder
count := 0
leading := true
for _, character := range text {
if count == limit {
break
}
count++
if leading {
switch character {
case ' ':
preview.WriteString(`\x20`)
continue
case '\t':
preview.WriteString(`\t`)
continue
default:
leading = false
}
}
if unicode.IsControl(character) && character != '\t' {
quoted := strconv.QuoteRune(character)
preview.WriteString(quoted[1 : len(quoted)-1])
continue
}
preview.WriteRune(character)
}
return preview.String()
}
func escapeReportControls(text string) string {
var escaped strings.Builder
for _, character := range text {
if unicode.IsControl(character) {
quoted := strconv.QuoteRune(character)
escaped.WriteString(quoted[1 : len(quoted)-1])
continue
}
escaped.WriteRune(character)
}
return escaped.String()
}