55package golang
66
77import (
8- "bytes"
98 "cmp"
109 "context"
1110 "go/ast"
@@ -65,7 +64,7 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
6564 switch n := cur .Node ().(type ) {
6665 case * ast.BlockStmt :
6766 // Fold between positions of or lines between "{" and "}".
68- start , end = getLineFoldingRange ( pgf , n .Lbrace , n .Rbrace , lineFoldingOnly )
67+ start , end = bracketedFoldingRange ( n .Lbrace , n .Rbrace )
6968
7069 case * ast.CaseClause :
7170 // Fold from position of ":" to end.
@@ -77,19 +76,19 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
7776
7877 case * ast.CallExpr :
7978 // Fold between positions of or lines between "(" and ")".
80- start , end = getLineFoldingRange ( pgf , n .Lparen , n .Rparen , lineFoldingOnly )
79+ start , end = bracketedFoldingRange ( n .Lparen , n .Rparen )
8180
8281 case * ast.FieldList :
8382 // Fold between positions of or lines between opening parenthesis/brace and closing parenthesis/brace.
84- start , end = getLineFoldingRange ( pgf , n .Opening , n .Closing , lineFoldingOnly )
83+ start , end = bracketedFoldingRange ( n .Opening , n .Closing )
8584
8685 case * ast.GenDecl :
8786 // If this is an import declaration, set the kind to be protocol.Imports.
8887 if n .Tok == token .IMPORT {
8988 kind = protocol .Imports
9089 }
9190 // Fold between positions of or lines between "(" and ")".
92- start , end = getLineFoldingRange ( pgf , n .Lparen , n .Rparen , lineFoldingOnly )
91+ start , end = bracketedFoldingRange ( n .Lparen , n .Rparen )
9392
9493 case * ast.BasicLit :
9594 // Fold raw string literals from position of "`" to position of "`".
@@ -99,7 +98,7 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
9998
10099 case * ast.CompositeLit :
101100 // Fold between positions of or lines between "{" and "}".
102- start , end = getLineFoldingRange ( pgf , n .Lbrace , n .Rbrace , lineFoldingOnly )
101+ start , end = bracketedFoldingRange ( n .Lbrace , n .Rbrace )
103102
104103 default :
105104 panic (n )
@@ -136,9 +135,9 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
136135 return ranges , nil
137136}
138137
139- // getLineFoldingRange returns the folding range for nodes with parentheses/braces/brackets
138+ // bracketedFoldingRange returns the folding range for nodes with parentheses/braces/brackets
140139// that potentially can take up multiple lines.
141- func getLineFoldingRange ( pgf * parsego. File , open , close token.Pos , lineFoldingOnly bool ) (token.Pos , token.Pos ) {
140+ func bracketedFoldingRange ( open , close token.Pos ) (token.Pos , token.Pos ) {
142141 if ! open .IsValid () || ! close .IsValid () {
143142 return token .NoPos , token .NoPos
144143 }
@@ -147,56 +146,38 @@ func getLineFoldingRange(pgf *parsego.File, open, close token.Pos, lineFoldingOn
147146 return token .NoPos , token .NoPos
148147 }
149148
150- if ! lineFoldingOnly {
151- // Can fold between opening and closing parenthesis/brace
152- // even if they are on the same line.
153- return open + 1 , close
154- }
155-
156149 // Clients with "LineFoldingOnly" set to true can fold only full lines.
157- // So, we return a folding range only when the closing parenthesis/brace
158- // and the end of the last argument/statement/element are on different lines.
150+ // This is checked in the caller.
159151 //
160- // We could skip the check for the opening parenthesis/brace and start of
161- // the first argument/statement/element. For example, the following code
152+ // Clients that support folding ranges can display them in various ways
153+ // (e.g., how are folding ranges marked? is the final line displayed?).
154+ // The most common client
155+ // is vscode, which displays the first line followed by ..., and then does not
156+ // display any other lines in the range, but other clients might also display
157+ // final line of the range. For example, the following code
162158 //
163159 // var x = []string{"a",
164160 // "b",
165161 // "c" }
166162 //
167- // can be folded to
163+ // can be folded (in vscode) to
164+ //
165+ // var x = []string{"a", ...
166+ //
167+ // or in some other client
168168 //
169169 // var x = []string{"a", ...
170170 // "c" }
171171 //
172- // However, this might look confusing. So, check the lines of "open" and
173- // "start" positions as well.
174-
175- // isOnlySpaceBetween returns true if there are only space characters between "from" and "to".
176- isOnlySpaceBetween := func (from token.Pos , to token.Pos ) bool {
177- text , err := pgf .PosText (from , to )
178- if err != nil {
179- bug .Reportf ("failed to get offsets: %s" , err ) // can't happen
180- return false
181- }
182- return len (bytes .TrimSpace (text )) == 0
183- }
184-
185- nextLine := safetoken .Line (pgf .Tok , open ) + 1
186- if nextLine > pgf .Tok .LineCount () {
187- return token .NoPos , token .NoPos
188- }
189- nextLineStart := pgf .Tok .LineStart (nextLine )
190- if ! isOnlySpaceBetween (open + 1 , nextLineStart ) {
191- return token .NoPos , token .NoPos
192- }
193-
194- prevLineEnd := pgf .Tok .LineStart (safetoken .Line (pgf .Tok , close )) - 1 // there must be a previous line
195- if ! isOnlySpaceBetween (prevLineEnd , close ) {
196- return token .NoPos , token .NoPos
197- }
172+ // This is a change in behavior. The old code would not fold this example,
173+ // nor would it have folded
174+ //
175+ // func foo() { // a non-godoc comment
176+ // ...
177+ // }
178+ // which seems wrong.
198179
199- return open + 1 , prevLineEnd
180+ return open + 1 , close
200181}
201182
202183// commentsFoldingRange returns the folding ranges for all comment blocks in file.
0 commit comments