1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:24:48 -07:00
go/internal/lsp/symbols.go
Rohan Challa e4e22f76d0 internal/lsp: support when hierarchicalDocumentSymbolSupport is false
This change adds support when hierarchicalDocumentSymbolSupport is false, this can happen with editors who have not supported textDocument/DocumentSymbol. As a result, these older lsp clients need to recieve []protocol.SymbolInformation rather than []protocol.DocumentSymbol. This change required some changes to internal/lsp/cmd to handle not knowing which type it is receiving, this required manual parsing inside of cmd/symbols.go.

Fixes golang/go#34893

Change-Id: I944ae24302f155b561047227f65bee34b160def1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/221823
Run-TryBot: Rohan Challa <rohan@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-03-06 18:17:37 +00:00

52 lines
1.6 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/lsp/telemetry"
"golang.org/x/tools/internal/telemetry/log"
"golang.org/x/tools/internal/telemetry/trace"
)
func (s *Server) documentSymbol(ctx context.Context, params *protocol.DocumentSymbolParams) ([]interface{}, error) {
ctx, done := trace.StartSpan(ctx, "lsp.Server.documentSymbol")
defer done()
snapshot, fh, ok, err := s.beginFileRequest(params.TextDocument.URI, source.Go)
if !ok {
return []interface{}{}, err
}
docSymbols, err := source.DocumentSymbols(ctx, snapshot, fh)
if err != nil {
log.Error(ctx, "DocumentSymbols failed", err, telemetry.URI.Of(fh.Identity().URI))
return []interface{}{}, nil
}
// Convert the symbols to an interface array.
// TODO: Remove this once the lsp deprecates SymbolInformation.
symbols := make([]interface{}, len(docSymbols))
for i, s := range docSymbols {
if snapshot.View().Options().HierarchicalDocumentSymbolSupport {
symbols[i] = s
continue
}
// If the client does not support hierarchical document symbols, then
// we need to be backwards compatible for now and return SymbolInformation.
symbols[i] = protocol.SymbolInformation{
Name: s.Name,
Kind: s.Kind,
Deprecated: s.Deprecated,
Location: protocol.Location{
URI: params.TextDocument.URI,
Range: s.Range,
},
}
}
return symbols, nil
}