Skip to content

Commit 86f1068

Browse files
committed
revert: filtering same line foldings and multiple foldings on the same line as it can be handled by clients
1 parent 9181b04 commit 86f1068

File tree

3 files changed

+42
-39
lines changed

3 files changed

+42
-39
lines changed

plugins/hls-code-range-plugin/src/Ide/Plugin/CodeRange.hs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ foldingRangeHandler ide _ FoldingRangeParams{..} = do
7979
toNormalizedFilePath' <$> uriToFilePath' uri
8080
foldingRanges <- ExceptT . liftIO . runIdeAction "FoldingRange" (shakeExtras ide) . runExceptT $
8181
getFoldingRanges filePath
82-
pure . List $ removeDupStartLineFoldings foldingRanges
82+
pure . List $ foldingRanges
8383
where
8484
uri :: Uri
8585
TextDocumentIdentifier uri = _textDocument
@@ -152,6 +152,21 @@ findPosition pos root = go Nothing root
152152
--
153153
-- It starts with the root node, converts that into a folding range then moves towards the children.
154154
-- 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
155170
findFoldingRanges :: CodeRange -> [FoldingRange]
156171
findFoldingRanges r@(CodeRange _ children _) =
157172
let frChildren :: [FoldingRange] = concat $ V.toList $ fmap findFoldingRanges children
@@ -165,41 +180,7 @@ createFoldingRange (CodeRange (Range (Position lineStart charStart) (Position li
165180
-- Type conversion of codeRangeKind to FoldingRangeKind
166181
let frk = crkToFrk ck
167182

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))
203184

204185
-- | Likes 'toCurrentPosition', but works on 'SelectionRange'
205186
toCurrentSelectionRange :: PositionMapping -> SelectionRange -> Maybe SelectionRange

plugins/hls-code-range-plugin/test/Ide/Plugin/CodeRangeTest.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ testTree =
5252
)
5353
],
5454

55+
-- TODO: Some more tests can be added on strange cases like
56+
-- 1. lots of blank lines in between type signature and the body
57+
-- 2. lots of blank lines in the function itself
58+
-- etc.
5559
testGroup "findFoldingRanges" $
5660
let check :: CodeRange -> [FoldingRange] -> Assertion
5761
check codeRange = (findFoldingRanges codeRange @?=)
@@ -88,7 +92,7 @@ testTree =
8892
-- Single line returns [] because single line ranges need not be folded
8993
testCase "Test Single Line" $ check
9094
(mkCodeRange (Position 1 0) (Position 1 15) [] CodeKindRegion)
91-
[],
95+
[FoldingRange 1 (Just 0) 1 (Just 15) (Just FoldingRangeRegion)],
9296

9397
-- MultiLine imports
9498
testCase "MultiLine Imports" $ check
@@ -110,6 +114,6 @@ testTree =
110114
-- If a range has the same start and end line it need not be folded so Nothing is expected
111115
testCase "Test Same Start Line" $ check
112116
(mkCodeRange (Position 1 1) (Position 1 10) [] CodeKindRegion)
113-
Nothing
117+
(Just (FoldingRange 1 (Just 1) 1 (Just 10) (Just FoldingRangeRegion)))
114118
]
115119
]
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
11
((4, 0) : (7, 21)) : FoldingRangeRegion
2-
((5, 0) : (7, 21)) : FoldingRangeRegion
2+
((4, 0) : (4, 25)) : FoldingRangeRegion
3+
((4, 0) : (4, 6)) : FoldingRangeRegion
4+
((4, 10) : (4, 25)) : FoldingRangeRegion
5+
((4, 10) : (4, 17)) : FoldingRangeRegion
6+
((4, 21) : (4, 25)) : FoldingRangeRegion
7+
((5, 0) : (7, 21)) : FoldingRangeRegion
8+
((5, 0) : (5, 6)) : FoldingRangeRegion
9+
((5, 7) : (5, 8)) : FoldingRangeRegion
10+
((5, 9) : (7, 21)) : FoldingRangeRegion
11+
((5, 11) : (7, 21)) : FoldingRangeRegion
12+
((5, 14) : (5, 28)) : FoldingRangeRegion
13+
((5, 14) : (5, 23)) : FoldingRangeRegion
14+
((5, 14) : (5, 15)) : FoldingRangeRegion
15+
((5, 16) : (5, 21)) : FoldingRangeRegion
16+
((5, 22) : (5, 23)) : FoldingRangeRegion
17+
((5, 24) : (5, 26)) : FoldingRangeRegion
18+
((5, 27) : (5, 28)) : FoldingRangeRegion
19+
((6, 16) : (6, 20)) : FoldingRangeRegion
20+
((7, 16) : (7, 21)) : FoldingRangeRegion

0 commit comments

Comments
 (0)