1// Copyright 2019 The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5package lsp 6 7import ( 8 "context" 9 10 "golang.org/x/tools/internal/event" 11 "golang.org/x/tools/internal/lsp/debug/tag" 12 "golang.org/x/tools/internal/lsp/protocol" 13 "golang.org/x/tools/internal/lsp/source" 14) 15 16func (s *Server) documentSymbol(ctx context.Context, params *protocol.DocumentSymbolParams) ([]interface{}, error) { 17 ctx, done := event.Start(ctx, "lsp.Server.documentSymbol") 18 defer done() 19 20 snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go) 21 defer release() 22 if !ok { 23 return []interface{}{}, err 24 } 25 docSymbols, err := source.DocumentSymbols(ctx, snapshot, fh) 26 if err != nil { 27 event.Error(ctx, "DocumentSymbols failed", err, tag.URI.Of(fh.URI())) 28 return []interface{}{}, nil 29 } 30 // Convert the symbols to an interface array. 31 // TODO: Remove this once the lsp deprecates SymbolInformation. 32 symbols := make([]interface{}, len(docSymbols)) 33 for i, s := range docSymbols { 34 if snapshot.View().Options().HierarchicalDocumentSymbolSupport { 35 symbols[i] = s 36 continue 37 } 38 // If the client does not support hierarchical document symbols, then 39 // we need to be backwards compatible for now and return SymbolInformation. 40 symbols[i] = protocol.SymbolInformation{ 41 Name: s.Name, 42 Kind: s.Kind, 43 Deprecated: s.Deprecated, 44 Location: protocol.Location{ 45 URI: params.TextDocument.URI, 46 Range: s.Range, 47 }, 48 } 49 } 50 return symbols, nil 51} 52