Skip to content

Commit d2585c4

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/golang: folding range: remove FoldingRangeInfo
Another unnecessary data type. Updates golang/go#71489 Change-Id: I374f677afe44abf818a35741202579abfed4aeb3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/645855 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 0d16805 commit d2585c4

File tree

2 files changed

+34
-51
lines changed

2 files changed

+34
-51
lines changed

gopls/internal/golang/folding_range.go

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package golang
66

77
import (
88
"bytes"
9+
"cmp"
910
"context"
1011
"go/ast"
1112
"go/token"
@@ -20,14 +21,8 @@ import (
2021
"golang.org/x/tools/gopls/internal/util/safetoken"
2122
)
2223

23-
// FoldingRangeInfo holds range and kind info of folding for an ast.Node
24-
type FoldingRangeInfo struct {
25-
Range protocol.Range
26-
Kind protocol.FoldingRangeKind
27-
}
28-
2924
// FoldingRange gets all of the folding range for f.
30-
func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, lineFoldingOnly bool) (ranges []*FoldingRangeInfo, err error) {
25+
func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, lineFoldingOnly bool) ([]protocol.FoldingRange, error) {
3126
// TODO(suzmue): consider limiting the number of folding ranges returned, and
3227
// implement a way to prioritize folding ranges in that case.
3328
pgf, err := snapshot.ParseGo(ctx, fh, parsego.Full)
@@ -48,27 +43,29 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
4843
}
4944

5045
// Get folding ranges for comments separately as they are not walked by ast.Inspect.
51-
ranges = append(ranges, commentsFoldingRange(pgf)...)
46+
ranges := commentsFoldingRange(pgf)
5247

53-
visit := func(n ast.Node) bool {
54-
rng := foldingRangeFunc(pgf, n, lineFoldingOnly)
55-
if rng != nil {
48+
// Walk the ast and collect folding ranges.
49+
ast.Inspect(pgf.File, func(n ast.Node) bool {
50+
if rng, ok := foldingRangeFunc(pgf, n, lineFoldingOnly); ok {
5651
ranges = append(ranges, rng)
5752
}
5853
return true
59-
}
60-
// Walk the ast and collect folding ranges.
61-
ast.Inspect(pgf.File, visit)
54+
})
6255

63-
slices.SortFunc(ranges, func(x, y *FoldingRangeInfo) int {
64-
return protocol.CompareRange(x.Range, y.Range)
56+
// Sort by start position.
57+
slices.SortFunc(ranges, func(x, y protocol.FoldingRange) int {
58+
if d := cmp.Compare(x.StartLine, y.StartLine); d != 0 {
59+
return d
60+
}
61+
return cmp.Compare(x.StartCharacter, y.StartCharacter)
6562
})
6663

6764
return ranges, nil
6865
}
6966

7067
// foldingRangeFunc calculates the line folding range for ast.Node n
71-
func foldingRangeFunc(pgf *parsego.File, n ast.Node, lineFoldingOnly bool) *FoldingRangeInfo {
68+
func foldingRangeFunc(pgf *parsego.File, n ast.Node, lineFoldingOnly bool) (protocol.FoldingRange, bool) {
7269
// TODO(suzmue): include trailing empty lines before the closing
7370
// parenthesis/brace.
7471
var kind protocol.FoldingRangeKind
@@ -109,25 +106,22 @@ func foldingRangeFunc(pgf *parsego.File, n ast.Node, lineFoldingOnly bool) *Fold
109106

110107
// Check that folding positions are valid.
111108
if !start.IsValid() || !end.IsValid() {
112-
return nil
109+
return protocol.FoldingRange{}, false
113110
}
114111
if start == end {
115112
// Nothing to fold.
116-
return nil
113+
return protocol.FoldingRange{}, false
117114
}
118115
// in line folding mode, do not fold if the start and end lines are the same.
119116
if lineFoldingOnly && safetoken.Line(pgf.Tok, start) == safetoken.Line(pgf.Tok, end) {
120-
return nil
117+
return protocol.FoldingRange{}, false
121118
}
122119
rng, err := pgf.PosRange(start, end)
123120
if err != nil {
124121
bug.Reportf("failed to create range: %s", err) // can't happen
125-
return nil
126-
}
127-
return &FoldingRangeInfo{
128-
Range: rng,
129-
Kind: kind,
122+
return protocol.FoldingRange{}, false
130123
}
124+
return foldingRange(kind, rng), true
131125
}
132126

133127
// getLineFoldingRange returns the folding range for nodes with parentheses/braces/brackets
@@ -196,7 +190,7 @@ func getLineFoldingRange(pgf *parsego.File, open, close token.Pos, lineFoldingOn
196190
// commentsFoldingRange returns the folding ranges for all comment blocks in file.
197191
// The folding range starts at the end of the first line of the comment block, and ends at the end of the
198192
// comment block and has kind protocol.Comment.
199-
func commentsFoldingRange(pgf *parsego.File) (comments []*FoldingRangeInfo) {
193+
func commentsFoldingRange(pgf *parsego.File) (comments []protocol.FoldingRange) {
200194
tokFile := pgf.Tok
201195
for _, commentGrp := range pgf.File.Comments {
202196
startGrpLine, endGrpLine := safetoken.Line(tokFile, commentGrp.Pos()), safetoken.Line(tokFile, commentGrp.End())
@@ -218,11 +212,19 @@ func commentsFoldingRange(pgf *parsego.File) (comments []*FoldingRangeInfo) {
218212
bug.Reportf("failed to create mapped range: %s", err) // can't happen
219213
continue
220214
}
221-
comments = append(comments, &FoldingRangeInfo{
222-
// Fold from the end of the first line comment to the end of the comment block.
223-
Range: rng,
224-
Kind: protocol.Comment,
225-
})
215+
// Fold from the end of the first line comment to the end of the comment block.
216+
comments = append(comments, foldingRange(protocol.Comment, rng))
226217
}
227218
return comments
228219
}
220+
221+
func foldingRange(kind protocol.FoldingRangeKind, rng protocol.Range) protocol.FoldingRange {
222+
return protocol.FoldingRange{
223+
// I have no idea why LSP doesn't use a protocol.Range here.
224+
StartLine: rng.Start.Line,
225+
StartCharacter: rng.Start.Character,
226+
EndLine: rng.End.Line,
227+
EndCharacter: rng.End.Character,
228+
Kind: string(kind),
229+
}
230+
}

gopls/internal/server/folding_range.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,5 @@ func (s *server) FoldingRange(ctx context.Context, params *protocol.FoldingRange
2626
if snapshot.FileKind(fh) != file.Go {
2727
return nil, nil // empty result
2828
}
29-
ranges, err := golang.FoldingRange(ctx, snapshot, fh, snapshot.Options().LineFoldingOnly)
30-
if err != nil {
31-
return nil, err
32-
}
33-
return toProtocolFoldingRanges(ranges)
34-
}
35-
36-
func toProtocolFoldingRanges(ranges []*golang.FoldingRangeInfo) ([]protocol.FoldingRange, error) {
37-
result := make([]protocol.FoldingRange, 0, len(ranges))
38-
for _, info := range ranges {
39-
rng := info.Range
40-
result = append(result, protocol.FoldingRange{
41-
StartLine: rng.Start.Line,
42-
StartCharacter: rng.Start.Character,
43-
EndLine: rng.End.Line,
44-
EndCharacter: rng.End.Character,
45-
Kind: string(info.Kind),
46-
})
47-
}
48-
return result, nil
29+
return golang.FoldingRange(ctx, snapshot, fh, snapshot.Options().LineFoldingOnly)
4930
}

0 commit comments

Comments
 (0)