1
0
mirror of https://github.com/golang/go synced 2024-11-05 16:56:16 -07: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 return err
} }
for _, s := range symbols { for _, s := range symbols {
s, ok := s.(map[string]interface{}) if m, ok := s.(map[string]interface{}); ok {
if !ok { s, err = mapToSymbol(m)
continue
}
bytes, err := json.Marshal(s)
if err != nil { if err != nil {
return err return err
} }
if _, ok := s["selectionRange"]; ok {
if err := parseDocumentSymbol(bytes); err != nil {
return err
} }
continue switch t := s.(type) {
} case protocol.DocumentSymbol:
if err := parseSymbolInformation(bytes); err != nil { printDocumentSymbol(t)
return err case protocol.SymbolInformation:
printSymbolInformation(t)
} }
} }
return nil return nil
} }
func parseDocumentSymbol(bytes []byte) error { func mapToSymbol(m map[string]interface{}) (interface{}, error) {
var s protocol.DocumentSymbol b, err := json.Marshal(m)
if err := json.Unmarshal(bytes, &s); err != nil { if err != nil {
return err 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)) fmt.Printf("%s %s %s\n", s.Name, s.Kind, positionToString(s.SelectionRange))
// Sort children for consistency // Sort children for consistency
sort.Slice(s.Children, func(i, j int) bool { sort.Slice(s.Children, func(i, j int) bool {
@ -87,16 +99,10 @@ func parseDocumentSymbol(bytes []byte) error {
for _, c := range s.Children { for _, c := range s.Children {
fmt.Printf("\t%s %s %s\n", c.Name, c.Kind, positionToString(c.SelectionRange)) fmt.Printf("\t%s %s %s\n", c.Name, c.Kind, positionToString(c.SelectionRange))
} }
return nil
} }
func parseSymbolInformation(bytes []byte) error { func printSymbolInformation(s protocol.SymbolInformation) {
var s protocol.SymbolInformation
if err := json.Unmarshal(bytes, &s); err != nil {
return err
}
fmt.Printf("%s %s %s\n", s.Name, s.Kind, positionToString(s.Location.Range)) fmt.Printf("%s %s %s\n", s.Name, s.Kind, positionToString(s.Location.Range))
return nil
} }
func positionToString(r protocol.Range) string { func positionToString(r protocol.Range) string {