-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathpretty.go
276 lines (245 loc) · 6.37 KB
/
pretty.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
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package pretty
import (
"fmt"
"strings"
)
// See the referenced paper in the package documentation for explanations
// of the below code. Methods, variables, and implementation details were
// made to resemble it as close as possible.
// docBest represents a selected document as described by the type
// "Doc" in the referenced paper (not "DOC"). This is the
// less-abstract representation constructed during "best layout"
// selection.
type docBest struct {
tag docBestType
i docPos
s string
d *docBest
}
type docBestType int
const (
textB docBestType = iota
lineB
spacesB
)
// Pretty returns a pretty-printed string for the Doc d at line length
// n and tab width t.
func Pretty(d Doc, n int, useTabs bool, tabWidth int) string {
var sb strings.Builder
b := beExec{
w: int16(n),
tabWidth: int16(tabWidth),
memoBe: make(map[beArgs]*docBest),
memoiDoc: make(map[iDoc]*iDoc),
}
ldoc := b.best(d)
b.layout(&sb, useTabs, ldoc)
return sb.String()
}
// w is the max line width.
func (b *beExec) best(x Doc) *docBest {
return b.be(docPos{0, 0}, b.iDoc(docPos{0, 0}, x, nil))
}
// iDoc represents the type [(Int,DOC)] in the paper,
// extended with arbitrary string prefixes (not just int).
// We'll use linked lists because this makes the
// recursion more efficient than slices.
type iDoc struct {
d Doc
next *iDoc
i docPos
}
type docPos struct {
tabs int16
spaces int16
}
type beExec struct {
// w is the available line width.
w int16
// tabWidth is the virtual tab width.
tabWidth int16
// memoBe internalizes the results of the be function, so that the
// same value is not computed multiple times.
memoBe map[beArgs]*docBest
// memo internalizes iDoc objects to ensure they are unique in memory,
// and we can use pointer-pointer comparisons.
memoiDoc map[iDoc]*iDoc
// docAlloc speeds up the allocations of be()'s return values
// by (*beExec).newDocBest() defined below.
docAlloc []docBest
// idocAlloc speeds up the allocations by (*beExec).iDoc() defined
// below.
idocAlloc []iDoc
}
func (b *beExec) be(k docPos, xlist *iDoc) *docBest {
// Shortcut: be k [] = Nil
if xlist == nil {
return nil
}
// If we've computed this result before, short cut here too.
memoKey := beArgs{k: k, d: xlist}
if cached, ok := b.memoBe[memoKey]; ok {
return cached
}
// General case.
d := *xlist
z := xlist.next
// Note: we'll need to memoize the result below.
var res *docBest
switch t := d.d.(type) {
case nilDoc:
res = b.be(k, z)
case concat:
res = b.be(k, b.iDoc(d.i, t.a, b.iDoc(d.i, t.b, z)))
case nests:
res = b.be(k, b.iDoc(docPos{d.i.tabs, d.i.spaces + t.n}, t.d, z))
case nestt:
res = b.be(k, b.iDoc(docPos{d.i.tabs + 1 + d.i.spaces/b.tabWidth, 0}, t.d, z))
case text:
res = b.newDocBest(docBest{
tag: textB,
s: string(t),
d: b.be(docPos{k.tabs, k.spaces + int16(len(t))}, z),
})
case line, softbreak:
res = b.newDocBest(docBest{
tag: lineB,
i: d.i,
d: b.be(d.i, z),
})
case union:
res = b.better(k,
b.be(k, b.iDoc(d.i, t.x, z)),
// We eta-lift the second argument to avoid eager evaluation.
func() *docBest {
return b.be(k, b.iDoc(d.i, t.y, z))
},
)
case *scolumn:
res = b.be(k, b.iDoc(d.i, t.f(k.spaces), z))
case *snesting:
res = b.be(k, b.iDoc(d.i, t.f(d.i.spaces), z))
case pad:
res = b.newDocBest(docBest{
tag: spacesB,
i: docPos{spaces: t.n},
d: b.be(docPos{k.tabs, k.spaces + t.n}, z),
})
default:
panic(fmt.Errorf("unknown type: %T", d.d))
}
// Memoize so we don't compute the same result twice.
b.memoBe[memoKey] = res
return res
}
// newDocBest makes a new docBest on the heap. Allocations
// are batched for more efficiency.
func (b *beExec) newDocBest(d docBest) *docBest {
buf := &b.docAlloc
if len(*buf) == 0 {
*buf = make([]docBest, 100)
}
r := &(*buf)[0]
*r = d
*buf = (*buf)[1:]
return r
}
// iDoc retrieves the unique instance of iDoc in memory for the given
// values of i, s, d and z. The object is constructed if it does not
// exist yet.
//
// The results of this function guarantee that the pointer addresses
// are equal if the arguments used to construct the value were equal.
func (b *beExec) iDoc(i docPos, d Doc, z *iDoc) *iDoc {
idoc := iDoc{i: i, d: d, next: z}
if m, ok := b.memoiDoc[idoc]; ok {
return m
}
r := b.newiDoc(idoc)
b.memoiDoc[idoc] = r
return r
}
// newiDoc makes a new iDoc on the heap. Allocations are batched
// for more efficiency. Do not use this directly! Instead
// use the iDoc() method defined above.
func (b *beExec) newiDoc(d iDoc) *iDoc {
buf := &b.idocAlloc
if len(*buf) == 0 {
*buf = make([]iDoc, 100)
}
r := &(*buf)[0]
*r = d
*buf = (*buf)[1:]
return r
}
type beArgs struct {
d *iDoc
k docPos
}
func (b *beExec) better(k docPos, x *docBest, y func() *docBest) *docBest {
remainder := b.w - k.spaces - k.tabs*b.tabWidth
if fits(remainder, x) {
return x
}
return y()
}
func fits(w int16, x *docBest) bool {
if w < 0 {
return false
}
if x == nil {
// Nil doc.
return true
}
switch x.tag {
case textB:
return fits(w-int16(len(x.s)), x.d)
case lineB:
return true
case spacesB:
return fits(w-x.i.spaces, x.d)
default:
panic(fmt.Errorf("unknown type: %d", x.tag))
}
}
func (b *beExec) layout(sb *strings.Builder, useTabs bool, d *docBest) {
for ; d != nil; d = d.d {
switch d.tag {
case textB:
sb.WriteString(d.s)
case lineB:
sb.WriteByte('\n')
// Fill the tabs first.
padTabs := d.i.tabs * b.tabWidth
if useTabs {
for i := int16(0); i < d.i.tabs; i++ {
sb.WriteByte('\t')
}
padTabs = 0
}
// Fill the remaining spaces.
for i := int16(0); i < padTabs+d.i.spaces; i++ {
sb.WriteByte(' ')
}
case spacesB:
for i := int16(0); i < d.i.spaces; i++ {
sb.WriteByte(' ')
}
default:
panic(fmt.Errorf("unknown type: %d", d.tag))
}
}
}