Skip to content

Commit cadeb78

Browse files
committed
Limit CodeActions within passed range
Return only actions within range specified by client.
1 parent 17fce67 commit cadeb78

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,21 @@ codeActionProvider ideState plId (CodeActionParams _ _ docId _ context) = Right
286286
where
287287

288288
getCodeActions = do
289-
diags <- getDiagnostics ideState
289+
allDiags <- getDiagnostics ideState
290290
let docNfp = toNormalizedFilePath' <$> uriToFilePath' (docId ^. LSP.uri)
291291
numHintsInDoc = length
292-
[d | (nfp, _, d) <- diags
292+
[d | (nfp, _, d) <- allDiags
293293
, validCommand d
294294
, Just nfp == docNfp
295295
]
296+
numHintsInContext = length
297+
[d | d <- diags
298+
, validCommand d
299+
]
296300
-- We only want to show the applyAll code action if there is more than 1
297-
-- hint in the current document
298-
if numHintsInDoc > 1 then do
301+
-- hint in the current document and if code action range contains at
302+
-- least one hint
303+
if numHintsInDoc > 1 && numHintsInContext > 0 then do
299304
pure $ applyAllAction:applyOneActions
300305
else
301306
pure applyOneActions

test/functional/FunctionalCodeAction.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@ hlintTests = testGroup "hlint suggestions" [
163163

164164
, testCase "apply-refact preserve regular comments" $ runHlintSession "" $ do
165165
testRefactor "ApplyRefact6.hs" "Redundant bracket" expectedComments
166+
167+
, testCase "applyAll is shown only when there is at least one diagnostic in range" $ runHlintSession "" $ do
168+
doc <- openDoc "ApplyRefact8.hs" "haskell"
169+
_ <- waitForDiagnosticsFromSource doc "hlint"
170+
171+
firstLine <- map fromAction <$> getCodeActions doc (mkRange 0 0 0 0)
172+
secondLine <- map fromAction <$> getCodeActions doc (mkRange 1 0 1 0)
173+
thirdLine <- map fromAction <$> getCodeActions doc (mkRange 2 0 2 0)
174+
multiLine <- map fromAction <$> getCodeActions doc (mkRange 0 0 2 0)
175+
176+
let hasApplyAll = isJust . find (\ca -> "Apply all hints" `T.isSuffixOf` (ca ^. L.title))
177+
178+
liftIO $ hasApplyAll firstLine @? "Missing apply all code action"
179+
liftIO $ hasApplyAll secondLine @? "Missing apply all code action"
180+
liftIO $ not (hasApplyAll thirdLine) @? "Unexpected apply all code action"
181+
liftIO $ hasApplyAll multiLine @? "Missing apply all code action"
166182
]
167183
where
168184
runHlintSession :: FilePath -> Session a -> IO a

test/testdata/hlint/ApplyRefact8.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
f = (1)
2+
g = (1)
3+

0 commit comments

Comments
 (0)