9 "golang.org/x/tools/internal/lsp/protocol"
12 // FoldingRangeInfo holds range and kind info of folding for an ast.Node
13 type FoldingRangeInfo struct {
15 Kind protocol.FoldingRangeKind
18 // FoldingRange gets all of the folding range for f.
19 func FoldingRange(ctx context.Context, snapshot Snapshot, fh FileHandle, lineFoldingOnly bool) (ranges []*FoldingRangeInfo, err error) {
20 // TODO(suzmue): consider limiting the number of folding ranges returned, and
21 // implement a way to prioritize folding ranges in that case.
22 pgf, err := snapshot.ParseGo(ctx, fh, ParseFull)
26 fset := snapshot.FileSet()
28 // Get folding ranges for comments separately as they are not walked by ast.Inspect.
29 ranges = append(ranges, commentsFoldingRange(fset, pgf.Mapper, pgf.File)...)
31 visit := func(n ast.Node) bool {
32 rng := foldingRangeFunc(fset, pgf.Mapper, n, lineFoldingOnly)
34 ranges = append(ranges, rng)
38 // Walk the ast and collect folding ranges.
39 ast.Inspect(pgf.File, visit)
41 sort.Slice(ranges, func(i, j int) bool {
42 irng, _ := ranges[i].Range()
43 jrng, _ := ranges[j].Range()
44 return protocol.CompareRange(irng, jrng) < 0
50 // foldingRangeFunc calculates the line folding range for ast.Node n
51 func foldingRangeFunc(fset *token.FileSet, m *protocol.ColumnMapper, n ast.Node, lineFoldingOnly bool) *FoldingRangeInfo {
52 // TODO(suzmue): include trailing empty lines before the closing
54 var kind protocol.FoldingRangeKind
55 var start, end token.Pos
56 switch n := n.(type) {
58 // Fold between positions of or lines between "{" and "}".
59 var startList, endList token.Pos
60 if num := len(n.List); num != 0 {
61 startList, endList = n.List[0].Pos(), n.List[num-1].End()
63 start, end = validLineFoldingRange(fset, n.Lbrace, n.Rbrace, startList, endList, lineFoldingOnly)
65 // Fold from position of ":" to end.
66 start, end = n.Colon+1, n.End()
68 // Fold from position of ":" to end.
69 start, end = n.Colon+1, n.End()
71 // Fold from position of "(" to position of ")".
72 start, end = n.Lparen+1, n.Rparen
74 // Fold between positions of or lines between opening parenthesis/brace and closing parenthesis/brace.
75 var startList, endList token.Pos
76 if num := len(n.List); num != 0 {
77 startList, endList = n.List[0].Pos(), n.List[num-1].End()
79 start, end = validLineFoldingRange(fset, n.Opening, n.Closing, startList, endList, lineFoldingOnly)
81 // If this is an import declaration, set the kind to be protocol.Imports.
82 if n.Tok == token.IMPORT {
83 kind = protocol.Imports
85 // Fold between positions of or lines between "(" and ")".
86 var startSpecs, endSpecs token.Pos
87 if num := len(n.Specs); num != 0 {
88 startSpecs, endSpecs = n.Specs[0].Pos(), n.Specs[num-1].End()
90 start, end = validLineFoldingRange(fset, n.Lparen, n.Rparen, startSpecs, endSpecs, lineFoldingOnly)
91 case *ast.CompositeLit:
92 // Fold between positions of or lines between "{" and "}".
93 var startElts, endElts token.Pos
94 if num := len(n.Elts); num != 0 {
95 startElts, endElts = n.Elts[0].Pos(), n.Elts[num-1].End()
97 start, end = validLineFoldingRange(fset, n.Lbrace, n.Rbrace, startElts, endElts, lineFoldingOnly)
100 // Check that folding positions are valid.
101 if !start.IsValid() || !end.IsValid() {
104 // in line folding mode, do not fold if the start and end lines are the same.
105 if lineFoldingOnly && fset.Position(start).Line == fset.Position(end).Line {
108 return &FoldingRangeInfo{
109 MappedRange: NewMappedRange(fset, m, start, end),
114 // validLineFoldingRange returns start and end token.Pos for folding range if the range is valid.
115 // returns token.NoPos otherwise, which fails token.IsValid check
116 func validLineFoldingRange(fset *token.FileSet, open, close, start, end token.Pos, lineFoldingOnly bool) (token.Pos, token.Pos) {
118 if !open.IsValid() || !close.IsValid() {
119 return token.NoPos, token.NoPos
122 // Don't want to fold if the start/end is on the same line as the open/close
123 // as an example, the example below should *not* fold:
124 // var x = [2]string{"d",
126 if fset.Position(open).Line == fset.Position(start).Line ||
127 fset.Position(close).Line == fset.Position(end).Line {
128 return token.NoPos, token.NoPos
133 return open + 1, close
136 // commentsFoldingRange returns the folding ranges for all comment blocks in file.
137 // The folding range starts at the end of the first comment, and ends at the end of the
138 // comment block and has kind protocol.Comment.
139 func commentsFoldingRange(fset *token.FileSet, m *protocol.ColumnMapper, file *ast.File) (comments []*FoldingRangeInfo) {
140 for _, commentGrp := range file.Comments {
141 // Don't fold single comments.
142 if len(commentGrp.List) <= 1 {
145 comments = append(comments, &FoldingRangeInfo{
146 // Fold from the end of the first line comment to the end of the comment block.
147 MappedRange: NewMappedRange(fset, m, commentGrp.List[0].End(), commentGrp.End()),
148 Kind: protocol.Comment,