forked from rogpeppe/godef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapt.go
283 lines (263 loc) · 6.31 KB
/
adapt.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
// The contents of this file are designed to adapt between the two implementations
// of godef, and should be removed when we fully switch to the go/pacakges
// implementation for all cases
import (
"bufio"
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
rpast "github.com/rogpeppe/godef/go/ast"
rpprinter "github.com/rogpeppe/godef/go/printer"
rptypes "github.com/rogpeppe/godef/go/types"
gotoken "go/token"
gotypes "go/types"
"golang.org/x/tools/go/packages"
)
var forcePackages triBool
func init() {
flag.Var(&forcePackages, "new-implementation", "force godef to use the new go/packages implentation")
}
// triBool is used as a unset, on or off valued flag
type triBool int
const (
// unset means the triBool does not yet have a value
unset = triBool(iota)
// on means the triBool has been set to true
on
// off means the triBool has been set to false
off
)
func (b *triBool) Set(s string) error {
v, err := strconv.ParseBool(s)
if v {
*b = on
} else {
*b = off
}
return err
}
func (b *triBool) Get() interface{} {
return *b
}
func (b *triBool) String() string {
switch *b {
case unset:
return "default"
case on:
return "true"
case off:
return "false"
default:
return "invalid"
}
}
func (b *triBool) IsBoolFlag() bool {
return true
}
func detectModuleMode(cfg *packages.Config) bool {
// first see if the config forces module mode
for _, e := range cfg.Env {
switch e {
case "GO111MODULE=off":
return false
case "GO111MODULE=on":
return true
}
}
// do a fast test for go.mod in the working directory
if _, err := os.Stat(filepath.Join(cfg.Dir, "go.mod")); !os.IsNotExist(err) {
return true
}
// fall back to invoking the go tool to see if it will pick module mode
cmd := exec.Command("go", "env", "GOMOD")
cmd.Env = cfg.Env
cmd.Dir = cfg.Dir
out, err := cmd.Output()
if err == nil {
return len(strings.TrimSpace(string(out))) > 0
}
// default to non module mode
return false
}
func adaptGodef(cfg *packages.Config, filename string, src []byte, searchpos int) (*Object, error) {
usePackages := false
switch forcePackages {
case unset:
usePackages = detectModuleMode(cfg)
case on:
usePackages = true
case off:
usePackages = false
}
if usePackages {
fset, obj, err := godefPackages(cfg, filename, src, searchpos)
if err != nil {
return nil, err
}
return adaptGoObject(fset, obj)
}
obj, typ, err := godef(filename, src, searchpos)
if err != nil {
return nil, err
}
return adaptRPObject(obj, typ)
}
func adaptRPObject(obj *rpast.Object, typ rptypes.Type) (*Object, error) {
pos := rptypes.FileSet.Position(rptypes.DeclPos(obj))
result := &Object{
Name: obj.Name,
Pkg: typ.Pkg,
Position: Position{
Filename: pos.Filename,
Line: pos.Line,
Column: pos.Column,
},
Type: typ,
}
switch obj.Kind {
case rpast.Bad:
result.Kind = BadKind
case rpast.Fun:
result.Kind = FuncKind
case rpast.Var:
result.Kind = VarKind
case rpast.Pkg:
result.Kind = ImportKind
result.Type = nil
if typ.Node != nil {
result.Value = typ.Node.(*rpast.ImportSpec).Path.Value
} else {
result.Kind = PathKind
result.Value = obj.Data.(string)
}
case rpast.Con:
result.Kind = ConstKind
if decl, ok := obj.Decl.(*rpast.ValueSpec); ok {
result.Value = decl.Values[0]
}
case rpast.Lbl:
result.Kind = LabelKind
result.Type = nil
case rpast.Typ:
result.Kind = TypeKind
result.Type = typ.Underlying(false)
}
for child := range typ.Iter() {
m, err := adaptRPObject(child, rptypes.Type{})
if err != nil {
return nil, err
}
result.Members = append(result.Members, m)
}
sort.Sort(orderedObjects(result.Members))
return result, nil
}
func adaptGoObject(fset *gotoken.FileSet, obj gotypes.Object) (*Object, error) {
result := &Object{
Name: obj.Name(),
Position: objToPos(fset, obj),
Type: obj.Type(),
}
switch obj := obj.(type) {
case *gotypes.Func:
result.Kind = FuncKind
case *gotypes.Var:
result.Kind = VarKind
case *gotypes.PkgName:
result.Kind = ImportKind
result.Type = nil
if obj.Pkg() != nil {
result.Value = strconv.Quote(obj.Imported().Path())
} else {
result.Value = obj.Imported().Path()
result.Kind = PathKind
}
case *gotypes.Const:
result.Kind = ConstKind
result.Value = obj.Val()
case *gotypes.Label:
result.Kind = LabelKind
result.Type = nil
case *gotypes.TypeName:
result.Kind = TypeKind
result.Type = obj.Type().Underlying()
default:
result.Kind = BadKind
}
return result, nil
}
func objToPos(fSet *gotoken.FileSet, obj gotypes.Object) Position {
p := obj.Pos()
f := fSet.File(p)
goPos := f.Position(p)
pos := Position{
Filename: cleanFilename(goPos.Filename),
Line: goPos.Line,
Column: goPos.Column,
}
if pos.Column != 1 {
return pos
}
// currently exportdata does not store the column
// until it does, we have a hacky fix to attempt to find the name within
// the line and patch the column to match
named, ok := obj.(interface{ Name() string })
if !ok {
return pos
}
in, err := os.Open(f.Name())
if err != nil {
return pos
}
for l, scanner := 1, bufio.NewScanner(in); scanner.Scan(); l++ {
if l < pos.Line {
continue
}
col := bytes.Index([]byte(scanner.Text()), []byte(named.Name()))
if col >= 0 {
pos.Column = col + 1
}
break
}
return pos
}
// cleanFilename normalizes any file names that come out of the fileset.
func cleanFilename(path string) string {
const prefix = "$GOROOT"
if len(path) < len(prefix) || !strings.EqualFold(prefix, path[:len(prefix)]) {
return path
}
//TODO: we need a better way to get the GOROOT that uses the packages api
return runtime.GOROOT() + path[len(prefix):]
}
type pretty struct {
n interface{}
}
func (p pretty) Format(f fmt.State, c rune) {
switch n := p.n.(type) {
case *rpast.BasicLit:
rpprinter.Fprint(f, rptypes.FileSet, n)
case rptypes.Type:
// TODO print path package when appropriate.
// Current issues with using p.n.Pkg:
// - we should actually print the local package identifier
// rather than the package path when possible.
// - p.n.Pkg is non-empty even when
// the type is not relative to the package.
rpprinter.Fprint(f, rptypes.FileSet, n.Node)
case gotypes.Type:
buf := &bytes.Buffer{}
gotypes.WriteType(buf, n, func(p *gotypes.Package) string { return "" })
buf.WriteTo(f)
default:
fmt.Fprint(f, n)
}
}