| 1 | // Copyright 2025 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 astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 10 | "go/printerscip-go gomod github.com/golang/go/src go1.25.0 `go/printer`/" |
| 11 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 12 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 13 | |
| 14 | "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/" |
| 15 | "golang.org/x/tools/internal/moreitersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/" |
| 16 | ) |
| 17 | |
| 18 | // NodeContains reports whether the Pos/End range of node n encloses |
| 19 | // the given range. |
| 20 | // |
| 21 | // It is inclusive of both end points, to allow hovering (etc) when |
| 22 | // the cursor is immediately after a node. |
| 23 | // |
| 24 | // Like [NodeRange], it treats the range of an [ast.File] as the |
| 25 | // file's complete extent. |
| 26 | // |
| 27 | // Precondition: n must not be nil. |
| 28 | func NodeContainsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/NodeContains().(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#, rng Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#) bool { |
| 29 | return NodeRangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/NodeRange().(n).Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#Contains().(rng) |
| 30 | } |
| 31 | |
| 32 | // NodeContainsPos reports whether the Pos/End range of node n encloses |
| 33 | // the given pos. |
| 34 | // |
| 35 | // Like [NodeRange], it treats the range of an [ast.File] as the |
| 36 | // file's complete extent. |
| 37 | func NodeContainsPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/NodeContainsPos().(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#, pos tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#) bool { |
| 38 | return NodeRangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/NodeRange().(n).ContainsPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#ContainsPos().(pos) |
| 39 | } |
| 40 | |
| 41 | // EnclosingFile returns the syntax tree for the file enclosing c. |
| 42 | // |
| 43 | // TODO(adonovan): promote this to a method of Cursor. |
| 44 | func EnclosingFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EnclosingFile().(c inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File# { |
| 45 | c, _ = moreitersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/.Firstscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/First().(c.Enclosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Enclosing().((*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#)(nil))) |
| 46 | return c.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#) |
| 47 | } |
| 48 | |
| 49 | // DocComment returns the doc comment for a node, if any. |
| 50 | func DocCommentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/DocComment().(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CommentGroupscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup# { |
| 51 | switch n := n.(type) { |
| 52 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#: |
| 53 | return n.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Doc. |
| 54 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.GenDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#: |
| 55 | return n.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Doc. |
| 56 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ValueSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#: |
| 57 | return n.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Doc. |
| 58 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.TypeSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#: |
| 59 | return n.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#Doc. |
| 60 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#: |
| 61 | return n.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Doc. |
| 62 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ImportSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#: |
| 63 | return n.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Doc. |
| 64 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Fieldscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#: |
| 65 | return n.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Doc. |
| 66 | } |
| 67 | return nil |
| 68 | } |
| 69 | |
| 70 | // Format returns a string representation of the node n. |
| 71 | func Formatscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Format().(fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#, n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) string { |
| 72 | var buf stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder# |
| 73 | printerscip-go gomod github.com/golang/go/src go1.25.0 `go/printer`/.Fprintscip-go gomod github.com/golang/go/src go1.25.0 `go/printer`/Fprint().(&buf, fset, n) // ignore errors |
| 74 | return buf.Stringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#String().() |
| 75 | } |
| 76 | |
| 77 | // -- Range -- |
| 78 | |
| 79 | // Range is a Pos interval. |
| 80 | // It implements [analysis.Range] and [ast.Node]. |
| 81 | type Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range# struct{ Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#Start., EndPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#EndPos. tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos# } |
| 82 | |
| 83 | // RangeOf constructs a Range. |
| 84 | // |
| 85 | // RangeOf exists to pacify the "unkeyed literal" (composites) vet |
| 86 | // check. It would be nice if there were a way for a type to add |
| 87 | // itself to the allowlist. |
| 88 | func RangeOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/RangeOf().(start, end tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#) Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range# { return Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#{start, end} } |
| 89 | |
| 90 | // NodeRange returns the extent of node n as a Range. |
| 91 | // |
| 92 | // For unfortunate historical reasons, the Pos/End extent of an |
| 93 | // ast.File runs from the start of its package declaration---excluding |
| 94 | // copyright comments, build tags, and package documentation---to the |
| 95 | // end of its last declaration, excluding any trailing comments. So, |
| 96 | // as a special case, if n is an [ast.File], NodeContains uses |
| 97 | // n.FileStart <= pos && pos <= n.FileEnd to report whether the |
| 98 | // position lies anywhere within the file. |
| 99 | func NodeRangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/NodeRange().(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range# { |
| 100 | if file, ok := n.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#); ok { |
| 101 | return Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#{file.FileStartscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#FileStart., file.FileEndscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#FileEnd.} // entire file |
| 102 | } |
| 103 | return Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#{n.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), n.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()} |
| 104 | } |
| 105 | |
| 106 | func (r Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#) Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#Pos().() tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos# { return r.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#Start. } |
| 107 | func (r Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#) Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#End().() tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos# { return r.EndPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#EndPos. } |
| 108 | |
| 109 | // ContainsPos reports whether the range (inclusive of both end points) |
| 110 | // includes the specified position. |
| 111 | func (r Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#) ContainsPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#ContainsPos().(pos tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#) bool { |
| 112 | return r.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#Contains().(RangeOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/RangeOf().(pos, pos)) |
| 113 | } |
| 114 | |
| 115 | // Contains reports whether the range (inclusive of both end points) |
| 116 | // includes the specified range. |
| 117 | func (r Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#) Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#Contains().(rng Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#) bool { |
| 118 | return r.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#Start. <= rng.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#Start. && rng.EndPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#EndPos. <= r.EndPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#EndPos. |
| 119 | } |
| 120 | |
| 121 | // IsValid reports whether the range is valid. |
| 122 | func (r Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#) IsValidscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#IsValid().() bool { return r.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#Start..IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() && r.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#Start. <= r.EndPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#EndPos. } |
| 123 | |
| 124 | // -- |
| 125 | |
| 126 | // Select returns the syntax nodes identified by a user's text |
| 127 | // selection. It returns three nodes: the innermost node that wholly |
| 128 | // encloses the selection; and the first and last nodes that are |
| 129 | // wholly enclosed by the selection. |
| 130 | // |
| 131 | // For example, given this selection: |
| 132 | // |
| 133 | // { f(); g(); /* comment */ } |
| 134 | // ~~~~~~~~~~~ |
| 135 | // |
| 136 | // Select returns the enclosing BlockStmt, the f() CallExpr, and the g() CallExpr. |
| 137 | // |
| 138 | // If the selection does not wholly enclose any nodes, Select returns an error |
| 139 | // and invalid start/end nodes, but it may return a valid enclosing node. |
| 140 | // |
| 141 | // Callers that require exactly one syntax tree (e.g. just f() or just |
| 142 | // g()) should check that the returned start and end nodes are |
| 143 | // identical. |
| 144 | // |
| 145 | // This function is intended to be called early in the handling of a |
| 146 | // user's request, since it is tolerant of sloppy selection including |
| 147 | // extraneous whitespace and comments. Use it in new code instead of |
| 148 | // PathEnclosingInterval. When the exact extent of a node is known, |
| 149 | // use [Cursor.FindByPos] instead. |
| 150 | // |
| 151 | // TODO(hxjiang): Consider refactoring the function signature. It is currently |
| 152 | // confusing that an error is returned even when a valid enclosing node is |
| 153 | // successfully found. Consider grouping all cursors into one struct. |
| 154 | func Selectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Select().(curFile inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#, start, end tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#) (_enclosing, _start, _end inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#, _ error) { |
| 155 | curEnclosing, ok := curFile.FindByPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#FindByPos().(start, end) |
| 156 | if !ok { |
| 157 | return noCursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/noCursor., noCursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/noCursor., noCursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/noCursor., fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("invalid selection") |
| 158 | } |
| 159 | |
| 160 | // Find the first and last node wholly within the (start, end) range. |
| 161 | // We'll narrow the effective selection to them, to exclude whitespace. |
| 162 | // (This matches the functionality of PathEnclosingInterval.) |
| 163 | var curStart, curEnd inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor# |
| 164 | rng := RangeOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/RangeOf().(start, end) |
| 165 | for cur := range curEnclosing.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().() { |
| 166 | if rng.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/Range#Contains().(NodeRangescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/NodeRange().(cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().())) { |
| 167 | // The start node has the least Pos. |
| 168 | if !curStart.Validscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Valid().() { |
| 169 | curStart = cur |
| 170 | } |
| 171 | // The end node has the greatest End. |
| 172 | // End positions do not change monotonically, |
| 173 | // so we must compute the max. |
| 174 | if !curEnd.Validscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Valid().() || |
| 175 | cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().() > curEnd.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().() { |
| 176 | curEnd = cur |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | if !curStart.Validscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Valid().() { |
| 181 | // The selection is valid (inside curEnclosing) but contains no |
| 182 | // complete nodes. This happens for point selections (start == end), |
| 183 | // or selections covering only only spaces, comments, and punctuation |
| 184 | // tokens. |
| 185 | // Return the enclosing node so the caller can still use the context. |
| 186 | return curEnclosing, noCursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/noCursor., noCursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/noCursor., fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("invalid selection") |
| 187 | } |
| 188 | return curEnclosing, curStart, curEnd, nil |
| 189 | } |
| 190 | |
| 191 | var noCursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/noCursor. inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor# |
| 192 | |
| 193 | // MaybeParenthesize returns new, possibly wrapped in parens if needed |
| 194 | // to preserve operator precedence when it replaces old, whose parent |
| 195 | // is parentNode. |
| 196 | // |
| 197 | // (This would be more naturally written in terms of Cursor, but one of |
| 198 | // the callers--the inliner--does not have cursors handy.) |
| 199 | func MaybeParenthesizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/MaybeParenthesize().(parentNode astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#, old, new astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr# { |
| 200 | if needsParensscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/needsParens().(parentNode, old, new) { |
| 201 | new = &astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ParenExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ParenExpr#{Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ParenExpr#X.: new} |
| 202 | } |
| 203 | return new |
| 204 | } |
| 205 | |
| 206 | func needsParensscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/needsParens().(parentNode astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#, old, new astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) bool { |
| 207 | // An expression beneath a non-expression |
| 208 | // has no precedence ambiguity. |
| 209 | parent, ok := parentNode.(astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) |
| 210 | if !ok { |
| 211 | return false |
| 212 | } |
| 213 | |
| 214 | precedence := func(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) int { |
| 215 | switch n := n.(type) { |
| 216 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.UnaryExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/UnaryExpr#, *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.StarExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/StarExpr#: |
| 217 | return tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.UnaryPrecscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/UnaryPrec. |
| 218 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BinaryExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BinaryExpr#: |
| 219 | return n.Opscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BinaryExpr#Op..Precedencescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Token#Precedence().() |
| 220 | } |
| 221 | return -1 |
| 222 | } |
| 223 | |
| 224 | // Parens are not required if the new node |
| 225 | // is not unary or binary. |
| 226 | newprec := precedence(new) |
| 227 | if newprec < 0 { |
| 228 | return false |
| 229 | } |
| 230 | |
| 231 | // Parens are required if parent and child are both |
| 232 | // unary or binary and the parent has higher precedence. |
| 233 | if precedence(parent) > newprec { |
| 234 | return true |
| 235 | } |
| 236 | |
| 237 | // Was the old node the operand of a postfix operator? |
| 238 | // f().sel |
| 239 | // f()[i:j] |
| 240 | // f()[i] |
| 241 | // f().(T) |
| 242 | // f()(x) |
| 243 | switch parent := parent.(type) { |
| 244 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#: |
| 245 | return parent.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#X. == old |
| 246 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IndexExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IndexExpr#: |
| 247 | return parent.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IndexExpr#X. == old |
| 248 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SliceExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SliceExpr#: |
| 249 | return parent.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SliceExpr#X. == old |
| 250 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.TypeAssertExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeAssertExpr#: |
| 251 | return parent.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeAssertExpr#X. == old |
| 252 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#: |
| 253 | return parent.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun. == old |
| 254 | } |
| 255 | return false |
| 256 | } |
| 257 | |
| 258 | func isscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/is().[T any](n any) bool { |
| 259 | _, ok := n.(T) |
| 260 | return ok |
| 261 | } |