1
0
mirror of https://github.com/golang/go synced 2024-10-01 07:38:32 -06:00
go/internal/lsp/folding_range.go
Rohan Challa ad473c03aa internal/lsp: add handling for go.mod files in internal/lsp functions
When we are processing a go.mod file, we are calling go/packages.load
when we should not be. It will always return 0 packages since it is
not a .go file. This CL adds branching inside each internal/lsp protocol
function and also adds a check in snapshot.PackageHandles for the file type
and returns an error. This will prevent `go list` from running on go.mod files for now.

Updates golang/go#31999

Change-Id: Ic6d0e9b7c81e1f404342b98e10b9c5387adde2ee
Reviewed-on: https://go-review.googlesource.com/c/tools/+/210757
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rohan Challa <rohan@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-12-11 23:24:34 +00:00

54 lines
1.3 KiB
Go

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)
view, err := s.session.ViewOf(uri)
if err != nil {
return nil, err
}
snapshot := view.Snapshot()
f, err := view.GetFile(ctx, uri)
if err != nil {
return nil, err
}
var ranges []*source.FoldingRangeInfo
switch f.Kind() {
case source.Go:
ranges, err = source.FoldingRange(ctx, snapshot, f, view.Options().LineFoldingOnly)
case source.Mod:
ranges = nil
}
if err != nil {
return nil, err
}
return toProtocolFoldingRanges(ranges)
}
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),
})
}
return result, nil
}