Skip to content
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

#2418 Also use .hlint.yaml fixity rules when HLINT_ON_LIB_GHC not defined #2464

Merged
merged 7 commits into from
Dec 11, 2021
Merged
4 changes: 4 additions & 0 deletions plugins/hls-hlint-plugin/hls-hlint-plugin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ library
, text
, transformers
, unordered-containers
-- can be removed if https://github.com/ndmitchell/hlint/pull/1325#issue-1077062712 is merged
-- and https://github.com/haskell/haskell-language-server/pull/2464#issue-1077133441 is updated
-- accordingly
, ghc-lib-parser-ex
eddiemundo marked this conversation as resolved.
Show resolved Hide resolved

if (flag(hlint33))
-- This mirrors the logic in hlint.cabal for hlint-3.3
Expand Down
20 changes: 18 additions & 2 deletions plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import Development.IDE.GHC.Compat.Core hiding
import Language.Haskell.GHC.ExactPrint.Delta (deltaOptions)
import Language.Haskell.GHC.ExactPrint.Parsers (postParseTransform)
import Language.Haskell.GHC.ExactPrint.Types (Rigidity (..))
import Language.Haskell.GhclibParserEx.Fixity as GhclibParserEx (applyFixities)
#endif

import Ide.Logger
Expand All @@ -104,7 +105,8 @@ import Language.LSP.Types hiding
import qualified Language.LSP.Types as LSP
import qualified Language.LSP.Types.Lens as LSP

import GHC.Generics (Generic)
import GHC.Generics (Associativity (LeftAssociative, NotAssociative, RightAssociative),
Generic)
import Text.Regex.TDFA.Text ()

import Development.IDE.GHC.Compat.Core (WarningFlag (Opt_WarnUnrecognisedPragmas),
Expand Down Expand Up @@ -251,9 +253,23 @@ getIdeas nfp = do
moduleEx _flags = do
mbpm <- getParsedModuleWithComments nfp
return $ createModule <$> mbpm
where createModule pm = Right (createModuleEx anns modu)
where
createModule pm = Right (createModuleEx anns (applyParseFlagsFixities modu))
where anns = pm_annotations pm
modu = pm_parsed_source pm

applyParseFlagsFixities :: ParsedSource -> ParsedSource
applyParseFlagsFixities modul = GhclibParserEx.applyFixities (parseFlagsToFixities _flags) modul

parseFlagsToFixities :: ParseFlags -> [(String, Fixity)]
parseFlagsToFixities = map toFixity . Hlint.fixities

toFixity :: FixityInfo -> (String, Fixity)
toFixity (name, dir, i) = (name, Fixity NoSourceText i $ f dir)
where
f LeftAssociative = InfixL
f RightAssociative = InfixR
f NotAssociative = InfixN
#else
moduleEx flags = do
mbpm <- getParsedModuleWithComments nfp
Expand Down
4 changes: 4 additions & 0 deletions plugins/hls-hlint-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ suggestionsTests =
contents <- skipManyTill anyMessage $ getDocumentEdit doc
liftIO $ contents @?= "main = undefined\nfoo = id\n"

, testCase ".hlint.yaml fixity rules are applied" $ runHlintSession "fixity" $ do
doc <- openDoc "FixityUse.hs" "haskell"
expectNoMoreDiagnostics 3 doc "hlint"

, testCase "changing document contents updates hlint diagnostics" $ runHlintSession "" $ do
doc <- openDoc "Base.hs" "haskell"
testHlintDiagnostics doc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- fixity: "infixl 3 <!>"
5 changes: 5 additions & 0 deletions plugins/hls-hlint-plugin/test/testdata/fixity/FixityDef.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module FixityDef where

infixl 3 <!>
(<!>) :: Maybe a -> Maybe (Maybe b) -> Maybe String
(<!>) a b = Nothing
6 changes: 6 additions & 0 deletions plugins/hls-hlint-plugin/test/testdata/fixity/FixityUse.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module FixityUse where

import FixityDef

foo :: Char -> Maybe Int -> Maybe String
foo c mInt = show <$> mInt <!> pure <$> Just c
6 changes: 6 additions & 0 deletions plugins/hls-hlint-plugin/test/testdata/fixity/hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cradle:
direct:
arguments:
- "FixityDef"
- "FixityUse"