github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/symbols.go (about) 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 5 package lsp 6 7 import ( 8 "context" 9 10 "github.com/powerman/golang-tools/internal/event" 11 "github.com/powerman/golang-tools/internal/lsp/debug/tag" 12 "github.com/powerman/golang-tools/internal/lsp/protocol" 13 "github.com/powerman/golang-tools/internal/lsp/source" 14 "github.com/powerman/golang-tools/internal/lsp/template" 15 ) 16 17 func (s *Server) documentSymbol(ctx context.Context, params *protocol.DocumentSymbolParams) ([]interface{}, error) { 18 ctx, done := event.Start(ctx, "lsp.Server.documentSymbol") 19 defer done() 20 21 snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.UnknownKind) 22 defer release() 23 if !ok { 24 return []interface{}{}, err 25 } 26 var docSymbols []protocol.DocumentSymbol 27 if snapshot.View().FileKind(fh) == source.Tmpl { 28 docSymbols, err = template.DocumentSymbols(snapshot, fh) 29 } else { 30 docSymbols, err = source.DocumentSymbols(ctx, snapshot, fh) 31 } 32 if err != nil { 33 event.Error(ctx, "DocumentSymbols failed", err, tag.URI.Of(fh.URI())) 34 return []interface{}{}, nil 35 } 36 // Convert the symbols to an interface array. 37 // TODO: Remove this once the lsp deprecates SymbolInformation. 38 symbols := make([]interface{}, len(docSymbols)) 39 for i, s := range docSymbols { 40 if snapshot.View().Options().HierarchicalDocumentSymbolSupport { 41 symbols[i] = s 42 continue 43 } 44 // If the client does not support hierarchical document symbols, then 45 // we need to be backwards compatible for now and return SymbolInformation. 46 symbols[i] = protocol.SymbolInformation{ 47 Name: s.Name, 48 Kind: s.Kind, 49 Deprecated: s.Deprecated, 50 Location: protocol.Location{ 51 URI: params.TextDocument.URI, 52 Range: s.Range, 53 }, 54 } 55 } 56 return symbols, nil 57 }