/
recalcs.go
450 lines (415 loc) · 12.8 KB
/
recalcs.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package main
import (
"fmt"
"go/ast"
"go/token"
"reflect"
"strings"
"github.com/petergtz/goextract/util"
)
func RecalcPoses(node ast.Node, pos token.Pos, offset *token.Pos, indent int) {
if node == nil {
return
}
if node.Pos() != token.NoPos && offset != nil {
// if *offset != token.NoPos && *offset != pos-node.Pos() {
// panic("Nested sub-node with already set poses must all have same offset. Existing offset:" + spew.Sdump(offset) + " vs new offset: " + spew.Sdump(pos-node.Pos()))
// }
if *offset == token.NoPos {
*offset = pos - node.Pos()
}
shiftPoses(node, *offset)
return
}
switch typedNode := node.(type) {
case *ast.File:
typedNode.Package = pos
// TODO do import statements
RecalcPoses(typedNode.Name, pos+8, offset, indent)
currentPos := typedNode.Name.End() + 2
for _, decl := range typedNode.Decls {
RecalcPoses(decl, currentPos, offset, indent)
currentPos = decl.End() + 1
}
case *ast.FuncDecl:
typedNode.Type.Func = pos
if typedNode.Recv != nil {
RecalcPoses(typedNode.Recv, pos+5, offset, indent)
pos = typedNode.Recv.End() + 1
} else {
pos += 5
}
RecalcPoses(typedNode.Name, pos, offset, indent)
pos = typedNode.Name.End()
RecalcPoses(typedNode.Type.Params, pos, offset, indent)
pos = typedNode.Type.Params.End() + 1
if typedNode.Type.Results != nil {
RecalcPoses(typedNode.Type.Results, pos, offset, indent)
pos = typedNode.Type.Results.End() + 1
}
RecalcPoses(typedNode.Body, pos, offset, indent)
case *ast.FieldList:
typedNode.Opening = pos
pos++
for _, field := range typedNode.List {
RecalcPoses(field, pos, offset, indent)
pos = field.End() + 1
}
if len(typedNode.List) > 0 {
pos--
}
typedNode.Closing = pos
case *ast.Field:
if typedNode.Tag != nil {
panic("Field tags not supported yet")
}
for _, name := range typedNode.Names {
RecalcPoses(name, pos, offset, indent)
pos = name.End() + 1
}
RecalcPoses(typedNode.Type, pos, offset, indent)
case *ast.BlockStmt:
typedNode.Lbrace = pos
pos += 2 + token.Pos(indent) + 1
for _, stmt := range typedNode.List {
RecalcPoses(stmt, pos, offset, indent+1)
pos = stmt.End() + 1 + token.Pos(indent) + 1 // new line for next stmt
}
typedNode.Rbrace = pos
case *ast.ReturnStmt:
typedNode.Return = pos
pos += 7
for _, result := range typedNode.Results {
RecalcPoses(result, pos, offset, indent)
pos = result.End() + 1
}
case *ast.GenDecl:
typedNode.TokPos = pos
typedNode.Lparen = pos + token.Pos(len(typedNode.Tok.String())) + 1
currentPos := typedNode.Lparen + 1 + 1 /* new line*/ + 1 /*indentation*/
for _, spec := range typedNode.Specs {
RecalcPoses(spec, currentPos, offset, indent)
currentPos = spec.End() + 1
}
typedNode.Rparen = currentPos
case *ast.ValueSpec:
currentPos := pos
for _, name := range typedNode.Names {
RecalcPoses(name, currentPos, offset, indent)
currentPos = name.End() + 1
}
for _, value := range typedNode.Values {
RecalcPoses(value, currentPos, offset, indent)
currentPos = value.End() + 1
}
RecalcPoses(typedNode.Type, currentPos, offset, indent)
case *ast.Ident:
typedNode.NamePos = pos
case *ast.Ellipsis:
typedNode.Ellipsis = pos
RecalcPoses(typedNode.Elt, pos+3, offset, indent)
case *ast.BasicLit:
typedNode.ValuePos = pos
case *ast.FuncLit:
RecalcPoses(typedNode.Type, pos, offset, indent)
RecalcPoses(typedNode.Body, typedNode.Type.End()+1, offset, indent)
case *ast.CompositeLit:
RecalcPoses(typedNode.Type, pos, offset, indent)
typedNode.Lbrace = typedNode.Type.End() + 1
currentPos := typedNode.Type.End() + 2
for _, elt := range typedNode.Elts {
RecalcPoses(elt, currentPos, offset, indent)
currentPos = elt.End() + 1
}
typedNode.Rbrace = currentPos
case *ast.ParenExpr:
typedNode.Lparen = pos
RecalcPoses(typedNode.X, pos+1, offset, indent)
typedNode.Rparen = typedNode.X.End() + 1
case *ast.CallExpr:
RecalcPoses(typedNode.Fun, pos, offset, indent)
typedNode.Lparen = typedNode.Fun.End()
currentPos := typedNode.Lparen + 1
for _, arg := range typedNode.Args {
RecalcPoses(arg, currentPos, offset, indent)
currentPos = arg.End() + 1
}
// TODO: is this the right treatment in all cases?
typedNode.Ellipsis = token.NoPos
typedNode.Rparen = currentPos
case *ast.BinaryExpr:
RecalcPoses(typedNode.X, pos, offset, indent)
typedNode.OpPos = typedNode.X.End() + 1
RecalcPoses(typedNode.Y, typedNode.OpPos+token.Pos(len(typedNode.Op.String())), offset, indent)
case *ast.FuncType:
// not needed because we do above
case *ast.StarExpr:
typedNode.Star = pos
RecalcPoses(typedNode.X, pos+1, offset, indent)
case *ast.ExprStmt:
RecalcPoses(typedNode.X, pos, offset, indent)
case *ast.AssignStmt:
for _, lhs := range typedNode.Lhs {
RecalcPoses(lhs, pos, offset, indent)
// TODO: should this increment by 2 to include space after comma?
pos = lhs.End() + 1
}
typedNode.TokPos = pos
pos += token.Pos(len(typedNode.Tok.String()))
for _, rhs := range typedNode.Rhs {
RecalcPoses(rhs, pos, offset, indent)
pos = rhs.End() + 1
}
case *ast.SelectorExpr:
RecalcPoses(typedNode.X, pos, offset, indent)
RecalcPoses(typedNode.Sel, typedNode.X.End()+1, offset, indent)
case *ast.ArrayType:
typedNode.Lbrack = pos
pos += 2
// TODO treat real arrays correctly
// RecalcPoses(typedNode.Len, pos, offset)
RecalcPoses(typedNode.Elt, pos, offset, indent)
default:
panic(fmt.Sprintf("recalcing not implemented for %v yet", reflect.TypeOf(node)))
}
}
func shiftPoses(node ast.Node, offset token.Pos) {
ast.Inspect(node, func(node ast.Node) bool {
if node == nil {
return true
}
shiftPosesNonRecursively(node, offset, 0)
return true
})
}
func getPosesForNode(node ast.Node) []*token.Pos {
switch typedNode := node.(type) {
case *ast.Ident:
return []*token.Pos{&typedNode.NamePos}
case *ast.File:
return []*token.Pos{&typedNode.Package}
case *ast.FuncDecl:
return []*token.Pos{}
case *ast.FieldList:
return []*token.Pos{&typedNode.Opening, &typedNode.Closing}
case *ast.Field:
return []*token.Pos{}
case *ast.BlockStmt:
return []*token.Pos{&typedNode.Lbrace, &typedNode.Rbrace}
case *ast.ReturnStmt:
return []*token.Pos{&typedNode.Return}
case *ast.AssignStmt:
return []*token.Pos{&typedNode.TokPos}
case *ast.GenDecl:
return []*token.Pos{&typedNode.TokPos, &typedNode.Lparen, &typedNode.Rparen}
case *ast.Ellipsis:
return []*token.Pos{&typedNode.Ellipsis}
case *ast.BasicLit:
return []*token.Pos{&typedNode.ValuePos}
case *ast.CompositeLit:
return []*token.Pos{&typedNode.Lbrace, &typedNode.Rbrace}
case *ast.ParenExpr:
return []*token.Pos{&typedNode.Lparen, &typedNode.Rparen}
case *ast.ValueSpec:
return []*token.Pos{}
case *ast.SelectorExpr:
return []*token.Pos{}
case *ast.IndexExpr:
return []*token.Pos{&typedNode.Lbrack, &typedNode.Rbrack}
case *ast.SliceExpr:
return []*token.Pos{&typedNode.Lbrack, &typedNode.Rbrack}
case *ast.CallExpr:
return []*token.Pos{&typedNode.Lparen, &typedNode.Ellipsis, &typedNode.Rparen}
case *ast.BinaryExpr:
return []*token.Pos{&typedNode.OpPos}
case *ast.FuncType:
return []*token.Pos{&typedNode.Func}
case *ast.ImportSpec:
return []*token.Pos{&typedNode.EndPos}
case *ast.ExprStmt:
return []*token.Pos{}
case *ast.RangeStmt:
return []*token.Pos{&typedNode.For, &typedNode.TokPos}
case *ast.ArrayType:
return []*token.Pos{&typedNode.Lbrack}
case *ast.TypeSpec:
return []*token.Pos{}
case *ast.StructType:
return []*token.Pos{&typedNode.Struct}
case *ast.UnaryExpr:
return []*token.Pos{&typedNode.OpPos}
case *ast.StarExpr:
return []*token.Pos{&typedNode.Star}
case *ast.IfStmt:
return []*token.Pos{&typedNode.If}
case *ast.TypeSwitchStmt:
return []*token.Pos{&typedNode.Switch}
case *ast.TypeAssertExpr:
return []*token.Pos{&typedNode.Lparen, &typedNode.Rparen}
case *ast.CaseClause:
return []*token.Pos{&typedNode.Case, &typedNode.Colon}
case *ast.DeclStmt:
return []*token.Pos{}
case *ast.MapType:
return []*token.Pos{&typedNode.Map}
case *ast.KeyValueExpr:
return []*token.Pos{&typedNode.Colon}
case *ast.BranchStmt:
return []*token.Pos{&typedNode.TokPos}
case *ast.CommentGroup:
return []*token.Pos{}
case *ast.Comment:
return []*token.Pos{&typedNode.Slash}
default:
panic(fmt.Sprintf("poses not implemented for %v yet", reflect.TypeOf(node)))
}
}
func shiftPosesNonRecursively(node ast.Node, offset token.Pos, lowerBound token.Pos) {
for _, pos := range getPosesForNode(node) {
if *pos > lowerBound {
*pos += offset
}
}
}
func resetPoses(node ast.Node) {
ast.Inspect(node, func(node ast.Node) bool {
if node == nil {
return true
}
for _, pos := range getPosesForNode(node) {
*pos = 0
}
return true
})
}
func lineLengthsFrom(fileSet *token.FileSet) []int {
return lineLengthsFromLines(strings.Split(util.ReadFileAsStringOrPanic(fileSet.File(1).Name()), "\n"))
}
func lineLengthsFromLines(lines []string) []int {
lineLengths := make([]int, len(lines))
for i, line := range lines {
lineLengths[i] = len(line)
}
return lineLengths
}
func replacementModifications(fileSet *token.FileSet, oldPos, oldEnd, newEnd token.Pos, lineLengths []int, areaRemoved []Range) (lineNum, numLinesToCut, newLength int) {
oldEndLine := fileSet.Position(oldEnd).Line
oldPosLine := fileSet.Position(oldPos).Line
numCutLines := oldEndLine - oldPosLine
newLinelen := areaRemoved[0].begin + (lineLengths[oldEndLine-1] - areaRemoved[len(areaRemoved)-1].end) + int(newEnd-oldPos)
return oldPosLine - 1, numCutLines, newLinelen
}
func ConvertLineOffsetsToLineLengths(offsets []int, endPos int) []int {
result := make([]int, len(offsets))
lastOffset := 0
for i, offset := range offsets[1:] {
result[i] = offset - lastOffset - 1
lastOffset = offset
}
result[len(result)-1] = int(endPos) - lastOffset
return result
}
func ConvertLineLengthsToLineOffsets(lineLengths []int) []int {
result := make([]int, len(lineLengths))
result[0] = 0
offset := 0
for i, lineLength := range lineLengths[:len(lineLengths)-1] {
result[i+1] = offset + lineLength + 1
offset += lineLength + 1
}
return result
}
func shiftPosesAfterPos(node ast.Node, newNode ast.Node, pos token.Pos, by token.Pos) {
// TODO: this must also move comments
var offset token.Pos
ast.Inspect(node, func(node ast.Node) bool {
if node == nil {
return true
}
if node == newNode {
return false
}
if node.End() > pos && offset == 0 {
offset = by
}
shiftPosesNonRecursively(node, offset, pos)
return true
})
}
func insertionModifications(astFile *ast.File, funcDecl *ast.FuncDecl, areaRemoved []Range) (areaToBeAppended []int) {
linelengths := make([]int, len(areaRemoved)-1)
for i, line := range areaRemoved[1:] {
linelengths[i] = line.end
}
result := []int{
int(funcDecl.Body.Pos() - funcDecl.Type.Func + 1),
}
if funcDecl.Type.Results == nil {
result = append(result, 1+areaRemoved[0].end-areaRemoved[0].begin)
} else {
result = append(result, 1+len(token.RETURN.String())+1+areaRemoved[0].end-areaRemoved[0].begin)
}
result = append(result, linelengths...)
result = append(result, 1)
return result
}
func insertionModificationsForStmts(astFile *ast.File, funcDecl *ast.FuncDecl, areaRemoved []Range, varsUsedAfterwards []ast.Expr) (areaToBeAppended []int) {
linelengths := make([]int, len(areaRemoved))
for i, line := range areaRemoved {
linelengths[i] = line.end
}
result := []int{
int(funcDecl.Body.Pos() - funcDecl.Type.Func + 1),
}
result = append(result, linelengths...)
// TODO: this if could type assert the last stmt to see if it is a return stmt
if len(varsUsedAfterwards) != 0 {
vars := funcDecl.Body.List[len(funcDecl.Body.List)-1].(*ast.ReturnStmt).Results
result = append(result, 1+len(token.RETURN.String())+1+int(vars[len(vars)-1].End()-vars[0].Pos()))
}
result = append(result, 1)
return result
}
// TODO this moves all comments instead of just correct ones.
func moveComments(astFile *ast.File, moveOffset token.Pos) {
for _, commentGroup := range astFile.Comments {
for _, comment := range commentGroup.List {
comment.Slash += moveOffset
}
}
}
type Range struct {
begin, end int
}
func areaRemoved(fileSet *token.FileSet, pos, end token.Pos) []Range {
lineLengths := lineLengthsFrom(fileSet)
b := fileSet.Position(pos)
e := fileSet.Position(end)
result := make([]Range, e.Line-b.Line+1)
if e.Line > b.Line {
result[0].begin = b.Column - 1
result[0].end = lineLengths[b.Line-1]
for i := 1; i < len(result)-1; i++ {
result[i].begin = 0
result[i].end = lineLengths[b.Line-1+i]
}
if len(result) > 1 {
result[len(result)-1].begin = 0
result[len(result)-1].end = e.Column - 1
}
} else {
result[0].begin = b.Column - 1
result[0].end = e.Column - 1
}
return result
}
func FakeContentForLineLengths(linesLengths []int) string {
result := ""
for _, linesLength := range linesLengths {
for i := 0; i < linesLength; i++ {
result += "x"
}
result += "\n"
}
return result
}