-
Notifications
You must be signed in to change notification settings - Fork 7
/
scip.go
254 lines (204 loc) · 6.41 KB
/
scip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package index
import (
_ "embed"
"fmt"
"go/ast"
"sort"
"strings"
"sync"
"sync/atomic"
"github.com/sourcegraph/scip-go/internal/config"
"github.com/sourcegraph/scip-go/internal/document"
"github.com/sourcegraph/scip-go/internal/funk"
"github.com/sourcegraph/scip-go/internal/handler"
impls "github.com/sourcegraph/scip-go/internal/implementations"
"github.com/sourcegraph/scip-go/internal/loader"
"github.com/sourcegraph/scip-go/internal/lookup"
"github.com/sourcegraph/scip-go/internal/newtypes"
"github.com/sourcegraph/scip-go/internal/output"
"github.com/sourcegraph/scip-go/internal/symbols"
"github.com/sourcegraph/scip-go/internal/visitors"
"github.com/sourcegraph/scip/bindings/go/scip"
"google.golang.org/protobuf/proto"
)
//go:embed version.txt
var versionFile string
var ScipGoVersion string = strings.TrimSpace(versionFile)
type documentLookup = map[string]*document.Document
func GetPackages(opts config.IndexOpts) (current []newtypes.PackageID, deps []newtypes.PackageID, err error) {
pkgs, pkgLookup, err := loader.LoadPackages(opts, opts.ModuleRoot)
if err != nil {
return nil, nil, err
}
for name := range pkgs {
current = append(current, name)
}
sort.Slice(current, func(i, j int) bool {
return current[i] < current[j]
})
for name := range pkgLookup {
deps = append(deps, name)
}
sort.Slice(deps, func(i, j int) bool {
return deps[i] < deps[j]
})
return
}
func ListMissing(opts config.IndexOpts) (missing []string, err error) {
pathToDocuments := map[string]*document.Document{}
globalSymbols := lookup.NewGlobalSymbols()
pkgs, pkgLookup, err := loader.LoadPackages(opts, opts.ModuleRoot)
if err != nil {
return nil, err
}
lookupNames := funk.Keys(pkgLookup)
for _, pkgName := range lookupNames {
pkg := pkgLookup[pkgName]
visitors.VisitPackageSyntax(opts.ModuleRoot, pkg, pathToDocuments, globalSymbols)
}
pkgNames := funk.Keys(pkgs)
for _, name := range pkgNames {
pkg := pkgs[name]
for _, f := range pkg.Syntax {
docName := pkg.Fset.File(f.Package).Name()
doc := pathToDocuments[docName]
if doc == nil {
missing = append(missing, docName)
}
}
}
return missing, nil
}
func Index(writer func(proto.Message), opts config.IndexOpts) error {
// Emit Metadata.
// NOTE: Must be the first field emitted
writer(&scip.Metadata{
Version: 0,
ToolInfo: &scip.ToolInfo{
Name: "scip-go",
Version: ScipGoVersion,
Arguments: []string{},
},
ProjectRoot: "file://" + opts.ModuleRoot,
TextDocumentEncoding: scip.TextEncoding_UTF8,
})
pkgs, allPackages, err := loader.LoadPackages(opts, opts.ModuleRoot)
if err != nil {
return err
}
pathToDocuments, globalSymbols := indexVisitPackages(opts, pkgs, allPackages)
if opts.SkipImplementations {
output.Println("Skipping implementation relationships")
output.Println("")
} else {
impls.AddImplementationRelationships(pkgs, allPackages, globalSymbols)
}
pkgIDs := funk.Keys(pkgs)
pkgLen := len(pkgIDs)
var count uint64
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for _, ID := range pkgIDs {
pkg := pkgs[ID]
pkgSymbols := globalSymbols.GetPackage(pkg)
for _, f := range pkg.Syntax {
doc := pathToDocuments[pkg.Fset.File(f.Package).Name()]
if doc == nil {
handler.Println("doc is nil for:", pkg.Fset.File(f.Package).Name())
continue
}
// If possible, any state required for created a scip document
// should be contained in the visitor. This makes sure that we can
// garbage collect everything that's there after each loop,
// rather than holding on to every occurrence and piece of data
visitor := visitors.NewFileVisitor(
doc,
pkg,
f,
allPackages,
pkgSymbols,
globalSymbols,
)
// Traverse the file
ast.Walk(visitor, f)
// Write the document
writer(visitor.ToScipDocument())
}
atomic.AddUint64(&count, 1)
}
}()
output.WithProgressParallel(&wg, "Visiting Project Files: ", &count, uint64(pkgLen))
return nil
}
func indexVisitPackages(
opts config.IndexOpts,
pkgs loader.PackageLookup,
pkgLookup loader.PackageLookup,
) (documentLookup, *lookup.Global) {
pathToDocuments := documentLookup{}
globalSymbols := lookup.NewGlobalSymbols()
var count uint64
var wg sync.WaitGroup
wg.Add(1)
lookupIDs := funk.Keys(pkgLookup)
// We have to visit all the packages to get the definition sites
// for all the symbols.
//
// We don't want to visit in the same depth as file visitors though,
// so we do ONLY do this
go func() {
defer wg.Done()
for _, pkgID := range lookupIDs {
pkg := pkgLookup[pkgID]
visitors.VisitPackageSyntax(opts.ModuleRoot, pkg, pathToDocuments, globalSymbols)
// Handle that packages can have many files for one package.
// This finds the "definitive" package declaration
pkgDeclaration, err := findBestPackageDefinitionPath(pkg)
if err != nil {
panic(fmt.Sprintf("Unhandled package declaration: %s", err))
}
if pkgDeclaration == nil {
atomic.AddUint64(&count, 1)
continue
}
globalSymbols.SetPkgName(pkg, pkgDeclaration)
// If we don't have this package anywhere, don't try to create a new symbol
if _, ok := pkgs[newtypes.GetID(pkg)]; !ok {
atomic.AddUint64(&count, 1)
continue
}
pkgSymbol := globalSymbols.GetPkgNameSymbol(pkg).Symbol
for _, f := range pkg.Syntax {
doc := pathToDocuments[pkg.Fset.File(f.Package).Name()]
if pkgDeclaration != nil {
position := pkg.Fset.Position(f.Name.NamePos)
role := int32(scip.SymbolRole_ReadAccess)
if f == pkgDeclaration {
doc.SetNewSymbolForPos(pkgSymbol, pkgDeclaration, f.Name, f.Name.NamePos)
role = int32(scip.SymbolRole_Definition)
}
doc.PackageOccurrence = &scip.Occurrence{
Range: symbols.RangeFromName(position, f.Name.Name, false),
Symbol: pkgSymbol,
SymbolRoles: role,
}
}
}
atomic.AddUint64(&count, 1)
}
}()
output.WithProgressParallel(&wg, "Visiting Packages", &count, uint64(len(lookupIDs)))
return pathToDocuments, globalSymbols
}
// packagePrefixes returns all prefix of the go package path. For example, the package
// `foo/bar/baz` will return the slice containing `foo/bar/baz`, `foo/bar`, and `foo`.
func packagePrefixes(packageName string) []string {
parts := strings.Split(packageName, "/")
prefixes := make([]string, len(parts))
for i := 1; i <= len(parts); i++ {
prefixes[len(parts)-i] = strings.Join(parts[:i], "/")
}
return prefixes
}