2019-08-28 19:48:29 -06:00
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) foldingRange(ctx context.Context, params *protocol.FoldingRangeParams) ([]protocol.FoldingRange, error) {
|
2020-07-02 16:34:10 -06:00
|
|
|
snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go)
|
|
|
|
defer release()
|
2020-02-13 11:46:49 -07:00
|
|
|
if !ok {
|
2019-11-15 10:43:45 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-10 09:51:34 -07:00
|
|
|
|
2020-02-13 11:46:49 -07:00
|
|
|
ranges, err := source.FoldingRange(ctx, snapshot, fh, snapshot.View().Options().LineFoldingOnly)
|
2019-08-28 19:48:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-05 18:14:09 -06:00
|
|
|
return toProtocolFoldingRanges(ranges)
|
|
|
|
}
|
2019-08-28 19:48:29 -06:00
|
|
|
|
2019-09-05 18:14:09 -06:00
|
|
|
func toProtocolFoldingRanges(ranges []*source.FoldingRangeInfo) ([]protocol.FoldingRange, error) {
|
|
|
|
result := make([]protocol.FoldingRange, 0, len(ranges))
|
|
|
|
for _, info := range ranges {
|
|
|
|
rng, err := info.Range()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, protocol.FoldingRange{
|
|
|
|
StartLine: rng.Start.Line,
|
|
|
|
StartCharacter: rng.Start.Character,
|
|
|
|
EndLine: rng.End.Line,
|
|
|
|
EndCharacter: rng.End.Character,
|
|
|
|
Kind: string(info.Kind),
|
|
|
|
})
|
2019-08-28 19:48:29 -06:00
|
|
|
}
|
2019-09-05 18:14:09 -06:00
|
|
|
return result, nil
|
2019-08-28 19:48:29 -06:00
|
|
|
}
|