From 5485bfc4412baeb3e4c548ebed004ce49d5decc9 Mon Sep 17 00:00:00 2001 From: Daisuke Suzuki Date: Thu, 7 May 2020 00:18:31 +0900 Subject: [PATCH] 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 Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot --- internal/lsp/cmd/symbols.go | 54 ++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/internal/lsp/cmd/symbols.go b/internal/lsp/cmd/symbols.go index 60bebbc324..b4a503b0ce 100644 --- a/internal/lsp/cmd/symbols.go +++ b/internal/lsp/cmd/symbols.go @@ -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 {