Skip to content

Commit 892a129

Browse files
committed
fix: find folding ranges function
1 parent a032c78 commit 892a129

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE OverloadedStrings #-}
22
{-# LANGUAGE RecordWildCards #-}
3+
{-# LANGUAGE ScopedTypeVariables #-}
34

45
module Ide.Plugin.CodeRange (
56
descriptor
@@ -86,7 +87,7 @@ getFoldingRanges :: NormalizedFilePath -> ExceptT String IdeAction [FoldingRange
8687
getFoldingRanges file = do
8788
(codeRange, _) <- maybeToExceptT "fail to get code range" $ useE GetCodeRange file
8889

89-
let foldingRanges = catMaybes $ findFoldingRanges codeRange
90+
let foldingRanges = findFoldingRanges codeRange
9091

9192
maybeToExceptT "Fail to generate folding range" (MaybeT . pure $ Just foldingRanges)
9293

@@ -148,24 +149,14 @@ findPosition pos root = go Nothing root
148149
startOfRight <- _start . _codeRange_range <$> V.headM right
149150
if pos < startOfRight then binarySearchPos left else binarySearchPos right
150151

151-
findFoldingRanges :: CodeRange -> [Maybe FoldingRange]
152-
findFoldingRanges root = do
153-
let acc = []
154-
let node = _codeRange_children root
155-
binarySearchFoldingRange node acc
156-
157-
binarySearchFoldingRange :: Vector CodeRange -> [Maybe FoldingRange] -> [Maybe FoldingRange]
158-
binarySearchFoldingRange v acc
159-
| V.null v = []
160-
| V.length v == 1 = do
161-
headOfCodeRange <- V.headM v
162-
let foldingRangesOfRange = createFoldingRange headOfCodeRange
163-
let acc' = acc ++ [foldingRangesOfRange]
164-
acc'
165-
| otherwise = do
166-
let (left, right) = V.splitAt (V.length v `div` 2) v
167-
_ <- binarySearchFoldingRange left acc
168-
binarySearchFoldingRange right acc
152+
findFoldingRanges :: CodeRange -> [FoldingRange]
153+
findFoldingRanges r@(CodeRange _ children _) =
154+
let frRoot :: [FoldingRange] = case createFoldingRange r of
155+
Just x -> [x]
156+
Nothing -> []
157+
158+
frChildren :: [FoldingRange] = concat $ V.toList $ fmap findFoldingRanges children
159+
in frRoot ++ frChildren
169160

170161
createFoldingRange :: CodeRange -> Maybe FoldingRange
171162
createFoldingRange node1 = do

0 commit comments

Comments
 (0)