1 |
747 |
jeremybenn |
// Copyright 2009 The Go Authors. All rights reserved.
|
2 |
|
|
// Use of this source code is governed by a BSD-style
|
3 |
|
|
// license that can be found in the LICENSE file.
|
4 |
|
|
|
5 |
|
|
// This file implements printing of AST nodes; specifically
|
6 |
|
|
// expressions, statements, declarations, and files. It uses
|
7 |
|
|
// the print functionality implemented in printer.go.
|
8 |
|
|
|
9 |
|
|
package printer
|
10 |
|
|
|
11 |
|
|
import (
|
12 |
|
|
"bytes"
|
13 |
|
|
"go/ast"
|
14 |
|
|
"go/token"
|
15 |
|
|
)
|
16 |
|
|
|
17 |
|
|
// Other formatting issues:
|
18 |
|
|
// - better comment formatting for /*-style comments at the end of a line (e.g. a declaration)
|
19 |
|
|
// when the comment spans multiple lines; if such a comment is just two lines, formatting is
|
20 |
|
|
// not idempotent
|
21 |
|
|
// - formatting of expression lists
|
22 |
|
|
// - should use blank instead of tab to separate one-line function bodies from
|
23 |
|
|
// the function header unless there is a group of consecutive one-liners
|
24 |
|
|
|
25 |
|
|
// ----------------------------------------------------------------------------
|
26 |
|
|
// Common AST nodes.
|
27 |
|
|
|
28 |
|
|
// Print as many newlines as necessary (but at least min newlines) to get to
|
29 |
|
|
// the current line. ws is printed before the first line break. If newSection
|
30 |
|
|
// is set, the first line break is printed as formfeed. Returns true if any
|
31 |
|
|
// line break was printed; returns false otherwise.
|
32 |
|
|
//
|
33 |
|
|
// TODO(gri): linebreak may add too many lines if the next statement at "line"
|
34 |
|
|
// is preceded by comments because the computation of n assumes
|
35 |
|
|
// the current position before the comment and the target position
|
36 |
|
|
// after the comment. Thus, after interspersing such comments, the
|
37 |
|
|
// space taken up by them is not considered to reduce the number of
|
38 |
|
|
// linebreaks. At the moment there is no easy way to know about
|
39 |
|
|
// future (not yet interspersed) comments in this function.
|
40 |
|
|
//
|
41 |
|
|
func (p *printer) linebreak(line, min int, ws whiteSpace, newSection bool) (printedBreak bool) {
|
42 |
|
|
n := nlimit(line - p.pos.Line)
|
43 |
|
|
if n < min {
|
44 |
|
|
n = min
|
45 |
|
|
}
|
46 |
|
|
if n > 0 {
|
47 |
|
|
p.print(ws)
|
48 |
|
|
if newSection {
|
49 |
|
|
p.print(formfeed)
|
50 |
|
|
n--
|
51 |
|
|
}
|
52 |
|
|
for ; n > 0; n-- {
|
53 |
|
|
p.print(newline)
|
54 |
|
|
}
|
55 |
|
|
printedBreak = true
|
56 |
|
|
}
|
57 |
|
|
return
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
// setComment sets g as the next comment if g != nil and if node comments
|
61 |
|
|
// are enabled - this mode is used when printing source code fragments such
|
62 |
|
|
// as exports only. It assumes that there are no other pending comments to
|
63 |
|
|
// intersperse.
|
64 |
|
|
func (p *printer) setComment(g *ast.CommentGroup) {
|
65 |
|
|
if g == nil || !p.useNodeComments {
|
66 |
|
|
return
|
67 |
|
|
}
|
68 |
|
|
if p.comments == nil {
|
69 |
|
|
// initialize p.comments lazily
|
70 |
|
|
p.comments = make([]*ast.CommentGroup, 1)
|
71 |
|
|
} else if p.cindex < len(p.comments) {
|
72 |
|
|
// for some reason there are pending comments; this
|
73 |
|
|
// should never happen - handle gracefully and flush
|
74 |
|
|
// all comments up to g, ignore anything after that
|
75 |
|
|
p.flush(p.posFor(g.List[0].Pos()), token.ILLEGAL)
|
76 |
|
|
}
|
77 |
|
|
p.comments[0] = g
|
78 |
|
|
p.cindex = 0
|
79 |
|
|
p.nextComment() // get comment ready for use
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
type exprListMode uint
|
83 |
|
|
|
84 |
|
|
const (
|
85 |
|
|
blankStart exprListMode = 1 << iota // print a blank before a non-empty list
|
86 |
|
|
blankEnd // print a blank after a non-empty list
|
87 |
|
|
commaSep // elements are separated by commas
|
88 |
|
|
commaTerm // list is optionally terminated by a comma
|
89 |
|
|
noIndent // no extra indentation in multi-line lists
|
90 |
|
|
periodSep // elements are separated by periods
|
91 |
|
|
)
|
92 |
|
|
|
93 |
|
|
// Sets multiLine to true if the identifier list spans multiple lines.
|
94 |
|
|
// If indent is set, a multi-line identifier list is indented after the
|
95 |
|
|
// first linebreak encountered.
|
96 |
|
|
func (p *printer) identList(list []*ast.Ident, indent bool, multiLine *bool) {
|
97 |
|
|
// convert into an expression list so we can re-use exprList formatting
|
98 |
|
|
xlist := make([]ast.Expr, len(list))
|
99 |
|
|
for i, x := range list {
|
100 |
|
|
xlist[i] = x
|
101 |
|
|
}
|
102 |
|
|
mode := commaSep
|
103 |
|
|
if !indent {
|
104 |
|
|
mode |= noIndent
|
105 |
|
|
}
|
106 |
|
|
p.exprList(token.NoPos, xlist, 1, mode, multiLine, token.NoPos)
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
// Print a list of expressions. If the list spans multiple
|
110 |
|
|
// source lines, the original line breaks are respected between
|
111 |
|
|
// expressions. Sets multiLine to true if the list spans multiple
|
112 |
|
|
// lines.
|
113 |
|
|
//
|
114 |
|
|
// TODO(gri) Consider rewriting this to be independent of []ast.Expr
|
115 |
|
|
// so that we can use the algorithm for any kind of list
|
116 |
|
|
// (e.g., pass list via a channel over which to range).
|
117 |
|
|
func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exprListMode, multiLine *bool, next0 token.Pos) {
|
118 |
|
|
if len(list) == 0 {
|
119 |
|
|
return
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
if mode&blankStart != 0 {
|
123 |
|
|
p.print(blank)
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
prev := p.posFor(prev0)
|
127 |
|
|
next := p.posFor(next0)
|
128 |
|
|
line := p.lineFor(list[0].Pos())
|
129 |
|
|
endLine := p.lineFor(list[len(list)-1].End())
|
130 |
|
|
|
131 |
|
|
if prev.IsValid() && prev.Line == line && line == endLine {
|
132 |
|
|
// all list entries on a single line
|
133 |
|
|
for i, x := range list {
|
134 |
|
|
if i > 0 {
|
135 |
|
|
if mode&commaSep != 0 {
|
136 |
|
|
p.print(token.COMMA)
|
137 |
|
|
}
|
138 |
|
|
p.print(blank)
|
139 |
|
|
}
|
140 |
|
|
p.expr0(x, depth, multiLine)
|
141 |
|
|
}
|
142 |
|
|
if mode&blankEnd != 0 {
|
143 |
|
|
p.print(blank)
|
144 |
|
|
}
|
145 |
|
|
return
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
// list entries span multiple lines;
|
149 |
|
|
// use source code positions to guide line breaks
|
150 |
|
|
|
151 |
|
|
// don't add extra indentation if noIndent is set;
|
152 |
|
|
// i.e., pretend that the first line is already indented
|
153 |
|
|
ws := ignore
|
154 |
|
|
if mode&noIndent == 0 {
|
155 |
|
|
ws = indent
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
// the first linebreak is always a formfeed since this section must not
|
159 |
|
|
// depend on any previous formatting
|
160 |
|
|
prevBreak := -1 // index of last expression that was followed by a linebreak
|
161 |
|
|
if prev.IsValid() && prev.Line < line && p.linebreak(line, 0, ws, true) {
|
162 |
|
|
ws = ignore
|
163 |
|
|
*multiLine = true
|
164 |
|
|
prevBreak = 0
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
// initialize expression/key size: a zero value indicates expr/key doesn't fit on a single line
|
168 |
|
|
size := 0
|
169 |
|
|
|
170 |
|
|
// print all list elements
|
171 |
|
|
for i, x := range list {
|
172 |
|
|
prevLine := line
|
173 |
|
|
line = p.lineFor(x.Pos())
|
174 |
|
|
|
175 |
|
|
// determine if the next linebreak, if any, needs to use formfeed:
|
176 |
|
|
// in general, use the entire node size to make the decision; for
|
177 |
|
|
// key:value expressions, use the key size
|
178 |
|
|
// TODO(gri) for a better result, should probably incorporate both
|
179 |
|
|
// the key and the node size into the decision process
|
180 |
|
|
useFF := true
|
181 |
|
|
|
182 |
|
|
// determine element size: all bets are off if we don't have
|
183 |
|
|
// position information for the previous and next token (likely
|
184 |
|
|
// generated code - simply ignore the size in this case by setting
|
185 |
|
|
// it to 0)
|
186 |
|
|
prevSize := size
|
187 |
|
|
const infinity = 1e6 // larger than any source line
|
188 |
|
|
size = p.nodeSize(x, infinity)
|
189 |
|
|
pair, isPair := x.(*ast.KeyValueExpr)
|
190 |
|
|
if size <= infinity && prev.IsValid() && next.IsValid() {
|
191 |
|
|
// x fits on a single line
|
192 |
|
|
if isPair {
|
193 |
|
|
size = p.nodeSize(pair.Key, infinity) // size <= infinity
|
194 |
|
|
}
|
195 |
|
|
} else {
|
196 |
|
|
// size too large or we don't have good layout information
|
197 |
|
|
size = 0
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
// if the previous line and the current line had single-
|
201 |
|
|
// line-expressions and the key sizes are small or the
|
202 |
|
|
// the ratio between the key sizes does not exceed a
|
203 |
|
|
// threshold, align columns and do not use formfeed
|
204 |
|
|
if prevSize > 0 && size > 0 {
|
205 |
|
|
const smallSize = 20
|
206 |
|
|
if prevSize <= smallSize && size <= smallSize {
|
207 |
|
|
useFF = false
|
208 |
|
|
} else {
|
209 |
|
|
const r = 4 // threshold
|
210 |
|
|
ratio := float64(size) / float64(prevSize)
|
211 |
|
|
useFF = ratio <= 1/r || r <= ratio
|
212 |
|
|
}
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
if i > 0 {
|
216 |
|
|
switch {
|
217 |
|
|
case mode&commaSep != 0:
|
218 |
|
|
p.print(token.COMMA)
|
219 |
|
|
case mode&periodSep != 0:
|
220 |
|
|
p.print(token.PERIOD)
|
221 |
|
|
}
|
222 |
|
|
needsBlank := mode&periodSep == 0 // period-separated list elements don't need a blank
|
223 |
|
|
if prevLine < line && prevLine > 0 && line > 0 {
|
224 |
|
|
// lines are broken using newlines so comments remain aligned
|
225 |
|
|
// unless forceFF is set or there are multiple expressions on
|
226 |
|
|
// the same line in which case formfeed is used
|
227 |
|
|
if p.linebreak(line, 0, ws, useFF || prevBreak+1 < i) {
|
228 |
|
|
ws = ignore
|
229 |
|
|
*multiLine = true
|
230 |
|
|
prevBreak = i
|
231 |
|
|
needsBlank = false // we got a line break instead
|
232 |
|
|
}
|
233 |
|
|
}
|
234 |
|
|
if needsBlank {
|
235 |
|
|
p.print(blank)
|
236 |
|
|
}
|
237 |
|
|
}
|
238 |
|
|
|
239 |
|
|
if isPair && size > 0 && len(list) > 1 {
|
240 |
|
|
// we have a key:value expression that fits onto one line and
|
241 |
|
|
// is in a list with more then one entry: use a column for the
|
242 |
|
|
// key such that consecutive entries can align if possible
|
243 |
|
|
p.expr(pair.Key, multiLine)
|
244 |
|
|
p.print(pair.Colon, token.COLON, vtab)
|
245 |
|
|
p.expr(pair.Value, multiLine)
|
246 |
|
|
} else {
|
247 |
|
|
p.expr0(x, depth, multiLine)
|
248 |
|
|
}
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
if mode&commaTerm != 0 && next.IsValid() && p.pos.Line < next.Line {
|
252 |
|
|
// print a terminating comma if the next token is on a new line
|
253 |
|
|
p.print(token.COMMA)
|
254 |
|
|
if ws == ignore && mode&noIndent == 0 {
|
255 |
|
|
// unindent if we indented
|
256 |
|
|
p.print(unindent)
|
257 |
|
|
}
|
258 |
|
|
p.print(formfeed) // terminating comma needs a line break to look good
|
259 |
|
|
return
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
if mode&blankEnd != 0 {
|
263 |
|
|
p.print(blank)
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
if ws == ignore && mode&noIndent == 0 {
|
267 |
|
|
// unindent if we indented
|
268 |
|
|
p.print(unindent)
|
269 |
|
|
}
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
// Sets multiLine to true if the the parameter list spans multiple lines.
|
273 |
|
|
func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) {
|
274 |
|
|
p.print(fields.Opening, token.LPAREN)
|
275 |
|
|
if len(fields.List) > 0 {
|
276 |
|
|
prevLine := p.lineFor(fields.Opening)
|
277 |
|
|
ws := indent
|
278 |
|
|
for i, par := range fields.List {
|
279 |
|
|
// determine par begin and end line (may be different
|
280 |
|
|
// if there are multiple parameter names for this par
|
281 |
|
|
// or the type is on a separate line)
|
282 |
|
|
var parLineBeg int
|
283 |
|
|
var parLineEnd = p.lineFor(par.Type.Pos())
|
284 |
|
|
if len(par.Names) > 0 {
|
285 |
|
|
parLineBeg = p.lineFor(par.Names[0].Pos())
|
286 |
|
|
} else {
|
287 |
|
|
parLineBeg = parLineEnd
|
288 |
|
|
}
|
289 |
|
|
// separating "," if needed
|
290 |
|
|
if i > 0 {
|
291 |
|
|
p.print(token.COMMA)
|
292 |
|
|
}
|
293 |
|
|
// separator if needed (linebreak or blank)
|
294 |
|
|
if 0 < prevLine && prevLine < parLineBeg && p.linebreak(parLineBeg, 0, ws, true) {
|
295 |
|
|
// break line if the opening "(" or previous parameter ended on a different line
|
296 |
|
|
ws = ignore
|
297 |
|
|
*multiLine = true
|
298 |
|
|
} else if i > 0 {
|
299 |
|
|
p.print(blank)
|
300 |
|
|
}
|
301 |
|
|
// parameter names
|
302 |
|
|
if len(par.Names) > 0 {
|
303 |
|
|
// Very subtle: If we indented before (ws == ignore), identList
|
304 |
|
|
// won't indent again. If we didn't (ws == indent), identList will
|
305 |
|
|
// indent if the identList spans multiple lines, and it will outdent
|
306 |
|
|
// again at the end (and still ws == indent). Thus, a subsequent indent
|
307 |
|
|
// by a linebreak call after a type, or in the next multi-line identList
|
308 |
|
|
// will do the right thing.
|
309 |
|
|
p.identList(par.Names, ws == indent, multiLine)
|
310 |
|
|
p.print(blank)
|
311 |
|
|
}
|
312 |
|
|
// parameter type
|
313 |
|
|
p.expr(par.Type, multiLine)
|
314 |
|
|
prevLine = parLineEnd
|
315 |
|
|
}
|
316 |
|
|
// if the closing ")" is on a separate line from the last parameter,
|
317 |
|
|
// print an additional "," and line break
|
318 |
|
|
if closing := p.lineFor(fields.Closing); 0 < prevLine && prevLine < closing {
|
319 |
|
|
p.print(",")
|
320 |
|
|
p.linebreak(closing, 0, ignore, true)
|
321 |
|
|
}
|
322 |
|
|
// unindent if we indented
|
323 |
|
|
if ws == ignore {
|
324 |
|
|
p.print(unindent)
|
325 |
|
|
}
|
326 |
|
|
}
|
327 |
|
|
p.print(fields.Closing, token.RPAREN)
|
328 |
|
|
}
|
329 |
|
|
|
330 |
|
|
// Sets multiLine to true if the signature spans multiple lines.
|
331 |
|
|
func (p *printer) signature(params, result *ast.FieldList, multiLine *bool) {
|
332 |
|
|
p.parameters(params, multiLine)
|
333 |
|
|
n := result.NumFields()
|
334 |
|
|
if n > 0 {
|
335 |
|
|
p.print(blank)
|
336 |
|
|
if n == 1 && result.List[0].Names == nil {
|
337 |
|
|
// single anonymous result; no ()'s
|
338 |
|
|
p.expr(result.List[0].Type, multiLine)
|
339 |
|
|
return
|
340 |
|
|
}
|
341 |
|
|
p.parameters(result, multiLine)
|
342 |
|
|
}
|
343 |
|
|
}
|
344 |
|
|
|
345 |
|
|
func identListSize(list []*ast.Ident, maxSize int) (size int) {
|
346 |
|
|
for i, x := range list {
|
347 |
|
|
if i > 0 {
|
348 |
|
|
size += 2 // ", "
|
349 |
|
|
}
|
350 |
|
|
size += len(x.Name)
|
351 |
|
|
if size >= maxSize {
|
352 |
|
|
break
|
353 |
|
|
}
|
354 |
|
|
}
|
355 |
|
|
return
|
356 |
|
|
}
|
357 |
|
|
|
358 |
|
|
func (p *printer) isOneLineFieldList(list []*ast.Field) bool {
|
359 |
|
|
if len(list) != 1 {
|
360 |
|
|
return false // allow only one field
|
361 |
|
|
}
|
362 |
|
|
f := list[0]
|
363 |
|
|
if f.Tag != nil || f.Comment != nil {
|
364 |
|
|
return false // don't allow tags or comments
|
365 |
|
|
}
|
366 |
|
|
// only name(s) and type
|
367 |
|
|
const maxSize = 30 // adjust as appropriate, this is an approximate value
|
368 |
|
|
namesSize := identListSize(f.Names, maxSize)
|
369 |
|
|
if namesSize > 0 {
|
370 |
|
|
namesSize = 1 // blank between names and types
|
371 |
|
|
}
|
372 |
|
|
typeSize := p.nodeSize(f.Type, maxSize)
|
373 |
|
|
return namesSize+typeSize <= maxSize
|
374 |
|
|
}
|
375 |
|
|
|
376 |
|
|
func (p *printer) setLineComment(text string) {
|
377 |
|
|
p.setComment(&ast.CommentGroup{[]*ast.Comment{{token.NoPos, text}}})
|
378 |
|
|
}
|
379 |
|
|
|
380 |
|
|
func (p *printer) fieldList(fields *ast.FieldList, isStruct, isIncomplete bool) {
|
381 |
|
|
lbrace := fields.Opening
|
382 |
|
|
list := fields.List
|
383 |
|
|
rbrace := fields.Closing
|
384 |
|
|
hasComments := isIncomplete || p.commentBefore(p.posFor(rbrace))
|
385 |
|
|
srcIsOneLine := lbrace.IsValid() && rbrace.IsValid() && p.lineFor(lbrace) == p.lineFor(rbrace)
|
386 |
|
|
|
387 |
|
|
if !hasComments && srcIsOneLine {
|
388 |
|
|
// possibly a one-line struct/interface
|
389 |
|
|
if len(list) == 0 {
|
390 |
|
|
// no blank between keyword and {} in this case
|
391 |
|
|
p.print(lbrace, token.LBRACE, rbrace, token.RBRACE)
|
392 |
|
|
return
|
393 |
|
|
} else if isStruct && p.isOneLineFieldList(list) { // for now ignore interfaces
|
394 |
|
|
// small enough - print on one line
|
395 |
|
|
// (don't use identList and ignore source line breaks)
|
396 |
|
|
p.print(lbrace, token.LBRACE, blank)
|
397 |
|
|
f := list[0]
|
398 |
|
|
for i, x := range f.Names {
|
399 |
|
|
if i > 0 {
|
400 |
|
|
p.print(token.COMMA, blank)
|
401 |
|
|
}
|
402 |
|
|
p.expr(x, ignoreMultiLine)
|
403 |
|
|
}
|
404 |
|
|
if len(f.Names) > 0 {
|
405 |
|
|
p.print(blank)
|
406 |
|
|
}
|
407 |
|
|
p.expr(f.Type, ignoreMultiLine)
|
408 |
|
|
p.print(blank, rbrace, token.RBRACE)
|
409 |
|
|
return
|
410 |
|
|
}
|
411 |
|
|
}
|
412 |
|
|
// hasComments || !srcIsOneLine
|
413 |
|
|
|
414 |
|
|
p.print(blank, lbrace, token.LBRACE, indent)
|
415 |
|
|
if hasComments || len(list) > 0 {
|
416 |
|
|
p.print(formfeed)
|
417 |
|
|
}
|
418 |
|
|
|
419 |
|
|
if isStruct {
|
420 |
|
|
|
421 |
|
|
sep := vtab
|
422 |
|
|
if len(list) == 1 {
|
423 |
|
|
sep = blank
|
424 |
|
|
}
|
425 |
|
|
var ml bool
|
426 |
|
|
for i, f := range list {
|
427 |
|
|
if i > 0 {
|
428 |
|
|
p.linebreak(p.lineFor(f.Pos()), 1, ignore, ml)
|
429 |
|
|
}
|
430 |
|
|
ml = false
|
431 |
|
|
extraTabs := 0
|
432 |
|
|
p.setComment(f.Doc)
|
433 |
|
|
if len(f.Names) > 0 {
|
434 |
|
|
// named fields
|
435 |
|
|
p.identList(f.Names, false, &ml)
|
436 |
|
|
p.print(sep)
|
437 |
|
|
p.expr(f.Type, &ml)
|
438 |
|
|
extraTabs = 1
|
439 |
|
|
} else {
|
440 |
|
|
// anonymous field
|
441 |
|
|
p.expr(f.Type, &ml)
|
442 |
|
|
extraTabs = 2
|
443 |
|
|
}
|
444 |
|
|
if f.Tag != nil {
|
445 |
|
|
if len(f.Names) > 0 && sep == vtab {
|
446 |
|
|
p.print(sep)
|
447 |
|
|
}
|
448 |
|
|
p.print(sep)
|
449 |
|
|
p.expr(f.Tag, &ml)
|
450 |
|
|
extraTabs = 0
|
451 |
|
|
}
|
452 |
|
|
if f.Comment != nil {
|
453 |
|
|
for ; extraTabs > 0; extraTabs-- {
|
454 |
|
|
p.print(sep)
|
455 |
|
|
}
|
456 |
|
|
p.setComment(f.Comment)
|
457 |
|
|
}
|
458 |
|
|
}
|
459 |
|
|
if isIncomplete {
|
460 |
|
|
if len(list) > 0 {
|
461 |
|
|
p.print(formfeed)
|
462 |
|
|
}
|
463 |
|
|
p.flush(p.posFor(rbrace), token.RBRACE) // make sure we don't lose the last line comment
|
464 |
|
|
p.setLineComment("// contains filtered or unexported fields")
|
465 |
|
|
}
|
466 |
|
|
|
467 |
|
|
} else { // interface
|
468 |
|
|
|
469 |
|
|
var ml bool
|
470 |
|
|
for i, f := range list {
|
471 |
|
|
if i > 0 {
|
472 |
|
|
p.linebreak(p.lineFor(f.Pos()), 1, ignore, ml)
|
473 |
|
|
}
|
474 |
|
|
ml = false
|
475 |
|
|
p.setComment(f.Doc)
|
476 |
|
|
if ftyp, isFtyp := f.Type.(*ast.FuncType); isFtyp {
|
477 |
|
|
// method
|
478 |
|
|
p.expr(f.Names[0], &ml)
|
479 |
|
|
p.signature(ftyp.Params, ftyp.Results, &ml)
|
480 |
|
|
} else {
|
481 |
|
|
// embedded interface
|
482 |
|
|
p.expr(f.Type, &ml)
|
483 |
|
|
}
|
484 |
|
|
p.setComment(f.Comment)
|
485 |
|
|
}
|
486 |
|
|
if isIncomplete {
|
487 |
|
|
if len(list) > 0 {
|
488 |
|
|
p.print(formfeed)
|
489 |
|
|
}
|
490 |
|
|
p.flush(p.posFor(rbrace), token.RBRACE) // make sure we don't lose the last line comment
|
491 |
|
|
p.setLineComment("// contains filtered or unexported methods")
|
492 |
|
|
}
|
493 |
|
|
|
494 |
|
|
}
|
495 |
|
|
p.print(unindent, formfeed, rbrace, token.RBRACE)
|
496 |
|
|
}
|
497 |
|
|
|
498 |
|
|
// ----------------------------------------------------------------------------
|
499 |
|
|
// Expressions
|
500 |
|
|
|
501 |
|
|
func walkBinary(e *ast.BinaryExpr) (has4, has5 bool, maxProblem int) {
|
502 |
|
|
switch e.Op.Precedence() {
|
503 |
|
|
case 4:
|
504 |
|
|
has4 = true
|
505 |
|
|
case 5:
|
506 |
|
|
has5 = true
|
507 |
|
|
}
|
508 |
|
|
|
509 |
|
|
switch l := e.X.(type) {
|
510 |
|
|
case *ast.BinaryExpr:
|
511 |
|
|
if l.Op.Precedence() < e.Op.Precedence() {
|
512 |
|
|
// parens will be inserted.
|
513 |
|
|
// pretend this is an *ast.ParenExpr and do nothing.
|
514 |
|
|
break
|
515 |
|
|
}
|
516 |
|
|
h4, h5, mp := walkBinary(l)
|
517 |
|
|
has4 = has4 || h4
|
518 |
|
|
has5 = has5 || h5
|
519 |
|
|
if maxProblem < mp {
|
520 |
|
|
maxProblem = mp
|
521 |
|
|
}
|
522 |
|
|
}
|
523 |
|
|
|
524 |
|
|
switch r := e.Y.(type) {
|
525 |
|
|
case *ast.BinaryExpr:
|
526 |
|
|
if r.Op.Precedence() <= e.Op.Precedence() {
|
527 |
|
|
// parens will be inserted.
|
528 |
|
|
// pretend this is an *ast.ParenExpr and do nothing.
|
529 |
|
|
break
|
530 |
|
|
}
|
531 |
|
|
h4, h5, mp := walkBinary(r)
|
532 |
|
|
has4 = has4 || h4
|
533 |
|
|
has5 = has5 || h5
|
534 |
|
|
if maxProblem < mp {
|
535 |
|
|
maxProblem = mp
|
536 |
|
|
}
|
537 |
|
|
|
538 |
|
|
case *ast.StarExpr:
|
539 |
|
|
if e.Op == token.QUO { // `*/`
|
540 |
|
|
maxProblem = 5
|
541 |
|
|
}
|
542 |
|
|
|
543 |
|
|
case *ast.UnaryExpr:
|
544 |
|
|
switch e.Op.String() + r.Op.String() {
|
545 |
|
|
case "/*", "&&", "&^":
|
546 |
|
|
maxProblem = 5
|
547 |
|
|
case "++", "--":
|
548 |
|
|
if maxProblem < 4 {
|
549 |
|
|
maxProblem = 4
|
550 |
|
|
}
|
551 |
|
|
}
|
552 |
|
|
}
|
553 |
|
|
return
|
554 |
|
|
}
|
555 |
|
|
|
556 |
|
|
func cutoff(e *ast.BinaryExpr, depth int) int {
|
557 |
|
|
has4, has5, maxProblem := walkBinary(e)
|
558 |
|
|
if maxProblem > 0 {
|
559 |
|
|
return maxProblem + 1
|
560 |
|
|
}
|
561 |
|
|
if has4 && has5 {
|
562 |
|
|
if depth == 1 {
|
563 |
|
|
return 5
|
564 |
|
|
}
|
565 |
|
|
return 4
|
566 |
|
|
}
|
567 |
|
|
if depth == 1 {
|
568 |
|
|
return 6
|
569 |
|
|
}
|
570 |
|
|
return 4
|
571 |
|
|
}
|
572 |
|
|
|
573 |
|
|
func diffPrec(expr ast.Expr, prec int) int {
|
574 |
|
|
x, ok := expr.(*ast.BinaryExpr)
|
575 |
|
|
if !ok || prec != x.Op.Precedence() {
|
576 |
|
|
return 1
|
577 |
|
|
}
|
578 |
|
|
return 0
|
579 |
|
|
}
|
580 |
|
|
|
581 |
|
|
func reduceDepth(depth int) int {
|
582 |
|
|
depth--
|
583 |
|
|
if depth < 1 {
|
584 |
|
|
depth = 1
|
585 |
|
|
}
|
586 |
|
|
return depth
|
587 |
|
|
}
|
588 |
|
|
|
589 |
|
|
// Format the binary expression: decide the cutoff and then format.
|
590 |
|
|
// Let's call depth == 1 Normal mode, and depth > 1 Compact mode.
|
591 |
|
|
// (Algorithm suggestion by Russ Cox.)
|
592 |
|
|
//
|
593 |
|
|
// The precedences are:
|
594 |
|
|
// 5 * / % << >> & &^
|
595 |
|
|
// 4 + - | ^
|
596 |
|
|
// 3 == != < <= > >=
|
597 |
|
|
// 2 &&
|
598 |
|
|
// 1 ||
|
599 |
|
|
//
|
600 |
|
|
// The only decision is whether there will be spaces around levels 4 and 5.
|
601 |
|
|
// There are never spaces at level 6 (unary), and always spaces at levels 3 and below.
|
602 |
|
|
//
|
603 |
|
|
// To choose the cutoff, look at the whole expression but excluding primary
|
604 |
|
|
// expressions (function calls, parenthesized exprs), and apply these rules:
|
605 |
|
|
//
|
606 |
|
|
// 1) If there is a binary operator with a right side unary operand
|
607 |
|
|
// that would clash without a space, the cutoff must be (in order):
|
608 |
|
|
//
|
609 |
|
|
// /* 6
|
610 |
|
|
// && 6
|
611 |
|
|
// &^ 6
|
612 |
|
|
// ++ 5
|
613 |
|
|
// -- 5
|
614 |
|
|
//
|
615 |
|
|
// (Comparison operators always have spaces around them.)
|
616 |
|
|
//
|
617 |
|
|
// 2) If there is a mix of level 5 and level 4 operators, then the cutoff
|
618 |
|
|
// is 5 (use spaces to distinguish precedence) in Normal mode
|
619 |
|
|
// and 4 (never use spaces) in Compact mode.
|
620 |
|
|
//
|
621 |
|
|
// 3) If there are no level 4 operators or no level 5 operators, then the
|
622 |
|
|
// cutoff is 6 (always use spaces) in Normal mode
|
623 |
|
|
// and 4 (never use spaces) in Compact mode.
|
624 |
|
|
//
|
625 |
|
|
// Sets multiLine to true if the binary expression spans multiple lines.
|
626 |
|
|
func (p *printer) binaryExpr(x *ast.BinaryExpr, prec1, cutoff, depth int, multiLine *bool) {
|
627 |
|
|
prec := x.Op.Precedence()
|
628 |
|
|
if prec < prec1 {
|
629 |
|
|
// parenthesis needed
|
630 |
|
|
// Note: The parser inserts an ast.ParenExpr node; thus this case
|
631 |
|
|
// can only occur if the AST is created in a different way.
|
632 |
|
|
p.print(token.LPAREN)
|
633 |
|
|
p.expr0(x, reduceDepth(depth), multiLine) // parentheses undo one level of depth
|
634 |
|
|
p.print(token.RPAREN)
|
635 |
|
|
return
|
636 |
|
|
}
|
637 |
|
|
|
638 |
|
|
printBlank := prec < cutoff
|
639 |
|
|
|
640 |
|
|
ws := indent
|
641 |
|
|
p.expr1(x.X, prec, depth+diffPrec(x.X, prec), multiLine)
|
642 |
|
|
if printBlank {
|
643 |
|
|
p.print(blank)
|
644 |
|
|
}
|
645 |
|
|
xline := p.pos.Line // before the operator (it may be on the next line!)
|
646 |
|
|
yline := p.lineFor(x.Y.Pos())
|
647 |
|
|
p.print(x.OpPos, x.Op)
|
648 |
|
|
if xline != yline && xline > 0 && yline > 0 {
|
649 |
|
|
// at least one line break, but respect an extra empty line
|
650 |
|
|
// in the source
|
651 |
|
|
if p.linebreak(yline, 1, ws, true) {
|
652 |
|
|
ws = ignore
|
653 |
|
|
*multiLine = true
|
654 |
|
|
printBlank = false // no blank after line break
|
655 |
|
|
}
|
656 |
|
|
}
|
657 |
|
|
if printBlank {
|
658 |
|
|
p.print(blank)
|
659 |
|
|
}
|
660 |
|
|
p.expr1(x.Y, prec+1, depth+1, multiLine)
|
661 |
|
|
if ws == ignore {
|
662 |
|
|
p.print(unindent)
|
663 |
|
|
}
|
664 |
|
|
}
|
665 |
|
|
|
666 |
|
|
func isBinary(expr ast.Expr) bool {
|
667 |
|
|
_, ok := expr.(*ast.BinaryExpr)
|
668 |
|
|
return ok
|
669 |
|
|
}
|
670 |
|
|
|
671 |
|
|
// If the expression contains one or more selector expressions, splits it into
|
672 |
|
|
// two expressions at the rightmost period. Writes entire expr to suffix when
|
673 |
|
|
// selector isn't found. Rewrites AST nodes for calls, index expressions and
|
674 |
|
|
// type assertions, all of which may be found in selector chains, to make them
|
675 |
|
|
// parts of the chain.
|
676 |
|
|
func splitSelector(expr ast.Expr) (body, suffix ast.Expr) {
|
677 |
|
|
switch x := expr.(type) {
|
678 |
|
|
case *ast.SelectorExpr:
|
679 |
|
|
body, suffix = x.X, x.Sel
|
680 |
|
|
return
|
681 |
|
|
case *ast.CallExpr:
|
682 |
|
|
body, suffix = splitSelector(x.Fun)
|
683 |
|
|
if body != nil {
|
684 |
|
|
suffix = &ast.CallExpr{suffix, x.Lparen, x.Args, x.Ellipsis, x.Rparen}
|
685 |
|
|
return
|
686 |
|
|
}
|
687 |
|
|
case *ast.IndexExpr:
|
688 |
|
|
body, suffix = splitSelector(x.X)
|
689 |
|
|
if body != nil {
|
690 |
|
|
suffix = &ast.IndexExpr{suffix, x.Lbrack, x.Index, x.Rbrack}
|
691 |
|
|
return
|
692 |
|
|
}
|
693 |
|
|
case *ast.SliceExpr:
|
694 |
|
|
body, suffix = splitSelector(x.X)
|
695 |
|
|
if body != nil {
|
696 |
|
|
suffix = &ast.SliceExpr{suffix, x.Lbrack, x.Low, x.High, x.Rbrack}
|
697 |
|
|
return
|
698 |
|
|
}
|
699 |
|
|
case *ast.TypeAssertExpr:
|
700 |
|
|
body, suffix = splitSelector(x.X)
|
701 |
|
|
if body != nil {
|
702 |
|
|
suffix = &ast.TypeAssertExpr{suffix, x.Type}
|
703 |
|
|
return
|
704 |
|
|
}
|
705 |
|
|
}
|
706 |
|
|
suffix = expr
|
707 |
|
|
return
|
708 |
|
|
}
|
709 |
|
|
|
710 |
|
|
// Convert an expression into an expression list split at the periods of
|
711 |
|
|
// selector expressions.
|
712 |
|
|
func selectorExprList(expr ast.Expr) (list []ast.Expr) {
|
713 |
|
|
// split expression
|
714 |
|
|
for expr != nil {
|
715 |
|
|
var suffix ast.Expr
|
716 |
|
|
expr, suffix = splitSelector(expr)
|
717 |
|
|
list = append(list, suffix)
|
718 |
|
|
}
|
719 |
|
|
|
720 |
|
|
// reverse list
|
721 |
|
|
for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 {
|
722 |
|
|
list[i], list[j] = list[j], list[i]
|
723 |
|
|
}
|
724 |
|
|
|
725 |
|
|
return
|
726 |
|
|
}
|
727 |
|
|
|
728 |
|
|
// Sets multiLine to true if the expression spans multiple lines.
|
729 |
|
|
func (p *printer) expr1(expr ast.Expr, prec1, depth int, multiLine *bool) {
|
730 |
|
|
p.print(expr.Pos())
|
731 |
|
|
|
732 |
|
|
switch x := expr.(type) {
|
733 |
|
|
case *ast.BadExpr:
|
734 |
|
|
p.print("BadExpr")
|
735 |
|
|
|
736 |
|
|
case *ast.Ident:
|
737 |
|
|
p.print(x)
|
738 |
|
|
|
739 |
|
|
case *ast.BinaryExpr:
|
740 |
|
|
if depth < 1 {
|
741 |
|
|
p.internalError("depth < 1:", depth)
|
742 |
|
|
depth = 1
|
743 |
|
|
}
|
744 |
|
|
p.binaryExpr(x, prec1, cutoff(x, depth), depth, multiLine)
|
745 |
|
|
|
746 |
|
|
case *ast.KeyValueExpr:
|
747 |
|
|
p.expr(x.Key, multiLine)
|
748 |
|
|
p.print(x.Colon, token.COLON, blank)
|
749 |
|
|
p.expr(x.Value, multiLine)
|
750 |
|
|
|
751 |
|
|
case *ast.StarExpr:
|
752 |
|
|
const prec = token.UnaryPrec
|
753 |
|
|
if prec < prec1 {
|
754 |
|
|
// parenthesis needed
|
755 |
|
|
p.print(token.LPAREN)
|
756 |
|
|
p.print(token.MUL)
|
757 |
|
|
p.expr(x.X, multiLine)
|
758 |
|
|
p.print(token.RPAREN)
|
759 |
|
|
} else {
|
760 |
|
|
// no parenthesis needed
|
761 |
|
|
p.print(token.MUL)
|
762 |
|
|
p.expr(x.X, multiLine)
|
763 |
|
|
}
|
764 |
|
|
|
765 |
|
|
case *ast.UnaryExpr:
|
766 |
|
|
const prec = token.UnaryPrec
|
767 |
|
|
if prec < prec1 {
|
768 |
|
|
// parenthesis needed
|
769 |
|
|
p.print(token.LPAREN)
|
770 |
|
|
p.expr(x, multiLine)
|
771 |
|
|
p.print(token.RPAREN)
|
772 |
|
|
} else {
|
773 |
|
|
// no parenthesis needed
|
774 |
|
|
p.print(x.Op)
|
775 |
|
|
if x.Op == token.RANGE {
|
776 |
|
|
// TODO(gri) Remove this code if it cannot be reached.
|
777 |
|
|
p.print(blank)
|
778 |
|
|
}
|
779 |
|
|
p.expr1(x.X, prec, depth, multiLine)
|
780 |
|
|
}
|
781 |
|
|
|
782 |
|
|
case *ast.BasicLit:
|
783 |
|
|
p.print(x)
|
784 |
|
|
|
785 |
|
|
case *ast.FuncLit:
|
786 |
|
|
p.expr(x.Type, multiLine)
|
787 |
|
|
p.funcBody(x.Body, p.distance(x.Type.Pos(), p.pos), true, multiLine)
|
788 |
|
|
|
789 |
|
|
case *ast.ParenExpr:
|
790 |
|
|
if _, hasParens := x.X.(*ast.ParenExpr); hasParens {
|
791 |
|
|
// don't print parentheses around an already parenthesized expression
|
792 |
|
|
// TODO(gri) consider making this more general and incorporate precedence levels
|
793 |
|
|
p.expr0(x.X, reduceDepth(depth), multiLine) // parentheses undo one level of depth
|
794 |
|
|
} else {
|
795 |
|
|
p.print(token.LPAREN)
|
796 |
|
|
p.expr0(x.X, reduceDepth(depth), multiLine) // parentheses undo one level of depth
|
797 |
|
|
p.print(x.Rparen, token.RPAREN)
|
798 |
|
|
}
|
799 |
|
|
|
800 |
|
|
case *ast.SelectorExpr:
|
801 |
|
|
parts := selectorExprList(expr)
|
802 |
|
|
p.exprList(token.NoPos, parts, depth, periodSep, multiLine, token.NoPos)
|
803 |
|
|
|
804 |
|
|
case *ast.TypeAssertExpr:
|
805 |
|
|
p.expr1(x.X, token.HighestPrec, depth, multiLine)
|
806 |
|
|
p.print(token.PERIOD, token.LPAREN)
|
807 |
|
|
if x.Type != nil {
|
808 |
|
|
p.expr(x.Type, multiLine)
|
809 |
|
|
} else {
|
810 |
|
|
p.print(token.TYPE)
|
811 |
|
|
}
|
812 |
|
|
p.print(token.RPAREN)
|
813 |
|
|
|
814 |
|
|
case *ast.IndexExpr:
|
815 |
|
|
// TODO(gri): should treat[] like parentheses and undo one level of depth
|
816 |
|
|
p.expr1(x.X, token.HighestPrec, 1, multiLine)
|
817 |
|
|
p.print(x.Lbrack, token.LBRACK)
|
818 |
|
|
p.expr0(x.Index, depth+1, multiLine)
|
819 |
|
|
p.print(x.Rbrack, token.RBRACK)
|
820 |
|
|
|
821 |
|
|
case *ast.SliceExpr:
|
822 |
|
|
// TODO(gri): should treat[] like parentheses and undo one level of depth
|
823 |
|
|
p.expr1(x.X, token.HighestPrec, 1, multiLine)
|
824 |
|
|
p.print(x.Lbrack, token.LBRACK)
|
825 |
|
|
if x.Low != nil {
|
826 |
|
|
p.expr0(x.Low, depth+1, multiLine)
|
827 |
|
|
}
|
828 |
|
|
// blanks around ":" if both sides exist and either side is a binary expression
|
829 |
|
|
if depth <= 1 && x.Low != nil && x.High != nil && (isBinary(x.Low) || isBinary(x.High)) {
|
830 |
|
|
p.print(blank, token.COLON, blank)
|
831 |
|
|
} else {
|
832 |
|
|
p.print(token.COLON)
|
833 |
|
|
}
|
834 |
|
|
if x.High != nil {
|
835 |
|
|
p.expr0(x.High, depth+1, multiLine)
|
836 |
|
|
}
|
837 |
|
|
p.print(x.Rbrack, token.RBRACK)
|
838 |
|
|
|
839 |
|
|
case *ast.CallExpr:
|
840 |
|
|
if len(x.Args) > 1 {
|
841 |
|
|
depth++
|
842 |
|
|
}
|
843 |
|
|
p.expr1(x.Fun, token.HighestPrec, depth, multiLine)
|
844 |
|
|
p.print(x.Lparen, token.LPAREN)
|
845 |
|
|
p.exprList(x.Lparen, x.Args, depth, commaSep|commaTerm, multiLine, x.Rparen)
|
846 |
|
|
if x.Ellipsis.IsValid() {
|
847 |
|
|
p.print(x.Ellipsis, token.ELLIPSIS)
|
848 |
|
|
}
|
849 |
|
|
p.print(x.Rparen, token.RPAREN)
|
850 |
|
|
|
851 |
|
|
case *ast.CompositeLit:
|
852 |
|
|
// composite literal elements that are composite literals themselves may have the type omitted
|
853 |
|
|
if x.Type != nil {
|
854 |
|
|
p.expr1(x.Type, token.HighestPrec, depth, multiLine)
|
855 |
|
|
}
|
856 |
|
|
p.print(x.Lbrace, token.LBRACE)
|
857 |
|
|
p.exprList(x.Lbrace, x.Elts, 1, commaSep|commaTerm, multiLine, x.Rbrace)
|
858 |
|
|
// do not insert extra line breaks because of comments before
|
859 |
|
|
// the closing '}' as it might break the code if there is no
|
860 |
|
|
// trailing ','
|
861 |
|
|
p.print(noExtraLinebreak, x.Rbrace, token.RBRACE, noExtraLinebreak)
|
862 |
|
|
|
863 |
|
|
case *ast.Ellipsis:
|
864 |
|
|
p.print(token.ELLIPSIS)
|
865 |
|
|
if x.Elt != nil {
|
866 |
|
|
p.expr(x.Elt, multiLine)
|
867 |
|
|
}
|
868 |
|
|
|
869 |
|
|
case *ast.ArrayType:
|
870 |
|
|
p.print(token.LBRACK)
|
871 |
|
|
if x.Len != nil {
|
872 |
|
|
p.expr(x.Len, multiLine)
|
873 |
|
|
}
|
874 |
|
|
p.print(token.RBRACK)
|
875 |
|
|
p.expr(x.Elt, multiLine)
|
876 |
|
|
|
877 |
|
|
case *ast.StructType:
|
878 |
|
|
p.print(token.STRUCT)
|
879 |
|
|
p.fieldList(x.Fields, true, x.Incomplete)
|
880 |
|
|
|
881 |
|
|
case *ast.FuncType:
|
882 |
|
|
p.print(token.FUNC)
|
883 |
|
|
p.signature(x.Params, x.Results, multiLine)
|
884 |
|
|
|
885 |
|
|
case *ast.InterfaceType:
|
886 |
|
|
p.print(token.INTERFACE)
|
887 |
|
|
p.fieldList(x.Methods, false, x.Incomplete)
|
888 |
|
|
|
889 |
|
|
case *ast.MapType:
|
890 |
|
|
p.print(token.MAP, token.LBRACK)
|
891 |
|
|
p.expr(x.Key, multiLine)
|
892 |
|
|
p.print(token.RBRACK)
|
893 |
|
|
p.expr(x.Value, multiLine)
|
894 |
|
|
|
895 |
|
|
case *ast.ChanType:
|
896 |
|
|
switch x.Dir {
|
897 |
|
|
case ast.SEND | ast.RECV:
|
898 |
|
|
p.print(token.CHAN)
|
899 |
|
|
case ast.RECV:
|
900 |
|
|
p.print(token.ARROW, token.CHAN)
|
901 |
|
|
case ast.SEND:
|
902 |
|
|
p.print(token.CHAN, token.ARROW)
|
903 |
|
|
}
|
904 |
|
|
p.print(blank)
|
905 |
|
|
p.expr(x.Value, multiLine)
|
906 |
|
|
|
907 |
|
|
default:
|
908 |
|
|
panic("unreachable")
|
909 |
|
|
}
|
910 |
|
|
|
911 |
|
|
return
|
912 |
|
|
}
|
913 |
|
|
|
914 |
|
|
func (p *printer) expr0(x ast.Expr, depth int, multiLine *bool) {
|
915 |
|
|
p.expr1(x, token.LowestPrec, depth, multiLine)
|
916 |
|
|
}
|
917 |
|
|
|
918 |
|
|
// Sets multiLine to true if the expression spans multiple lines.
|
919 |
|
|
func (p *printer) expr(x ast.Expr, multiLine *bool) {
|
920 |
|
|
const depth = 1
|
921 |
|
|
p.expr1(x, token.LowestPrec, depth, multiLine)
|
922 |
|
|
}
|
923 |
|
|
|
924 |
|
|
// ----------------------------------------------------------------------------
|
925 |
|
|
// Statements
|
926 |
|
|
|
927 |
|
|
// Print the statement list indented, but without a newline after the last statement.
|
928 |
|
|
// Extra line breaks between statements in the source are respected but at most one
|
929 |
|
|
// empty line is printed between statements.
|
930 |
|
|
func (p *printer) stmtList(list []ast.Stmt, _indent int, nextIsRBrace bool) {
|
931 |
|
|
// TODO(gri): fix _indent code
|
932 |
|
|
if _indent > 0 {
|
933 |
|
|
p.print(indent)
|
934 |
|
|
}
|
935 |
|
|
var multiLine bool
|
936 |
|
|
for i, s := range list {
|
937 |
|
|
// _indent == 0 only for lists of switch/select case clauses;
|
938 |
|
|
// in those cases each clause is a new section
|
939 |
|
|
p.linebreak(p.lineFor(s.Pos()), 1, ignore, i == 0 || _indent == 0 || multiLine)
|
940 |
|
|
multiLine = false
|
941 |
|
|
p.stmt(s, nextIsRBrace && i == len(list)-1, &multiLine)
|
942 |
|
|
}
|
943 |
|
|
if _indent > 0 {
|
944 |
|
|
p.print(unindent)
|
945 |
|
|
}
|
946 |
|
|
}
|
947 |
|
|
|
948 |
|
|
// block prints an *ast.BlockStmt; it always spans at least two lines.
|
949 |
|
|
func (p *printer) block(s *ast.BlockStmt, indent int) {
|
950 |
|
|
p.print(s.Pos(), token.LBRACE)
|
951 |
|
|
p.stmtList(s.List, indent, true)
|
952 |
|
|
p.linebreak(p.lineFor(s.Rbrace), 1, ignore, true)
|
953 |
|
|
p.print(s.Rbrace, token.RBRACE)
|
954 |
|
|
}
|
955 |
|
|
|
956 |
|
|
func isTypeName(x ast.Expr) bool {
|
957 |
|
|
switch t := x.(type) {
|
958 |
|
|
case *ast.Ident:
|
959 |
|
|
return true
|
960 |
|
|
case *ast.SelectorExpr:
|
961 |
|
|
return isTypeName(t.X)
|
962 |
|
|
}
|
963 |
|
|
return false
|
964 |
|
|
}
|
965 |
|
|
|
966 |
|
|
func stripParens(x ast.Expr) ast.Expr {
|
967 |
|
|
if px, strip := x.(*ast.ParenExpr); strip {
|
968 |
|
|
// parentheses must not be stripped if there are any
|
969 |
|
|
// unparenthesized composite literals starting with
|
970 |
|
|
// a type name
|
971 |
|
|
ast.Inspect(px.X, func(node ast.Node) bool {
|
972 |
|
|
switch x := node.(type) {
|
973 |
|
|
case *ast.ParenExpr:
|
974 |
|
|
// parentheses protect enclosed composite literals
|
975 |
|
|
return false
|
976 |
|
|
case *ast.CompositeLit:
|
977 |
|
|
if isTypeName(x.Type) {
|
978 |
|
|
strip = false // do not strip parentheses
|
979 |
|
|
}
|
980 |
|
|
return false
|
981 |
|
|
}
|
982 |
|
|
// in all other cases, keep inspecting
|
983 |
|
|
return true
|
984 |
|
|
})
|
985 |
|
|
if strip {
|
986 |
|
|
return stripParens(px.X)
|
987 |
|
|
}
|
988 |
|
|
}
|
989 |
|
|
return x
|
990 |
|
|
}
|
991 |
|
|
|
992 |
|
|
func (p *printer) controlClause(isForStmt bool, init ast.Stmt, expr ast.Expr, post ast.Stmt) {
|
993 |
|
|
p.print(blank)
|
994 |
|
|
needsBlank := false
|
995 |
|
|
if init == nil && post == nil {
|
996 |
|
|
// no semicolons required
|
997 |
|
|
if expr != nil {
|
998 |
|
|
p.expr(stripParens(expr), ignoreMultiLine)
|
999 |
|
|
needsBlank = true
|
1000 |
|
|
}
|
1001 |
|
|
} else {
|
1002 |
|
|
// all semicolons required
|
1003 |
|
|
// (they are not separators, print them explicitly)
|
1004 |
|
|
if init != nil {
|
1005 |
|
|
p.stmt(init, false, ignoreMultiLine)
|
1006 |
|
|
}
|
1007 |
|
|
p.print(token.SEMICOLON, blank)
|
1008 |
|
|
if expr != nil {
|
1009 |
|
|
p.expr(stripParens(expr), ignoreMultiLine)
|
1010 |
|
|
needsBlank = true
|
1011 |
|
|
}
|
1012 |
|
|
if isForStmt {
|
1013 |
|
|
p.print(token.SEMICOLON, blank)
|
1014 |
|
|
needsBlank = false
|
1015 |
|
|
if post != nil {
|
1016 |
|
|
p.stmt(post, false, ignoreMultiLine)
|
1017 |
|
|
needsBlank = true
|
1018 |
|
|
}
|
1019 |
|
|
}
|
1020 |
|
|
}
|
1021 |
|
|
if needsBlank {
|
1022 |
|
|
p.print(blank)
|
1023 |
|
|
}
|
1024 |
|
|
}
|
1025 |
|
|
|
1026 |
|
|
// Sets multiLine to true if the statements spans multiple lines.
|
1027 |
|
|
func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool, multiLine *bool) {
|
1028 |
|
|
p.print(stmt.Pos())
|
1029 |
|
|
|
1030 |
|
|
switch s := stmt.(type) {
|
1031 |
|
|
case *ast.BadStmt:
|
1032 |
|
|
p.print("BadStmt")
|
1033 |
|
|
|
1034 |
|
|
case *ast.DeclStmt:
|
1035 |
|
|
p.decl(s.Decl, multiLine)
|
1036 |
|
|
|
1037 |
|
|
case *ast.EmptyStmt:
|
1038 |
|
|
// nothing to do
|
1039 |
|
|
|
1040 |
|
|
case *ast.LabeledStmt:
|
1041 |
|
|
// a "correcting" unindent immediately following a line break
|
1042 |
|
|
// is applied before the line break if there is no comment
|
1043 |
|
|
// between (see writeWhitespace)
|
1044 |
|
|
p.print(unindent)
|
1045 |
|
|
p.expr(s.Label, multiLine)
|
1046 |
|
|
p.print(s.Colon, token.COLON, indent)
|
1047 |
|
|
if e, isEmpty := s.Stmt.(*ast.EmptyStmt); isEmpty {
|
1048 |
|
|
if !nextIsRBrace {
|
1049 |
|
|
p.print(newline, e.Pos(), token.SEMICOLON)
|
1050 |
|
|
break
|
1051 |
|
|
}
|
1052 |
|
|
} else {
|
1053 |
|
|
p.linebreak(p.lineFor(s.Stmt.Pos()), 1, ignore, true)
|
1054 |
|
|
}
|
1055 |
|
|
p.stmt(s.Stmt, nextIsRBrace, multiLine)
|
1056 |
|
|
|
1057 |
|
|
case *ast.ExprStmt:
|
1058 |
|
|
const depth = 1
|
1059 |
|
|
p.expr0(s.X, depth, multiLine)
|
1060 |
|
|
|
1061 |
|
|
case *ast.SendStmt:
|
1062 |
|
|
const depth = 1
|
1063 |
|
|
p.expr0(s.Chan, depth, multiLine)
|
1064 |
|
|
p.print(blank, s.Arrow, token.ARROW, blank)
|
1065 |
|
|
p.expr0(s.Value, depth, multiLine)
|
1066 |
|
|
|
1067 |
|
|
case *ast.IncDecStmt:
|
1068 |
|
|
const depth = 1
|
1069 |
|
|
p.expr0(s.X, depth+1, multiLine)
|
1070 |
|
|
p.print(s.TokPos, s.Tok)
|
1071 |
|
|
|
1072 |
|
|
case *ast.AssignStmt:
|
1073 |
|
|
var depth = 1
|
1074 |
|
|
if len(s.Lhs) > 1 && len(s.Rhs) > 1 {
|
1075 |
|
|
depth++
|
1076 |
|
|
}
|
1077 |
|
|
p.exprList(s.Pos(), s.Lhs, depth, commaSep, multiLine, s.TokPos)
|
1078 |
|
|
p.print(blank, s.TokPos, s.Tok)
|
1079 |
|
|
p.exprList(s.TokPos, s.Rhs, depth, blankStart|commaSep, multiLine, token.NoPos)
|
1080 |
|
|
|
1081 |
|
|
case *ast.GoStmt:
|
1082 |
|
|
p.print(token.GO, blank)
|
1083 |
|
|
p.expr(s.Call, multiLine)
|
1084 |
|
|
|
1085 |
|
|
case *ast.DeferStmt:
|
1086 |
|
|
p.print(token.DEFER, blank)
|
1087 |
|
|
p.expr(s.Call, multiLine)
|
1088 |
|
|
|
1089 |
|
|
case *ast.ReturnStmt:
|
1090 |
|
|
p.print(token.RETURN)
|
1091 |
|
|
if s.Results != nil {
|
1092 |
|
|
p.exprList(s.Pos(), s.Results, 1, blankStart|commaSep, multiLine, token.NoPos)
|
1093 |
|
|
}
|
1094 |
|
|
|
1095 |
|
|
case *ast.BranchStmt:
|
1096 |
|
|
p.print(s.Tok)
|
1097 |
|
|
if s.Label != nil {
|
1098 |
|
|
p.print(blank)
|
1099 |
|
|
p.expr(s.Label, multiLine)
|
1100 |
|
|
}
|
1101 |
|
|
|
1102 |
|
|
case *ast.BlockStmt:
|
1103 |
|
|
p.block(s, 1)
|
1104 |
|
|
*multiLine = true
|
1105 |
|
|
|
1106 |
|
|
case *ast.IfStmt:
|
1107 |
|
|
p.print(token.IF)
|
1108 |
|
|
p.controlClause(false, s.Init, s.Cond, nil)
|
1109 |
|
|
p.block(s.Body, 1)
|
1110 |
|
|
*multiLine = true
|
1111 |
|
|
if s.Else != nil {
|
1112 |
|
|
p.print(blank, token.ELSE, blank)
|
1113 |
|
|
switch s.Else.(type) {
|
1114 |
|
|
case *ast.BlockStmt, *ast.IfStmt:
|
1115 |
|
|
p.stmt(s.Else, nextIsRBrace, ignoreMultiLine)
|
1116 |
|
|
default:
|
1117 |
|
|
p.print(token.LBRACE, indent, formfeed)
|
1118 |
|
|
p.stmt(s.Else, true, ignoreMultiLine)
|
1119 |
|
|
p.print(unindent, formfeed, token.RBRACE)
|
1120 |
|
|
}
|
1121 |
|
|
}
|
1122 |
|
|
|
1123 |
|
|
case *ast.CaseClause:
|
1124 |
|
|
if s.List != nil {
|
1125 |
|
|
p.print(token.CASE)
|
1126 |
|
|
p.exprList(s.Pos(), s.List, 1, blankStart|commaSep, multiLine, s.Colon)
|
1127 |
|
|
} else {
|
1128 |
|
|
p.print(token.DEFAULT)
|
1129 |
|
|
}
|
1130 |
|
|
p.print(s.Colon, token.COLON)
|
1131 |
|
|
p.stmtList(s.Body, 1, nextIsRBrace)
|
1132 |
|
|
|
1133 |
|
|
case *ast.SwitchStmt:
|
1134 |
|
|
p.print(token.SWITCH)
|
1135 |
|
|
p.controlClause(false, s.Init, s.Tag, nil)
|
1136 |
|
|
p.block(s.Body, 0)
|
1137 |
|
|
*multiLine = true
|
1138 |
|
|
|
1139 |
|
|
case *ast.TypeSwitchStmt:
|
1140 |
|
|
p.print(token.SWITCH)
|
1141 |
|
|
if s.Init != nil {
|
1142 |
|
|
p.print(blank)
|
1143 |
|
|
p.stmt(s.Init, false, ignoreMultiLine)
|
1144 |
|
|
p.print(token.SEMICOLON)
|
1145 |
|
|
}
|
1146 |
|
|
p.print(blank)
|
1147 |
|
|
p.stmt(s.Assign, false, ignoreMultiLine)
|
1148 |
|
|
p.print(blank)
|
1149 |
|
|
p.block(s.Body, 0)
|
1150 |
|
|
*multiLine = true
|
1151 |
|
|
|
1152 |
|
|
case *ast.CommClause:
|
1153 |
|
|
if s.Comm != nil {
|
1154 |
|
|
p.print(token.CASE, blank)
|
1155 |
|
|
p.stmt(s.Comm, false, ignoreMultiLine)
|
1156 |
|
|
} else {
|
1157 |
|
|
p.print(token.DEFAULT)
|
1158 |
|
|
}
|
1159 |
|
|
p.print(s.Colon, token.COLON)
|
1160 |
|
|
p.stmtList(s.Body, 1, nextIsRBrace)
|
1161 |
|
|
|
1162 |
|
|
case *ast.SelectStmt:
|
1163 |
|
|
p.print(token.SELECT, blank)
|
1164 |
|
|
body := s.Body
|
1165 |
|
|
if len(body.List) == 0 && !p.commentBefore(p.posFor(body.Rbrace)) {
|
1166 |
|
|
// print empty select statement w/o comments on one line
|
1167 |
|
|
p.print(body.Lbrace, token.LBRACE, body.Rbrace, token.RBRACE)
|
1168 |
|
|
} else {
|
1169 |
|
|
p.block(body, 0)
|
1170 |
|
|
*multiLine = true
|
1171 |
|
|
}
|
1172 |
|
|
|
1173 |
|
|
case *ast.ForStmt:
|
1174 |
|
|
p.print(token.FOR)
|
1175 |
|
|
p.controlClause(true, s.Init, s.Cond, s.Post)
|
1176 |
|
|
p.block(s.Body, 1)
|
1177 |
|
|
*multiLine = true
|
1178 |
|
|
|
1179 |
|
|
case *ast.RangeStmt:
|
1180 |
|
|
p.print(token.FOR, blank)
|
1181 |
|
|
p.expr(s.Key, multiLine)
|
1182 |
|
|
if s.Value != nil {
|
1183 |
|
|
p.print(token.COMMA, blank)
|
1184 |
|
|
p.expr(s.Value, multiLine)
|
1185 |
|
|
}
|
1186 |
|
|
p.print(blank, s.TokPos, s.Tok, blank, token.RANGE, blank)
|
1187 |
|
|
p.expr(stripParens(s.X), multiLine)
|
1188 |
|
|
p.print(blank)
|
1189 |
|
|
p.block(s.Body, 1)
|
1190 |
|
|
*multiLine = true
|
1191 |
|
|
|
1192 |
|
|
default:
|
1193 |
|
|
panic("unreachable")
|
1194 |
|
|
}
|
1195 |
|
|
|
1196 |
|
|
return
|
1197 |
|
|
}
|
1198 |
|
|
|
1199 |
|
|
// ----------------------------------------------------------------------------
|
1200 |
|
|
// Declarations
|
1201 |
|
|
|
1202 |
|
|
// The keepTypeColumn function determines if the type column of a series of
|
1203 |
|
|
// consecutive const or var declarations must be kept, or if initialization
|
1204 |
|
|
// values (V) can be placed in the type column (T) instead. The i'th entry
|
1205 |
|
|
// in the result slice is true if the type column in spec[i] must be kept.
|
1206 |
|
|
//
|
1207 |
|
|
// For example, the declaration:
|
1208 |
|
|
//
|
1209 |
|
|
// const (
|
1210 |
|
|
// foobar int = 42 // comment
|
1211 |
|
|
// x = 7 // comment
|
1212 |
|
|
// foo
|
1213 |
|
|
// bar = 991
|
1214 |
|
|
// )
|
1215 |
|
|
//
|
1216 |
|
|
// leads to the type/values matrix below. A run of value columns (V) can
|
1217 |
|
|
// be moved into the type column if there is no type for any of the values
|
1218 |
|
|
// in that column (we only move entire columns so that they align properly).
|
1219 |
|
|
//
|
1220 |
|
|
// matrix formatted result
|
1221 |
|
|
// matrix
|
1222 |
|
|
// T V -> T V -> true there is a T and so the type
|
1223 |
|
|
// - V - V true column must be kept
|
1224 |
|
|
// - - - - false
|
1225 |
|
|
// - V V - false V is moved into T column
|
1226 |
|
|
//
|
1227 |
|
|
func keepTypeColumn(specs []ast.Spec) []bool {
|
1228 |
|
|
m := make([]bool, len(specs))
|
1229 |
|
|
|
1230 |
|
|
populate := func(i, j int, keepType bool) {
|
1231 |
|
|
if keepType {
|
1232 |
|
|
for ; i < j; i++ {
|
1233 |
|
|
m[i] = true
|
1234 |
|
|
}
|
1235 |
|
|
}
|
1236 |
|
|
}
|
1237 |
|
|
|
1238 |
|
|
i0 := -1 // if i0 >= 0 we are in a run and i0 is the start of the run
|
1239 |
|
|
var keepType bool
|
1240 |
|
|
for i, s := range specs {
|
1241 |
|
|
t := s.(*ast.ValueSpec)
|
1242 |
|
|
if t.Values != nil {
|
1243 |
|
|
if i0 < 0 {
|
1244 |
|
|
// start of a run of ValueSpecs with non-nil Values
|
1245 |
|
|
i0 = i
|
1246 |
|
|
keepType = false
|
1247 |
|
|
}
|
1248 |
|
|
} else {
|
1249 |
|
|
if i0 >= 0 {
|
1250 |
|
|
// end of a run
|
1251 |
|
|
populate(i0, i, keepType)
|
1252 |
|
|
i0 = -1
|
1253 |
|
|
}
|
1254 |
|
|
}
|
1255 |
|
|
if t.Type != nil {
|
1256 |
|
|
keepType = true
|
1257 |
|
|
}
|
1258 |
|
|
}
|
1259 |
|
|
if i0 >= 0 {
|
1260 |
|
|
// end of a run
|
1261 |
|
|
populate(i0, len(specs), keepType)
|
1262 |
|
|
}
|
1263 |
|
|
|
1264 |
|
|
return m
|
1265 |
|
|
}
|
1266 |
|
|
|
1267 |
|
|
func (p *printer) valueSpec(s *ast.ValueSpec, keepType, doIndent bool, multiLine *bool) {
|
1268 |
|
|
p.setComment(s.Doc)
|
1269 |
|
|
p.identList(s.Names, doIndent, multiLine) // always present
|
1270 |
|
|
extraTabs := 3
|
1271 |
|
|
if s.Type != nil || keepType {
|
1272 |
|
|
p.print(vtab)
|
1273 |
|
|
extraTabs--
|
1274 |
|
|
}
|
1275 |
|
|
if s.Type != nil {
|
1276 |
|
|
p.expr(s.Type, multiLine)
|
1277 |
|
|
}
|
1278 |
|
|
if s.Values != nil {
|
1279 |
|
|
p.print(vtab, token.ASSIGN)
|
1280 |
|
|
p.exprList(token.NoPos, s.Values, 1, blankStart|commaSep, multiLine, token.NoPos)
|
1281 |
|
|
extraTabs--
|
1282 |
|
|
}
|
1283 |
|
|
if s.Comment != nil {
|
1284 |
|
|
for ; extraTabs > 0; extraTabs-- {
|
1285 |
|
|
p.print(vtab)
|
1286 |
|
|
}
|
1287 |
|
|
p.setComment(s.Comment)
|
1288 |
|
|
}
|
1289 |
|
|
}
|
1290 |
|
|
|
1291 |
|
|
// The parameter n is the number of specs in the group. If doIndent is set,
|
1292 |
|
|
// multi-line identifier lists in the spec are indented when the first
|
1293 |
|
|
// linebreak is encountered.
|
1294 |
|
|
// Sets multiLine to true if the spec spans multiple lines.
|
1295 |
|
|
//
|
1296 |
|
|
func (p *printer) spec(spec ast.Spec, n int, doIndent bool, multiLine *bool) {
|
1297 |
|
|
switch s := spec.(type) {
|
1298 |
|
|
case *ast.ImportSpec:
|
1299 |
|
|
p.setComment(s.Doc)
|
1300 |
|
|
if s.Name != nil {
|
1301 |
|
|
p.expr(s.Name, multiLine)
|
1302 |
|
|
p.print(blank)
|
1303 |
|
|
}
|
1304 |
|
|
p.expr(s.Path, multiLine)
|
1305 |
|
|
p.setComment(s.Comment)
|
1306 |
|
|
p.print(s.EndPos)
|
1307 |
|
|
|
1308 |
|
|
case *ast.ValueSpec:
|
1309 |
|
|
if n != 1 {
|
1310 |
|
|
p.internalError("expected n = 1; got", n)
|
1311 |
|
|
}
|
1312 |
|
|
p.setComment(s.Doc)
|
1313 |
|
|
p.identList(s.Names, doIndent, multiLine) // always present
|
1314 |
|
|
if s.Type != nil {
|
1315 |
|
|
p.print(blank)
|
1316 |
|
|
p.expr(s.Type, multiLine)
|
1317 |
|
|
}
|
1318 |
|
|
if s.Values != nil {
|
1319 |
|
|
p.print(blank, token.ASSIGN)
|
1320 |
|
|
p.exprList(token.NoPos, s.Values, 1, blankStart|commaSep, multiLine, token.NoPos)
|
1321 |
|
|
}
|
1322 |
|
|
p.setComment(s.Comment)
|
1323 |
|
|
|
1324 |
|
|
case *ast.TypeSpec:
|
1325 |
|
|
p.setComment(s.Doc)
|
1326 |
|
|
p.expr(s.Name, multiLine)
|
1327 |
|
|
if n == 1 {
|
1328 |
|
|
p.print(blank)
|
1329 |
|
|
} else {
|
1330 |
|
|
p.print(vtab)
|
1331 |
|
|
}
|
1332 |
|
|
p.expr(s.Type, multiLine)
|
1333 |
|
|
p.setComment(s.Comment)
|
1334 |
|
|
|
1335 |
|
|
default:
|
1336 |
|
|
panic("unreachable")
|
1337 |
|
|
}
|
1338 |
|
|
}
|
1339 |
|
|
|
1340 |
|
|
// Sets multiLine to true if the declaration spans multiple lines.
|
1341 |
|
|
func (p *printer) genDecl(d *ast.GenDecl, multiLine *bool) {
|
1342 |
|
|
p.setComment(d.Doc)
|
1343 |
|
|
p.print(d.Pos(), d.Tok, blank)
|
1344 |
|
|
|
1345 |
|
|
if d.Lparen.IsValid() {
|
1346 |
|
|
// group of parenthesized declarations
|
1347 |
|
|
p.print(d.Lparen, token.LPAREN)
|
1348 |
|
|
if n := len(d.Specs); n > 0 {
|
1349 |
|
|
p.print(indent, formfeed)
|
1350 |
|
|
if n > 1 && (d.Tok == token.CONST || d.Tok == token.VAR) {
|
1351 |
|
|
// two or more grouped const/var declarations:
|
1352 |
|
|
// determine if the type column must be kept
|
1353 |
|
|
keepType := keepTypeColumn(d.Specs)
|
1354 |
|
|
var ml bool
|
1355 |
|
|
for i, s := range d.Specs {
|
1356 |
|
|
if i > 0 {
|
1357 |
|
|
p.linebreak(p.lineFor(s.Pos()), 1, ignore, ml)
|
1358 |
|
|
}
|
1359 |
|
|
ml = false
|
1360 |
|
|
p.valueSpec(s.(*ast.ValueSpec), keepType[i], false, &ml)
|
1361 |
|
|
}
|
1362 |
|
|
} else {
|
1363 |
|
|
var ml bool
|
1364 |
|
|
for i, s := range d.Specs {
|
1365 |
|
|
if i > 0 {
|
1366 |
|
|
p.linebreak(p.lineFor(s.Pos()), 1, ignore, ml)
|
1367 |
|
|
}
|
1368 |
|
|
ml = false
|
1369 |
|
|
p.spec(s, n, false, &ml)
|
1370 |
|
|
}
|
1371 |
|
|
}
|
1372 |
|
|
p.print(unindent, formfeed)
|
1373 |
|
|
*multiLine = true
|
1374 |
|
|
}
|
1375 |
|
|
p.print(d.Rparen, token.RPAREN)
|
1376 |
|
|
|
1377 |
|
|
} else {
|
1378 |
|
|
// single declaration
|
1379 |
|
|
p.spec(d.Specs[0], 1, true, multiLine)
|
1380 |
|
|
}
|
1381 |
|
|
}
|
1382 |
|
|
|
1383 |
|
|
// nodeSize determines the size of n in chars after formatting.
|
1384 |
|
|
// The result is <= maxSize if the node fits on one line with at
|
1385 |
|
|
// most maxSize chars and the formatted output doesn't contain
|
1386 |
|
|
// any control chars. Otherwise, the result is > maxSize.
|
1387 |
|
|
//
|
1388 |
|
|
func (p *printer) nodeSize(n ast.Node, maxSize int) (size int) {
|
1389 |
|
|
// nodeSize invokes the printer, which may invoke nodeSize
|
1390 |
|
|
// recursively. For deep composite literal nests, this can
|
1391 |
|
|
// lead to an exponential algorithm. Remember previous
|
1392 |
|
|
// results to prune the recursion (was issue 1628).
|
1393 |
|
|
if size, found := p.nodeSizes[n]; found {
|
1394 |
|
|
return size
|
1395 |
|
|
}
|
1396 |
|
|
|
1397 |
|
|
size = maxSize + 1 // assume n doesn't fit
|
1398 |
|
|
p.nodeSizes[n] = size
|
1399 |
|
|
|
1400 |
|
|
// nodeSize computation must be independent of particular
|
1401 |
|
|
// style so that we always get the same decision; print
|
1402 |
|
|
// in RawFormat
|
1403 |
|
|
cfg := Config{Mode: RawFormat}
|
1404 |
|
|
var buf bytes.Buffer
|
1405 |
|
|
if err := cfg.fprint(&buf, p.fset, n, p.nodeSizes); err != nil {
|
1406 |
|
|
return
|
1407 |
|
|
}
|
1408 |
|
|
if buf.Len() <= maxSize {
|
1409 |
|
|
for _, ch := range buf.Bytes() {
|
1410 |
|
|
if ch < ' ' {
|
1411 |
|
|
return
|
1412 |
|
|
}
|
1413 |
|
|
}
|
1414 |
|
|
size = buf.Len() // n fits
|
1415 |
|
|
p.nodeSizes[n] = size
|
1416 |
|
|
}
|
1417 |
|
|
return
|
1418 |
|
|
}
|
1419 |
|
|
|
1420 |
|
|
func (p *printer) isOneLineFunc(b *ast.BlockStmt, headerSize int) bool {
|
1421 |
|
|
pos1 := b.Pos()
|
1422 |
|
|
pos2 := b.Rbrace
|
1423 |
|
|
if pos1.IsValid() && pos2.IsValid() && p.lineFor(pos1) != p.lineFor(pos2) {
|
1424 |
|
|
// opening and closing brace are on different lines - don't make it a one-liner
|
1425 |
|
|
return false
|
1426 |
|
|
}
|
1427 |
|
|
if len(b.List) > 5 || p.commentBefore(p.posFor(pos2)) {
|
1428 |
|
|
// too many statements or there is a comment inside - don't make it a one-liner
|
1429 |
|
|
return false
|
1430 |
|
|
}
|
1431 |
|
|
// otherwise, estimate body size
|
1432 |
|
|
const maxSize = 100
|
1433 |
|
|
bodySize := 0
|
1434 |
|
|
for i, s := range b.List {
|
1435 |
|
|
if i > 0 {
|
1436 |
|
|
bodySize += 2 // space for a semicolon and blank
|
1437 |
|
|
}
|
1438 |
|
|
bodySize += p.nodeSize(s, maxSize)
|
1439 |
|
|
}
|
1440 |
|
|
return headerSize+bodySize <= maxSize
|
1441 |
|
|
}
|
1442 |
|
|
|
1443 |
|
|
// Sets multiLine to true if the function body spans multiple lines.
|
1444 |
|
|
func (p *printer) funcBody(b *ast.BlockStmt, headerSize int, isLit bool, multiLine *bool) {
|
1445 |
|
|
if b == nil {
|
1446 |
|
|
return
|
1447 |
|
|
}
|
1448 |
|
|
|
1449 |
|
|
if p.isOneLineFunc(b, headerSize) {
|
1450 |
|
|
sep := vtab
|
1451 |
|
|
if isLit {
|
1452 |
|
|
sep = blank
|
1453 |
|
|
}
|
1454 |
|
|
p.print(sep, b.Lbrace, token.LBRACE)
|
1455 |
|
|
if len(b.List) > 0 {
|
1456 |
|
|
p.print(blank)
|
1457 |
|
|
for i, s := range b.List {
|
1458 |
|
|
if i > 0 {
|
1459 |
|
|
p.print(token.SEMICOLON, blank)
|
1460 |
|
|
}
|
1461 |
|
|
p.stmt(s, i == len(b.List)-1, ignoreMultiLine)
|
1462 |
|
|
}
|
1463 |
|
|
p.print(blank)
|
1464 |
|
|
}
|
1465 |
|
|
p.print(b.Rbrace, token.RBRACE)
|
1466 |
|
|
return
|
1467 |
|
|
}
|
1468 |
|
|
|
1469 |
|
|
p.print(blank)
|
1470 |
|
|
p.block(b, 1)
|
1471 |
|
|
*multiLine = true
|
1472 |
|
|
}
|
1473 |
|
|
|
1474 |
|
|
// distance returns the column difference between from and to if both
|
1475 |
|
|
// are on the same line; if they are on different lines (or unknown)
|
1476 |
|
|
// the result is infinity.
|
1477 |
|
|
func (p *printer) distance(from0 token.Pos, to token.Position) int {
|
1478 |
|
|
from := p.posFor(from0)
|
1479 |
|
|
if from.IsValid() && to.IsValid() && from.Line == to.Line {
|
1480 |
|
|
return to.Column - from.Column
|
1481 |
|
|
}
|
1482 |
|
|
return infinity
|
1483 |
|
|
}
|
1484 |
|
|
|
1485 |
|
|
// Sets multiLine to true if the declaration spans multiple lines.
|
1486 |
|
|
func (p *printer) funcDecl(d *ast.FuncDecl, multiLine *bool) {
|
1487 |
|
|
p.setComment(d.Doc)
|
1488 |
|
|
p.print(d.Pos(), token.FUNC, blank)
|
1489 |
|
|
if d.Recv != nil {
|
1490 |
|
|
p.parameters(d.Recv, multiLine) // method: print receiver
|
1491 |
|
|
p.print(blank)
|
1492 |
|
|
}
|
1493 |
|
|
p.expr(d.Name, multiLine)
|
1494 |
|
|
p.signature(d.Type.Params, d.Type.Results, multiLine)
|
1495 |
|
|
p.funcBody(d.Body, p.distance(d.Pos(), p.pos), false, multiLine)
|
1496 |
|
|
}
|
1497 |
|
|
|
1498 |
|
|
// Sets multiLine to true if the declaration spans multiple lines.
|
1499 |
|
|
func (p *printer) decl(decl ast.Decl, multiLine *bool) {
|
1500 |
|
|
switch d := decl.(type) {
|
1501 |
|
|
case *ast.BadDecl:
|
1502 |
|
|
p.print(d.Pos(), "BadDecl")
|
1503 |
|
|
case *ast.GenDecl:
|
1504 |
|
|
p.genDecl(d, multiLine)
|
1505 |
|
|
case *ast.FuncDecl:
|
1506 |
|
|
p.funcDecl(d, multiLine)
|
1507 |
|
|
default:
|
1508 |
|
|
panic("unreachable")
|
1509 |
|
|
}
|
1510 |
|
|
}
|
1511 |
|
|
|
1512 |
|
|
// ----------------------------------------------------------------------------
|
1513 |
|
|
// Files
|
1514 |
|
|
|
1515 |
|
|
func declToken(decl ast.Decl) (tok token.Token) {
|
1516 |
|
|
tok = token.ILLEGAL
|
1517 |
|
|
switch d := decl.(type) {
|
1518 |
|
|
case *ast.GenDecl:
|
1519 |
|
|
tok = d.Tok
|
1520 |
|
|
case *ast.FuncDecl:
|
1521 |
|
|
tok = token.FUNC
|
1522 |
|
|
}
|
1523 |
|
|
return
|
1524 |
|
|
}
|
1525 |
|
|
|
1526 |
|
|
func (p *printer) file(src *ast.File) {
|
1527 |
|
|
p.setComment(src.Doc)
|
1528 |
|
|
p.print(src.Pos(), token.PACKAGE, blank)
|
1529 |
|
|
p.expr(src.Name, ignoreMultiLine)
|
1530 |
|
|
|
1531 |
|
|
if len(src.Decls) > 0 {
|
1532 |
|
|
tok := token.ILLEGAL
|
1533 |
|
|
for _, d := range src.Decls {
|
1534 |
|
|
prev := tok
|
1535 |
|
|
tok = declToken(d)
|
1536 |
|
|
// if the declaration token changed (e.g., from CONST to TYPE)
|
1537 |
|
|
// or the next declaration has documentation associated with it,
|
1538 |
|
|
// print an empty line between top-level declarations
|
1539 |
|
|
// (because p.linebreak is called with the position of d, which
|
1540 |
|
|
// is past any documentation, the minimum requirement is satisfied
|
1541 |
|
|
// even w/o the extra getDoc(d) nil-check - leave it in case the
|
1542 |
|
|
// linebreak logic improves - there's already a TODO).
|
1543 |
|
|
min := 1
|
1544 |
|
|
if prev != tok || getDoc(d) != nil {
|
1545 |
|
|
min = 2
|
1546 |
|
|
}
|
1547 |
|
|
p.linebreak(p.lineFor(d.Pos()), min, ignore, false)
|
1548 |
|
|
p.decl(d, ignoreMultiLine)
|
1549 |
|
|
}
|
1550 |
|
|
}
|
1551 |
|
|
|
1552 |
|
|
p.print(newline)
|
1553 |
|
|
}
|