| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package symbols |
| 8 |
|
| 9 |
import ( |
| 10 |
"go/ast" |
| 11 |
"go/token" |
| 12 |
"go/types" |
| 13 |
"strings" |
| 14 |
|
| 15 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 16 |
"golang.org/x/tools/gopls/internal/protocol" |
| 17 |
"golang.org/x/tools/gopls/internal/util/frob" |
| 18 |
"golang.org/x/tools/internal/astutil" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
type Symbol struct { |
| 25 |
Name string |
| 26 |
Kind protocol.SymbolKind |
| 27 |
Range protocol.Range |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
type Package struct { |
| 36 |
Files []protocol.DocumentURI |
| 37 |
Symbols [][]Symbol |
| 38 |
} |
| 39 |
|
| 40 |
var codec = frob.CodecFor[*Package]() |
| 41 |
|
| 42 |
|
| 43 |
func Decode(data []byte) *Package { |
| 44 |
return codec.Decode(data) |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
func (pkg *Package) Encode() []byte { |
| 49 |
return codec.Encode(pkg) |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func New(files []*parsego.File) *Package { |
| 54 |
var ( |
| 55 |
uris []protocol.DocumentURI |
| 56 |
symbols [][]Symbol |
| 57 |
) |
| 58 |
for _, pgf := range files { |
| 59 |
uris = append(uris, pgf.URI) |
| 60 |
syms := symbolizeFile(pgf) |
| 61 |
symbols = append(symbols, syms) |
| 62 |
} |
| 63 |
return &Package{ |
| 64 |
Files: uris, |
| 65 |
Symbols: symbols, |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func symbolizeFile(pgf *parsego.File) []Symbol { |
| 71 |
w := &symbolWalker{ |
| 72 |
nodeRange: pgf.NodeRange, |
| 73 |
} |
| 74 |
|
| 75 |
for _, decl := range pgf.File.Decls { |
| 76 |
switch decl := decl.(type) { |
| 77 |
case *ast.FuncDecl: |
| 78 |
kind := protocol.Function |
| 79 |
var recv *ast.Ident |
| 80 |
if decl.Recv.NumFields() > 0 { |
| 81 |
kind = protocol.Method |
| 82 |
_, recv, _ = astutil.UnpackRecv(decl.Recv.List[0].Type) |
| 83 |
} |
| 84 |
w.declare(decl.Name.Name, kind, decl.Name, recv) |
| 85 |
|
| 86 |
case *ast.GenDecl: |
| 87 |
for _, spec := range decl.Specs { |
| 88 |
switch spec := spec.(type) { |
| 89 |
case *ast.TypeSpec: |
| 90 |
kind := protocol.Class |
| 91 |
switch spec.Type.(type) { |
| 92 |
case *ast.InterfaceType: |
| 93 |
kind = protocol.Interface |
| 94 |
case *ast.StructType: |
| 95 |
kind = protocol.Struct |
| 96 |
case *ast.FuncType: |
| 97 |
kind = protocol.Function |
| 98 |
} |
| 99 |
w.declare(spec.Name.Name, kind, spec.Name) |
| 100 |
w.walkType(spec.Type, spec.Name) |
| 101 |
case *ast.ValueSpec: |
| 102 |
for _, name := range spec.Names { |
| 103 |
kind := protocol.Variable |
| 104 |
if decl.Tok == token.CONST { |
| 105 |
kind = protocol.Constant |
| 106 |
} |
| 107 |
w.declare(name.Name, kind, name) |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return w.symbols |
| 115 |
} |
| 116 |
|
| 117 |
type symbolWalker struct { |
| 118 |
nodeRange func(node ast.Node) (protocol.Range, error) |
| 119 |
|
| 120 |
symbols []Symbol |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
func (w *symbolWalker) declare(name string, kind protocol.SymbolKind, node ast.Node, path ...*ast.Ident) { |
| 125 |
var b strings.Builder |
| 126 |
for _, ident := range path { |
| 127 |
if ident != nil { |
| 128 |
b.WriteString(ident.Name) |
| 129 |
b.WriteString(".") |
| 130 |
} |
| 131 |
} |
| 132 |
b.WriteString(name) |
| 133 |
|
| 134 |
rng, err := w.nodeRange(node) |
| 135 |
if err != nil { |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
return |
| 142 |
} |
| 143 |
sym := Symbol{ |
| 144 |
Name: b.String(), |
| 145 |
Kind: kind, |
| 146 |
Range: rng, |
| 147 |
} |
| 148 |
w.symbols = append(w.symbols, sym) |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
func (w *symbolWalker) walkType(typ ast.Expr, path ...*ast.Ident) { |
| 154 |
switch st := typ.(type) { |
| 155 |
case *ast.StructType: |
| 156 |
for _, field := range st.Fields.List { |
| 157 |
w.walkField(field, protocol.Field, protocol.Field, path...) |
| 158 |
} |
| 159 |
case *ast.InterfaceType: |
| 160 |
for _, field := range st.Methods.List { |
| 161 |
w.walkField(field, protocol.Interface, protocol.Method, path...) |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
func (w *symbolWalker) walkField(field *ast.Field, unnamedKind, namedKind protocol.SymbolKind, path ...*ast.Ident) { |
| 171 |
if len(field.Names) == 0 { |
| 172 |
switch typ := field.Type.(type) { |
| 173 |
case *ast.SelectorExpr: |
| 174 |
|
| 175 |
w.declare(typ.Sel.Name, unnamedKind, field, path...) |
| 176 |
default: |
| 177 |
w.declare(types.ExprString(field.Type), unnamedKind, field, path...) |
| 178 |
} |
| 179 |
} |
| 180 |
for _, name := range field.Names { |
| 181 |
w.declare(name.Name, namedKind, name, path...) |
| 182 |
w.walkType(field.Type, append(path, name)...) |
| 183 |
} |
| 184 |
} |