2019-03-18 16:43:08 -06:00
|
|
|
// 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"
|
2019-03-26 15:38:40 -06:00
|
|
|
"fmt"
|
2019-03-18 16:43:08 -06:00
|
|
|
"go/ast"
|
2019-03-26 15:38:40 -06:00
|
|
|
"go/types"
|
2019-03-18 16:43:08 -06:00
|
|
|
|
2019-09-05 16:54:05 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
2019-03-18 16:43:08 -06:00
|
|
|
)
|
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
func DocumentSymbols(ctx context.Context, view View, f File) ([]protocol.DocumentSymbol, error) {
|
2019-06-26 20:46:12 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "source.DocumentSymbols")
|
|
|
|
defer done()
|
2019-07-11 19:05:55 -06:00
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
_, cphs, err := view.CheckPackageHandles(ctx, f)
|
2019-09-09 17:26:26 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-10 13:22:30 -06:00
|
|
|
cph, err := NarrowestCheckPackageHandle(cphs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-09 17:26:26 -06:00
|
|
|
pkg, err := cph.Check(ctx)
|
2019-07-09 15:52:23 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-20 13:23:02 -06:00
|
|
|
}
|
2019-09-17 09:19:11 -06:00
|
|
|
ph, err := pkg.File(f.URI())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-09-06 21:58:07 -06:00
|
|
|
}
|
2019-10-24 13:44:41 -06:00
|
|
|
file, m, _, err := ph.Cached()
|
2019-09-17 09:19:11 -06:00
|
|
|
if err != nil {
|
2019-09-06 21:58:07 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:38:40 -06:00
|
|
|
info := pkg.GetTypesInfo()
|
|
|
|
q := qualifier(file, pkg.GetTypes(), info)
|
|
|
|
|
2019-09-05 16:54:05 -06:00
|
|
|
methodsToReceiver := make(map[types.Type][]protocol.DocumentSymbol)
|
2019-03-30 14:18:22 -06:00
|
|
|
symbolsToReceiver := make(map[types.Type]int)
|
2019-09-05 16:54:05 -06:00
|
|
|
var symbols []protocol.DocumentSymbol
|
2019-03-26 15:38:40 -06:00
|
|
|
for _, decl := range file.Decls {
|
2019-03-18 16:43:08 -06:00
|
|
|
switch decl := decl.(type) {
|
|
|
|
case *ast.FuncDecl:
|
2019-03-29 11:46:33 -06:00
|
|
|
if obj := info.ObjectOf(decl.Name); obj != nil {
|
2019-09-16 16:17:51 -06:00
|
|
|
if fs := funcSymbol(ctx, view, m, decl, obj, q); fs.Kind == protocol.Method {
|
2019-03-30 14:18:22 -06:00
|
|
|
// Store methods separately, as we want them to appear as children
|
|
|
|
// of the corresponding type (which we may not have seen yet).
|
|
|
|
rtype := obj.Type().(*types.Signature).Recv().Type()
|
|
|
|
methodsToReceiver[rtype] = append(methodsToReceiver[rtype], fs)
|
|
|
|
} else {
|
|
|
|
symbols = append(symbols, fs)
|
|
|
|
}
|
2019-03-29 11:46:33 -06:00
|
|
|
}
|
2019-03-18 16:43:08 -06:00
|
|
|
case *ast.GenDecl:
|
|
|
|
for _, spec := range decl.Specs {
|
|
|
|
switch spec := spec.(type) {
|
|
|
|
case *ast.TypeSpec:
|
2019-03-29 11:46:33 -06:00
|
|
|
if obj := info.ObjectOf(spec.Name); obj != nil {
|
2019-09-16 16:17:51 -06:00
|
|
|
ts := typeSymbol(ctx, view, m, info, spec, obj, q)
|
2019-03-30 14:18:22 -06:00
|
|
|
symbols = append(symbols, ts)
|
|
|
|
symbolsToReceiver[obj.Type()] = len(symbols) - 1
|
2019-03-29 11:46:33 -06:00
|
|
|
}
|
2019-03-18 16:43:08 -06:00
|
|
|
case *ast.ValueSpec:
|
|
|
|
for _, name := range spec.Names {
|
2019-03-29 11:46:33 -06:00
|
|
|
if obj := info.ObjectOf(name); obj != nil {
|
2019-09-16 16:17:51 -06:00
|
|
|
symbols = append(symbols, varSymbol(ctx, view, m, decl, name, obj, q))
|
2019-03-29 11:46:33 -06:00
|
|
|
}
|
2019-03-18 16:43:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-30 14:18:22 -06:00
|
|
|
|
|
|
|
// Attempt to associate methods to the corresponding type symbol.
|
|
|
|
for typ, methods := range methodsToReceiver {
|
|
|
|
if ptr, ok := typ.(*types.Pointer); ok {
|
|
|
|
typ = ptr.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := symbolsToReceiver[typ]; ok {
|
|
|
|
symbols[i].Children = append(symbols[i].Children, methods...)
|
|
|
|
} else {
|
|
|
|
// The type definition for the receiver of these methods was not in the document.
|
|
|
|
symbols = append(symbols, methods...)
|
|
|
|
}
|
|
|
|
}
|
2019-06-21 15:00:02 -06:00
|
|
|
return symbols, nil
|
2019-03-18 16:43:08 -06:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:17:51 -06:00
|
|
|
func funcSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, decl *ast.FuncDecl, obj types.Object, q types.Qualifier) protocol.DocumentSymbol {
|
2019-09-05 16:54:05 -06:00
|
|
|
s := protocol.DocumentSymbol{
|
2019-03-26 15:38:40 -06:00
|
|
|
Name: obj.Name(),
|
2019-09-05 16:54:05 -06:00
|
|
|
Kind: protocol.Function,
|
2019-03-18 16:43:08 -06:00
|
|
|
}
|
2019-09-16 16:17:51 -06:00
|
|
|
if span, err := nodeToProtocolRange(ctx, view, m, decl); err == nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
s.Range = span
|
2019-03-18 16:43:08 -06:00
|
|
|
}
|
2019-09-16 16:17:51 -06:00
|
|
|
if span, err := nodeToProtocolRange(ctx, view, m, decl.Name); err == nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
s.SelectionRange = span
|
2019-03-18 16:43:08 -06:00
|
|
|
}
|
2019-03-26 15:38:40 -06:00
|
|
|
sig, _ := obj.Type().(*types.Signature)
|
|
|
|
if sig != nil {
|
|
|
|
if sig.Recv() != nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
s.Kind = protocol.Method
|
2019-03-26 15:38:40 -06:00
|
|
|
}
|
|
|
|
s.Detail += "("
|
|
|
|
for i := 0; i < sig.Params().Len(); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
s.Detail += ", "
|
|
|
|
}
|
|
|
|
param := sig.Params().At(i)
|
|
|
|
label := types.TypeString(param.Type(), q)
|
|
|
|
if param.Name() != "" {
|
|
|
|
label = fmt.Sprintf("%s %s", param.Name(), label)
|
|
|
|
}
|
|
|
|
s.Detail += label
|
|
|
|
}
|
|
|
|
s.Detail += ")"
|
2019-03-18 16:43:08 -06:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:54:05 -06:00
|
|
|
func setKind(s *protocol.DocumentSymbol, typ types.Type, q types.Qualifier) {
|
2019-03-30 14:18:22 -06:00
|
|
|
switch typ := typ.Underlying().(type) {
|
|
|
|
case *types.Interface:
|
2019-09-05 16:54:05 -06:00
|
|
|
s.Kind = protocol.Interface
|
2019-03-30 14:18:22 -06:00
|
|
|
case *types.Struct:
|
2019-09-05 16:54:05 -06:00
|
|
|
s.Kind = protocol.Struct
|
2019-03-30 14:18:22 -06:00
|
|
|
case *types.Signature:
|
2019-09-05 16:54:05 -06:00
|
|
|
s.Kind = protocol.Function
|
2019-03-30 14:18:22 -06:00
|
|
|
if typ.Recv() != nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
s.Kind = protocol.Method
|
2019-03-30 14:18:22 -06:00
|
|
|
}
|
|
|
|
case *types.Named:
|
|
|
|
setKind(s, typ.Underlying(), q)
|
|
|
|
case *types.Basic:
|
|
|
|
i := typ.Info()
|
|
|
|
switch {
|
|
|
|
case i&types.IsNumeric != 0:
|
2019-09-05 16:54:05 -06:00
|
|
|
s.Kind = protocol.Number
|
2019-03-30 14:18:22 -06:00
|
|
|
case i&types.IsBoolean != 0:
|
2019-09-05 16:54:05 -06:00
|
|
|
s.Kind = protocol.Boolean
|
2019-03-30 14:18:22 -06:00
|
|
|
case i&types.IsString != 0:
|
2019-09-05 16:54:05 -06:00
|
|
|
s.Kind = protocol.String
|
2019-03-30 14:18:22 -06:00
|
|
|
}
|
|
|
|
default:
|
2019-09-05 16:54:05 -06:00
|
|
|
s.Kind = protocol.Variable
|
2019-03-27 18:00:20 -06:00
|
|
|
}
|
2019-03-30 14:18:22 -06:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:17:51 -06:00
|
|
|
func typeSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, info *types.Info, spec *ast.TypeSpec, obj types.Object, q types.Qualifier) protocol.DocumentSymbol {
|
2019-09-05 16:54:05 -06:00
|
|
|
s := protocol.DocumentSymbol{
|
|
|
|
Name: obj.Name(),
|
|
|
|
}
|
2019-03-30 14:18:22 -06:00
|
|
|
s.Detail, _ = formatType(obj.Type(), q)
|
|
|
|
setKind(&s, obj.Type(), q)
|
|
|
|
|
2019-09-16 16:17:51 -06:00
|
|
|
if span, err := nodeToProtocolRange(ctx, view, m, spec); err == nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
s.Range = span
|
2019-03-18 16:43:08 -06:00
|
|
|
}
|
2019-09-16 16:17:51 -06:00
|
|
|
if span, err := nodeToProtocolRange(ctx, view, m, spec.Name); err == nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
s.SelectionRange = span
|
2019-03-26 15:38:40 -06:00
|
|
|
}
|
2019-04-15 18:24:28 -06:00
|
|
|
t, objIsStruct := obj.Type().Underlying().(*types.Struct)
|
|
|
|
st, specIsStruct := spec.Type.(*ast.StructType)
|
|
|
|
if objIsStruct && specIsStruct {
|
2019-03-30 14:18:22 -06:00
|
|
|
for i := 0; i < t.NumFields(); i++ {
|
|
|
|
f := t.Field(i)
|
2019-09-05 16:54:05 -06:00
|
|
|
child := protocol.DocumentSymbol{
|
|
|
|
Name: f.Name(),
|
|
|
|
Kind: protocol.Field,
|
|
|
|
}
|
2019-03-30 14:18:22 -06:00
|
|
|
child.Detail, _ = formatType(f.Type(), q)
|
|
|
|
|
|
|
|
spanNode, selectionNode := nodesForStructField(i, st)
|
2019-09-16 16:17:51 -06:00
|
|
|
if span, err := nodeToProtocolRange(ctx, view, m, spanNode); err == nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
child.Range = span
|
2019-03-30 14:18:22 -06:00
|
|
|
}
|
2019-09-16 16:17:51 -06:00
|
|
|
if span, err := nodeToProtocolRange(ctx, view, m, selectionNode); err == nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
child.SelectionRange = span
|
2019-03-30 14:18:22 -06:00
|
|
|
}
|
|
|
|
s.Children = append(s.Children, child)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 18:08:10 -06:00
|
|
|
ti, objIsInterface := obj.Type().Underlying().(*types.Interface)
|
|
|
|
ai, specIsInterface := spec.Type.(*ast.InterfaceType)
|
|
|
|
if objIsInterface && specIsInterface {
|
|
|
|
for i := 0; i < ti.NumExplicitMethods(); i++ {
|
|
|
|
method := ti.ExplicitMethod(i)
|
2019-09-05 16:54:05 -06:00
|
|
|
child := protocol.DocumentSymbol{
|
2019-04-21 18:08:10 -06:00
|
|
|
Name: method.Name(),
|
2019-09-05 16:54:05 -06:00
|
|
|
Kind: protocol.Method,
|
2019-04-21 18:08:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var spanNode, selectionNode ast.Node
|
|
|
|
Methods:
|
|
|
|
for _, f := range ai.Methods.List {
|
|
|
|
for _, id := range f.Names {
|
|
|
|
if id.Name == method.Name() {
|
|
|
|
spanNode, selectionNode = f, id
|
|
|
|
break Methods
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 16:17:51 -06:00
|
|
|
if span, err := nodeToProtocolRange(ctx, view, m, spanNode); err == nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
child.Range = span
|
2019-04-21 18:08:10 -06:00
|
|
|
}
|
2019-09-16 16:17:51 -06:00
|
|
|
if span, err := nodeToProtocolRange(ctx, view, m, selectionNode); err == nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
child.SelectionRange = span
|
2019-04-21 18:08:10 -06:00
|
|
|
}
|
|
|
|
s.Children = append(s.Children, child)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < ti.NumEmbeddeds(); i++ {
|
|
|
|
embedded := ti.EmbeddedType(i)
|
|
|
|
nt, isNamed := embedded.(*types.Named)
|
|
|
|
if !isNamed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:54:05 -06:00
|
|
|
child := protocol.DocumentSymbol{
|
|
|
|
Name: types.TypeString(embedded, q),
|
|
|
|
}
|
2019-04-21 18:08:10 -06:00
|
|
|
setKind(&child, embedded, q)
|
|
|
|
var spanNode, selectionNode ast.Node
|
|
|
|
Embeddeds:
|
|
|
|
for _, f := range ai.Methods.List {
|
|
|
|
if len(f.Names) > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if t := info.TypeOf(f.Type); types.Identical(nt, t) {
|
|
|
|
spanNode, selectionNode = f, f.Type
|
|
|
|
break Embeddeds
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 16:17:51 -06:00
|
|
|
if rng, err := nodeToProtocolRange(ctx, view, m, spanNode); err == nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
child.Range = rng
|
2019-04-21 18:08:10 -06:00
|
|
|
}
|
2019-09-16 16:17:51 -06:00
|
|
|
if span, err := nodeToProtocolRange(ctx, view, m, selectionNode); err == nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
child.SelectionRange = span
|
2019-04-21 18:08:10 -06:00
|
|
|
}
|
|
|
|
s.Children = append(s.Children, child)
|
|
|
|
}
|
|
|
|
}
|
2019-03-18 16:43:08 -06:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-03-30 14:18:22 -06:00
|
|
|
func nodesForStructField(i int, st *ast.StructType) (span, selection ast.Node) {
|
|
|
|
j := 0
|
|
|
|
for _, field := range st.Fields.List {
|
|
|
|
if len(field.Names) == 0 {
|
|
|
|
if i == j {
|
|
|
|
return field, field.Type
|
|
|
|
}
|
|
|
|
j++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, name := range field.Names {
|
|
|
|
if i == j {
|
|
|
|
return field, name
|
|
|
|
}
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:17:51 -06:00
|
|
|
func varSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, decl ast.Node, name *ast.Ident, obj types.Object, q types.Qualifier) protocol.DocumentSymbol {
|
2019-09-05 16:54:05 -06:00
|
|
|
s := protocol.DocumentSymbol{
|
2019-03-26 15:38:40 -06:00
|
|
|
Name: obj.Name(),
|
2019-09-05 16:54:05 -06:00
|
|
|
Kind: protocol.Variable,
|
2019-03-18 16:43:08 -06:00
|
|
|
}
|
2019-03-26 18:11:20 -06:00
|
|
|
if _, ok := obj.(*types.Const); ok {
|
2019-09-05 16:54:05 -06:00
|
|
|
s.Kind = protocol.Constant
|
2019-03-26 18:11:20 -06:00
|
|
|
}
|
2019-09-16 16:17:51 -06:00
|
|
|
if rng, err := nodeToProtocolRange(ctx, view, m, decl); err == nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
s.Range = rng
|
2019-03-18 16:43:08 -06:00
|
|
|
}
|
2019-09-16 16:17:51 -06:00
|
|
|
if span, err := nodeToProtocolRange(ctx, view, m, name); err == nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
s.SelectionRange = span
|
2019-03-26 15:38:40 -06:00
|
|
|
}
|
|
|
|
s.Detail = types.TypeString(obj.Type(), q)
|
2019-03-18 16:43:08 -06:00
|
|
|
return s
|
|
|
|
}
|