@@ -56,15 +56,13 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
5656 (* ast .GenDecl )(nil ),
5757 }
5858 for cur := range pgf .Cursor .Preorder (filter ... ) {
59- // TODO(suzmue): include trailing empty lines before the closing
60- // parenthesis/brace.
6159 var kind protocol.FoldingRangeKind
6260 // start and end define the range of content to fold away.
6361 var start , end token.Pos
6462 switch n := cur .Node ().(type ) {
6563 case * ast.BlockStmt :
6664 // 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 )
6866
6967 case * ast.CaseClause :
7068 // Fold from position of ":" to end.
@@ -76,19 +74,19 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
7674
7775 case * ast.CallExpr :
7876 // 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 )
8078
8179 case * ast.FieldList :
8280 // 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 )
8482
8583 case * ast.GenDecl :
8684 // If this is an import declaration, set the kind to be protocol.Imports.
8785 if n .Tok == token .IMPORT {
8886 kind = protocol .Imports
8987 }
9088 // 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 )
9290
9391 case * ast.BasicLit :
9492 // 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,
9896
9997 case * ast.CompositeLit :
10098 // 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 )
102100
103101 default :
104102 panic (n )
@@ -137,7 +135,7 @@ func FoldingRange(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle,
137135
138136// bracketedFoldingRange returns the folding range for nodes with parentheses/braces/brackets
139137// 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 ) {
141139 if ! open .IsValid () || ! close .IsValid () {
142140 return token .NoPos , token .NoPos
143141 }
@@ -146,8 +144,12 @@ func bracketedFoldingRange(open, close token.Pos) (token.Pos, token.Pos) {
146144 return token .NoPos , token .NoPos
147145 }
148146
147+ if ! lineFoldingOnly {
148+ return open + 1 , close
149+ }
150+
149151 // 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.
151153 //
152154 // Clients that support folding ranges can display them in various ways
153155 // (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) {
169171 // var x = []string{"a", ...
170172 // "c" }
171173 //
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
179176
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
181182}
182183
183184// commentsFoldingRange returns the folding ranges for all comment blocks in file.
0 commit comments