Skip to content

Commit baf419e

Browse files
committed
fix: duplicate start line foldingranges and remove single line
foldingranges
1 parent a430a43 commit baf419e

File tree

3 files changed

+19
-24
lines changed

3 files changed

+19
-24
lines changed

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

Lines changed: 12 additions & 3 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 $ foldingRanges
82+
pure . List $ removeDupStartLineFoldings foldingRanges
8383
where
8484
uri :: Uri
8585
TextDocumentIdentifier uri = _textDocument
@@ -164,11 +164,20 @@ findFoldingRanges r@(CodeRange _ children _) =
164164
createFoldingRange :: CodeRange -> Maybe FoldingRange
165165
createFoldingRange (CodeRange (Range (Position lineStart charStart) (Position lineEnd charEnd)) _ ck) = do
166166
let frk = crkToFrk ck
167-
168-
case frk of
167+
if lineStart == lineEnd
168+
then Nothing
169+
else case frk of
169170
Just _ -> Just (FoldingRange lineStart (Just charStart) lineEnd (Just charEnd) frk)
170171
Nothing -> Nothing
171172

173+
-- Removes all small foldings that start from the same line
174+
removeDupStartLineFoldings :: [FoldingRange] -> [FoldingRange]
175+
removeDupStartLineFoldings [] = []
176+
removeDupStartLineFoldings [x] = [x]
177+
removeDupStartLineFoldings (frx@(FoldingRange x _ _ _ _):xs@((FoldingRange y _ _ _ _):xs2))
178+
| x == y = removeDupStartLineFoldings ([frx]++xs2)
179+
| otherwise = frx : removeDupStartLineFoldings xs
180+
172181
-- | Likes 'toCurrentPosition', but works on 'SelectionRange'
173182
toCurrentSelectionRange :: PositionMapping -> SelectionRange -> Maybe SelectionRange
174183
toCurrentSelectionRange positionMapping SelectionRange{..} = do

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ testTree =
8888
-- Single line
8989
testCase "Test Single Line" $ check
9090
(mkCodeRange (Position 1 0) (Position 1 15) [] CodeKindRegion)
91-
[FoldingRange 1 (Just 0) 1 (Just 15) (Just FoldingRangeRegion)],
91+
[],
9292

9393
-- MultiLine imports
9494
testCase "MultiLine Imports" $ check
@@ -106,6 +106,10 @@ testTree =
106106
-- General test
107107
testCase "Test General Code Block" $ check
108108
(mkCodeRange (Position 1 1) (Position 5 10) [] CodeKindRegion)
109-
(Just (FoldingRange 1 (Just 1) 5 (Just 10) (Just FoldingRangeRegion)))
109+
(Just (FoldingRange 1 (Just 1) 5 (Just 10) (Just FoldingRangeRegion))),
110+
-- General test
111+
testCase "Test Same Start Line" $ check
112+
(mkCodeRange (Position 1 1) (Position 1 10) [] CodeKindRegion)
113+
Nothing
110114
]
111115
]
Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,2 @@
11
((4, 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
2+
((5, 0) : (7, 21)) : FoldingRangeRegion

0 commit comments

Comments
 (0)