Skip to content

Limit CodeActions within passed range #1442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,21 @@ codeActionProvider ideState plId (CodeActionParams _ _ docId _ context) = Right
where

getCodeActions = do
diags <- getDiagnostics ideState
allDiags <- getDiagnostics ideState
let docNfp = toNormalizedFilePath' <$> uriToFilePath' (docId ^. LSP.uri)
numHintsInDoc = length
[d | (nfp, _, d) <- diags
[d | (nfp, _, d) <- allDiags
, validCommand d
, Just nfp == docNfp
]
numHintsInContext = length
[d | d <- diags
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this also assuming that the diagnostics we in the context are exclusively hlint hints? I'm pretty sure that's not true: you could get other warnings or errors as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the impression that validCommand checks that, is it not the case?

, validCommand d
]
-- We only want to show the applyAll code action if there is more than 1
-- hint in the current document
if numHintsInDoc > 1 then do
-- hint in the current document and if code action range contains at
-- least one hint
if numHintsInDoc > 1 && numHintsInContext > 0 then do
pure $ applyAllAction:applyOneActions
else
pure applyOneActions
Expand Down
16 changes: 16 additions & 0 deletions test/functional/FunctionalCodeAction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ hlintTests = testGroup "hlint suggestions" [

, testCase "apply-refact preserve regular comments" $ runHlintSession "" $ do
testRefactor "ApplyRefact6.hs" "Redundant bracket" expectedComments

, testCase "applyAll is shown only when there is at least one diagnostic in range" $ runHlintSession "" $ do
doc <- openDoc "ApplyRefact8.hs" "haskell"
_ <- waitForDiagnosticsFromSource doc "hlint"

firstLine <- map fromAction <$> getCodeActions doc (mkRange 0 0 0 0)
secondLine <- map fromAction <$> getCodeActions doc (mkRange 1 0 1 0)
thirdLine <- map fromAction <$> getCodeActions doc (mkRange 2 0 2 0)
multiLine <- map fromAction <$> getCodeActions doc (mkRange 0 0 2 0)

let hasApplyAll = isJust . find (\ca -> "Apply all hints" `T.isSuffixOf` (ca ^. L.title))

liftIO $ hasApplyAll firstLine @? "Missing apply all code action"
liftIO $ hasApplyAll secondLine @? "Missing apply all code action"
liftIO $ not (hasApplyAll thirdLine) @? "Unexpected apply all code action"
liftIO $ hasApplyAll multiLine @? "Missing apply all code action"
]
where
runHlintSession :: FilePath -> Session a -> IO a
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/hlint/ApplyRefact8.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
f = (1)
g = (1)