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 lsp
|
|
|
|
|
|
|
|
import (
|
2019-04-17 13:37:20 -06:00
|
|
|
"context"
|
|
|
|
|
2019-03-18 16:43:08 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-03-26 15:38:40 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-06-21 14:28:23 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry/trace"
|
2019-04-17 13:37:20 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-03-18 16:43:08 -06:00
|
|
|
)
|
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
func (s *Server) documentSymbol(ctx context.Context, params *protocol.DocumentSymbolParams) ([]protocol.DocumentSymbol, error) {
|
2019-06-21 14:28:23 -06:00
|
|
|
ctx, ts := trace.StartSpan(ctx, "lsp.Server.documentSymbol")
|
|
|
|
defer ts.End()
|
2019-04-17 13:37:20 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
2019-05-15 10:24:49 -06:00
|
|
|
view := s.session.ViewOf(uri)
|
2019-05-03 22:04:18 -06:00
|
|
|
f, m, err := getGoFile(ctx, view, uri)
|
2019-04-17 13:37:20 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-21 15:00:02 -06:00
|
|
|
symbols, err := source.DocumentSymbols(ctx, f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-17 13:37:20 -06:00
|
|
|
return toProtocolDocumentSymbols(m, symbols), nil
|
|
|
|
}
|
|
|
|
|
2019-03-18 16:43:08 -06:00
|
|
|
func toProtocolDocumentSymbols(m *protocol.ColumnMapper, symbols []source.Symbol) []protocol.DocumentSymbol {
|
|
|
|
result := make([]protocol.DocumentSymbol, 0, len(symbols))
|
|
|
|
for _, s := range symbols {
|
|
|
|
ps := protocol.DocumentSymbol{
|
|
|
|
Name: s.Name,
|
|
|
|
Kind: toProtocolSymbolKind(s.Kind),
|
|
|
|
Detail: s.Detail,
|
|
|
|
Children: toProtocolDocumentSymbols(m, s.Children),
|
|
|
|
}
|
|
|
|
if r, err := m.Range(s.Span); err == nil {
|
|
|
|
ps.Range = r
|
2019-03-26 15:38:40 -06:00
|
|
|
}
|
|
|
|
if r, err := m.Range(s.SelectionSpan); err == nil {
|
2019-03-18 16:43:08 -06:00
|
|
|
ps.SelectionRange = r
|
|
|
|
}
|
|
|
|
result = append(result, ps)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func toProtocolSymbolKind(kind source.SymbolKind) protocol.SymbolKind {
|
|
|
|
switch kind {
|
|
|
|
case source.StructSymbol:
|
|
|
|
return protocol.Struct
|
|
|
|
case source.PackageSymbol:
|
|
|
|
return protocol.Package
|
|
|
|
case source.VariableSymbol:
|
|
|
|
return protocol.Variable
|
|
|
|
case source.ConstantSymbol:
|
|
|
|
return protocol.Constant
|
|
|
|
case source.FunctionSymbol:
|
|
|
|
return protocol.Function
|
|
|
|
case source.MethodSymbol:
|
|
|
|
return protocol.Method
|
2019-03-27 18:00:20 -06:00
|
|
|
case source.InterfaceSymbol:
|
|
|
|
return protocol.Interface
|
2019-03-30 14:18:22 -06:00
|
|
|
case source.NumberSymbol:
|
|
|
|
return protocol.Number
|
|
|
|
case source.StringSymbol:
|
|
|
|
return protocol.String
|
|
|
|
case source.BooleanSymbol:
|
|
|
|
return protocol.Boolean
|
|
|
|
case source.FieldSymbol:
|
|
|
|
return protocol.Field
|
2019-03-18 16:43:08 -06:00
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|