1
0
mirror of https://github.com/golang/go synced 2024-09-30 14:18:32 -06:00

internal/lsp/cmd: fix not displaying symbols result

When run in CLI, "DocumentSymbol()" returns "[]protocol.DocumentSymbol"
or "[]protocol.SymbolInformation", so need to handle that as well.

Change-Id: I7885d3c53899103553df57f7f0ceceb2a33ec021
Reviewed-on: https://go-review.googlesource.com/c/tools/+/232557
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Daisuke Suzuki 2020-05-07 00:18:31 +09:00 committed by Rebecca Stambler
parent 9abf76cc03
commit 5485bfc441

View File

@ -53,32 +53,44 @@ func (r *symbols) Run(ctx context.Context, args ...string) error {
return err
}
for _, s := range symbols {
s, ok := s.(map[string]interface{})
if !ok {
continue
}
bytes, err := json.Marshal(s)
if err != nil {
return err
}
if _, ok := s["selectionRange"]; ok {
if err := parseDocumentSymbol(bytes); err != nil {
if m, ok := s.(map[string]interface{}); ok {
s, err = mapToSymbol(m)
if err != nil {
return err
}
continue
}
if err := parseSymbolInformation(bytes); err != nil {
return err
switch t := s.(type) {
case protocol.DocumentSymbol:
printDocumentSymbol(t)
case protocol.SymbolInformation:
printSymbolInformation(t)
}
}
return nil
}
func parseDocumentSymbol(bytes []byte) error {
var s protocol.DocumentSymbol
if err := json.Unmarshal(bytes, &s); err != nil {
return err
func mapToSymbol(m map[string]interface{}) (interface{}, error) {
b, err := json.Marshal(m)
if err != nil {
return nil, err
}
if _, ok := m["selectionRange"]; ok {
var s protocol.DocumentSymbol
if err := json.Unmarshal(b, &s); err != nil {
return nil, err
}
return s, nil
}
var s protocol.SymbolInformation
if err := json.Unmarshal(b, &s); err != nil {
return nil, err
}
return s, nil
}
func printDocumentSymbol(s protocol.DocumentSymbol) {
fmt.Printf("%s %s %s\n", s.Name, s.Kind, positionToString(s.SelectionRange))
// Sort children for consistency
sort.Slice(s.Children, func(i, j int) bool {
@ -87,16 +99,10 @@ func parseDocumentSymbol(bytes []byte) error {
for _, c := range s.Children {
fmt.Printf("\t%s %s %s\n", c.Name, c.Kind, positionToString(c.SelectionRange))
}
return nil
}
func parseSymbolInformation(bytes []byte) error {
var s protocol.SymbolInformation
if err := json.Unmarshal(bytes, &s); err != nil {
return err
}
func printSymbolInformation(s protocol.SymbolInformation) {
fmt.Printf("%s %s %s\n", s.Name, s.Kind, positionToString(s.Location.Range))
return nil
}
func positionToString(r protocol.Range) string {