diff --git a/plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs b/plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs index 40f4a272fb..7629ef5445 100644 --- a/plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs +++ b/plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs @@ -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 + , 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 diff --git a/test/functional/FunctionalCodeAction.hs b/test/functional/FunctionalCodeAction.hs index 1b33a5c571..80d7201905 100644 --- a/test/functional/FunctionalCodeAction.hs +++ b/test/functional/FunctionalCodeAction.hs @@ -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 diff --git a/test/testdata/hlint/ApplyRefact8.hs b/test/testdata/hlint/ApplyRefact8.hs new file mode 100644 index 0000000000..3ea5093476 --- /dev/null +++ b/test/testdata/hlint/ApplyRefact8.hs @@ -0,0 +1,3 @@ +f = (1) +g = (1) +