@@ -56,15 +56,13 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
56
56
(* ast .GenDecl )(nil ),
57
57
}
58
58
for cur := range pgf .Cursor .Preorder (filter ... ) {
59
- // TODO(suzmue): include trailing empty lines before the closing
60
- // parenthesis/brace.
61
59
var kind protocol.FoldingRangeKind
62
60
// start and end define the range of content to fold away.
63
61
var start , end token.Pos
64
62
switch n := cur .Node ().(type ) {
65
63
case * ast.BlockStmt :
66
64
// Fold between positions of or lines between "{" and "}".
67
- start , end = bracketedFoldingRange (n .Lbrace , n .Rbrace )
65
+ start , end = bracketedFoldingRange (pgf , n .Lbrace , n .Rbrace , lineFoldingOnly )
68
66
69
67
case * ast.CaseClause :
70
68
// Fold from position of ":" to end.
@@ -76,19 +74,19 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
76
74
77
75
case * ast.CallExpr :
78
76
// Fold between positions of or lines between "(" and ")".
79
- start , end = bracketedFoldingRange (n .Lparen , n .Rparen )
77
+ start , end = bracketedFoldingRange (pgf , n .Lparen , n .Rparen , lineFoldingOnly )
80
78
81
79
case * ast.FieldList :
82
80
// Fold between positions of or lines between opening parenthesis/brace and closing parenthesis/brace.
83
- start , end = bracketedFoldingRange (n .Opening , n .Closing )
81
+ start , end = bracketedFoldingRange (pgf , n .Opening , n .Closing , lineFoldingOnly )
84
82
85
83
case * ast.GenDecl :
86
84
// If this is an import declaration, set the kind to be protocol.Imports.
87
85
if n .Tok == token .IMPORT {
88
86
kind = protocol .Imports
89
87
}
90
88
// Fold between positions of or lines between "(" and ")".
91
- start , end = bracketedFoldingRange (n .Lparen , n .Rparen )
89
+ start , end = bracketedFoldingRange (pgf , n .Lparen , n .Rparen , lineFoldingOnly )
92
90
93
91
case * ast.BasicLit :
94
92
// Fold raw string literals from position of "`" to position of "`".
@@ -98,7 +96,7 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
98
96
99
97
case * ast.CompositeLit :
100
98
// Fold between positions of or lines between "{" and "}".
101
- start , end = bracketedFoldingRange (n .Lbrace , n .Rbrace )
99
+ start , end = bracketedFoldingRange (pgf , n .Lbrace , n .Rbrace , lineFoldingOnly )
102
100
103
101
default :
104
102
panic (n )
@@ -137,7 +135,7 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
137
135
138
136
// bracketedFoldingRange returns the folding range for nodes with parentheses/braces/brackets
139
137
// that potentially can take up multiple lines.
140
- func bracketedFoldingRange (open , close token.Pos ) (token.Pos , token.Pos ) {
138
+ func bracketedFoldingRange (pgf * parsego. File , open , close token.Pos , lineFoldingOnly bool ) (token.Pos , token.Pos ) {
141
139
if ! open .IsValid () || ! close .IsValid () {
142
140
return token .NoPos , token .NoPos
143
141
}
@@ -146,8 +144,12 @@ func bracketedFoldingRange(open, close token.Pos) (token.Pos, token.Pos) {
146
144
return token .NoPos , token .NoPos
147
145
}
148
146
147
+ if ! lineFoldingOnly {
148
+ return open + 1 , close
149
+ }
150
+
149
151
// Clients with "LineFoldingOnly" set to true can fold only full lines.
150
- // This is checked in the caller.
152
+ // This is also checked in the caller.
151
153
//
152
154
// Clients that support folding ranges can display them in various ways
153
155
// (e.g., how are folding ranges marked? is the final line displayed?).
@@ -169,15 +171,14 @@ func bracketedFoldingRange(open, close token.Pos) (token.Pos, token.Pos) {
169
171
// var x = []string{"a", ...
170
172
// "c" }
171
173
//
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.
174
+ // This code displays the final line containing ),},], but not the closing quote
175
+ // of a multi-line string
179
176
180
- return open + 1 , close
177
+ prevLineEnd := pgf .Tok .LineStart (safetoken .Line (pgf .Tok , close )) - 1 // there was a previous line
178
+ if prevLineEnd <= open { // all the same line
179
+ return token .NoPos , token .NoPos
180
+ }
181
+ return open + 1 , prevLineEnd
181
182
}
182
183
183
184
// commentsFoldingRange returns the folding ranges for all comment blocks in file.
0 commit comments