| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package golang |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"go/ast" |
| 10 |
"go/printer" |
| 11 |
"go/token" |
| 12 |
"go/types" |
| 13 |
"regexp" |
| 14 |
"slices" |
| 15 |
"strings" |
| 16 |
"unicode" |
| 17 |
|
| 18 |
"golang.org/x/tools/gopls/internal/cache" |
| 19 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 20 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 21 |
"golang.org/x/tools/gopls/internal/protocol" |
| 22 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 23 |
"golang.org/x/tools/gopls/internal/util/tokeninternal" |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
func IsGenerated(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) bool { |
| 29 |
fh, err := snapshot.ReadFile(ctx, uri) |
| 30 |
if err != nil { |
| 31 |
return false |
| 32 |
} |
| 33 |
pgf, err := snapshot.ParseGo(ctx, fh, parsego.Header) |
| 34 |
if err != nil { |
| 35 |
return false |
| 36 |
} |
| 37 |
return ast.IsGenerated(pgf.File) |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
func FormatNode(fset *token.FileSet, n ast.Node) string { |
| 42 |
var buf strings.Builder |
| 43 |
if err := printer.Fprint(&buf, fset, n); err != nil { |
| 44 |
|
| 45 |
|
| 46 |
return "" |
| 47 |
} |
| 48 |
return buf.String() |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
func formatNodeFile(file *token.File, n ast.Node) string { |
| 54 |
fset := tokeninternal.FileSetFor(file) |
| 55 |
return FormatNode(fset, n) |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
func findFileInDeps(s metadata.Source, mp *metadata.Package, uri protocol.DocumentURI) *metadata.Package { |
| 61 |
seen := make(map[PackageID]bool) |
| 62 |
var search func(*metadata.Package) *metadata.Package |
| 63 |
search = func(mp *metadata.Package) *metadata.Package { |
| 64 |
if seen[mp.ID] { |
| 65 |
return nil |
| 66 |
} |
| 67 |
seen[mp.ID] = true |
| 68 |
if slices.Contains(mp.CompiledGoFiles, uri) { |
| 69 |
return mp |
| 70 |
} |
| 71 |
for _, dep := range mp.DepsByPkgPath { |
| 72 |
mp := s.Metadata(dep) |
| 73 |
if mp == nil { |
| 74 |
bug.Reportf("nil metadata for %q", dep) |
| 75 |
continue |
| 76 |
} |
| 77 |
if found := search(mp); found != nil { |
| 78 |
return found |
| 79 |
} |
| 80 |
} |
| 81 |
return nil |
| 82 |
} |
| 83 |
return search(mp) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
func requalifier(s metadata.Source, targetFile *ast.File, targetMeta *metadata.Package, mq MetadataQualifier) func(string) string { |
| 89 |
qm := map[string]string{ |
| 90 |
"": mq(targetMeta.Name, "", targetMeta.PkgPath), |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
for _, imp := range targetFile.Imports { |
| 95 |
name, pkgName, impPath, pkgPath := importInfo(s, imp, targetMeta) |
| 96 |
|
| 97 |
|
| 98 |
qm[name] = mq(pkgName, impPath, pkgPath) |
| 99 |
} |
| 100 |
|
| 101 |
return func(name string) string { |
| 102 |
if newName, ok := qm[name]; ok { |
| 103 |
return newName |
| 104 |
} |
| 105 |
return name |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
type MetadataQualifier func(PackageName, ImportPath, PackagePath) string |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
func MetadataQualifierForFile(s metadata.Source, f *ast.File, mp *metadata.Package) MetadataQualifier { |
| 120 |
|
| 121 |
localNames := make(map[ImportPath]string) |
| 122 |
for _, imp := range f.Imports { |
| 123 |
name, _, impPath, _ := importInfo(s, imp, mp) |
| 124 |
localNames[impPath] = name |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
inverseDeps := make(map[PackageID]PackagePath) |
| 129 |
for path, id := range mp.DepsByPkgPath { |
| 130 |
inverseDeps[id] = path |
| 131 |
} |
| 132 |
importsByPkgPath := make(map[PackagePath]ImportPath) |
| 133 |
for impPath, id := range mp.DepsByImpPath { |
| 134 |
if id == "" { |
| 135 |
continue |
| 136 |
} |
| 137 |
pkgPath := inverseDeps[id] |
| 138 |
_, hasPath := importsByPkgPath[pkgPath] |
| 139 |
_, hasImp := localNames[impPath] |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if !hasPath || hasImp { |
| 144 |
importsByPkgPath[pkgPath] = impPath |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return func(pkgName PackageName, impPath ImportPath, pkgPath PackagePath) string { |
| 149 |
|
| 150 |
|
| 151 |
if pkgPath != "" { |
| 152 |
if srcImp := importsByPkgPath[pkgPath]; srcImp != "" { |
| 153 |
impPath = srcImp |
| 154 |
} |
| 155 |
if pkgPath == mp.PkgPath { |
| 156 |
return "" |
| 157 |
} |
| 158 |
} |
| 159 |
if localName, ok := localNames[impPath]; ok && impPath != "" { |
| 160 |
return localName |
| 161 |
} |
| 162 |
if pkgName != "" { |
| 163 |
return string(pkgName) |
| 164 |
} |
| 165 |
idx := strings.LastIndexByte(string(impPath), '/') |
| 166 |
return string(impPath[idx+1:]) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
func importInfo(s metadata.Source, imp *ast.ImportSpec, mp *metadata.Package) (string, PackageName, ImportPath, PackagePath) { |
| 182 |
var ( |
| 183 |
name string |
| 184 |
pkgName PackageName |
| 185 |
impPath = metadata.UnquoteImportPath(imp) |
| 186 |
pkgPath PackagePath |
| 187 |
) |
| 188 |
|
| 189 |
|
| 190 |
if imp.Name != nil { |
| 191 |
name = imp.Name.Name |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
if depID := mp.DepsByImpPath[impPath]; depID != "" { |
| 197 |
if depMP := s.Metadata(depID); depMP != nil { |
| 198 |
if name == "" { |
| 199 |
name = string(depMP.Name) |
| 200 |
} |
| 201 |
pkgName = depMP.Name |
| 202 |
pkgPath = depMP.PkgPath |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
if name == "" { |
| 208 |
idx := strings.LastIndexByte(string(impPath), '/') |
| 209 |
name = string(impPath[idx+1:]) |
| 210 |
} |
| 211 |
return name, pkgName, impPath, pkgPath |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
func isDirective(c string) bool { |
| 218 |
if len(c) < 3 { |
| 219 |
return false |
| 220 |
} |
| 221 |
if c[1] != '/' { |
| 222 |
return false |
| 223 |
} |
| 224 |
|
| 225 |
c = c[2:] |
| 226 |
if len(c) == 0 { |
| 227 |
|
| 228 |
return false |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
if strings.HasPrefix(c, "line ") { |
| 233 |
return true |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
colon := strings.Index(c, ":") |
| 239 |
if colon <= 0 || colon+1 >= len(c) { |
| 240 |
return false |
| 241 |
} |
| 242 |
for i := 0; i <= colon+1; i++ { |
| 243 |
if i == colon { |
| 244 |
continue |
| 245 |
} |
| 246 |
b := c[i] |
| 247 |
if !('a' <= b && b <= 'z' || '0' <= b && b <= '9') { |
| 248 |
return false |
| 249 |
} |
| 250 |
} |
| 251 |
return true |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
func embeddedIdent(x ast.Expr) *ast.Ident { |
| 260 |
if star, ok := x.(*ast.StarExpr); ok { |
| 261 |
x = star.X |
| 262 |
} |
| 263 |
switch ix := x.(type) { |
| 264 |
case *ast.IndexExpr: |
| 265 |
x = ix.X |
| 266 |
case *ast.IndexListExpr: |
| 267 |
x = ix.X |
| 268 |
} |
| 269 |
switch x := x.(type) { |
| 270 |
case *ast.Ident: |
| 271 |
return x |
| 272 |
case *ast.SelectorExpr: |
| 273 |
if _, ok := x.X.(*ast.Ident); ok { |
| 274 |
return x.Sel |
| 275 |
} |
| 276 |
} |
| 277 |
return nil |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
type ImporterFunc func(path string) (*types.Package, error) |
| 283 |
|
| 284 |
func (f ImporterFunc) Import(path string) (*types.Package, error) { return f(path) } |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
func isBuiltin(obj types.Object) bool { return !obj.Pos().IsValid() } |
| 289 |
|
| 290 |
|
| 291 |
func btoi(b bool) int { |
| 292 |
if b { |
| 293 |
return 1 |
| 294 |
} else { |
| 295 |
return 0 |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
func boolCompare(x, y bool) int { |
| 302 |
return btoi(x) - btoi(y) |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
func AbbreviateVarName(s string) string { |
| 310 |
var ( |
| 311 |
b strings.Builder |
| 312 |
useNextUpper bool |
| 313 |
) |
| 314 |
for i, r := range s { |
| 315 |
|
| 316 |
if !unicode.IsLetter(r) && !unicode.IsNumber(r) { |
| 317 |
break |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
if i == 0 { |
| 323 |
b.WriteRune(unicode.ToLower(r)) |
| 324 |
} |
| 325 |
|
| 326 |
if unicode.IsUpper(r) { |
| 327 |
if useNextUpper { |
| 328 |
b.WriteRune(unicode.ToLower(r)) |
| 329 |
useNextUpper = false |
| 330 |
} |
| 331 |
} else { |
| 332 |
useNextUpper = true |
| 333 |
} |
| 334 |
} |
| 335 |
return b.String() |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
func CopyrightComment(file *ast.File) *ast.CommentGroup { |
| 341 |
if len(file.Comments) == 0 { |
| 342 |
return nil |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
if c := file.Comments[0]; c.Pos() < file.Package && c != file.Doc && |
| 348 |
!isDirective(c.List[0].Text) && |
| 349 |
strings.Contains(strings.ToLower(c.List[0].Text), "copyright") { |
| 350 |
return c |
| 351 |
} |
| 352 |
|
| 353 |
return nil |
| 354 |
} |
| 355 |
|
| 356 |
var buildConstraintRe = regexp.MustCompile(`^//(go:build|\s*\+build).*`) |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
func buildConstraintComment(file *ast.File) *ast.Comment { |
| 362 |
for _, cg := range file.Comments { |
| 363 |
|
| 364 |
|
| 365 |
if cg.Pos() > file.Package { |
| 366 |
return nil |
| 367 |
} |
| 368 |
|
| 369 |
for _, c := range cg.List { |
| 370 |
|
| 371 |
if buildConstraintRe.MatchString(c.Text) { |
| 372 |
return c |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
return nil |
| 378 |
} |