| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package golang |
| 6 |
|
| 7 |
import ( |
| 8 |
"cmp" |
| 9 |
"context" |
| 10 |
"go/ast" |
| 11 |
"go/token" |
| 12 |
"slices" |
| 13 |
"strings" |
| 14 |
|
| 15 |
"golang.org/x/tools/gopls/internal/cache" |
| 16 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 17 |
"golang.org/x/tools/gopls/internal/file" |
| 18 |
"golang.org/x/tools/gopls/internal/protocol" |
| 19 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 20 |
"golang.org/x/tools/gopls/internal/util/safetoken" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, lineFoldingOnly bool) ([]protocol.FoldingRange, error) { |
| 25 |
|
| 26 |
|
| 27 |
pgf, err := snapshot.ParseGo(ctx, fh, parsego.Full) |
| 28 |
if err != nil { |
| 29 |
return nil, err |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
if pgf.ParseErr != nil { |
| 41 |
return nil, nil |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
ranges := commentsFoldingRange(pgf) |
| 46 |
|
| 47 |
|
| 48 |
filter := []ast.Node{ |
| 49 |
(*ast.BasicLit)(nil), |
| 50 |
(*ast.BlockStmt)(nil), |
| 51 |
(*ast.CallExpr)(nil), |
| 52 |
(*ast.CaseClause)(nil), |
| 53 |
(*ast.CommClause)(nil), |
| 54 |
(*ast.CompositeLit)(nil), |
| 55 |
(*ast.FieldList)(nil), |
| 56 |
(*ast.GenDecl)(nil), |
| 57 |
} |
| 58 |
for cur := range pgf.Cursor().Preorder(filter...) { |
| 59 |
var kind protocol.FoldingRangeKind |
| 60 |
|
| 61 |
var start, end token.Pos |
| 62 |
switch n := cur.Node().(type) { |
| 63 |
case *ast.BlockStmt: |
| 64 |
|
| 65 |
start, end = bracketedFoldingRange(pgf, n.Lbrace, n.Rbrace, lineFoldingOnly) |
| 66 |
|
| 67 |
case *ast.CaseClause: |
| 68 |
|
| 69 |
start, end = n.Colon+1, n.End() |
| 70 |
|
| 71 |
case *ast.CommClause: |
| 72 |
|
| 73 |
start, end = n.Colon+1, n.End() |
| 74 |
|
| 75 |
case *ast.CallExpr: |
| 76 |
|
| 77 |
start, end = bracketedFoldingRange(pgf, n.Lparen, n.Rparen, lineFoldingOnly) |
| 78 |
|
| 79 |
case *ast.FieldList: |
| 80 |
|
| 81 |
start, end = bracketedFoldingRange(pgf, n.Opening, n.Closing, lineFoldingOnly) |
| 82 |
|
| 83 |
case *ast.GenDecl: |
| 84 |
|
| 85 |
if n.Tok == token.IMPORT { |
| 86 |
kind = protocol.Imports |
| 87 |
} |
| 88 |
|
| 89 |
start, end = bracketedFoldingRange(pgf, n.Lparen, n.Rparen, lineFoldingOnly) |
| 90 |
|
| 91 |
case *ast.BasicLit: |
| 92 |
|
| 93 |
if n.Kind == token.STRING && len(n.Value) >= 2 && n.Value[0] == '`' && n.Value[len(n.Value)-1] == '`' { |
| 94 |
start, end = n.Pos(), n.End() |
| 95 |
} |
| 96 |
|
| 97 |
case *ast.CompositeLit: |
| 98 |
|
| 99 |
start, end = bracketedFoldingRange(pgf, n.Lbrace, n.Rbrace, lineFoldingOnly) |
| 100 |
|
| 101 |
default: |
| 102 |
panic(n) |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
if !start.IsValid() || !end.IsValid() { |
| 107 |
continue |
| 108 |
} |
| 109 |
if start == end { |
| 110 |
|
| 111 |
continue |
| 112 |
} |
| 113 |
|
| 114 |
if lineFoldingOnly && safetoken.Line(pgf.Tok, start) == safetoken.Line(pgf.Tok, end) { |
| 115 |
continue |
| 116 |
} |
| 117 |
rng, err := pgf.PosRange(start, end) |
| 118 |
if err != nil { |
| 119 |
bug.Reportf("failed to create range: %s", err) |
| 120 |
continue |
| 121 |
} |
| 122 |
ranges = append(ranges, foldingRange(kind, rng)) |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
slices.SortFunc(ranges, func(x, y protocol.FoldingRange) int { |
| 127 |
if d := cmp.Compare(*x.StartLine, *y.StartLine); d != 0 { |
| 128 |
return d |
| 129 |
} |
| 130 |
return cmp.Compare(*x.StartCharacter, *y.StartCharacter) |
| 131 |
}) |
| 132 |
|
| 133 |
return ranges, nil |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
func bracketedFoldingRange(pgf *parsego.File, open, close token.Pos, lineFoldingOnly bool) (token.Pos, token.Pos) { |
| 139 |
if !open.IsValid() || !close.IsValid() { |
| 140 |
return token.NoPos, token.NoPos |
| 141 |
} |
| 142 |
if open+1 == close { |
| 143 |
|
| 144 |
return token.NoPos, token.NoPos |
| 145 |
} |
| 146 |
|
| 147 |
if !lineFoldingOnly { |
| 148 |
return open + 1, close |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
prevLineEnd := pgf.Tok.LineStart(safetoken.Line(pgf.Tok, close)) - 1 |
| 178 |
if prevLineEnd <= open { |
| 179 |
return token.NoPos, token.NoPos |
| 180 |
} |
| 181 |
return open + 1, prevLineEnd |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
func commentsFoldingRange(pgf *parsego.File) (comments []protocol.FoldingRange) { |
| 188 |
tokFile := pgf.Tok |
| 189 |
for _, commentGrp := range pgf.File.Comments { |
| 190 |
startGrpLine, endGrpLine := safetoken.Line(tokFile, commentGrp.Pos()), safetoken.Line(tokFile, commentGrp.End()) |
| 191 |
if startGrpLine == endGrpLine { |
| 192 |
|
| 193 |
continue |
| 194 |
} |
| 195 |
|
| 196 |
firstComment := commentGrp.List[0] |
| 197 |
startPos, endLinePos := firstComment.Pos(), firstComment.End() |
| 198 |
startCmmntLine, endCmmntLine := safetoken.Line(tokFile, startPos), safetoken.Line(tokFile, endLinePos) |
| 199 |
if startCmmntLine != endCmmntLine { |
| 200 |
|
| 201 |
|
| 202 |
endLinePos = token.Pos(int(startPos) + len(strings.Split(firstComment.Text, "\n")[0])) |
| 203 |
} |
| 204 |
rng, err := pgf.PosRange(endLinePos, commentGrp.End()) |
| 205 |
if err != nil { |
| 206 |
bug.Reportf("failed to create mapped range: %s", err) |
| 207 |
continue |
| 208 |
} |
| 209 |
|
| 210 |
comments = append(comments, foldingRange(protocol.Comment, rng)) |
| 211 |
} |
| 212 |
return comments |
| 213 |
} |
| 214 |
|
| 215 |
func foldingRange(kind protocol.FoldingRangeKind, rng protocol.Range) protocol.FoldingRange { |
| 216 |
return protocol.FoldingRange{ |
| 217 |
|
| 218 |
|
| 219 |
StartLine: varOf(rng.Start.Line), |
| 220 |
StartCharacter: varOf(rng.Start.Character), |
| 221 |
EndLine: varOf(rng.End.Line), |
| 222 |
EndCharacter: varOf(rng.End.Character), |
| 223 |
Kind: string(kind), |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
func varOf[T any](x T) *T { return &x } |