| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package server |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
|
| 11 |
"golang.org/x/tools/go/ast/astutil" |
| 12 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 13 |
"golang.org/x/tools/gopls/internal/file" |
| 14 |
"golang.org/x/tools/gopls/internal/protocol" |
| 15 |
"golang.org/x/tools/internal/event" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
func (s *server) SelectionRange(ctx context.Context, params *protocol.SelectionRangeParams) ([]protocol.SelectionRange, error) { |
| 30 |
ctx, done := event.Start(ctx, "server.SelectionRange") |
| 31 |
defer done() |
| 32 |
|
| 33 |
fh, snapshot, release, err := s.session.FileOf(ctx, params.TextDocument.URI) |
| 34 |
if err != nil { |
| 35 |
return nil, err |
| 36 |
} |
| 37 |
defer release() |
| 38 |
|
| 39 |
if kind := snapshot.FileKind(fh); kind != file.Go { |
| 40 |
return nil, fmt.Errorf("SelectionRange not supported for file of type %s", kind) |
| 41 |
} |
| 42 |
|
| 43 |
pgf, err := snapshot.ParseGo(ctx, fh, parsego.Full) |
| 44 |
if err != nil { |
| 45 |
return nil, err |
| 46 |
} |
| 47 |
|
| 48 |
result := make([]protocol.SelectionRange, len(params.Positions)) |
| 49 |
for i, protocolPos := range params.Positions { |
| 50 |
pos, err := pgf.PositionPos(protocolPos) |
| 51 |
if err != nil { |
| 52 |
return nil, err |
| 53 |
} |
| 54 |
|
| 55 |
path, _ := astutil.PathEnclosingInterval(pgf.File, pos, pos) |
| 56 |
|
| 57 |
tail := &result[i] |
| 58 |
|
| 59 |
for j, node := range path { |
| 60 |
rng, err := pgf.NodeRange(node) |
| 61 |
if err != nil { |
| 62 |
return nil, err |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
if j > 0 { |
| 67 |
tail.Parent = &protocol.SelectionRange{} |
| 68 |
tail = tail.Parent |
| 69 |
} |
| 70 |
tail.Range = rng |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return result, nil |
| 75 |
} |