| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package server |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
|
| 10 |
"golang.org/x/tools/gopls/internal/file" |
| 11 |
"golang.org/x/tools/gopls/internal/golang" |
| 12 |
"golang.org/x/tools/gopls/internal/label" |
| 13 |
"golang.org/x/tools/gopls/internal/protocol" |
| 14 |
"golang.org/x/tools/gopls/internal/template" |
| 15 |
"golang.org/x/tools/internal/event" |
| 16 |
) |
| 17 |
|
| 18 |
func (s *server) DocumentSymbol(ctx context.Context, params *protocol.DocumentSymbolParams) ([]any, error) { |
| 19 |
ctx, done := event.Start(ctx, "server.DocumentSymbol", label.URI.Of(params.TextDocument.URI)) |
| 20 |
defer done() |
| 21 |
|
| 22 |
fh, snapshot, release, err := s.session.FileOf(ctx, params.TextDocument.URI) |
| 23 |
if err != nil { |
| 24 |
return nil, err |
| 25 |
} |
| 26 |
defer release() |
| 27 |
|
| 28 |
var docSymbols []protocol.DocumentSymbol |
| 29 |
switch snapshot.FileKind(fh) { |
| 30 |
case file.Tmpl: |
| 31 |
docSymbols, err = template.DocumentSymbols(snapshot, fh) |
| 32 |
case file.Go: |
| 33 |
docSymbols, err = golang.DocumentSymbols(ctx, snapshot, fh) |
| 34 |
default: |
| 35 |
return nil, nil |
| 36 |
} |
| 37 |
if err != nil { |
| 38 |
event.Error(ctx, "DocumentSymbols failed", err) |
| 39 |
return nil, nil |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
symbols := make([]any, len(docSymbols)) |
| 44 |
for i, s := range docSymbols { |
| 45 |
if snapshot.Options().HierarchicalDocumentSymbolSupport { |
| 46 |
symbols[i] = s |
| 47 |
continue |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
symbols[i] = protocol.SymbolInformation{ |
| 52 |
Name: s.Name, |
| 53 |
Kind: s.Kind, |
| 54 |
Deprecated: s.Deprecated, |
| 55 |
Location: params.TextDocument.URI.Location(s.Range), |
| 56 |
} |
| 57 |
} |
| 58 |
return symbols, nil |
| 59 |
} |