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
|
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package source
import (
"context"
"fmt"
"go/ast"
"go/token"
"go/types"
"golang.org/x/tools/gopls/internal/lsp/lsppos"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/internal/event"
)
func DocumentSymbols(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.DocumentSymbol, error) {
ctx, done := event.Start(ctx, "source.DocumentSymbols")
defer done()
content, err := fh.Read()
if err != nil {
return nil, err
}
pgf, err := snapshot.ParseGo(ctx, fh, ParseFull)
if err != nil {
return nil, fmt.Errorf("getting file for DocumentSymbols: %w", err)
}
m := lsppos.NewTokenMapper(content, pgf.Tok)
// Build symbols for file declarations. When encountering a declaration with
// errors (typically because positions are invalid), we skip the declaration
// entirely. VS Code fails to show any symbols if one of the top-level
// symbols is missing position information.
var symbols []protocol.DocumentSymbol
for _, decl := range pgf.File.Decls {
switch decl := decl.(type) {
case *ast.FuncDecl:
if decl.Name.Name == "_" {
continue
}
fs, err := funcSymbol(m, decl)
if err == nil {
// If function is a method, prepend the type of the method.
if decl.Recv != nil && len(decl.Recv.List) > 0 {
fs.Name = fmt.Sprintf("(%s).%s", types.ExprString(decl.Recv.List[0].Type), fs.Name)
}
symbols = append(symbols, fs)
}
case *ast.GenDecl:
for _, spec := range decl.Specs {
switch spec := spec.(type) {
case *ast.TypeSpec:
if spec.Name.Name == "_" {
continue
}
ts, err := typeSymbol(m, spec)
if err == nil {
symbols = append(symbols, ts)
}
case *ast.ValueSpec:
for _, name := range spec.Names {
if name.Name == "_" {
continue
}
vs, err := varSymbol(m, spec, name, decl.Tok == token.CONST)
if err == nil {
symbols = append(symbols, vs)
}
}
}
}
}
}
return symbols, nil
}
func funcSymbol(m *lsppos.TokenMapper, decl *ast.FuncDecl) (protocol.DocumentSymbol, error) {
s := protocol.DocumentSymbol{
Name: decl.Name.Name,
Kind: protocol.Function,
}
if decl.Recv != nil {
s.Kind = protocol.Method
}
var err error
s.Range, err = m.Range(decl.Pos(), decl.End())
if err != nil {
return protocol.DocumentSymbol{}, err
}
s.SelectionRange, err = m.Range(decl.Name.Pos(), decl.Name.End())
if err != nil {
return protocol.DocumentSymbol{}, err
}
s.Detail = types.ExprString(decl.Type)
return s, nil
}
func typeSymbol(m *lsppos.TokenMapper, spec *ast.TypeSpec) (protocol.DocumentSymbol, error) {
s := protocol.DocumentSymbol{
Name: spec.Name.Name,
}
var err error
s.Range, err = m.NodeRange(spec)
if err != nil {
return protocol.DocumentSymbol{}, err
}
s.SelectionRange, err = m.NodeRange(spec.Name)
if err != nil {
return protocol.DocumentSymbol{}, err
}
s.Kind, s.Detail, s.Children = typeDetails(m, spec.Type)
return s, nil
}
func typeDetails(m *lsppos.TokenMapper, typExpr ast.Expr) (kind protocol.SymbolKind, detail string, children []protocol.DocumentSymbol) {
switch typExpr := typExpr.(type) {
case *ast.StructType:
kind = protocol.Struct
children = fieldListSymbols(m, typExpr.Fields, protocol.Field)
if len(children) > 0 {
detail = "struct{...}"
} else {
detail = "struct{}"
}
// Find interface methods and embedded types.
case *ast.InterfaceType:
kind = protocol.Interface
children = fieldListSymbols(m, typExpr.Methods, protocol.Method)
if len(children) > 0 {
detail = "interface{...}"
} else {
detail = "interface{}"
}
case *ast.FuncType:
kind = protocol.Function
detail = types.ExprString(typExpr)
default:
kind = protocol.Class // catch-all, for cases where we don't know the kind syntactically
detail = types.ExprString(typExpr)
}
return
}
func fieldListSymbols(m *lsppos.TokenMapper, fields *ast.FieldList, fieldKind protocol.SymbolKind) []protocol.DocumentSymbol {
if fields == nil {
return nil
}
var symbols []protocol.DocumentSymbol
for _, field := range fields.List {
detail, children := "", []protocol.DocumentSymbol(nil)
if field.Type != nil {
_, detail, children = typeDetails(m, field.Type)
}
if len(field.Names) == 0 { // embedded interface or struct field
// By default, use the formatted type details as the name of this field.
// This handles potentially invalid syntax, as well as type embeddings in
// interfaces.
child := protocol.DocumentSymbol{
Name: detail,
Kind: protocol.Field, // consider all embeddings to be fields
Children: children,
}
// If the field is a valid embedding, promote the type name to field
// name.
selection := field.Type
if id := embeddedIdent(field.Type); id != nil {
child.Name = id.Name
child.Detail = detail
selection = id
}
if rng, err := m.NodeRange(field.Type); err == nil {
child.Range = rng
}
if rng, err := m.NodeRange(selection); err == nil {
child.SelectionRange = rng
}
symbols = append(symbols, child)
} else {
for _, name := range field.Names {
child := protocol.DocumentSymbol{
Name: name.Name,
Kind: fieldKind,
Detail: detail,
Children: children,
}
if rng, err := m.NodeRange(field); err == nil {
child.Range = rng
}
if rng, err := m.NodeRange(name); err == nil {
child.SelectionRange = rng
}
symbols = append(symbols, child)
}
}
}
return symbols
}
func varSymbol(m *lsppos.TokenMapper, spec *ast.ValueSpec, name *ast.Ident, isConst bool) (protocol.DocumentSymbol, error) {
s := protocol.DocumentSymbol{
Name: name.Name,
Kind: protocol.Variable,
}
if isConst {
s.Kind = protocol.Constant
}
var err error
s.Range, err = m.NodeRange(spec)
if err != nil {
return protocol.DocumentSymbol{}, err
}
s.SelectionRange, err = m.NodeRange(name)
if err != nil {
return protocol.DocumentSymbol{}, err
}
if spec.Type != nil { // type may be missing from the syntax
_, s.Detail, s.Children = typeDetails(m, spec.Type)
}
return s, nil
}
|