1
0
mirror of https://github.com/golang/go synced 2024-11-05 20:06:10 -07:00
go/internal/lsp/symbols.go
Rebecca Stambler 34437f544f internal/lsp: refactor server.go to separate into LSP categories
This change separates the different behaviors of server.go by the
categories defined in the spec. This allows us to differentiate more
easily between the language features and the text synchronization code.

I also renamed the "Symbols" function to "Symbol", which fits with the
specification
(https://microsoft.github.io/language-server-protocol/specification#workspace_symbol),
and makes clearer the distinction between DocumentSymbols and Symbol.

Change-Id: I926b8a772c478f6ae426352fb12dc4403f0e736a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/172637
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-04-17 20:54:51 +00:00

74 lines
1.9 KiB
Go

// 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 (
"context"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
)
func (s *Server) documentSymbol(ctx context.Context, params *protocol.DocumentSymbolParams) ([]protocol.DocumentSymbol, error) {
uri := span.NewURI(params.TextDocument.URI)
view := s.findView(ctx, uri)
f, m, err := newColumnMap(ctx, view, uri)
if err != nil {
return nil, err
}
symbols := source.DocumentSymbols(ctx, f)
return toProtocolDocumentSymbols(m, symbols), nil
}
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
}
if r, err := m.Range(s.SelectionSpan); err == nil {
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
case source.InterfaceSymbol:
return protocol.Interface
case source.NumberSymbol:
return protocol.Number
case source.StringSymbol:
return protocol.String
case source.BooleanSymbol:
return protocol.Boolean
case source.FieldSymbol:
return protocol.Field
default:
return 0
}
}