Skip to content

Commit 959a53b

Browse files
committed
added more descriptive comments and encorporate code review suggestions
1 parent c46a7f4 commit 959a53b

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

docs/features.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ Contributions welcome!
370370
| Signature help | Unimplemented | `textDocument/signatureHelp` |
371371
| Jump to declaration | Unclear if useful | `textDocument/declaration` |
372372
| Jump to implementation | Unclear if useful | `textDocument/implementation` |
373-
| Folding | Unimplemented | `textDocument/foldingRange` |
374373
| Semantic tokens | Unimplemented | `textDocument/semanticTokens` |
375374
| Linked editing | Unimplemented | `textDocument/linkedEditingRange` |
376375
| Document links | Unimplemented | `textDocument/documentLink` |

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

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ getFoldingRanges :: NormalizedFilePath -> ExceptT String IdeAction [FoldingRange
8888
getFoldingRanges file = do
8989
(codeRange, _) <- maybeToExceptT "fail to get code range" $ useE GetCodeRange file
9090

91-
let foldingRanges = findFoldingRanges codeRange
92-
93-
maybeToExceptT "Fail to generate folding range" (MaybeT . pure $ Just foldingRanges)
91+
pure $ findFoldingRanges codeRange
9492

9593
selectionRangeHandler :: IdeState -> PluginId -> SelectionRangeParams -> LspM c (Either ResponseError (List SelectionRange))
9694
selectionRangeHandler ide _ SelectionRangeParams{..} = do
@@ -150,32 +148,59 @@ findPosition pos root = go Nothing root
150148
startOfRight <- _start . _codeRange_range <$> V.headM right
151149
if pos < startOfRight then binarySearchPos left else binarySearchPos right
152150

153-
-- | Traverses through the code range and children to a folding ranges
151+
-- | Traverses through the code range and it children to a folding ranges.
152+
--
153+
-- It starts with the root node, converts that into a folding range then moves towards the children.
154+
-- It converts each child of each root node and parses it to folding range and moves to its children.
154155
findFoldingRanges :: CodeRange -> [FoldingRange]
155156
findFoldingRanges r@(CodeRange _ children _) =
156-
let frRoot :: [FoldingRange] = case createFoldingRange r of
157-
Just x -> [x]
157+
let frChildren :: [FoldingRange] = concat $ V.toList $ fmap findFoldingRanges children
158+
in case createFoldingRange r of
159+
Just x -> x:frChildren
158160
Nothing -> []
159161

160-
frChildren :: [FoldingRange] = concat $ V.toList $ fmap findFoldingRanges children
161-
in frRoot ++ frChildren
162-
163162
-- | Parses code range to folding range
164163
createFoldingRange :: CodeRange -> Maybe FoldingRange
165164
createFoldingRange (CodeRange (Range (Position lineStart charStart) (Position lineEnd charEnd)) _ ck) = do
165+
-- Type conversion of codeRangeKind to FoldingRangeKind
166166
let frk = crkToFrk ck
167+
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.
167172
if lineStart == lineEnd
168173
then Nothing
169174
else case frk of
170175
Just _ -> Just (FoldingRange lineStart (Just charStart) lineEnd (Just charEnd) frk)
171176
Nothing -> Nothing
172177

173-
-- Removes all small foldings that start from the same line
178+
-- | Removes all small foldings that start from the same line.
179+
--
180+
-- As we are converting nodes of the ast into folding ranges, there are multiple nodes starting from a single line.
181+
-- A single line of code doesn't mean a single node in AST, so this function removes all the nodes that have a duplicate
182+
-- start line, ie. they start from the same line.
183+
--
184+
-- This function preserves the largest folding range from the ranges that start from the same line.
185+
--
186+
-- Eg. A multi-line function that also has a multi-line if statement starting from the same line should have the folding
187+
-- according to the function.
188+
--
189+
-- This is done by breaking the [FoldingRange] into parts -->
190+
-- frx: Head
191+
-- xs: rest of the array
192+
-- fry(not shown as it is not used): head of xs
193+
-- xs2: rest of the array other than the first two elements
194+
-- slx and sly: start line of frx and fry
195+
--
196+
-- We compare the start line of the first two elements in the array and if the start line is the same we remove the
197+
-- second one as it is the smaller one amoung the two.
198+
-- otherwise frx is returned and the function runs recursively on xs.
174199
removeDupStartLineFoldings :: [FoldingRange] -> [FoldingRange]
175200
removeDupStartLineFoldings [] = []
176201
removeDupStartLineFoldings [x] = [x]
177-
removeDupStartLineFoldings (frx@(FoldingRange x _ _ _ _):xs@((FoldingRange y _ _ _ _):xs2))
178-
| x == y = removeDupStartLineFoldings (frx:xs2)
202+
removeDupStartLineFoldings (frx@(FoldingRange slx _ _ _ _):xs@((FoldingRange sly _ _ _ _):xs2))
203+
| slx == sly = removeDupStartLineFoldings (frx:xs2)
179204
| otherwise = frx : removeDupStartLineFoldings xs
180205

181206
-- | Likes 'toCurrentPosition', but works on 'SelectionRange'

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ testTree =
8585
FoldingRange 1 (Just 2) 3 (Just 6) (Just FoldingRangeRegion),
8686
FoldingRange 3 (Just 7) 5 (Just 10) (Just FoldingRangeRegion)],
8787

88-
-- Single line
88+
-- Single line returns [] because single line ranges need not be folded
8989
testCase "Test Single Line" $ check
9090
(mkCodeRange (Position 1 0) (Position 1 15) [] CodeKindRegion)
9191
[],
@@ -103,11 +103,11 @@ testTree =
103103
mkCodeRange :: Position -> Position -> V.Vector CodeRange -> CodeRangeKind -> CodeRange
104104
mkCodeRange start end children crk = CodeRange (Range start end) children crk
105105
in [
106-
-- General test
106+
-- General tests
107107
testCase "Test General Code Block" $ check
108108
(mkCodeRange (Position 1 1) (Position 5 10) [] CodeKindRegion)
109109
(Just (FoldingRange 1 (Just 1) 5 (Just 10) (Just FoldingRangeRegion))),
110-
-- General test
110+
-- If a range has the same start and end line it need not be folded so Nothing is expected
111111
testCase "Test Same Start Line" $ check
112112
(mkCodeRange (Position 1 1) (Position 1 10) [] CodeKindRegion)
113113
Nothing

0 commit comments

Comments
 (0)