| 1 | // 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 | // Package printer implements printing of AST nodes. |
| 6 | package printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/ |
| 7 | |
| 8 | import ( |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.20 fmt/" |
| 10 | "go/astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/" |
| 11 | "go/build/constraintscip-go gomod github.com/golang/go/src go1.20 `go/build/constraint`/" |
| 12 | "go/tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/" |
| 13 | "ioscip-go gomod github.com/golang/go/src go1.20 io/" |
| 14 | "osscip-go gomod github.com/golang/go/src go1.20 os/" |
| 15 | "stringsscip-go gomod github.com/golang/go/src go1.20 strings/" |
| 16 | "syncscip-go gomod github.com/golang/go/src go1.20 sync/" |
| 17 | "text/tabwriterscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/" |
| 18 | "unicodescip-go gomod github.com/golang/go/src go1.20 unicode/" |
| 19 | ) |
| 20 | |
| 21 | const ( |
| 22 | maxNewlinesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/maxNewlines. = 2 // max. number of newlines between source text |
| 23 | debugscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/debug. = false // enable for debugging |
| 24 | infinityscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/infinity. = 1 << 30 |
| 25 | ) |
| 26 | |
| 27 | type whiteSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/whiteSpace# byte |
| 28 | |
| 29 | const ( |
| 30 | ignorescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/ignore. = whiteSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/whiteSpace#(0) |
| 31 | blankscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/blank. = whiteSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/whiteSpace#(' ') |
| 32 | vtabscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/vtab. = whiteSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/whiteSpace#('\v') |
| 33 | newlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/newline. = whiteSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/whiteSpace#('\n') |
| 34 | formfeedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/formfeed. = whiteSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/whiteSpace#('\f') |
| 35 | indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/indent. = whiteSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/whiteSpace#('>') |
| 36 | unindentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/unindent. = whiteSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/whiteSpace#('<') |
| 37 | ) |
| 38 | |
| 39 | // A pmode value represents the current printer mode. |
| 40 | type pmodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/pmode# int |
| 41 | |
| 42 | const ( |
| 43 | noExtraBlankscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/noExtraBlank. pmodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/pmode# = 1 << iota // disables extra blank after /*-style comment |
| 44 | noExtraLinebreakscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/noExtraLinebreak. // disables extra line break after /*-style comment |
| 45 | ) |
| 46 | |
| 47 | type commentInfoscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo# struct { |
| 48 | cindexscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#cindex. int // current comment index |
| 49 | commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#comment. *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.CommentGroupscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup# // = printer.comments[cindex]; or nil |
| 50 | commentOffsetscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#commentOffset. int // = printer.posFor(printer.comments[cindex].List[0].Pos()).Offset; or infinity |
| 51 | commentNewlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#commentNewline. bool // true if the comment group contains newlines |
| 52 | } |
| 53 | |
| 54 | type printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer# struct { |
| 55 | // Configuration (does not change after initialization) |
| 56 | Configscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#Config. |
| 57 | fsetscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#fset. *tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.20 `go/token`/FileSet# |
| 58 | |
| 59 | // Current state |
| 60 | outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output. []byte // raw printer result |
| 61 | indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent. int // current indentation |
| 62 | levelscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#level. int // level == 0: outside composite literal; level > 0: inside composite literal |
| 63 | modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#mode. pmodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/pmode# // current printer mode |
| 64 | endAlignmentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#endAlignment. bool // if set, terminate alignment immediately |
| 65 | impliedSemiscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#impliedSemi. bool // if set, a linebreak implies a semicolon |
| 66 | lastTokscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lastTok. tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/Token# // last token printed (token.ILLEGAL if it's whitespace) |
| 67 | prevOpenscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#prevOpen. tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/Token# // previous non-brace "open" token (, [, or token.ILLEGAL |
| 68 | wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf. []whiteSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/whiteSpace# // delayed white space |
| 69 | goBuildscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#goBuild. []int // start index of all //go:build comments in output |
| 70 | plusBuildscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#plusBuild. []int // start index of all // +build comments in output |
| 71 | |
| 72 | // Positions |
| 73 | // The out position differs from the pos position when the result |
| 74 | // formatting differs from the source formatting (in the amount of |
| 75 | // white space). If there's a difference and SourcePos is set in |
| 76 | // ConfigMode, //line directives are used in the output to restore |
| 77 | // original source positions for a reader. |
| 78 | posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos. tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position# // current position in AST (source) space |
| 79 | outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out. tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position# // current position in output space |
| 80 | lastscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#last. tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position# // value of pos after calling writeString |
| 81 | linePtrscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#linePtr. *int // if set, record out.Line for the next token in *linePtr |
| 82 | sourcePosErrscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#sourcePosErr. error // if non-nil, the first error emitting a //line directive |
| 83 | |
| 84 | // The list of all source comments, in order of appearance. |
| 85 | commentsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#comments. []*astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.CommentGroupscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup# // may be nil |
| 86 | useNodeCommentsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#useNodeComments. bool // if not set, ignore lead and line comments of nodes |
| 87 | |
| 88 | // Information about p.comments[p.cindex]; set up by nextComment. |
| 89 | commentInfoscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#commentInfo. |
| 90 | |
| 91 | // Cache of already computed node sizes. |
| 92 | nodeSizesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#nodeSizes. map[astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.20 `go/ast`/Node#]int |
| 93 | |
| 94 | // Cache of most recently computed line position. |
| 95 | cachedPosscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#cachedPos. tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.20 `go/token`/Pos# |
| 96 | cachedLinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#cachedLine. int // line corresponding to cachedPos |
| 97 | } |
| 98 | |
| 99 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) internalErrorscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#internalError().(msg ...any) { |
| 100 | if debugscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/debug. { |
| 101 | fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Printscip-go gomod github.com/golang/go/src go1.20 fmt/Print().(p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos..Stringscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#String().() + ": ") |
| 102 | fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.20 fmt/Println().(msg...) |
| 103 | panic("mvdan.cc/gofumpt/internal/govendor/go/printer") |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // commentsHaveNewline reports whether a list of comments belonging to |
| 108 | // an *ast.CommentGroup contains newlines. Because the position information |
| 109 | // may only be partially correct, we also have to read the comment text. |
| 110 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) commentsHaveNewlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#commentsHaveNewline().(list []*astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#) bool { |
| 111 | // len(list) > 0 |
| 112 | line := p.lineForscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lineFor().(list[0].Posscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Pos().()) |
| 113 | for i, c := range list { |
| 114 | if i > 0 && p.lineForscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lineFor().(list[i].Posscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Pos().()) != line { |
| 115 | // not all comments on the same line |
| 116 | return true |
| 117 | } |
| 118 | if t := c.Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text.; len(t) >= 2 && (t[1] == '/' || stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Containsscip-go gomod github.com/golang/go/src go1.20 strings/Contains().(t, "\n")) { |
| 119 | return true |
| 120 | } |
| 121 | } |
| 122 | _ = line |
| 123 | return false |
| 124 | } |
| 125 | |
| 126 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) nextCommentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#nextComment().() { |
| 127 | for p.cindexscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#cindex. < len(p.commentsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#comments.) { |
| 128 | c := p.commentsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#comments.[p.cindexscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#cindex.] |
| 129 | p.cindexscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#cindex.++ |
| 130 | if list := c.Listscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#List.; len(list) > 0 { |
| 131 | p.commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#comment. = c |
| 132 | p.commentOffsetscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#commentOffset. = p.posForscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#posFor().(list[0].Posscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Pos().()).Offsetscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Offset. |
| 133 | p.commentNewlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#commentNewline. = p.commentsHaveNewlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#commentsHaveNewline().(list) |
| 134 | return |
| 135 | } |
| 136 | // we should not reach here (correct ASTs don't have empty |
| 137 | // ast.CommentGroup nodes), but be conservative and try again |
| 138 | } |
| 139 | // no more comments |
| 140 | p.commentOffsetscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#commentOffset. = infinityscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/infinity. |
| 141 | } |
| 142 | |
| 143 | // commentBefore reports whether the current comment group occurs |
| 144 | // before the next position in the source code and printing it does |
| 145 | // not introduce implicit semicolons. |
| 146 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) commentBeforescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#commentBefore().(next tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#) bool { |
| 147 | return p.commentOffsetscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#commentOffset. < next.Offsetscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Offset. && (!p.impliedSemiscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#impliedSemi. || !p.commentNewlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#commentNewline.) |
| 148 | } |
| 149 | |
| 150 | // commentSizeBefore returns the estimated size of the |
| 151 | // comments on the same line before the next position. |
| 152 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) commentSizeBeforescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#commentSizeBefore().(next tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#) int { |
| 153 | // save/restore current p.commentInfo (p.nextComment() modifies it) |
| 154 | defer func(info commentInfoscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#) { |
| 155 | p.commentInfoscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#commentInfo. = info |
| 156 | }(p.commentInfoscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#commentInfo.) |
| 157 | |
| 158 | size := 0 |
| 159 | for p.commentBeforescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#commentBefore().(next) { |
| 160 | for _, c := range p.commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#comment..Listscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#List. { |
| 161 | size += len(c.Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text.) |
| 162 | } |
| 163 | p.nextCommentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#nextComment().() |
| 164 | } |
| 165 | return size |
| 166 | } |
| 167 | |
| 168 | // recordLine records the output line number for the next non-whitespace |
| 169 | // token in *linePtr. It is used to compute an accurate line number for a |
| 170 | // formatted construct, independent of pending (not yet emitted) whitespace |
| 171 | // or comments. |
| 172 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) recordLinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#recordLine().(linePtr *int) { |
| 173 | p.linePtrscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#linePtr. = linePtr |
| 174 | } |
| 175 | |
| 176 | // linesFrom returns the number of output lines between the current |
| 177 | // output line and the line argument, ignoring any pending (not yet |
| 178 | // emitted) whitespace or comments. It is used to compute an accurate |
| 179 | // size (in number of lines) for a formatted construct. |
| 180 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) linesFromscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#linesFrom().(line int) int { |
| 181 | return p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. - line |
| 182 | } |
| 183 | |
| 184 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) posForscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#posFor().(pos tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.20 `go/token`/Pos#) tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position# { |
| 185 | // not used frequently enough to cache entire token.Position |
| 186 | return p.fsetscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#fset..PositionForscip-go gomod github.com/golang/go/src go1.20 `go/token`/FileSet#PositionFor().(pos, false /* absolute position */) |
| 187 | } |
| 188 | |
| 189 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) lineForscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lineFor().(pos tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.20 `go/token`/Pos#) int { |
| 190 | if pos != p.cachedPosscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#cachedPos. { |
| 191 | p.cachedPosscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#cachedPos. = pos |
| 192 | p.cachedLinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#cachedLine. = p.fsetscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#fset..PositionForscip-go gomod github.com/golang/go/src go1.20 `go/token`/FileSet#PositionFor().(pos, false /* absolute position */).Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. |
| 193 | } |
| 194 | return p.cachedLinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#cachedLine. |
| 195 | } |
| 196 | |
| 197 | // writeLineDirective writes a //line directive if necessary. |
| 198 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) writeLineDirectivescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeLineDirective().(pos tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#) { |
| 199 | if pos.IsValidscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#IsValid().() && (p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. != pos.Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. || p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Filenamescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Filename. != pos.Filenamescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Filename.) { |
| 200 | if stringsscip-go gomod github.com/golang/go/src go1.20 strings/.ContainsAnyscip-go gomod github.com/golang/go/src go1.20 strings/ContainsAny().(pos.Filenamescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Filename., "\r\n") { |
| 201 | if p.sourcePosErrscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#sourcePosErr. == nil { |
| 202 | p.sourcePosErrscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#sourcePosErr. = fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.20 fmt/Errorf().("mvdan.cc/gofumpt/internal/govendor/go/printer: source filename contains unexpected newline character: %q", pos.Filenamescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Filename.) |
| 203 | } |
| 204 | return |
| 205 | } |
| 206 | |
| 207 | p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output. = append(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output., tabwriterscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/.Escapescip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/Escape.) // protect '\n' in //line from tabwriter interpretation |
| 208 | p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output. = append(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output., fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.20 fmt/Sprintf().("//line %s:%d\n", pos.Filenamescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Filename., pos.Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line.)...) |
| 209 | p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output. = append(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output., tabwriterscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/.Escapescip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/Escape.) |
| 210 | // p.out must match the //line directive |
| 211 | p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Filenamescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Filename. = pos.Filenamescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Filename. |
| 212 | p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. = pos.Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // writeIndent writes indentation. |
| 217 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) writeIndentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeIndent().() { |
| 218 | // use "hard" htabs - indentation columns |
| 219 | // must not be discarded by the tabwriter |
| 220 | n := p.Configscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#Config..Indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Indent. + p.indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent. // include base indentation |
| 221 | for i := 0; i < n; i++ { |
| 222 | p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output. = append(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output., '\t') |
| 223 | } |
| 224 | |
| 225 | // update positions |
| 226 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos..Offsetscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Offset. += n |
| 227 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos..Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. += n |
| 228 | p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. += n |
| 229 | } |
| 230 | |
| 231 | // writeByte writes ch n times to p.output and updates p.pos. |
| 232 | // Only used to write formatting (white space) characters. |
| 233 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) writeBytescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeByte().(ch byte, n int) { |
| 234 | if p.endAlignmentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#endAlignment. { |
| 235 | // Ignore any alignment control character; |
| 236 | // and at the end of the line, break with |
| 237 | // a formfeed to indicate termination of |
| 238 | // existing columns. |
| 239 | switch ch { |
| 240 | case '\t', '\v': |
| 241 | ch = ' ' |
| 242 | case '\n', '\f': |
| 243 | ch = '\f' |
| 244 | p.endAlignmentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#endAlignment. = false |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. == 1 { |
| 249 | // no need to write line directives before white space |
| 250 | p.writeIndentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeIndent().() |
| 251 | } |
| 252 | |
| 253 | for i := 0; i < n; i++ { |
| 254 | p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output. = append(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output., ch) |
| 255 | } |
| 256 | |
| 257 | // update positions |
| 258 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos..Offsetscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Offset. += n |
| 259 | if ch == '\n' || ch == '\f' { |
| 260 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos..Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. += n |
| 261 | p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. += n |
| 262 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos..Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. = 1 |
| 263 | p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. = 1 |
| 264 | return |
| 265 | } |
| 266 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos..Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. += n |
| 267 | p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. += n |
| 268 | } |
| 269 | |
| 270 | // writeString writes the string s to p.output and updates p.pos, p.out, |
| 271 | // and p.last. If isLit is set, s is escaped w/ tabwriter.Escape characters |
| 272 | // to protect s from being interpreted by the tabwriter. |
| 273 | // |
| 274 | // Note: writeString is only used to write Go tokens, literals, and |
| 275 | // comments, all of which must be written literally. Thus, it is correct |
| 276 | // to always set isLit = true. However, setting it explicitly only when |
| 277 | // needed (i.e., when we don't know that s contains no tabs or line breaks) |
| 278 | // avoids processing extra escape characters and reduces run time of the |
| 279 | // printer benchmark by up to 10%. |
| 280 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) writeStringscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeString().(pos tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#, s string, isLit bool) { |
| 281 | if p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. == 1 { |
| 282 | if p.Configscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#Config..Modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Mode.&SourcePosscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/SourcePos. != 0 { |
| 283 | p.writeLineDirectivescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeLineDirective().(pos) |
| 284 | } |
| 285 | p.writeIndentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeIndent().() |
| 286 | } |
| 287 | |
| 288 | if pos.IsValidscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#IsValid().() { |
| 289 | // update p.pos (if pos is invalid, continue with existing p.pos) |
| 290 | // Note: Must do this after handling line beginnings because |
| 291 | // writeIndent updates p.pos if there's indentation, but p.pos |
| 292 | // is the position of s. |
| 293 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos. = pos |
| 294 | } |
| 295 | |
| 296 | if isLit { |
| 297 | // Protect s such that is passes through the tabwriter |
| 298 | // unchanged. Note that valid Go programs cannot contain |
| 299 | // tabwriter.Escape bytes since they do not appear in legal |
| 300 | // UTF-8 sequences. |
| 301 | p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output. = append(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output., tabwriterscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/.Escapescip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/Escape.) |
| 302 | } |
| 303 | |
| 304 | if debugscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/debug. { |
| 305 | p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output. = append(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output., fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.20 fmt/Sprintf().("/*%s*/", pos)...) // do not update p.pos! |
| 306 | } |
| 307 | p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output. = append(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output., s...) |
| 308 | |
| 309 | // update positions |
| 310 | nlines := 0 |
| 311 | var li int // index of last newline; valid if nlines > 0 |
| 312 | for i := 0; i < len(s); i++ { |
| 313 | // Raw string literals may contain any character except back quote (`). |
| 314 | if ch := s[i]; ch == '\n' || ch == '\f' { |
| 315 | // account for line break |
| 316 | nlines++ |
| 317 | li = i |
| 318 | // A line break inside a literal will break whatever column |
| 319 | // formatting is in place; ignore any further alignment through |
| 320 | // the end of the line. |
| 321 | p.endAlignmentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#endAlignment. = true |
| 322 | } |
| 323 | } |
| 324 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos..Offsetscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Offset. += len(s) |
| 325 | if nlines > 0 { |
| 326 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos..Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. += nlines |
| 327 | p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. += nlines |
| 328 | c := len(s) - li |
| 329 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos..Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. = c |
| 330 | p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. = c |
| 331 | } else { |
| 332 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos..Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. += len(s) |
| 333 | p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. += len(s) |
| 334 | } |
| 335 | |
| 336 | if isLit { |
| 337 | p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output. = append(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output., tabwriterscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/.Escapescip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/Escape.) |
| 338 | } |
| 339 | |
| 340 | p.lastscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#last. = p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos. |
| 341 | } |
| 342 | |
| 343 | // writeCommentPrefix writes the whitespace before a comment. |
| 344 | // If there is any pending whitespace, it consumes as much of |
| 345 | // it as is likely to help position the comment nicely. |
| 346 | // pos is the comment position, next the position of the item |
| 347 | // after all pending comments, prev is the previous comment in |
| 348 | // a group of comments (or nil), and tok is the next token. |
| 349 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) writeCommentPrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeCommentPrefix().(pos, next tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#, prev *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#, tok tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/Token#) { |
| 350 | if len(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output.) == 0 { |
| 351 | // the comment is the first item to be printed - don't write any whitespace |
| 352 | return |
| 353 | } |
| 354 | |
| 355 | if pos.IsValidscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#IsValid().() && pos.Filenamescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Filename. != p.lastscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#last..Filenamescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Filename. { |
| 356 | // comment in a different file - separate with newlines |
| 357 | p.writeBytescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeByte().('\f', maxNewlinesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/maxNewlines.) |
| 358 | return |
| 359 | } |
| 360 | |
| 361 | if pos.Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. == p.lastscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#last..Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. && (prev == nil || prev.Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text.[1] != '/') { |
| 362 | // comment on the same line as last item: |
| 363 | // separate with at least one separator |
| 364 | hasSep := false |
| 365 | if prev == nil { |
| 366 | // first comment of a comment group |
| 367 | j := 0 |
| 368 | for i, ch := range p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf. { |
| 369 | switch ch { |
| 370 | case blankscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/blank.: |
| 371 | // ignore any blanks before a comment |
| 372 | p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[i] = ignorescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/ignore. |
| 373 | continue |
| 374 | case vtabscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/vtab.: |
| 375 | // respect existing tabs - important |
| 376 | // for proper formatting of commented structs |
| 377 | hasSep = true |
| 378 | continue |
| 379 | case indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/indent.: |
| 380 | // apply pending indentation |
| 381 | continue |
| 382 | } |
| 383 | j = i |
| 384 | break |
| 385 | } |
| 386 | p.writeWhitespacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeWhitespace().(j) |
| 387 | } |
| 388 | // make sure there is at least one separator |
| 389 | if !hasSep { |
| 390 | sep := byte('\t') |
| 391 | if pos.Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. == next.Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. { |
| 392 | // next item is on the same line as the comment |
| 393 | // (which must be a /*-style comment): separate |
| 394 | // with a blank instead of a tab |
| 395 | sep = ' ' |
| 396 | } |
| 397 | p.writeBytescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeByte().(sep, 1) |
| 398 | } |
| 399 | |
| 400 | } else { |
| 401 | // comment on a different line: |
| 402 | // separate with at least one line break |
| 403 | droppedLinebreak := false |
| 404 | j := 0 |
| 405 | for i, ch := range p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf. { |
| 406 | switch ch { |
| 407 | case blankscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/blank., vtabscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/vtab.: |
| 408 | // ignore any horizontal whitespace before line breaks |
| 409 | p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[i] = ignorescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/ignore. |
| 410 | continue |
| 411 | case indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/indent.: |
| 412 | // apply pending indentation |
| 413 | continue |
| 414 | case unindentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/unindent.: |
| 415 | // if this is not the last unindent, apply it |
| 416 | // as it is (likely) belonging to the last |
| 417 | // construct (e.g., a multi-line expression list) |
| 418 | // and is not part of closing a block |
| 419 | if i+1 < len(p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.) && p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[i+1] == unindentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/unindent. { |
| 420 | continue |
| 421 | } |
| 422 | // if the next token is not a closing }, apply the unindent |
| 423 | // if it appears that the comment is aligned with the |
| 424 | // token; otherwise assume the unindent is part of a |
| 425 | // closing block and stop (this scenario appears with |
| 426 | // comments before a case label where the comments |
| 427 | // apply to the next case instead of the current one) |
| 428 | if tok != tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.RBRACEscip-go gomod github.com/golang/go/src go1.20 `go/token`/RBRACE. && pos.Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. == next.Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. { |
| 429 | continue |
| 430 | } |
| 431 | case newlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/newline., formfeedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/formfeed.: |
| 432 | p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[i] = ignorescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/ignore. |
| 433 | droppedLinebreak = prev == nil // record only if first comment of a group |
| 434 | } |
| 435 | j = i |
| 436 | break |
| 437 | } |
| 438 | p.writeWhitespacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeWhitespace().(j) |
| 439 | |
| 440 | // determine number of linebreaks before the comment |
| 441 | n := 0 |
| 442 | if pos.IsValidscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#IsValid().() && p.lastscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#last..IsValidscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#IsValid().() { |
| 443 | n = pos.Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. - p.lastscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#last..Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. |
| 444 | if n < 0 { // should never happen |
| 445 | n = 0 |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | // at the package scope level only (p.indent == 0), |
| 450 | // add an extra newline if we dropped one before: |
| 451 | // this preserves a blank line before documentation |
| 452 | // comments at the package scope level (issue 2570) |
| 453 | if p.indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent. == 0 && droppedLinebreak { |
| 454 | n++ |
| 455 | } |
| 456 | |
| 457 | // make sure there is at least one line break |
| 458 | // if the previous comment was a line comment |
| 459 | if n == 0 && prev != nil && prev.Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text.[1] == '/' { |
| 460 | n = 1 |
| 461 | } |
| 462 | |
| 463 | if n > 0 { |
| 464 | // use formfeeds to break columns before a comment; |
| 465 | // this is analogous to using formfeeds to separate |
| 466 | // individual lines of /*-style comments |
| 467 | p.writeBytescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeByte().('\f', nlimitscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/nlimit().(n)) |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // Returns true if s contains only white space |
| 473 | // (only tabs and blanks can appear in the printer's context). |
| 474 | func isBlankscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/isBlank().(s string) bool { |
| 475 | for i := 0; i < len(s); i++ { |
| 476 | if s[i] > ' ' { |
| 477 | return false |
| 478 | } |
| 479 | } |
| 480 | return true |
| 481 | } |
| 482 | |
| 483 | // commonPrefix returns the common prefix of a and b. |
| 484 | func commonPrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commonPrefix().(a, b string) string { |
| 485 | i := 0 |
| 486 | for i < len(a) && i < len(b) && a[i] == b[i] && (a[i] <= ' ' || a[i] == '*') { |
| 487 | i++ |
| 488 | } |
| 489 | return a[0:i] |
| 490 | } |
| 491 | |
| 492 | // trimRight returns s with trailing whitespace removed. |
| 493 | func trimRightscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimRight().(s string) string { |
| 494 | return stringsscip-go gomod github.com/golang/go/src go1.20 strings/.TrimRightFuncscip-go gomod github.com/golang/go/src go1.20 strings/TrimRightFunc().(s, unicodescip-go gomod github.com/golang/go/src go1.20 unicode/.IsSpacescip-go gomod github.com/golang/go/src go1.20 unicode/IsSpace().) |
| 495 | } |
| 496 | |
| 497 | // stripCommonPrefix removes a common prefix from /*-style comment lines (unless no |
| 498 | // comment line is indented, all but the first line have some form of space prefix). |
| 499 | // The prefix is computed using heuristics such that is likely that the comment |
| 500 | // contents are nicely laid out after re-printing each line using the printer's |
| 501 | // current indentation. |
| 502 | func stripCommonPrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/stripCommonPrefix().(lines []string) { |
| 503 | if len(lines) <= 1 { |
| 504 | return // at most one line - nothing to do |
| 505 | } |
| 506 | // len(lines) > 1 |
| 507 | |
| 508 | // The heuristic in this function tries to handle a few |
| 509 | // common patterns of /*-style comments: Comments where |
| 510 | // the opening /* and closing */ are aligned and the |
| 511 | // rest of the comment text is aligned and indented with |
| 512 | // blanks or tabs, cases with a vertical "line of stars" |
| 513 | // on the left, and cases where the closing */ is on the |
| 514 | // same line as the last comment text. |
| 515 | |
| 516 | // Compute maximum common white prefix of all but the first, |
| 517 | // last, and blank lines, and replace blank lines with empty |
| 518 | // lines (the first line starts with /* and has no prefix). |
| 519 | // In cases where only the first and last lines are not blank, |
| 520 | // such as two-line comments, or comments where all inner lines |
| 521 | // are blank, consider the last line for the prefix computation |
| 522 | // since otherwise the prefix would be empty. |
| 523 | // |
| 524 | // Note that the first and last line are never empty (they |
| 525 | // contain the opening /* and closing */ respectively) and |
| 526 | // thus they can be ignored by the blank line check. |
| 527 | prefix := "" |
| 528 | prefixSet := false |
| 529 | if len(lines) > 2 { |
| 530 | for i, line := range lines[1 : len(lines)-1] { |
| 531 | if isBlankscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/isBlank().(line) { |
| 532 | lines[1+i] = "" // range starts with lines[1] |
| 533 | } else { |
| 534 | if !prefixSet { |
| 535 | prefix = line |
| 536 | prefixSet = true |
| 537 | } |
| 538 | prefix = commonPrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commonPrefix().(prefix, line) |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | // If we don't have a prefix yet, consider the last line. |
| 543 | if !prefixSet { |
| 544 | line := lines[len(lines)-1] |
| 545 | prefix = commonPrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commonPrefix().(line, line) |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * Check for vertical "line of stars" and correct prefix accordingly. |
| 550 | */ |
| 551 | lineOfStars := false |
| 552 | if p, _, ok := stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Cutscip-go gomod github.com/golang/go/src go1.20 strings/Cut().(prefix, "*"); ok { |
| 553 | // remove trailing blank from prefix so stars remain aligned |
| 554 | prefix = stringsscip-go gomod github.com/golang/go/src go1.20 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.20 strings/TrimSuffix().(p, " ") |
| 555 | lineOfStars = true |
| 556 | } else { |
| 557 | // No line of stars present. |
| 558 | // Determine the white space on the first line after the /* |
| 559 | // and before the beginning of the comment text, assume two |
| 560 | // blanks instead of the /* unless the first character after |
| 561 | // the /* is a tab. If the first comment line is empty but |
| 562 | // for the opening /*, assume up to 3 blanks or a tab. This |
| 563 | // whitespace may be found as suffix in the common prefix. |
| 564 | first := lines[0] |
| 565 | if isBlankscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/isBlank().(first[2:]) { |
| 566 | // no comment text on the first line: |
| 567 | // reduce prefix by up to 3 blanks or a tab |
| 568 | // if present - this keeps comment text indented |
| 569 | // relative to the /* and */'s if it was indented |
| 570 | // in the first place |
| 571 | i := len(prefix) |
| 572 | for n := 0; n < 3 && i > 0 && prefix[i-1] == ' '; n++ { |
| 573 | i-- |
| 574 | } |
| 575 | if i == len(prefix) && i > 0 && prefix[i-1] == '\t' { |
| 576 | i-- |
| 577 | } |
| 578 | prefix = prefix[0:i] |
| 579 | } else { |
| 580 | // comment text on the first line |
| 581 | suffix := make([]byte, len(first)) |
| 582 | n := 2 // start after opening /* |
| 583 | for n < len(first) && first[n] <= ' ' { |
| 584 | suffix[n] = first[n] |
| 585 | n++ |
| 586 | } |
| 587 | if n > 2 && suffix[2] == '\t' { |
| 588 | // assume the '\t' compensates for the /* |
| 589 | suffix = suffix[2:n] |
| 590 | } else { |
| 591 | // otherwise assume two blanks |
| 592 | suffix[0], suffix[1] = ' ', ' ' |
| 593 | suffix = suffix[0:n] |
| 594 | } |
| 595 | // Shorten the computed common prefix by the length of |
| 596 | // suffix, if it is found as suffix of the prefix. |
| 597 | prefix = stringsscip-go gomod github.com/golang/go/src go1.20 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.20 strings/TrimSuffix().(prefix, string(suffix)) |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | // Handle last line: If it only contains a closing */, align it |
| 602 | // with the opening /*, otherwise align the text with the other |
| 603 | // lines. |
| 604 | last := lines[len(lines)-1] |
| 605 | closing := "*/" |
| 606 | before, _, _ := stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Cutscip-go gomod github.com/golang/go/src go1.20 strings/Cut().(last, closing) // closing always present |
| 607 | if isBlankscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/isBlank().(before) { |
| 608 | // last line only contains closing */ |
| 609 | if lineOfStars { |
| 610 | closing = " */" // add blank to align final star |
| 611 | } |
| 612 | lines[len(lines)-1] = prefix + closing |
| 613 | } else { |
| 614 | // last line contains more comment text - assume |
| 615 | // it is aligned like the other lines and include |
| 616 | // in prefix computation |
| 617 | prefix = commonPrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commonPrefix().(prefix, last) |
| 618 | } |
| 619 | |
| 620 | // Remove the common prefix from all but the first and empty lines. |
| 621 | for i, line := range lines { |
| 622 | if i > 0 && line != "" { |
| 623 | lines[i] = line[len(prefix):] |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) writeCommentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeComment().(comment *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#) { |
| 629 | text := comment.Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text. |
| 630 | pos := p.posForscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#posFor().(comment.Posscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Pos().()) |
| 631 | |
| 632 | const linePrefix = "//line " |
| 633 | if stringsscip-go gomod github.com/golang/go/src go1.20 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.20 strings/HasPrefix().(text, linePrefix) && (!pos.IsValidscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#IsValid().() || pos.Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. == 1) { |
| 634 | // Possibly a //-style line directive. |
| 635 | // Suspend indentation temporarily to keep line directive valid. |
| 636 | defer func(indent int) { p.indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent. = indent }(p.indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent.) |
| 637 | p.indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent. = 0 |
| 638 | } |
| 639 | |
| 640 | // shortcut common case of //-style comments |
| 641 | if text[1] == '/' { |
| 642 | if constraintscip-go gomod github.com/golang/go/src go1.20 `go/build/constraint`/.IsGoBuildscip-go gomod github.com/golang/go/src go1.20 `go/build/constraint`/IsGoBuild().(text) { |
| 643 | p.goBuildscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#goBuild. = append(p.goBuildscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#goBuild., len(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output.)) |
| 644 | } else if constraintscip-go gomod github.com/golang/go/src go1.20 `go/build/constraint`/.IsPlusBuildscip-go gomod github.com/golang/go/src go1.20 `go/build/constraint`/IsPlusBuild().(text) { |
| 645 | p.plusBuildscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#plusBuild. = append(p.plusBuildscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#plusBuild., len(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output.)) |
| 646 | } |
| 647 | p.writeStringscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeString().(pos, trimRightscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimRight().(text), true) |
| 648 | return |
| 649 | } |
| 650 | |
| 651 | // for /*-style comments, print line by line and let the |
| 652 | // write function take care of the proper indentation |
| 653 | lines := stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Splitscip-go gomod github.com/golang/go/src go1.20 strings/Split().(text, "\n") |
| 654 | |
| 655 | // The comment started in the first column but is going |
| 656 | // to be indented. For an idempotent result, add indentation |
| 657 | // to all lines such that they look like they were indented |
| 658 | // before - this will make sure the common prefix computation |
| 659 | // is the same independent of how many times formatting is |
| 660 | // applied (was issue 1835). |
| 661 | if pos.IsValidscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#IsValid().() && pos.Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. == 1 && p.indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent. > 0 { |
| 662 | for i, line := range lines[1:] { |
| 663 | lines[1+i] = " " + line |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | stripCommonPrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/stripCommonPrefix().(lines) |
| 668 | |
| 669 | // write comment lines, separated by formfeed, |
| 670 | // without a line break after the last line |
| 671 | for i, line := range lines { |
| 672 | if i > 0 { |
| 673 | p.writeBytescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeByte().('\f', 1) |
| 674 | pos = p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos. |
| 675 | } |
| 676 | if len(line) > 0 { |
| 677 | p.writeStringscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeString().(pos, trimRightscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimRight().(line), true) |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | // writeCommentSuffix writes a line break after a comment if indicated |
| 683 | // and processes any leftover indentation information. If a line break |
| 684 | // is needed, the kind of break (newline vs formfeed) depends on the |
| 685 | // pending whitespace. The writeCommentSuffix result indicates if a |
| 686 | // newline was written or if a formfeed was dropped from the whitespace |
| 687 | // buffer. |
| 688 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) writeCommentSuffixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeCommentSuffix().(needsLinebreak bool) (wroteNewline, droppedFF bool) { |
| 689 | for i, ch := range p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf. { |
| 690 | switch ch { |
| 691 | case blankscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/blank., vtabscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/vtab.: |
| 692 | // ignore trailing whitespace |
| 693 | p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[i] = ignorescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/ignore. |
| 694 | case indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/indent., unindentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/unindent.: |
| 695 | // don't lose indentation information |
| 696 | case newlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/newline., formfeedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/formfeed.: |
| 697 | // if we need a line break, keep exactly one |
| 698 | // but remember if we dropped any formfeeds |
| 699 | if needsLinebreak { |
| 700 | needsLinebreak = false |
| 701 | wroteNewline = true |
| 702 | } else { |
| 703 | if ch == formfeedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/formfeed. { |
| 704 | droppedFF = true |
| 705 | } |
| 706 | p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[i] = ignorescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/ignore. |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | p.writeWhitespacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeWhitespace().(len(p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.)) |
| 711 | |
| 712 | // make sure we have a line break |
| 713 | if needsLinebreak { |
| 714 | p.writeBytescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeByte().('\n', 1) |
| 715 | wroteNewline = true |
| 716 | } |
| 717 | |
| 718 | return |
| 719 | } |
| 720 | |
| 721 | // containsLinebreak reports whether the whitespace buffer contains any line breaks. |
| 722 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) containsLinebreakscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#containsLinebreak().() bool { |
| 723 | for _, ch := range p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf. { |
| 724 | if ch == newlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/newline. || ch == formfeedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/formfeed. { |
| 725 | return true |
| 726 | } |
| 727 | } |
| 728 | return false |
| 729 | } |
| 730 | |
| 731 | // intersperseComments consumes all comments that appear before the next token |
| 732 | // tok and prints it together with the buffered whitespace (i.e., the whitespace |
| 733 | // that needs to be written before the next token). A heuristic is used to mix |
| 734 | // the comments and whitespace. The intersperseComments result indicates if a |
| 735 | // newline was written or if a formfeed was dropped from the whitespace buffer. |
| 736 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) intersperseCommentsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#intersperseComments().(next tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#, tok tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/Token#) (wroteNewline, droppedFF bool) { |
| 737 | var last *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment# |
| 738 | for p.commentBeforescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#commentBefore().(next) { |
| 739 | list := p.commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#comment..Listscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#List. |
| 740 | changed := false |
| 741 | if p.lastTokscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lastTok. != tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.IMPORTscip-go gomod github.com/golang/go/src go1.20 `go/token`/IMPORT. && // do not rewrite cgo's import "C" comments |
| 742 | p.posForscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#posFor().(p.commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#comment..Posscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#Pos().()).Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column. == 1 && |
| 743 | p.posForscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#posFor().(p.commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#comment..Endscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#End().()+1) == next { |
| 744 | // Unindented comment abutting next token position: |
| 745 | // a top-level doc comment. |
| 746 | list = formatDocCommentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/formatDocComment().(list) |
| 747 | changed = true |
| 748 | |
| 749 | if len(p.commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#comment..Listscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#List.) > 0 && len(list) == 0 { |
| 750 | // The doc comment was removed entirely. |
| 751 | // Keep preceding whitespace. |
| 752 | p.writeCommentPrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeCommentPrefix().(p.posForscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#posFor().(p.commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#comment..Posscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#Pos().()), next, last, tok) |
| 753 | // Change print state to continue at next. |
| 754 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos. = next |
| 755 | p.lastscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#last. = next |
| 756 | // There can't be any more comments. |
| 757 | p.nextCommentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#nextComment().() |
| 758 | return p.writeCommentSuffixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeCommentSuffix().(false) |
| 759 | } |
| 760 | } |
| 761 | for _, c := range list { |
| 762 | p.writeCommentPrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeCommentPrefix().(p.posForscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#posFor().(c.Posscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Pos().()), next, last, tok) |
| 763 | p.writeCommentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeComment().(c) |
| 764 | last = c |
| 765 | } |
| 766 | // In case list was rewritten, change print state to where |
| 767 | // the original list would have ended. |
| 768 | if len(p.commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#comment..Listscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#List.) > 0 && changed { |
| 769 | last = p.commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#comment..Listscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#List.[len(p.commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/commentInfo#comment..Listscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#List.)-1] |
| 770 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos. = p.posForscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#posFor().(last.Endscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#End().()) |
| 771 | p.lastscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#last. = p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos. |
| 772 | } |
| 773 | p.nextCommentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#nextComment().() |
| 774 | } |
| 775 | |
| 776 | if last != nil { |
| 777 | // If the last comment is a /*-style comment and the next item |
| 778 | // follows on the same line but is not a comma, and not a "closing" |
| 779 | // token immediately following its corresponding "opening" token, |
| 780 | // add an extra separator unless explicitly disabled. Use a blank |
| 781 | // as separator unless we have pending linebreaks, they are not |
| 782 | // disabled, and we are outside a composite literal, in which case |
| 783 | // we want a linebreak (issue 15137). |
| 784 | // TODO(gri) This has become overly complicated. We should be able |
| 785 | // to track whether we're inside an expression or statement and |
| 786 | // use that information to decide more directly. |
| 787 | needsLinebreak := false |
| 788 | if p.modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#mode.&noExtraBlankscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/noExtraBlank. == 0 && |
| 789 | last.Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text.[1] == '*' && p.lineForscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lineFor().(last.Posscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Pos().()) == next.Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. && |
| 790 | tok != tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.COMMAscip-go gomod github.com/golang/go/src go1.20 `go/token`/COMMA. && |
| 791 | (tok != tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.RPARENscip-go gomod github.com/golang/go/src go1.20 `go/token`/RPAREN. || p.prevOpenscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#prevOpen. == tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.LPARENscip-go gomod github.com/golang/go/src go1.20 `go/token`/LPAREN.) && |
| 792 | (tok != tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.RBRACKscip-go gomod github.com/golang/go/src go1.20 `go/token`/RBRACK. || p.prevOpenscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#prevOpen. == tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.LBRACKscip-go gomod github.com/golang/go/src go1.20 `go/token`/LBRACK.) { |
| 793 | if p.containsLinebreakscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#containsLinebreak().() && p.modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#mode.&noExtraLinebreakscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/noExtraLinebreak. == 0 && p.levelscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#level. == 0 { |
| 794 | needsLinebreak = true |
| 795 | } else { |
| 796 | p.writeBytescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeByte().(' ', 1) |
| 797 | } |
| 798 | } |
| 799 | // Ensure that there is a line break after a //-style comment, |
| 800 | // before EOF, and before a closing '}' unless explicitly disabled. |
| 801 | if last.Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text.[1] == '/' || |
| 802 | tok == tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.EOFscip-go gomod github.com/golang/go/src go1.20 `go/token`/EOF. || |
| 803 | tok == tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.RBRACEscip-go gomod github.com/golang/go/src go1.20 `go/token`/RBRACE. && p.modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#mode.&noExtraLinebreakscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/noExtraLinebreak. == 0 { |
| 804 | needsLinebreak = true |
| 805 | } |
| 806 | return p.writeCommentSuffixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeCommentSuffix().(needsLinebreak) |
| 807 | } |
| 808 | |
| 809 | // no comment was written - we should never reach here since |
| 810 | // intersperseComments should not be called in that case |
| 811 | p.internalErrorscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#internalError().("intersperseComments called without pending comments") |
| 812 | return |
| 813 | } |
| 814 | |
| 815 | // writeWhitespace writes the first n whitespace entries. |
| 816 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) writeWhitespacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeWhitespace().(n int) { |
| 817 | // write entries |
| 818 | for i := 0; i < n; i++ { |
| 819 | switch ch := p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[i]; ch { |
| 820 | case ignorescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/ignore.: |
| 821 | // ignore! |
| 822 | case indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/indent.: |
| 823 | p.indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent.++ |
| 824 | case unindentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/unindent.: |
| 825 | p.indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent.-- |
| 826 | if p.indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent. < 0 { |
| 827 | p.internalErrorscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#internalError().("negative indentation:", p.indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent.) |
| 828 | p.indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent. = 0 |
| 829 | } |
| 830 | case newlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/newline., formfeedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/formfeed.: |
| 831 | // A line break immediately followed by a "correcting" |
| 832 | // unindent is swapped with the unindent - this permits |
| 833 | // proper label positioning. If a comment is between |
| 834 | // the line break and the label, the unindent is not |
| 835 | // part of the comment whitespace prefix and the comment |
| 836 | // will be positioned correctly indented. |
| 837 | if i+1 < n && p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[i+1] == unindentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/unindent. { |
| 838 | // Use a formfeed to terminate the current section. |
| 839 | // Otherwise, a long label name on the next line leading |
| 840 | // to a wide column may increase the indentation column |
| 841 | // of lines before the label; effectively leading to wrong |
| 842 | // indentation. |
| 843 | p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[i], p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[i+1] = unindentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/unindent., formfeedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/formfeed. |
| 844 | i-- // do it again |
| 845 | continue |
| 846 | } |
| 847 | fallthrough |
| 848 | default: |
| 849 | p.writeBytescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeByte().(byte(ch), 1) |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | // shift remaining entries down |
| 854 | l := copy(p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf., p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[n:]) |
| 855 | p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf. = p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[:l] |
| 856 | } |
| 857 | |
| 858 | // ---------------------------------------------------------------------------- |
| 859 | // Printing interface |
| 860 | |
| 861 | // nlimit limits n to maxNewlines. |
| 862 | func nlimitscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/nlimit().(n int) int { |
| 863 | if n > maxNewlinesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/maxNewlines. { |
| 864 | n = maxNewlinesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/maxNewlines. |
| 865 | } |
| 866 | return n |
| 867 | } |
| 868 | |
| 869 | func mayCombinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/mayCombine().(prev tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/Token#, next byte) (b bool) { |
| 870 | switch prev { |
| 871 | case tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.INTscip-go gomod github.com/golang/go/src go1.20 `go/token`/INT.: |
| 872 | b = next == '.' // 1. |
| 873 | case tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.ADDscip-go gomod github.com/golang/go/src go1.20 `go/token`/ADD.: |
| 874 | b = next == '+' // ++ |
| 875 | case tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.SUBscip-go gomod github.com/golang/go/src go1.20 `go/token`/SUB.: |
| 876 | b = next == '-' // -- |
| 877 | case tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.QUOscip-go gomod github.com/golang/go/src go1.20 `go/token`/QUO.: |
| 878 | b = next == '*' // /* |
| 879 | case tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.LSSscip-go gomod github.com/golang/go/src go1.20 `go/token`/LSS.: |
| 880 | b = next == '-' || next == '<' // <- or << |
| 881 | case tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.ANDscip-go gomod github.com/golang/go/src go1.20 `go/token`/AND.: |
| 882 | b = next == '&' || next == '^' // && or &^ |
| 883 | } |
| 884 | return |
| 885 | } |
| 886 | |
| 887 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) setPosscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#setPos().(pos tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.20 `go/token`/Pos#) { |
| 888 | if pos.IsValidscip-go gomod github.com/golang/go/src go1.20 `go/token`/Pos#IsValid().() { |
| 889 | p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos. = p.posForscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#posFor().(pos) // accurate position of next item |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | // print prints a list of "items" (roughly corresponding to syntactic |
| 894 | // tokens, but also including whitespace and formatting information). |
| 895 | // It is the only print function that should be called directly from |
| 896 | // any of the AST printing functions in nodes.go. |
| 897 | // |
| 898 | // Whitespace is accumulated until a non-whitespace token appears. Any |
| 899 | // comments that need to appear before that token are printed first, |
| 900 | // taking into account the amount and structure of any pending white- |
| 901 | // space for best comment placement. Then, any leftover whitespace is |
| 902 | // printed, followed by the actual token. |
| 903 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) printscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#print().(args ...any) { |
| 904 | for _, arg := range args { |
| 905 | // information about the current arg |
| 906 | var data string |
| 907 | var isLit bool |
| 908 | var impliedSemi bool // value for p.impliedSemi after this arg |
| 909 | |
| 910 | // record previous opening token, if any |
| 911 | switch p.lastTokscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lastTok. { |
| 912 | case tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.ILLEGALscip-go gomod github.com/golang/go/src go1.20 `go/token`/ILLEGAL.: |
| 913 | // ignore (white space) |
| 914 | case tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.LPARENscip-go gomod github.com/golang/go/src go1.20 `go/token`/LPAREN., tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.LBRACKscip-go gomod github.com/golang/go/src go1.20 `go/token`/LBRACK.: |
| 915 | p.prevOpenscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#prevOpen. = p.lastTokscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lastTok. |
| 916 | default: |
| 917 | // other tokens followed any opening token |
| 918 | p.prevOpenscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#prevOpen. = tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.ILLEGALscip-go gomod github.com/golang/go/src go1.20 `go/token`/ILLEGAL. |
| 919 | } |
| 920 | |
| 921 | switch x := arg.(type) { |
| 922 | case pmodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/pmode#: |
| 923 | // toggle printer mode |
| 924 | p.modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#mode. ^= x |
| 925 | continue |
| 926 | |
| 927 | case whiteSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/whiteSpace#: |
| 928 | if x == ignorescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/ignore. { |
| 929 | // don't add ignore's to the buffer; they |
| 930 | // may screw up "correcting" unindents (see |
| 931 | // LabeledStmt) |
| 932 | continue |
| 933 | } |
| 934 | i := len(p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.) |
| 935 | if i == cap(p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.) { |
| 936 | // Whitespace sequences are very short so this should |
| 937 | // never happen. Handle gracefully (but possibly with |
| 938 | // bad comment placement) if it does happen. |
| 939 | p.writeWhitespacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeWhitespace().(i) |
| 940 | i = 0 |
| 941 | } |
| 942 | p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf. = p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[0 : i+1] |
| 943 | p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[i] = x |
| 944 | if x == newlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/newline. || x == formfeedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/formfeed. { |
| 945 | // newlines affect the current state (p.impliedSemi) |
| 946 | // and not the state after printing arg (impliedSemi) |
| 947 | // because comments can be interspersed before the arg |
| 948 | // in this case |
| 949 | p.impliedSemiscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#impliedSemi. = false |
| 950 | } |
| 951 | p.lastTokscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lastTok. = tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.ILLEGALscip-go gomod github.com/golang/go/src go1.20 `go/token`/ILLEGAL. |
| 952 | continue |
| 953 | |
| 954 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Ident#: |
| 955 | data = x.Namescip-go gomod github.com/golang/go/src go1.20 `go/ast`/Ident#Name. |
| 956 | impliedSemi = true |
| 957 | p.lastTokscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lastTok. = tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.IDENTscip-go gomod github.com/golang/go/src go1.20 `go/token`/IDENT. |
| 958 | |
| 959 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.20 `go/ast`/BasicLit#: |
| 960 | data = x.Valuescip-go gomod github.com/golang/go/src go1.20 `go/ast`/BasicLit#Value. |
| 961 | isLit = true |
| 962 | impliedSemi = true |
| 963 | p.lastTokscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lastTok. = x.Kindscip-go gomod github.com/golang/go/src go1.20 `go/ast`/BasicLit#Kind. |
| 964 | |
| 965 | case tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/Token#: |
| 966 | s := x.Stringscip-go gomod github.com/golang/go/src go1.20 `go/token`/Token#String().() |
| 967 | if mayCombinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/mayCombine().(p.lastTokscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lastTok., s[0]) { |
| 968 | // the previous and the current token must be |
| 969 | // separated by a blank otherwise they combine |
| 970 | // into a different incorrect token sequence |
| 971 | // (except for token.INT followed by a '.' this |
| 972 | // should never happen because it is taken care |
| 973 | // of via binary expression formatting) |
| 974 | if len(p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.) != 0 { |
| 975 | p.internalErrorscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#internalError().("whitespace buffer not empty") |
| 976 | } |
| 977 | p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf. = p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[0:1] |
| 978 | p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[0] = ' ' |
| 979 | } |
| 980 | data = s |
| 981 | // some keywords followed by a newline imply a semicolon |
| 982 | switch x { |
| 983 | case tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.BREAKscip-go gomod github.com/golang/go/src go1.20 `go/token`/BREAK., tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.CONTINUEscip-go gomod github.com/golang/go/src go1.20 `go/token`/CONTINUE., tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.FALLTHROUGHscip-go gomod github.com/golang/go/src go1.20 `go/token`/FALLTHROUGH., tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.RETURNscip-go gomod github.com/golang/go/src go1.20 `go/token`/RETURN., |
| 984 | tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.INCscip-go gomod github.com/golang/go/src go1.20 `go/token`/INC., tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.DECscip-go gomod github.com/golang/go/src go1.20 `go/token`/DEC., tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.RPARENscip-go gomod github.com/golang/go/src go1.20 `go/token`/RPAREN., tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.RBRACKscip-go gomod github.com/golang/go/src go1.20 `go/token`/RBRACK., tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.RBRACEscip-go gomod github.com/golang/go/src go1.20 `go/token`/RBRACE.: |
| 985 | impliedSemi = true |
| 986 | } |
| 987 | p.lastTokscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lastTok. = x |
| 988 | |
| 989 | case string: |
| 990 | // incorrect AST - print error message |
| 991 | data = x |
| 992 | isLit = true |
| 993 | impliedSemi = true |
| 994 | p.lastTokscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lastTok. = tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.STRINGscip-go gomod github.com/golang/go/src go1.20 `go/token`/STRING. |
| 995 | |
| 996 | default: |
| 997 | fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.20 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.20 os/.Stderrscip-go gomod github.com/golang/go/src go1.20 os/Stderr., "print: unsupported argument %v (%T)\n", arg, arg) |
| 998 | panic("mvdan.cc/gofumpt/internal/govendor/go/printer type") |
| 999 | } |
| 1000 | // data != "" |
| 1001 | |
| 1002 | next := p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos. // estimated/accurate position of next item |
| 1003 | wroteNewline, droppedFF := p.flushscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#flush().(next, p.lastTokscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#lastTok.) |
| 1004 | |
| 1005 | // intersperse extra newlines if present in the source and |
| 1006 | // if they don't cause extra semicolons (don't do this in |
| 1007 | // flush as it will cause extra newlines at the end of a file) |
| 1008 | if !p.impliedSemiscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#impliedSemi. { |
| 1009 | n := nlimitscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/nlimit().(next.Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. - p.posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos..Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line.) |
| 1010 | // don't exceed maxNewlines if we already wrote one |
| 1011 | if wroteNewline && n == maxNewlinesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/maxNewlines. { |
| 1012 | n = maxNewlinesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/maxNewlines. - 1 |
| 1013 | } |
| 1014 | if n > 0 { |
| 1015 | ch := byte('\n') |
| 1016 | if droppedFF { |
| 1017 | ch = '\f' // use formfeed since we dropped one before |
| 1018 | } |
| 1019 | p.writeBytescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeByte().(ch, n) |
| 1020 | impliedSemi = false |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | // the next token starts now - record its line number if requested |
| 1025 | if p.linePtrscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#linePtr. != nil { |
| 1026 | *p.linePtrscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#linePtr. = p.outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out..Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line. |
| 1027 | p.linePtrscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#linePtr. = nil |
| 1028 | } |
| 1029 | |
| 1030 | p.writeStringscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeString().(next, data, isLit) |
| 1031 | p.impliedSemiscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#impliedSemi. = impliedSemi |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | // flush prints any pending comments and whitespace occurring textually |
| 1036 | // before the position of the next token tok. The flush result indicates |
| 1037 | // if a newline was written or if a formfeed was dropped from the whitespace |
| 1038 | // buffer. |
| 1039 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) flushscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#flush().(next tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#, tok tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/Token#) (wroteNewline, droppedFF bool) { |
| 1040 | if p.commentBeforescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#commentBefore().(next) { |
| 1041 | // if there are comments before the next item, intersperse them |
| 1042 | wroteNewline, droppedFF = p.intersperseCommentsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#intersperseComments().(next, tok) |
| 1043 | } else { |
| 1044 | // otherwise, write any leftover whitespace |
| 1045 | p.writeWhitespacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#writeWhitespace().(len(p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.)) |
| 1046 | } |
| 1047 | return |
| 1048 | } |
| 1049 | |
| 1050 | // getDoc returns the ast.CommentGroup associated with n, if any. |
| 1051 | func getDocscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/getDoc().(n astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.20 `go/ast`/Node#) *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.CommentGroupscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup# { |
| 1052 | switch n := n.(type) { |
| 1053 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Fieldscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Field#: |
| 1054 | return n.Docscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Field#Doc. |
| 1055 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.ImportSpecscip-go gomod github.com/golang/go/src go1.20 `go/ast`/ImportSpec#: |
| 1056 | return n.Docscip-go gomod github.com/golang/go/src go1.20 `go/ast`/ImportSpec#Doc. |
| 1057 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.ValueSpecscip-go gomod github.com/golang/go/src go1.20 `go/ast`/ValueSpec#: |
| 1058 | return n.Docscip-go gomod github.com/golang/go/src go1.20 `go/ast`/ValueSpec#Doc. |
| 1059 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.TypeSpecscip-go gomod github.com/golang/go/src go1.20 `go/ast`/TypeSpec#: |
| 1060 | return n.Docscip-go gomod github.com/golang/go/src go1.20 `go/ast`/TypeSpec#Doc. |
| 1061 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.GenDeclscip-go gomod github.com/golang/go/src go1.20 `go/ast`/GenDecl#: |
| 1062 | return n.Docscip-go gomod github.com/golang/go/src go1.20 `go/ast`/GenDecl#Doc. |
| 1063 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.20 `go/ast`/FuncDecl#: |
| 1064 | return n.Docscip-go gomod github.com/golang/go/src go1.20 `go/ast`/FuncDecl#Doc. |
| 1065 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.20 `go/ast`/File#: |
| 1066 | return n.Docscip-go gomod github.com/golang/go/src go1.20 `go/ast`/File#Doc. |
| 1067 | } |
| 1068 | return nil |
| 1069 | } |
| 1070 | |
| 1071 | func getLastCommentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/getLastComment().(n astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.20 `go/ast`/Node#) *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.CommentGroupscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup# { |
| 1072 | switch n := n.(type) { |
| 1073 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Fieldscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Field#: |
| 1074 | return n.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Field#Comment. |
| 1075 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.ImportSpecscip-go gomod github.com/golang/go/src go1.20 `go/ast`/ImportSpec#: |
| 1076 | return n.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/ImportSpec#Comment. |
| 1077 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.ValueSpecscip-go gomod github.com/golang/go/src go1.20 `go/ast`/ValueSpec#: |
| 1078 | return n.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/ValueSpec#Comment. |
| 1079 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.TypeSpecscip-go gomod github.com/golang/go/src go1.20 `go/ast`/TypeSpec#: |
| 1080 | return n.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/TypeSpec#Comment. |
| 1081 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.GenDeclscip-go gomod github.com/golang/go/src go1.20 `go/ast`/GenDecl#: |
| 1082 | if len(n.Specsscip-go gomod github.com/golang/go/src go1.20 `go/ast`/GenDecl#Specs.) > 0 { |
| 1083 | return getLastCommentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/getLastComment().(n.Specsscip-go gomod github.com/golang/go/src go1.20 `go/ast`/GenDecl#Specs.[len(n.Specsscip-go gomod github.com/golang/go/src go1.20 `go/ast`/GenDecl#Specs.)-1]) |
| 1084 | } |
| 1085 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.20 `go/ast`/File#: |
| 1086 | if len(n.Commentsscip-go gomod github.com/golang/go/src go1.20 `go/ast`/File#Comments.) > 0 { |
| 1087 | return n.Commentsscip-go gomod github.com/golang/go/src go1.20 `go/ast`/File#Comments.[len(n.Commentsscip-go gomod github.com/golang/go/src go1.20 `go/ast`/File#Comments.)-1] |
| 1088 | } |
| 1089 | } |
| 1090 | return nil |
| 1091 | } |
| 1092 | |
| 1093 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) printNodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#printNode().(node any) error { |
| 1094 | // unpack *CommentedNode, if any |
| 1095 | var comments []*astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.CommentGroupscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup# |
| 1096 | if cnode, ok := node.(*CommentedNodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/CommentedNode#); ok { |
| 1097 | node = cnode.Nodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/CommentedNode#Node. |
| 1098 | comments = cnode.Commentsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/CommentedNode#Comments. |
| 1099 | } |
| 1100 | |
| 1101 | if comments != nil { |
| 1102 | // commented node - restrict comment list to relevant range |
| 1103 | n, ok := node.(astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.20 `go/ast`/Node#) |
| 1104 | if !ok { |
| 1105 | goto unsupported |
| 1106 | } |
| 1107 | beg := n.Posscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Node#Pos().() |
| 1108 | end := n.Endscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Node#End().() |
| 1109 | // if the node has associated documentation, |
| 1110 | // include that commentgroup in the range |
| 1111 | // (the comment list is sorted in the order |
| 1112 | // of the comment appearance in the source code) |
| 1113 | if doc := getDocscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/getDoc().(n); doc != nil { |
| 1114 | beg = doc.Posscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#Pos().() |
| 1115 | } |
| 1116 | if com := getLastCommentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/getLastComment().(n); com != nil { |
| 1117 | if e := com.Endscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#End().(); e > end { |
| 1118 | end = e |
| 1119 | } |
| 1120 | } |
| 1121 | // token.Pos values are global offsets, we can |
| 1122 | // compare them directly |
| 1123 | i := 0 |
| 1124 | for i < len(comments) && comments[i].Endscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#End().() < beg { |
| 1125 | i++ |
| 1126 | } |
| 1127 | j := i |
| 1128 | for j < len(comments) && comments[j].Posscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#Pos().() < end { |
| 1129 | j++ |
| 1130 | } |
| 1131 | if i < j { |
| 1132 | p.commentsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#comments. = comments[i:j] |
| 1133 | } |
| 1134 | } else if n, ok := node.(*astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.20 `go/ast`/File#); ok { |
| 1135 | // use ast.File comments, if any |
| 1136 | p.commentsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#comments. = n.Commentsscip-go gomod github.com/golang/go/src go1.20 `go/ast`/File#Comments. |
| 1137 | } |
| 1138 | |
| 1139 | // if there are no comments, use node comments |
| 1140 | p.useNodeCommentsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#useNodeComments. = p.commentsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#comments. == nil |
| 1141 | |
| 1142 | // get comments ready for use |
| 1143 | p.nextCommentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#nextComment().() |
| 1144 | |
| 1145 | p.printscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#print().(pmodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/pmode#(0)) |
| 1146 | |
| 1147 | // format node |
| 1148 | switch n := node.(type) { |
| 1149 | case astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Expr#: |
| 1150 | p.exprscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#expr().(n) |
| 1151 | case astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Stmt#: |
| 1152 | // A labeled statement will un-indent to position the label. |
| 1153 | // Set p.indent to 1 so we don't get indent "underflow". |
| 1154 | if _, ok := n.(*astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.LabeledStmtscip-go gomod github.com/golang/go/src go1.20 `go/ast`/LabeledStmt#); ok { |
| 1155 | p.indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent. = 1 |
| 1156 | } |
| 1157 | p.stmtscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#stmt().(n, false) |
| 1158 | case astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Declscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Decl#: |
| 1159 | p.declscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#decl().(n) |
| 1160 | case astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Specscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Spec#: |
| 1161 | p.specscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#spec().(n, 1, false) |
| 1162 | case []astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Stmt#: |
| 1163 | // A labeled statement will un-indent to position the label. |
| 1164 | // Set p.indent to 1 so we don't get indent "underflow". |
| 1165 | for _, s := range n { |
| 1166 | if _, ok := s.(*astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.LabeledStmtscip-go gomod github.com/golang/go/src go1.20 `go/ast`/LabeledStmt#); ok { |
| 1167 | p.indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#indent. = 1 |
| 1168 | } |
| 1169 | } |
| 1170 | p.stmtListscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#stmtList().(n, 0, false) |
| 1171 | case []astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Declscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Decl#: |
| 1172 | p.declListscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#declList().(n) |
| 1173 | case *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.20 `go/ast`/File#: |
| 1174 | p.filescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#file().(n) |
| 1175 | default: |
| 1176 | goto unsupported |
| 1177 | } |
| 1178 | |
| 1179 | return p.sourcePosErrscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#sourcePosErr. |
| 1180 | |
| 1181 | unsupported: |
| 1182 | return fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.20 fmt/Errorf().("mvdan.cc/gofumpt/internal/govendor/go/printer: unsupported node type %T", node) |
| 1183 | } |
| 1184 | |
| 1185 | // ---------------------------------------------------------------------------- |
| 1186 | // Trimmer |
| 1187 | |
| 1188 | // A trimmer is an io.Writer filter for stripping tabwriter.Escape |
| 1189 | // characters, trailing blanks and tabs, and for converting formfeed |
| 1190 | // and vtab characters into newlines and htabs (in case no tabwriter |
| 1191 | // is used). Text bracketed by tabwriter.Escape characters is passed |
| 1192 | // through unchanged. |
| 1193 | type trimmerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer# struct { |
| 1194 | outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#output. ioscip-go gomod github.com/golang/go/src go1.20 io/.Writerscip-go gomod github.com/golang/go/src go1.20 io/Writer# |
| 1195 | statescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#state. int |
| 1196 | spacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#space. []byte |
| 1197 | } |
| 1198 | |
| 1199 | // trimmer is implemented as a state machine. |
| 1200 | // It can be in one of the following states: |
| 1201 | const ( |
| 1202 | inSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/inSpace. = iota // inside space |
| 1203 | inEscapescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/inEscape. // inside text bracketed by tabwriter.Escapes |
| 1204 | inTextscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/inText. // inside text |
| 1205 | ) |
| 1206 | |
| 1207 | func (p *trimmerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#) resetSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#resetSpace().() { |
| 1208 | p.statescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#state. = inSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/inSpace. |
| 1209 | p.spacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#space. = p.spacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#space.[0:0] |
| 1210 | } |
| 1211 | |
| 1212 | // Design note: It is tempting to eliminate extra blanks occurring in |
| 1213 | // whitespace in this function as it could simplify some |
| 1214 | // of the blanks logic in the node printing functions. |
| 1215 | // However, this would mess up any formatting done by |
| 1216 | // the tabwriter. |
| 1217 | |
| 1218 | var aNewlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/aNewline. = []byte("\n") |
| 1219 | |
| 1220 | func (p *trimmerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#) Writescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#Write().(data []byte) (n int, err error) { |
| 1221 | // invariants: |
| 1222 | // p.state == inSpace: |
| 1223 | // p.space is unwritten |
| 1224 | // p.state == inEscape, inText: |
| 1225 | // data[m:n] is unwritten |
| 1226 | m := 0 |
| 1227 | var b byte |
| 1228 | for n, b = range data { |
| 1229 | if b == '\v' { |
| 1230 | b = '\t' // convert to htab |
| 1231 | } |
| 1232 | switch p.statescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#state. { |
| 1233 | case inSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/inSpace.: |
| 1234 | switch b { |
| 1235 | case '\t', ' ': |
| 1236 | p.spacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#space. = append(p.spacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#space., b) |
| 1237 | case '\n', '\f': |
| 1238 | p.resetSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#resetSpace().() // discard trailing space |
| 1239 | _, err = p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#output..Writescip-go gomod github.com/golang/go/src go1.20 io/Writer#Write().(aNewlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/aNewline.) |
| 1240 | case tabwriterscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/.Escapescip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/Escape.: |
| 1241 | _, err = p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#output..Writescip-go gomod github.com/golang/go/src go1.20 io/Writer#Write().(p.spacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#space.) |
| 1242 | p.statescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#state. = inEscapescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/inEscape. |
| 1243 | m = n + 1 // +1: skip tabwriter.Escape |
| 1244 | default: |
| 1245 | _, err = p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#output..Writescip-go gomod github.com/golang/go/src go1.20 io/Writer#Write().(p.spacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#space.) |
| 1246 | p.statescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#state. = inTextscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/inText. |
| 1247 | m = n |
| 1248 | } |
| 1249 | case inEscapescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/inEscape.: |
| 1250 | if b == tabwriterscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/.Escapescip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/Escape. { |
| 1251 | _, err = p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#output..Writescip-go gomod github.com/golang/go/src go1.20 io/Writer#Write().(data[m:n]) |
| 1252 | p.resetSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#resetSpace().() |
| 1253 | } |
| 1254 | case inTextscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/inText.: |
| 1255 | switch b { |
| 1256 | case '\t', ' ': |
| 1257 | _, err = p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#output..Writescip-go gomod github.com/golang/go/src go1.20 io/Writer#Write().(data[m:n]) |
| 1258 | p.resetSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#resetSpace().() |
| 1259 | p.spacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#space. = append(p.spacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#space., b) |
| 1260 | case '\n', '\f': |
| 1261 | _, err = p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#output..Writescip-go gomod github.com/golang/go/src go1.20 io/Writer#Write().(data[m:n]) |
| 1262 | p.resetSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#resetSpace().() |
| 1263 | if err == nil { |
| 1264 | _, err = p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#output..Writescip-go gomod github.com/golang/go/src go1.20 io/Writer#Write().(aNewlinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/aNewline.) |
| 1265 | } |
| 1266 | case tabwriterscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/.Escapescip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/Escape.: |
| 1267 | _, err = p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#output..Writescip-go gomod github.com/golang/go/src go1.20 io/Writer#Write().(data[m:n]) |
| 1268 | p.statescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#state. = inEscapescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/inEscape. |
| 1269 | m = n + 1 // +1: skip tabwriter.Escape |
| 1270 | } |
| 1271 | default: |
| 1272 | panic("unreachable") |
| 1273 | } |
| 1274 | if err != nil { |
| 1275 | return |
| 1276 | } |
| 1277 | } |
| 1278 | n = len(data) |
| 1279 | |
| 1280 | switch p.statescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#state. { |
| 1281 | case inEscapescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/inEscape., inTextscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/inText.: |
| 1282 | _, err = p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#output..Writescip-go gomod github.com/golang/go/src go1.20 io/Writer#Write().(data[m:n]) |
| 1283 | p.resetSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#resetSpace().() |
| 1284 | } |
| 1285 | |
| 1286 | return |
| 1287 | } |
| 1288 | |
| 1289 | // ---------------------------------------------------------------------------- |
| 1290 | // Public interface |
| 1291 | |
| 1292 | // A Mode value is a set of flags (or 0). They control printing. |
| 1293 | type Modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Mode# uint |
| 1294 | |
| 1295 | const ( |
| 1296 | RawFormatscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/RawFormat. Modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Mode# = 1 << iota // do not use a tabwriter; if set, UseSpaces is ignored |
| 1297 | TabIndentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/TabIndent. // use tabs for indentation independent of UseSpaces |
| 1298 | UseSpacesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/UseSpaces. // use spaces instead of tabs for alignment |
| 1299 | SourcePosscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/SourcePos. // emit //line directives to preserve original source positions |
| 1300 | ) |
| 1301 | |
| 1302 | // The mode below is not included in printer's public API because |
| 1303 | // editing code text is deemed out of scope. Because this mode is |
| 1304 | // unexported, it's also possible to modify or remove it based on |
| 1305 | // the evolving needs of mvdan.cc/gofumpt/internal/govendor/go/format and cmd/gofmt without breaking |
| 1306 | // users. See discussion in CL 240683. |
| 1307 | const ( |
| 1308 | // normalizeNumbers means to canonicalize number |
| 1309 | // literal prefixes and exponents while printing. |
| 1310 | // |
| 1311 | // This value is known in and used by mvdan.cc/gofumpt/internal/govendor/go/format and cmd/gofmt. |
| 1312 | // It is currently more convenient and performant for those |
| 1313 | // packages to apply number normalization during printing, |
| 1314 | // rather than by modifying the AST in advance. |
| 1315 | normalizeNumbersscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/normalizeNumbers. Modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Mode# = 1 << 30 |
| 1316 | ) |
| 1317 | |
| 1318 | // A Config node controls the output of Fprint. |
| 1319 | type Configscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config# struct { |
| 1320 | Modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Mode. Modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Mode# // default: 0 |
| 1321 | Tabwidthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Tabwidth. int // default: 8 |
| 1322 | Indentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Indent. int // default: 0 (all code is indented at least by this much) |
| 1323 | } |
| 1324 | |
| 1325 | var printerPoolscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printerPool. = syncscip-go gomod github.com/golang/go/src go1.20 sync/.Poolscip-go gomod github.com/golang/go/src go1.20 sync/Pool#{ |
| 1326 | Newscip-go gomod github.com/golang/go/src go1.20 sync/Pool#New.: func() any { |
| 1327 | return &printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#{ |
| 1328 | // Whitespace sequences are short. |
| 1329 | wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.: make([]whiteSpacescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/whiteSpace#, 0, 16), |
| 1330 | // We start the printer with a 16K output buffer, which is currently |
| 1331 | // larger than about 80% of Go files in the standard library. |
| 1332 | outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output.: make([]byte, 0, 16<<10), |
| 1333 | } |
| 1334 | }, |
| 1335 | } |
| 1336 | |
| 1337 | func newPrinterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/newPrinter().(cfg *Configscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#, fset *tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.20 `go/token`/FileSet#, nodeSizes map[astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.20 `go/ast`/Node#]int) *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer# { |
| 1338 | p := printerPoolscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printerPool..Getscip-go gomod github.com/golang/go/src go1.20 sync/Pool#Get().().(*printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) |
| 1339 | *p = printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#{ |
| 1340 | Configscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#Config.: *cfg, |
| 1341 | fsetscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#fset.: fset, |
| 1342 | posscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#pos.: tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#{Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line.: 1, Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column.: 1}, |
| 1343 | outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#out.: tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#{Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line.: 1, Columnscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Column.: 1}, |
| 1344 | wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.: p.wsbufscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#wsbuf.[:0], |
| 1345 | nodeSizesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#nodeSizes.: nodeSizes, |
| 1346 | cachedPosscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#cachedPos.: -1, |
| 1347 | outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output.: p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output.[:0], |
| 1348 | } |
| 1349 | return p |
| 1350 | } |
| 1351 | |
| 1352 | func (p *printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#) freescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#free().() { |
| 1353 | // Hard limit on buffer size; see https://golang.org/issue/23199. |
| 1354 | if cap(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output.) > 64<<10 { |
| 1355 | return |
| 1356 | } |
| 1357 | |
| 1358 | printerPoolscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printerPool..Putscip-go gomod github.com/golang/go/src go1.20 sync/Pool#Put().(p) |
| 1359 | } |
| 1360 | |
| 1361 | // fprint implements Fprint and takes a nodesSizes map for setting up the printer state. |
| 1362 | func (cfg *Configscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#) fprintscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#fprint().(output ioscip-go gomod github.com/golang/go/src go1.20 io/.Writerscip-go gomod github.com/golang/go/src go1.20 io/Writer#, fset *tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.20 `go/token`/FileSet#, node any, nodeSizes map[astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.20 `go/ast`/Node#]int) (err error) { |
| 1363 | // print node |
| 1364 | p := newPrinterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/newPrinter().(cfg, fset, nodeSizes) |
| 1365 | defer p.freescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#free().() |
| 1366 | if err = p.printNodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#printNode().(node); err != nil { |
| 1367 | return |
| 1368 | } |
| 1369 | // print outstanding comments |
| 1370 | p.impliedSemiscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#impliedSemi. = false // EOF acts like a newline |
| 1371 | p.flushscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#flush().(tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#{Offsetscip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Offset.: infinityscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/infinity., Linescip-go gomod github.com/golang/go/src go1.20 `go/token`/Position#Line.: infinityscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/infinity.}, tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.EOFscip-go gomod github.com/golang/go/src go1.20 `go/token`/EOF.) |
| 1372 | |
| 1373 | // output is buffered in p.output now. |
| 1374 | // fix //go:build and // +build comments if needed. |
| 1375 | p.fixGoBuildLinesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#fixGoBuildLines().() |
| 1376 | |
| 1377 | // redirect output through a trimmer to eliminate trailing whitespace |
| 1378 | // (Input to a tabwriter must be untrimmed since trailing tabs provide |
| 1379 | // formatting information. The tabwriter could provide trimming |
| 1380 | // functionality but no tabwriter is used when RawFormat is set.) |
| 1381 | output = &trimmerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#{outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/trimmer#output.: output} |
| 1382 | |
| 1383 | // redirect output through a tabwriter if necessary |
| 1384 | if cfg.Modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Mode.&RawFormatscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/RawFormat. == 0 { |
| 1385 | minwidth := cfg.Tabwidthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Tabwidth. |
| 1386 | |
| 1387 | padchar := byte('\t') |
| 1388 | if cfg.Modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Mode.&UseSpacesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/UseSpaces. != 0 { |
| 1389 | padchar = ' ' |
| 1390 | } |
| 1391 | |
| 1392 | twmode := tabwriterscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/.DiscardEmptyColumnsscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/DiscardEmptyColumns. |
| 1393 | if cfg.Modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Mode.&TabIndentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/TabIndent. != 0 { |
| 1394 | minwidth = 0 |
| 1395 | twmode |= tabwriterscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/.TabIndentscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/TabIndent. |
| 1396 | } |
| 1397 | |
| 1398 | output = tabwriterscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/.NewWriterscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/NewWriter().(output, minwidth, cfg.Tabwidthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Tabwidth., 1, padchar, twmode) |
| 1399 | } |
| 1400 | |
| 1401 | // write printer result via tabwriter/trimmer to output |
| 1402 | if _, err = output.Writescip-go gomod github.com/golang/go/src go1.20 io/Writer#Write().(p.outputscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/printer#output.); err != nil { |
| 1403 | return |
| 1404 | } |
| 1405 | |
| 1406 | // flush tabwriter, if any |
| 1407 | if tw, _ := output.(*tabwriterscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/.Writerscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/Writer#); tw != nil { |
| 1408 | err = tw.Flushscip-go gomod github.com/golang/go/src go1.20 `text/tabwriter`/Writer#Flush().() |
| 1409 | } |
| 1410 | |
| 1411 | return |
| 1412 | } |
| 1413 | |
| 1414 | // A CommentedNode bundles an AST node and corresponding comments. |
| 1415 | // It may be provided as argument to any of the Fprint functions. |
| 1416 | type CommentedNodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/CommentedNode# struct { |
| 1417 | Nodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/CommentedNode#Node. any // *ast.File, or ast.Expr, ast.Decl, ast.Spec, or ast.Stmt |
| 1418 | Commentsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/CommentedNode#Comments. []*astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.CommentGroupscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup# |
| 1419 | } |
| 1420 | |
| 1421 | // Fprint "pretty-prints" an AST node to output for a given configuration cfg. |
| 1422 | // Position information is interpreted relative to the file set fset. |
| 1423 | // The node type must be *ast.File, *CommentedNode, []ast.Decl, []ast.Stmt, |
| 1424 | // or assignment-compatible to ast.Expr, ast.Decl, ast.Spec, or ast.Stmt. |
| 1425 | func (cfg *Configscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#) Fprintscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Fprint().(output ioscip-go gomod github.com/golang/go/src go1.20 io/.Writerscip-go gomod github.com/golang/go/src go1.20 io/Writer#, fset *tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.20 `go/token`/FileSet#, node any) error { |
| 1426 | return cfg.fprintscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#fprint().(output, fset, node, make(map[astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.20 `go/ast`/Node#]int)) |
| 1427 | } |
| 1428 | |
| 1429 | // Fprint "pretty-prints" an AST node to output. |
| 1430 | // It calls Config.Fprint with default settings. |
| 1431 | // Note that gofmt uses tabs for indentation but spaces for alignment; |
| 1432 | // use format.Node (package mvdan.cc/gofumpt/internal/govendor/go/format) for output that matches gofmt. |
| 1433 | func Fprintscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Fprint().(output ioscip-go gomod github.com/golang/go/src go1.20 io/.Writerscip-go gomod github.com/golang/go/src go1.20 io/Writer#, fset *tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.20 `go/token`/FileSet#, node any) error { |
| 1434 | return (&Configscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#{Tabwidthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Tabwidth.: 8}).Fprintscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Fprint().(output, fset, node) |
| 1435 | } |