2019-03-18 16:43:08 -06:00
|
|
|
// 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 (
|
2019-04-17 13:37:20 -06:00
|
|
|
"context"
|
|
|
|
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2020-03-10 21:09:39 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
2019-03-18 16:43:08 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-03-26 15:38:40 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-03-18 16:43:08 -06:00
|
|
|
)
|
|
|
|
|
2020-03-02 15:18:50 -07:00
|
|
|
func (s *Server) documentSymbol(ctx context.Context, params *protocol.DocumentSymbolParams) ([]interface{}, error) {
|
2020-04-20 10:14:12 -06:00
|
|
|
ctx, done := event.Start(ctx, "lsp.Server.documentSymbol")
|
2019-06-26 20:46:12 -06:00
|
|
|
defer done()
|
2019-09-05 16:54:05 -06:00
|
|
|
|
2020-02-13 11:46:49 -07:00
|
|
|
snapshot, fh, ok, err := s.beginFileRequest(params.TextDocument.URI, source.Go)
|
|
|
|
if !ok {
|
2020-03-02 15:18:50 -07:00
|
|
|
return []interface{}{}, err
|
2019-08-16 11:49:17 -06:00
|
|
|
}
|
2020-03-02 15:18:50 -07:00
|
|
|
docSymbols, err := source.DocumentSymbols(ctx, snapshot, fh)
|
2019-12-04 11:18:11 -07:00
|
|
|
if err != nil {
|
2020-03-10 21:09:39 -06:00
|
|
|
event.Error(ctx, "DocumentSymbols failed", err, tag.URI.Of(fh.Identity().URI))
|
2020-03-02 15:18:50 -07:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
2019-12-04 11:18:11 -07:00
|
|
|
}
|
|
|
|
return symbols, nil
|
2019-03-18 16:43:08 -06:00
|
|
|
}
|