Skip to content

Commit f128085

Browse files
eddiemundojneira
andauthored
#2418 Also use .hlint.yaml fixity rules when HLINT_ON_LIB_GHC not defined (#2464)
* fix #2418 until upstream hlint change * remove debug import * add comment about when ghc-lib-parser-ex dependency can be removed Co-authored-by: Javier Neira <atreyu.bbb@gmail.com>
1 parent 89c44bf commit f128085

File tree

7 files changed

+44
-2
lines changed

7 files changed

+44
-2
lines changed

plugins/hls-hlint-plugin/hls-hlint-plugin.cabal

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ library
6868
, text
6969
, transformers
7070
, unordered-containers
71+
-- can be removed if https://github.com/ndmitchell/hlint/pull/1325#issue-1077062712 is merged
72+
-- and https://github.com/haskell/haskell-language-server/pull/2464#issue-1077133441 is updated
73+
-- accordingly
74+
, ghc-lib-parser-ex
7175

7276
if (flag(hlint33))
7377
-- This mirrors the logic in hlint.cabal for hlint-3.3

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import Development.IDE.GHC.Compat.Core hiding
8484
import Language.Haskell.GHC.ExactPrint.Delta (deltaOptions)
8585
import Language.Haskell.GHC.ExactPrint.Parsers (postParseTransform)
8686
import Language.Haskell.GHC.ExactPrint.Types (Rigidity (..))
87+
import Language.Haskell.GhclibParserEx.Fixity as GhclibParserEx (applyFixities)
8788
#endif
8889

8990
import Ide.Logger
@@ -104,7 +105,8 @@ import Language.LSP.Types hiding
104105
import qualified Language.LSP.Types as LSP
105106
import qualified Language.LSP.Types.Lens as LSP
106107

107-
import GHC.Generics (Generic)
108+
import GHC.Generics (Associativity (LeftAssociative, NotAssociative, RightAssociative),
109+
Generic)
108110
import Text.Regex.TDFA.Text ()
109111

110112
import Development.IDE.GHC.Compat.Core (WarningFlag (Opt_WarnUnrecognisedPragmas),
@@ -251,9 +253,23 @@ getIdeas nfp = do
251253
moduleEx _flags = do
252254
mbpm <- getParsedModuleWithComments nfp
253255
return $ createModule <$> mbpm
254-
where createModule pm = Right (createModuleEx anns modu)
256+
where
257+
createModule pm = Right (createModuleEx anns (applyParseFlagsFixities modu))
255258
where anns = pm_annotations pm
256259
modu = pm_parsed_source pm
260+
261+
applyParseFlagsFixities :: ParsedSource -> ParsedSource
262+
applyParseFlagsFixities modul = GhclibParserEx.applyFixities (parseFlagsToFixities _flags) modul
263+
264+
parseFlagsToFixities :: ParseFlags -> [(String, Fixity)]
265+
parseFlagsToFixities = map toFixity . Hlint.fixities
266+
267+
toFixity :: FixityInfo -> (String, Fixity)
268+
toFixity (name, dir, i) = (name, Fixity NoSourceText i $ f dir)
269+
where
270+
f LeftAssociative = InfixL
271+
f RightAssociative = InfixR
272+
f NotAssociative = InfixN
257273
#else
258274
moduleEx flags = do
259275
mbpm <- getParsedModuleWithComments nfp

plugins/hls-hlint-plugin/test/Main.hs

+4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ suggestionsTests =
9999
contents <- skipManyTill anyMessage $ getDocumentEdit doc
100100
liftIO $ contents @?= "main = undefined\nfoo = id\n"
101101

102+
, testCase ".hlint.yaml fixity rules are applied" $ runHlintSession "fixity" $ do
103+
doc <- openDoc "FixityUse.hs" "haskell"
104+
expectNoMoreDiagnostics 3 doc "hlint"
105+
102106
, testCase "changing document contents updates hlint diagnostics" $ runHlintSession "" $ do
103107
doc <- openDoc "Base.hs" "haskell"
104108
testHlintDiagnostics doc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- fixity: "infixl 3 <!>"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module FixityDef where
2+
3+
infixl 3 <!>
4+
(<!>) :: Maybe a -> Maybe (Maybe b) -> Maybe String
5+
(<!>) a b = Nothing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module FixityUse where
2+
3+
import FixityDef
4+
5+
foo :: Char -> Maybe Int -> Maybe String
6+
foo c mInt = show <$> mInt <!> pure <$> Just c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cradle:
2+
direct:
3+
arguments:
4+
- "FixityDef"
5+
- "FixityUse"
6+

0 commit comments

Comments
 (0)