Debian Codegraph @

internal/refactor/imports.go

raw symbols SCIP
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 refactorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/
6
7 // This file defines operations for computing edits to imports.
8
9 import (
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 "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/"
13 pathpkgscip-go gomod github.com/golang/go/src go1.25.0 path/ "pathscip-go gomod github.com/golang/go/src go1.25.0 path/"
14 "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/"
15
16 "golang.org/x/tools/internal/packagepathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagepath`/"
17 )
18
19 // AddImport returns the prefix (either "pkg." or "") that should be
20 // used to qualify references to the desired symbol (member) imported
21 // from the specified package, plus any necessary edits to the file's
22 // import declaration to add a new import.
23 //
24 // If the import already exists, and is accessible at pos, AddImport
25 // returns the existing name and no edits. (If the existing import is
26 // a dot import, the prefix is "".)
27 //
28 // Otherwise, it adds a new import, using a local name derived from
29 // the preferred name. To request a blank import, use a preferredName
30 // of "_", and discard the prefix result; member is ignored in this
31 // case.
32 //
33 // AddImport accepts the caller's implicit claim that the imported
34 // package declares member.
35 //
36 // AddImport does not mutate its arguments.
37 func AddImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/AddImport().(info *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#, file *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#, preferredName, pkgpath, member string, 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#) (prefix string, edits []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#) {
38 // Find innermost enclosing lexical block.
39 scope := info.Scopesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Scopes.[file].Innermostscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Innermost().(pos)
40 if scope == nil {
41 panic("no enclosing lexical block")
42 }
43
44 // Is there an existing import of this package?
45 // If so, are we in its scope? (not shadowed)
46 for _, spec := range file.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Imports. {
47 pkgname := info.PkgNameOfscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#PkgNameOf().(spec)
48 if pkgname != nil && pkgname.Importedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/PkgName#Imported().().Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Path().() == pkgpath {
49 name := pkgname.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Name().()
50 if preferredName == "_" {
51 // Request for blank import; any existing import will do.
52 return "", nil
53 }
54 if name == "." {
55 // The scope of ident must be the file scope.
56 if s, _ := scope.LookupParentscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#LookupParent().(member, pos); s == info.Scopesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Scopes.[file] {
57 return "", nil
58 }
59 } else if _, obj := scope.LookupParentscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#LookupParent().(name, pos); obj == pkgname {
60 return name + ".", nil
61 }
62 }
63 }
64
65 // We must add a new import.
66
67 // Ensure we have a fresh name.
68 newName := preferredName
69 if preferredName != "_" {
70 newName = FreshNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/FreshName().(scope, pos, preferredName)
71 prefix = newName + "."
72 }
73
74 // Use a renaming import whenever the preferred name is not
75 // available, or the chosen name does not match the last
76 // segment of its path.
77 if newName == preferredName && newName == pathpkgscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(pkgpath) {
78 newName = ""
79 }
80
81 return prefix, AddImportEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/AddImportEdits().(file, newName, pkgpath)
82 }
83
84 // AddImportEdits returns the edits to add an import of the specified
85 // package, without any analysis of whether this is necessary or safe.
86 // If name is nonempty, it is used as an explicit [ImportSpec.Name].
87 //
88 // A sequence of calls to AddImportEdits that each add the file's
89 // first import (or in a file that does not have a grouped import) may
90 // result in multiple import declarations, rather than a single one
91 // with multiple ImportSpecs. However, a subsequent run of
92 // x/tools/cmd/goimports ([imports.Process]) will combine them.
93 //
94 // AddImportEdits does not mutate the AST.
95 func AddImportEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/AddImportEdits().(file *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, pkgpath string) []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit# {
96 newText := 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().(pkgpath)
97 if name != "" {
98 newText = name + " " + newText
99 }
100
101 // Create a new import declaration either before the first existing
102 // declaration (which must exist), including its comments; or
103 // inside the declaration, if it is an import group.
104 decl0 := file.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.[0]
105 before := decl0.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().()
106 switch decl0 := decl0.(type) {
107 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#:
108 if decl0.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Doc. != nil {
109 before = decl0.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Doc..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().()
110 }
111 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#:
112 if decl0.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Doc. != nil {
113 before = decl0.Docscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Doc..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().()
114 }
115 }
116 var 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#
117 if gd, ok := decl0.(*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#); ok && gd.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. && gd.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().() {
118 // Have existing grouped import ( ... ) decl.
119 if packagepathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagepath`/.IsStdPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagepath`/IsStdPackage().(pkgpath) && len(gd.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs.) > 0 {
120 // Add spec for a std package before
121 // first existing spec, followed by
122 // a blank line if the next one is non-std.
123 first := gd.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#)
124 pos = first.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Pos().()
125 if !packagepathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagepath`/.IsStdPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagepath`/IsStdPackage().(first.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.) {
126 newText += "\n"
127 }
128 newText += "\n\t"
129 } else {
130 // Add spec at end of group.
131 pos = gd.Rparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Rparen.
132 newText = "\t" + newText + "\n"
133 }
134 } else {
135 // No import decl, or non-grouped import.
136 // Add a new import decl before first decl.
137 // (gofmt will merge multiple import decls.)
138 //
139 // TODO(adonovan): do better here; plunder the
140 // mergeImports logic from [imports.Process].
141 pos = before
142 newText = "import " + newText + "\n\n"
143 }
144 return []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/Edit#{{
145 Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: pos,
146 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: pos,
147 NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: []byte(newText),
148 }}
149 }