github.com/v2fly/tools@v0.100.0/internal/lsp/folding_range.go (about) 1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package lsp 6 7 import ( 8 "context" 9 10 "github.com/v2fly/tools/internal/lsp/protocol" 11 "github.com/v2fly/tools/internal/lsp/source" 12 ) 13 14 func (s *Server) foldingRange(ctx context.Context, params *protocol.FoldingRangeParams) ([]protocol.FoldingRange, error) { 15 snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go) 16 defer release() 17 if !ok { 18 return nil, err 19 } 20 21 ranges, err := source.FoldingRange(ctx, snapshot, fh, snapshot.View().Options().LineFoldingOnly) 22 if err != nil { 23 return nil, err 24 } 25 return toProtocolFoldingRanges(ranges) 26 } 27 28 func toProtocolFoldingRanges(ranges []*source.FoldingRangeInfo) ([]protocol.FoldingRange, error) { 29 result := make([]protocol.FoldingRange, 0, len(ranges)) 30 for _, info := range ranges { 31 rng, err := info.Range() 32 if err != nil { 33 return nil, err 34 } 35 result = append(result, protocol.FoldingRange{ 36 StartLine: rng.Start.Line, 37 StartCharacter: rng.Start.Character, 38 EndLine: rng.End.Line, 39 EndCharacter: rng.End.Character, 40 Kind: string(info.Kind), 41 }) 42 } 43 return result, nil 44 }