@@ -79,7 +79,7 @@ foldingRangeHandler ide _ FoldingRangeParams{..} = do
79
79
toNormalizedFilePath' <$> uriToFilePath' uri
80
80
foldingRanges <- ExceptT . liftIO . runIdeAction " FoldingRange" (shakeExtras ide) . runExceptT $
81
81
getFoldingRanges filePath
82
- pure . List $ removeDupStartLineFoldings foldingRanges
82
+ pure . List $ foldingRanges
83
83
where
84
84
uri :: Uri
85
85
TextDocumentIdentifier uri = _textDocument
@@ -152,6 +152,21 @@ findPosition pos root = go Nothing root
152
152
--
153
153
-- It starts with the root node, converts that into a folding range then moves towards the children.
154
154
-- It converts each child of each root node and parses it to folding range and moves to its children.
155
+ --
156
+ -- Two cases to that are assumed to be taken care on the client side are:
157
+ --
158
+ -- 1. When a folding range starts and ends on the same line, it is upto the client if it wants to
159
+ -- fold a single line folding or not.
160
+ --
161
+ -- 2. As we are converting nodes of the ast into folding ranges, there are multiple nodes starting from a single line.
162
+ -- A single line of code doesn't mean a single node in AST, so this function removes all the nodes that have a duplicate
163
+ -- start line, ie. they start from the same line.
164
+ -- Eg. A multi-line function that also has a multi-line if statement starting from the same line should have the folding
165
+ -- according to the function.
166
+ --
167
+ -- We think the client can handle this, if not we could change to remove these in future
168
+ --
169
+ -- Discussion reference: https://github.com/haskell/haskell-language-server/pull/3058#discussion_r973737211
155
170
findFoldingRanges :: CodeRange -> [FoldingRange ]
156
171
findFoldingRanges r@ (CodeRange _ children _) =
157
172
let frChildren :: [FoldingRange ] = concat $ V. toList $ fmap findFoldingRanges children
@@ -165,41 +180,7 @@ createFoldingRange (CodeRange (Range (Position lineStart charStart) (Position li
165
180
-- Type conversion of codeRangeKind to FoldingRangeKind
166
181
let frk = crkToFrk ck
167
182
168
- -- Filtering code ranges that start and end on the same line as need/can not be folded.
169
- --
170
- -- Eg. A single line function will also generate a Folding Range but it cannot be folded
171
- -- because it is already single line, so omiting it.
172
- if lineStart == lineEnd
173
- then Nothing
174
- else Just (FoldingRange lineStart (Just charStart) lineEnd (Just charEnd) (Just frk))
175
-
176
- -- | Removes all small foldings that start from the same line.
177
- --
178
- -- As we are converting nodes of the ast into folding ranges, there are multiple nodes starting from a single line.
179
- -- A single line of code doesn't mean a single node in AST, so this function removes all the nodes that have a duplicate
180
- -- start line, ie. they start from the same line.
181
- --
182
- -- This function preserves the largest folding range from the ranges that start from the same line.
183
- --
184
- -- Eg. A multi-line function that also has a multi-line if statement starting from the same line should have the folding
185
- -- according to the function.
186
- --
187
- -- This is done by breaking the [FoldingRange] into parts -->
188
- -- frx: Head
189
- -- xs: rest of the array
190
- -- fry(not shown as it is not used): head of xs
191
- -- xs2: rest of the array other than the first two elements
192
- -- slx and sly: start line of frx and fry
193
- --
194
- -- We compare the start line of the first two elements in the array and if the start line is the same we remove the
195
- -- second one as it is the smaller one amoung the two.
196
- -- otherwise frx is returned and the function runs recursively on xs.
197
- removeDupStartLineFoldings :: [FoldingRange ] -> [FoldingRange ]
198
- removeDupStartLineFoldings [] = []
199
- removeDupStartLineFoldings [x] = [x]
200
- removeDupStartLineFoldings (frx@ (FoldingRange slx _ _ _ _): xs@ ((FoldingRange sly _ _ _ _): xs2))
201
- | slx == sly = removeDupStartLineFoldings (frx: xs2)
202
- | otherwise = frx : removeDupStartLineFoldings xs
183
+ Just (FoldingRange lineStart (Just charStart) lineEnd (Just charEnd) (Just frk))
203
184
204
185
-- | Likes 'toCurrentPosition', but works on 'SelectionRange'
205
186
toCurrentSelectionRange :: PositionMapping -> SelectionRange -> Maybe SelectionRange
0 commit comments