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"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) foldingRange(ctx context.Context, params *protocol.FoldingRangeParams) ([]protocol.FoldingRange, error) {
|
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
2019-11-15 10:43:45 -07:00
|
|
|
view, err := s.session.ViewOf(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
f, err := view.GetFile(ctx, uri)
|
2019-08-28 19:48:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-11 16:21:01 -06:00
|
|
|
ranges, err := source.FoldingRange(ctx, view, f, 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
|
|
|
}
|