| 1 | // Copyright 2013 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 astutil contains common utilities for working with the Go AST. |
| 6 | package astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/ // import "golang.org/x/tools/go/ast/astutil" |
| 7 | |
| 8 | import ( |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 10 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 11 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 12 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 13 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 14 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 15 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 16 | ) |
| 17 | |
| 18 | // AddImport adds the import path to the file f, if absent. |
| 19 | func AddImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/AddImport().(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#, f *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#, path string) (added bool) { |
| 20 | return AddNamedImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/AddNamedImport().(fset, f, "", path) |
| 21 | } |
| 22 | |
| 23 | // AddNamedImport adds the import with the given name and path to the file f, if absent. |
| 24 | // If name is not empty, it is used to rename the import. |
| 25 | // |
| 26 | // For example, calling |
| 27 | // |
| 28 | // AddNamedImport(fset, f, "pathpkg", "path") |
| 29 | // |
| 30 | // adds |
| 31 | // |
| 32 | // import pathpkg "path" |
| 33 | func AddNamedImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/AddNamedImport().(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#, f *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#, name, path string) (added bool) { |
| 34 | if importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/imports().(f, name, path) { |
| 35 | return false |
| 36 | } |
| 37 | |
| 38 | newImport := &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#{ |
| 39 | Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path.: &astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#{ |
| 40 | Kindscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Kind.: tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.STRINGscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/STRING., |
| 41 | Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value.: strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Quotescip-go gomod github.com/golang/go/src go1.25.0 strconv/Quote().(path), |
| 42 | }, |
| 43 | } |
| 44 | if name != "" { |
| 45 | newImport.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Name. = &astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#{Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.: name} |
| 46 | } |
| 47 | |
| 48 | // Find an import decl to add to. |
| 49 | // The goal is to find an existing import |
| 50 | // whose import path has the longest shared |
| 51 | // prefix with path. |
| 52 | var ( |
| 53 | bestMatch = -1 // length of longest shared prefix |
| 54 | lastImport = -1 // index in f.Decls of the file's final import decl |
| 55 | impDecl *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# // import decl containing the best match |
| 56 | impIndex = -1 // spec index in impDecl containing the best match |
| 57 | |
| 58 | isThirdPartyPath = isThirdPartyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/isThirdParty().(path) |
| 59 | ) |
| 60 | for i, decl := range f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls. { |
| 61 | gen, ok := decl.(*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#) |
| 62 | if ok && gen.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Tok. == tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.IMPORTscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/IMPORT. { |
| 63 | lastImport = i |
| 64 | // Do not add to import "C", to avoid disrupting the |
| 65 | // association with its doc comment, breaking cgo. |
| 66 | if declImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/declImports().(gen, "C") { |
| 67 | continue |
| 68 | } |
| 69 | |
| 70 | // Match an empty import decl if that's all that is available. |
| 71 | if len(gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.) == 0 && bestMatch == -1 { |
| 72 | impDecl = gen |
| 73 | } |
| 74 | |
| 75 | // Compute longest shared prefix with imports in this group and find best |
| 76 | // matched import spec. |
| 77 | // 1. Always prefer import spec with longest shared prefix. |
| 78 | // 2. While match length is 0, |
| 79 | // - for stdlib package: prefer first import spec. |
| 80 | // - for third party package: prefer first third party import spec. |
| 81 | // We cannot use last import spec as best match for third party package |
| 82 | // because grouped imports are usually placed last by goimports -local |
| 83 | // flag. |
| 84 | // See issue #19190. |
| 85 | seenAnyThirdParty := false |
| 86 | for j, spec := range gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. { |
| 87 | impspec := spec.(*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#) |
| 88 | p := importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/importPath().(impspec) |
| 89 | n := matchLenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/matchLen().(p, path) |
| 90 | if n > bestMatch || (bestMatch == 0 && !seenAnyThirdParty && isThirdPartyPath) { |
| 91 | bestMatch = n |
| 92 | impDecl = gen |
| 93 | impIndex = j |
| 94 | } |
| 95 | seenAnyThirdParty = seenAnyThirdParty || isThirdPartyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/isThirdParty().(p) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // If no import decl found, add one after the last import. |
| 101 | if impDecl == nil { |
| 102 | impDecl = &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#{ |
| 103 | Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Tok.: tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.IMPORTscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/IMPORT., |
| 104 | } |
| 105 | if lastImport >= 0 { |
| 106 | impDecl.TokPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#TokPos. = f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.[lastImport].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().() |
| 107 | } else { |
| 108 | // There are no existing imports. |
| 109 | // Our new import, preceded by a blank line, goes after the package declaration |
| 110 | // and after the comment, if any, that starts on the same line as the |
| 111 | // package declaration. |
| 112 | impDecl.TokPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#TokPos. = f.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Package. |
| 113 | |
| 114 | file := fset.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(f.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Package.) |
| 115 | pkgLine := file.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Line().(f.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Package.) |
| 116 | for _, c := range f.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments. { |
| 117 | if file.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Line().(c.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().()) > pkgLine { |
| 118 | break |
| 119 | } |
| 120 | // +2 for a blank line |
| 121 | impDecl.TokPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#TokPos. = c.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#End().() + 2 |
| 122 | } |
| 123 | } |
| 124 | f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls. = append(f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls., nil) |
| 125 | copy(f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.[lastImport+2:], f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.[lastImport+1:]) |
| 126 | f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.[lastImport+1] = impDecl |
| 127 | } |
| 128 | |
| 129 | // Insert new import at insertAt. |
| 130 | insertAt := 0 |
| 131 | if impIndex >= 0 { |
| 132 | // insert after the found import |
| 133 | insertAt = impIndex + 1 |
| 134 | } |
| 135 | impDecl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. = append(impDecl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs., nil) |
| 136 | copy(impDecl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[insertAt+1:], impDecl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[insertAt:]) |
| 137 | impDecl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[insertAt] = newImport |
| 138 | pos := impDecl.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Pos().() |
| 139 | if insertAt > 0 { |
| 140 | // If there is a comment after an existing import, preserve the comment |
| 141 | // position by adding the new import after the comment. |
| 142 | if spec, ok := impDecl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[insertAt-1].(*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#); ok && spec.Commentscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Comment. != nil { |
| 143 | pos = spec.Commentscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Comment..Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#End().() |
| 144 | } else { |
| 145 | // Assign same position as the previous import, |
| 146 | // so that the sorter sees it as being in the same block. |
| 147 | pos = impDecl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[insertAt-1].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() |
| 148 | } |
| 149 | } |
| 150 | if newImport.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Name. != nil { |
| 151 | newImport.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Name..NamePosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#NamePos. = pos |
| 152 | } |
| 153 | updateBasicLitPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/updateBasicLitPos().(newImport.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path., pos) |
| 154 | newImport.EndPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#EndPos. = pos |
| 155 | |
| 156 | // Clean up parens. impDecl contains at least one spec. |
| 157 | if len(impDecl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.) == 1 { |
| 158 | // Remove unneeded parens. |
| 159 | impDecl.Lparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Lparen. = tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NoPosscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NoPos. |
| 160 | } else if !impDecl.Lparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Lparen..IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() { |
| 161 | // impDecl needs parens added. |
| 162 | impDecl.Lparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Lparen. = impDecl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() |
| 163 | } |
| 164 | |
| 165 | f.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Imports. = append(f.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Imports., newImport) |
| 166 | |
| 167 | if len(f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.) <= 1 { |
| 168 | return true |
| 169 | } |
| 170 | |
| 171 | // Merge all the import declarations into the first one. |
| 172 | var first *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# |
| 173 | for i := 0; i < len(f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.); i++ { |
| 174 | decl := f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.[i] |
| 175 | gen, ok := decl.(*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#) |
| 176 | if !ok || gen.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Tok. != tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.IMPORTscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/IMPORT. || declImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/declImports().(gen, "C") { |
| 177 | continue |
| 178 | } |
| 179 | if first == nil { |
| 180 | first = gen |
| 181 | continue // Don't touch the first one. |
| 182 | } |
| 183 | // We now know there is more than one package in this import |
| 184 | // declaration. Ensure that it ends up parenthesized. |
| 185 | first.Lparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Lparen. = first.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Pos().() |
| 186 | // Move the imports of the other import declaration to the first one. |
| 187 | for _, spec := range gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. { |
| 188 | updateBasicLitPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/updateBasicLitPos().(spec.(*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#).Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path., first.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Pos().()) |
| 189 | first.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. = append(first.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs., spec) |
| 190 | } |
| 191 | f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls. = slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Deletescip-go gomod github.com/golang/go/src go1.25.0 slices/Delete().(f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls., i, i+1) |
| 192 | i-- |
| 193 | } |
| 194 | |
| 195 | return true |
| 196 | } |
| 197 | |
| 198 | func isThirdPartyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/isThirdParty().(importPath string) bool { |
| 199 | // Third party package import path usually contains "." (".com", ".org", ...) |
| 200 | // This logic is taken from golang.org/x/tools/imports package. |
| 201 | return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(importPath, ".") |
| 202 | } |
| 203 | |
| 204 | // DeleteImport deletes the import path from the file f, if present. |
| 205 | // If there are duplicate import declarations, all matching ones are deleted. |
| 206 | func DeleteImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/DeleteImport().(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#, f *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#, path string) (deleted bool) { |
| 207 | return DeleteNamedImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/DeleteNamedImport().(fset, f, "", path) |
| 208 | } |
| 209 | |
| 210 | // DeleteNamedImport deletes the import with the given name and path from the file f, if present. |
| 211 | // If there are duplicate import declarations, all matching ones are deleted. |
| 212 | func DeleteNamedImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/DeleteNamedImport().(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#, f *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#, name, path string) (deleted bool) { |
| 213 | var ( |
| 214 | delspecs = make(map[*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#]bool) |
| 215 | delcomments = make(map[*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#]bool) |
| 216 | ) |
| 217 | |
| 218 | // Find the import nodes that import path, if any. |
| 219 | for i := 0; i < len(f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.); i++ { |
| 220 | gen, ok := f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.[i].(*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#) |
| 221 | if !ok || gen.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Tok. != tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.IMPORTscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/IMPORT. { |
| 222 | continue |
| 223 | } |
| 224 | for j := 0; j < len(gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.); j++ { |
| 225 | impspec := gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[j].(*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#) |
| 226 | if importNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/importName().(impspec) != name || importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/importPath().(impspec) != path { |
| 227 | continue |
| 228 | } |
| 229 | |
| 230 | // We found an import spec that imports path. |
| 231 | // Delete it. |
| 232 | delspecs[impspec] = true |
| 233 | deleted = true |
| 234 | gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. = slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Deletescip-go gomod github.com/golang/go/src go1.25.0 slices/Delete().(gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs., j, j+1) |
| 235 | |
| 236 | // If this was the last import spec in this decl, |
| 237 | // delete the decl, too. |
| 238 | if len(gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.) == 0 { |
| 239 | f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls. = slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Deletescip-go gomod github.com/golang/go/src go1.25.0 slices/Delete().(f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls., i, i+1) |
| 240 | i-- |
| 241 | break |
| 242 | } else if len(gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.) == 1 { |
| 243 | if impspec.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Doc. != nil { |
| 244 | delcomments[impspec.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Doc.] = true |
| 245 | } |
| 246 | if impspec.Commentscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Comment. != nil { |
| 247 | delcomments[impspec.Commentscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Comment.] = true |
| 248 | } |
| 249 | for _, cg := range f.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments. { |
| 250 | // Found comment on the same line as the import spec. |
| 251 | if cg.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#End().() < impspec.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Pos().() && fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(cg.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#End().()).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line. == fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(impspec.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Pos().()).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line. { |
| 252 | delcomments[cg] = true |
| 253 | break |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | spec := gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[0].(*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#) |
| 258 | |
| 259 | // Move the documentation right after the import decl. |
| 260 | if spec.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Doc. != nil { |
| 261 | for fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(gen.TokPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#TokPos.).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line.+1 < fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(spec.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Doc..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().()).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line. { |
| 262 | fset.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(gen.TokPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#TokPos.).MergeLinescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#MergeLine().(fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(gen.TokPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#TokPos.).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line.) |
| 263 | } |
| 264 | } |
| 265 | for _, cg := range f.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments. { |
| 266 | if cg.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#End().() < spec.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Pos().() && fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(cg.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#End().()).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line. == fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(spec.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Pos().()).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line. { |
| 267 | for fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(gen.TokPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#TokPos.).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line.+1 < fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(spec.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Pos().()).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line. { |
| 268 | fset.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(gen.TokPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#TokPos.).MergeLinescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#MergeLine().(fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(gen.TokPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#TokPos.).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line.) |
| 269 | } |
| 270 | break |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | if j > 0 { |
| 275 | lastImpspec := gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[j-1].(*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#) |
| 276 | lastLine := fset.PositionForscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#PositionFor().(lastImpspec.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path..ValuePosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#ValuePos., false).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line. |
| 277 | line := fset.PositionForscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#PositionFor().(impspec.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path..ValuePosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#ValuePos., false).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line. |
| 278 | |
| 279 | // We deleted an entry but now there may be |
| 280 | // a blank line-sized hole where the import was. |
| 281 | if line-lastLine > 1 || !gen.Rparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Rparen..IsValidscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#IsValid().() { |
| 282 | // There was a blank line immediately preceding the deleted import, |
| 283 | // so there's no need to close the hole. The right parenthesis is |
| 284 | // invalid after AddImport to an import statement without parenthesis. |
| 285 | // Do nothing. |
| 286 | } else if line != fset.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(gen.Rparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Rparen.).LineCountscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#LineCount().() { |
| 287 | // There was no blank line. Close the hole. |
| 288 | fset.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(gen.Rparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Rparen.).MergeLinescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#MergeLine().(line) |
| 289 | } |
| 290 | } |
| 291 | j-- |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // Delete imports from f.Imports. |
| 296 | before := len(f.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Imports.) |
| 297 | f.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Imports. = slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.DeleteFuncscip-go gomod github.com/golang/go/src go1.25.0 slices/DeleteFunc().(f.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Imports., func(imp *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#) bool { |
| 298 | _, ok := delspecs[imp] |
| 299 | return ok |
| 300 | }) |
| 301 | if len(f.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Imports.)+len(delspecs) != before { |
| 302 | // This can happen when the AST is invalid (i.e. imports differ between f.Decls and f.Imports). |
| 303 | panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("deleted specs from Decls but not Imports: %v", delspecs)) |
| 304 | } |
| 305 | |
| 306 | // Delete comments from f.Comments. |
| 307 | f.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments. = slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.DeleteFuncscip-go gomod github.com/golang/go/src go1.25.0 slices/DeleteFunc().(f.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments., func(cg *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#) bool { |
| 308 | _, ok := delcomments[cg] |
| 309 | return ok |
| 310 | }) |
| 311 | |
| 312 | return |
| 313 | } |
| 314 | |
| 315 | // RewriteImport rewrites any import of path oldPath to path newPath. |
| 316 | func RewriteImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/RewriteImport().(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#, f *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#, oldPath, newPath string) (rewrote bool) { |
| 317 | for _, imp := range f.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Imports. { |
| 318 | if importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/importPath().(imp) == oldPath { |
| 319 | rewrote = true |
| 320 | // record old End, because the default is to compute |
| 321 | // it using the length of imp.Path.Value. |
| 322 | imp.EndPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#EndPos. = imp.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#End().() |
| 323 | imp.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path..Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value. = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Quotescip-go gomod github.com/golang/go/src go1.25.0 strconv/Quote().(newPath) |
| 324 | } |
| 325 | } |
| 326 | return |
| 327 | } |
| 328 | |
| 329 | // UsesImport reports whether a given import is used. |
| 330 | // The provided File must have been parsed with syntactic object resolution |
| 331 | // (not using go/parser.SkipObjectResolution). |
| 332 | func UsesImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/UsesImport().(f *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#, path string) (used bool) { |
| 333 | if f.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Scope. == nil { |
| 334 | panic("file f was not parsed with syntactic object resolution") |
| 335 | } |
| 336 | spec := importSpecscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/importSpec().(f, path) |
| 337 | if spec == nil { |
| 338 | return |
| 339 | } |
| 340 | |
| 341 | name := spec.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Name..Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#String().() |
| 342 | switch name { |
| 343 | case "<nil>": |
| 344 | // If the package name is not explicitly specified, |
| 345 | // make an educated guess. This is not guaranteed to be correct. |
| 346 | lastSlash := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.25.0 strings/LastIndex().(path, "/") |
| 347 | if lastSlash == -1 { |
| 348 | name = path |
| 349 | } else { |
| 350 | name = path[lastSlash+1:] |
| 351 | } |
| 352 | case "_", ".": |
| 353 | // Not sure if this import is used - err on the side of caution. |
| 354 | return true |
| 355 | } |
| 356 | |
| 357 | astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Walkscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Walk().(visitFnscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/visitFn#(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#) { |
| 358 | sel, ok := n.(*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#) |
| 359 | if ok && isTopNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/isTopName().(sel.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#X., name) { |
| 360 | used = true |
| 361 | } |
| 362 | }), f) |
| 363 | |
| 364 | return |
| 365 | } |
| 366 | |
| 367 | type visitFnscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/visitFn# func(node 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#) |
| 368 | |
| 369 | func (fn visitFnscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/visitFn#) Visitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/visitFn#Visit().(node 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`/.Visitorscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Visitor# { |
| 370 | fn(node) |
| 371 | return fn |
| 372 | } |
| 373 | |
| 374 | // imports reports whether f has an import with the specified name and path. |
| 375 | func importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/imports().(f *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#, name, path string) bool { |
| 376 | for _, s := range f.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Imports. { |
| 377 | if importNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/importName().(s) == name && importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/importPath().(s) == path { |
| 378 | return true |
| 379 | } |
| 380 | } |
| 381 | return false |
| 382 | } |
| 383 | |
| 384 | // importSpec returns the import spec if f imports path, |
| 385 | // or nil otherwise. |
| 386 | func importSpecscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/importSpec().(f *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#, path string) *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# { |
| 387 | for _, s := range f.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Imports. { |
| 388 | if importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/importPath().(s) == path { |
| 389 | return s |
| 390 | } |
| 391 | } |
| 392 | return nil |
| 393 | } |
| 394 | |
| 395 | // importName returns the name of s, |
| 396 | // or "" if the import is not named. |
| 397 | func importNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/importName().(s *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#) string { |
| 398 | if s.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Name. == nil { |
| 399 | return "" |
| 400 | } |
| 401 | return s.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. |
| 402 | } |
| 403 | |
| 404 | // importPath returns the unquoted import path of s, |
| 405 | // or "" if the path is not properly quoted. |
| 406 | func importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/importPath().(s *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#) string { |
| 407 | t, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Unquotescip-go gomod github.com/golang/go/src go1.25.0 strconv/Unquote().(s.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path..Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value.) |
| 408 | if err != nil { |
| 409 | return "" |
| 410 | } |
| 411 | return t |
| 412 | } |
| 413 | |
| 414 | // declImports reports whether gen contains an import of path. |
| 415 | func declImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/declImports().(gen *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#, path string) bool { |
| 416 | if gen.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Tok. != tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.IMPORTscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/IMPORT. { |
| 417 | return false |
| 418 | } |
| 419 | for _, spec := range gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. { |
| 420 | impspec := spec.(*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#) |
| 421 | if importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/importPath().(impspec) == path { |
| 422 | return true |
| 423 | } |
| 424 | } |
| 425 | return false |
| 426 | } |
| 427 | |
| 428 | // matchLen returns the length of the longest path segment prefix shared by x and y. |
| 429 | func matchLenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/matchLen().(x, y string) int { |
| 430 | n := 0 |
| 431 | for i := 0; i < len(x) && i < len(y) && x[i] == y[i]; i++ { |
| 432 | if x[i] == '/' { |
| 433 | n++ |
| 434 | } |
| 435 | } |
| 436 | return n |
| 437 | } |
| 438 | |
| 439 | // isTopName returns true if n is a top-level unresolved identifier with the given name. |
| 440 | func isTopNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/isTopName().(n 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#, name string) bool { |
| 441 | id, ok := n.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 442 | return ok && id.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. == name && id.Objscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Obj. == nil |
| 443 | } |
| 444 | |
| 445 | // Imports returns the file imports grouped by paragraph. |
| 446 | func Importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/Imports().(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#, f *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#) [][]*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# { |
| 447 | var groups [][]*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# |
| 448 | |
| 449 | for _, decl := range f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls. { |
| 450 | genDecl, ok := decl.(*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#) |
| 451 | if !ok || genDecl.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Tok. != tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.IMPORTscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/IMPORT. { |
| 452 | break |
| 453 | } |
| 454 | |
| 455 | group := []*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#{} |
| 456 | |
| 457 | var lastLine int |
| 458 | for _, spec := range genDecl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. { |
| 459 | importSpec := spec.(*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#) |
| 460 | pos := importSpec.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path..ValuePosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#ValuePos. |
| 461 | line := fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(pos).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line. |
| 462 | if lastLine > 0 && pos > 0 && line-lastLine > 1 { |
| 463 | groups = append(groups, group) |
| 464 | group = []*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#{} |
| 465 | } |
| 466 | group = append(group, importSpec) |
| 467 | lastLine = line |
| 468 | } |
| 469 | groups = append(groups, group) |
| 470 | } |
| 471 | |
| 472 | return groups |
| 473 | } |
| 474 | |
| 475 | // updateBasicLitPos updates lit.Pos, |
| 476 | // ensuring that lit.End (if set) is displaced by the same amount. |
| 477 | // (See https://go.dev/issue/76395.) |
| 478 | func updateBasicLitPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/updateBasicLitPos().(lit *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#, 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#) { |
| 479 | len := lit.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#End().() - lit.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Pos().() |
| 480 | lit.ValuePosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#ValuePos. = pos |
| 481 | // TODO(adonovan): after go1.26, simplify to: |
| 482 | // lit.ValueEnd = pos + len |
| 483 | v := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(lit).Elemscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Elem().().FieldByNamescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#FieldByName().("ValueEnd") |
| 484 | if v.IsValidscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsValid().() && v.Intscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Int().() != 0 { |
| 485 | v.SetIntscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#SetInt().(int64(pos + len)) |
| 486 | } |
| 487 | } |