-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathast.go
36 lines (31 loc) · 782 Bytes
/
ast.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
package ident
import (
"path"
"path/filepath"
"github.com/rogpeppe/godef/go/ast"
"github.com/rogpeppe/godef/go/parser"
"github.com/rogpeppe/godef/go/token"
"github.com/rogpeppe/godef/go/types"
)
var fileset = types.FileSet
var scopes = map[string]*ast.Scope{}
func getScope(filepath string) *ast.Scope {
dirpath := path.Base(filepath)
scope, ok := scopes[dirpath]
if !ok {
scope = ast.NewScope(parser.Universe)
scopes[dirpath] = scope
}
return scope
}
func getDefPosition(expr ast.Expr) *token.Position {
obj, _ := types.ExprType(expr, types.DefaultImporter, fileset)
if obj == nil {
return nil
}
pos := fileset.Position(types.DeclPos(obj))
if realname, err := filepath.EvalSymlinks(pos.Filename); err == nil {
pos.Filename = realname
}
return &pos
}