| 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 | // Hacked up copy of go/ast/import.go |
| 6 | // Modified to use a single token.File in preference to a FileSet. |
| 7 | |
| 8 | package importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ |
| 9 | |
| 10 | import ( |
| 11 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 12 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 13 | "logscip-go gomod github.com/golang/go/src go1.25.0 log/" |
| 14 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 15 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 16 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 17 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 18 | ) |
| 19 | |
| 20 | // sortImports sorts runs of consecutive import lines in import blocks in f. |
| 21 | // It also removes duplicate imports when it is possible to do so without data loss. |
| 22 | // |
| 23 | // It may mutate the token.File and the ast.File. |
| 24 | func sortImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/sortImports().(localPrefix string, tokFile *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#, 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#) { |
| 25 | for i, d := range f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls. { |
| 26 | d, ok := d.(*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#) |
| 27 | if !ok || d.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. { |
| 28 | // Not an import declaration, so we're done. |
| 29 | // Imports are always first. |
| 30 | break |
| 31 | } |
| 32 | |
| 33 | if len(d.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.) == 0 { |
| 34 | // Empty import block, remove it. |
| 35 | 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) |
| 36 | } |
| 37 | |
| 38 | if !d.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().() { |
| 39 | // Not a block: sorted by default. |
| 40 | continue |
| 41 | } |
| 42 | |
| 43 | // Identify and sort runs of specs on successive lines. |
| 44 | i := 0 |
| 45 | specs := d.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[:0] |
| 46 | for j, s := range d.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. { |
| 47 | if j > i && tokFile.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Line().(s.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().()) > 1+tokFile.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Line().(d.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[j-1].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()) { |
| 48 | // j begins a new run. End this one. |
| 49 | specs = append(specs, sortSpecsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/sortSpecs().(localPrefix, tokFile, f, d.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[i:j])...) |
| 50 | i = j |
| 51 | } |
| 52 | } |
| 53 | specs = append(specs, sortSpecsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/sortSpecs().(localPrefix, tokFile, f, d.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[i:])...) |
| 54 | d.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. = specs |
| 55 | |
| 56 | // Deduping can leave a blank line before the rparen; clean that up. |
| 57 | // Ignore line directives. |
| 58 | if len(d.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.) > 0 { |
| 59 | lastSpec := d.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.[len(d.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.)-1] |
| 60 | lastLine := tokFile.PositionForscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#PositionFor().(lastSpec.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), false).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line. |
| 61 | if rParenLine := tokFile.PositionForscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#PositionFor().(d.Rparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Rparen., false).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line.; rParenLine > lastLine+1 { |
| 62 | tokFile.MergeLinescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#MergeLine().(rParenLine - 1) // has side effects! |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // mergeImports merges all the import declarations into the first one. |
| 69 | // Taken from golang.org/x/tools/go/ast/astutil. |
| 70 | // This does not adjust line numbers properly |
| 71 | func mergeImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/mergeImports().(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#) { |
| 72 | if len(f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.) <= 1 { |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | // Merge all the import declarations into the first one. |
| 77 | 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# |
| 78 | for i := 0; i < len(f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.); i++ { |
| 79 | decl := f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.[i] |
| 80 | 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#) |
| 81 | 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/internal/imports`/declImports().(gen, "C") { |
| 82 | continue |
| 83 | } |
| 84 | if first == nil { |
| 85 | first = gen |
| 86 | continue // Don't touch the first one. |
| 87 | } |
| 88 | // We now know there is more than one package in this import |
| 89 | // declaration. Ensure that it ends up parenthesized. |
| 90 | 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().() |
| 91 | // Move the imports of the other import declaration to the first one. |
| 92 | for _, spec := range gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. { |
| 93 | updateBasicLitPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/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().()) |
| 94 | 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) |
| 95 | } |
| 96 | 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) |
| 97 | i-- |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // declImports reports whether gen contains an import of path. |
| 102 | // Taken from golang.org/x/tools/go/ast/astutil. |
| 103 | func declImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/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 { |
| 104 | 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. { |
| 105 | return false |
| 106 | } |
| 107 | for _, spec := range gen.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. { |
| 108 | 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#) |
| 109 | if importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importPath().(impspec) == path { |
| 110 | return true |
| 111 | } |
| 112 | } |
| 113 | return false |
| 114 | } |
| 115 | |
| 116 | func importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importPath().(s astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Specscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Spec#) string { |
| 117 | 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.(*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..Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value.) |
| 118 | if err == nil { |
| 119 | return t |
| 120 | } |
| 121 | return "" |
| 122 | } |
| 123 | |
| 124 | func importNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importName().(s astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Specscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Spec#) string { |
| 125 | n := 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#).Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Name. |
| 126 | if n == nil { |
| 127 | return "" |
| 128 | } |
| 129 | return n.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. |
| 130 | } |
| 131 | |
| 132 | func importCommentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importComment().(s astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Specscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Spec#) string { |
| 133 | c := 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#).Commentscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Comment. |
| 134 | if c == nil { |
| 135 | return "" |
| 136 | } |
| 137 | return c.Textscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Text().() |
| 138 | } |
| 139 | |
| 140 | // collapse indicates whether prev may be removed, leaving only next. |
| 141 | func collapsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/collapse().(prev, next astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Specscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Spec#) bool { |
| 142 | if importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importPath().(next) != importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importPath().(prev) || importNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importName().(next) != importNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importName().(prev) { |
| 143 | return false |
| 144 | } |
| 145 | return prev.(*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#).Commentscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Comment. == nil |
| 146 | } |
| 147 | |
| 148 | type posSpanscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan# struct { |
| 149 | Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#Start. 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# |
| 150 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#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# |
| 151 | } |
| 152 | |
| 153 | // sortSpecs sorts the import specs within each import decl. |
| 154 | // It may mutate the token.File. |
| 155 | func sortSpecsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/sortSpecs().(localPrefix string, tokFile *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#, 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#, specs []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Specscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Spec#) []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Specscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Spec# { |
| 156 | // Can't short-circuit here even if specs are already sorted, |
| 157 | // since they might yet need deduplication. |
| 158 | // A lone import, however, may be safely ignored. |
| 159 | if len(specs) <= 1 { |
| 160 | return specs |
| 161 | } |
| 162 | |
| 163 | // Record positions for specs. |
| 164 | pos := make([]posSpanscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#, len(specs)) |
| 165 | for i, s := range specs { |
| 166 | pos[i] = posSpanscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#{s.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), s.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()} |
| 167 | } |
| 168 | |
| 169 | // Identify comments in this range. |
| 170 | // Any comment from pos[0].Start to the final line counts. |
| 171 | lastLine := tokFile.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Line().(pos[len(pos)-1].Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#End.) |
| 172 | cstart := len(f.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments.) |
| 173 | cend := len(f.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments.) |
| 174 | for i, g := range f.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments. { |
| 175 | if g.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().() < pos[0].Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#Start. { |
| 176 | continue |
| 177 | } |
| 178 | if i < cstart { |
| 179 | cstart = i |
| 180 | } |
| 181 | if tokFile.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Line().(g.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#End().()) > lastLine { |
| 182 | cend = i |
| 183 | break |
| 184 | } |
| 185 | } |
| 186 | comments := f.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments.[cstart:cend] |
| 187 | |
| 188 | // Assign each comment to the import spec preceding it. |
| 189 | importComment := 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#][]*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#{} |
| 190 | specIndex := 0 |
| 191 | for _, g := range comments { |
| 192 | for specIndex+1 < len(specs) && pos[specIndex+1].Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#Start. <= g.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().() { |
| 193 | specIndex++ |
| 194 | } |
| 195 | s := specs[specIndex].(*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#) |
| 196 | importComment[s] = append(importComment[s], g) |
| 197 | } |
| 198 | |
| 199 | // Sort the import specs by import path. |
| 200 | // Remove duplicates, when possible without data loss. |
| 201 | // Reassign the import paths to have the same position sequence. |
| 202 | // Reassign each comment to abut the end of its spec. |
| 203 | // Sort the comments by new position. |
| 204 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Sortscip-go gomod github.com/golang/go/src go1.25.0 sort/Sort().(byImportSpecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#{localPrefix, specs}) |
| 205 | |
| 206 | // Dedup. Thanks to our sorting, we can just consider |
| 207 | // adjacent pairs of imports. |
| 208 | deduped := specs[:0] |
| 209 | for i, s := range specs { |
| 210 | if i == len(specs)-1 || !collapsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/collapse().(s, specs[i+1]) { |
| 211 | deduped = append(deduped, s) |
| 212 | } else { |
| 213 | p := s.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() |
| 214 | tokFile.MergeLinescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#MergeLine().(tokFile.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Line().(p)) // has side effects! |
| 215 | } |
| 216 | } |
| 217 | specs = deduped |
| 218 | |
| 219 | // Fix up comment positions |
| 220 | for i, s := range specs { |
| 221 | s := 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#) |
| 222 | if s.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Name. != nil { |
| 223 | s.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[i].Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#Start. |
| 224 | } |
| 225 | updateBasicLitPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/updateBasicLitPos().(s.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path., pos[i].Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#Start.) |
| 226 | s.EndPosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#EndPos. = pos[i].Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#End. |
| 227 | nextSpecPos := pos[i].Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#End. |
| 228 | |
| 229 | for _, g := range importComment[s] { |
| 230 | for _, c := range g.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#List. { |
| 231 | c.Slashscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#Slash. = pos[i].Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#End. |
| 232 | nextSpecPos = c.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Comment#End().() |
| 233 | } |
| 234 | } |
| 235 | if i < len(specs)-1 { |
| 236 | pos[i+1].Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#Start. = nextSpecPos |
| 237 | pos[i+1].Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/posSpan#End. = nextSpecPos |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Sortscip-go gomod github.com/golang/go/src go1.25.0 sort/Sort().(byCommentPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byCommentPos#(comments)) |
| 242 | |
| 243 | // Fixup comments can insert blank lines, because import specs are on different lines. |
| 244 | // We remove those blank lines here by merging import spec to the first import spec line. |
| 245 | firstSpecLine := tokFile.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Line().(specs[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().()) |
| 246 | for _, s := range specs[1:] { |
| 247 | p := s.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() |
| 248 | line := tokFile.Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Line().(p) |
| 249 | for previousLine := line - 1; previousLine >= firstSpecLine; { |
| 250 | // MergeLine can panic. Avoid the panic at the cost of not removing the blank line |
| 251 | // golang/go#50329 |
| 252 | if previousLine > 0 && previousLine < tokFile.LineCountscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#LineCount().() { |
| 253 | tokFile.MergeLinescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#MergeLine().(previousLine) // has side effects! |
| 254 | previousLine-- |
| 255 | } else { |
| 256 | // try to gather some data to diagnose how this could happen |
| 257 | req := "Please report what the imports section of your go file looked like." |
| 258 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("panic avoided: first:%d line:%d previous:%d max:%d. %s", |
| 259 | firstSpecLine, line, previousLine, tokFile.LineCountscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#LineCount().(), req) |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | return specs |
| 264 | } |
| 265 | |
| 266 | type byImportSpecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec# struct { |
| 267 | localPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#localPrefix. string |
| 268 | specsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#specs. []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Specscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Spec# // slice of *ast.ImportSpec |
| 269 | } |
| 270 | |
| 271 | func (x byImportSpecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#) Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#Len().() int { return len(x.specsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#specs.) } |
| 272 | func (x byImportSpecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#) Swapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#Swap().(i, j int) { x.specsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#specs.[i], x.specsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#specs.[j] = x.specsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#specs.[j], x.specsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#specs.[i] } |
| 273 | func (x byImportSpecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#) Lessscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#Less().(i, j int) bool { |
| 274 | ipath := importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importPath().(x.specsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#specs.[i]) |
| 275 | jpath := importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importPath().(x.specsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#specs.[j]) |
| 276 | |
| 277 | igroup := importGroupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importGroup().(x.localPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#localPrefix., ipath) |
| 278 | jgroup := importGroupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importGroup().(x.localPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#localPrefix., jpath) |
| 279 | if igroup != jgroup { |
| 280 | return igroup < jgroup |
| 281 | } |
| 282 | |
| 283 | if ipath != jpath { |
| 284 | return ipath < jpath |
| 285 | } |
| 286 | iname := importNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importName().(x.specsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#specs.[i]) |
| 287 | jname := importNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importName().(x.specsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#specs.[j]) |
| 288 | |
| 289 | if iname != jname { |
| 290 | return iname < jname |
| 291 | } |
| 292 | return importCommentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importComment().(x.specsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#specs.[i]) < importCommentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importComment().(x.specsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byImportSpec#specs.[j]) |
| 293 | } |
| 294 | |
| 295 | type byCommentPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byCommentPos# []*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# |
| 296 | |
| 297 | func (x byCommentPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byCommentPos#) Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byCommentPos#Len().() int { return len(x) } |
| 298 | func (x byCommentPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byCommentPos#) Swapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byCommentPos#Swap().(i, j int) { x[i], x[j] = x[j], x[i] } |
| 299 | func (x byCommentPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byCommentPos#) Lessscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byCommentPos#Less().(i, j int) bool { return x[i].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().() < x[j].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().() } |
| 300 | |
| 301 | // updateBasicLitPos updates lit.Pos, |
| 302 | // ensuring that lit.End (if set) is displaced by the same amount. |
| 303 | // (See https://go.dev/issue/76395.) |
| 304 | func updateBasicLitPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/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#) { |
| 305 | 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().() |
| 306 | lit.ValuePosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#ValuePos. = pos |
| 307 | // TODO(adonovan): after go1.26, simplify to: |
| 308 | // lit.ValueEnd = pos + len |
| 309 | 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") |
| 310 | 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 { |
| 311 | v.SetIntscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#SetInt().(int64(pos + len)) |
| 312 | } |
| 313 | } |