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-02-12 14:36:46 -07:00
|
|
|
uri := params.TextDocument.URI.SpanURI()
|
2019-11-15 10:43:45 -07:00
|
|
|
view, err := s.session.ViewOf(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-20 14:38:43 -07:00
|
|
|
snapshot := view.Snapshot()
|
2020-01-10 15:37:29 -07:00
|
|
|
fh, err := snapshot.GetFile(uri)
|
2019-08-28 19:48:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-10 09:51:34 -07:00
|
|
|
var ranges []*source.FoldingRangeInfo
|
2019-12-17 16:57:54 -07:00
|
|
|
switch fh.Identity().Kind {
|
2019-12-10 09:51:34 -07:00
|
|
|
case source.Go:
|
2019-12-17 16:57:54 -07:00
|
|
|
ranges, err = source.FoldingRange(ctx, snapshot, fh, view.Options().LineFoldingOnly)
|
2019-12-10 09:51:34 -07:00
|
|
|
case source.Mod:
|
|
|
|
ranges = nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|