Skip to content

Commit 6b9b288

Browse files
committed
Remove a bit of CPP by refactoring it into Compat layer
1 parent a1fd2ea commit 6b9b288

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

ghcide/src/Development/IDE/Core/Compile.hs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -821,23 +821,14 @@ parseHeader dflags filename contents = do
821821
case unP Compat.parseHeader (initParserState (initParserOpts dflags) contents loc) of
822822
#if MIN_VERSION_ghc(8,10,0)
823823
PFailed pst ->
824-
throwE $ diagFromErrMsgs "parser" dflags
825-
#if MIN_VERSION_ghc(9,2,0)
826-
$ fmap pprError
827-
#endif
828-
$ getErrorMessages pst
829-
#if !MIN_VERSION_ghc(9,2,0)
830-
dflags
831-
#endif
824+
throwE $ diagFromErrMsgs "parser" dflags $ getErrorMessages' pst dflags
832825
#else
833826
PFailed _ locErr msgErr ->
834827
throwE $ diagFromErrMsg "parser" dflags $ mkPlainErrMsg dflags locErr msgErr
835828
#endif
836829
POk pst rdr_module -> do
837-
let (warns, errs) = getMessages pst
838-
#if !MIN_VERSION_ghc(9,2,0)
839-
dflags
840-
#endif
830+
let (warns, errs) = getMessages' pst dflags
831+
841832
-- Just because we got a `POk`, it doesn't mean there
842833
-- weren't errors! To clarify, the GHC parser
843834
-- distinguishes between fatal and non-fatal
@@ -848,9 +839,9 @@ parseHeader dflags filename contents = do
848839
-- errors are those from which a parse tree just can't
849840
-- be produced.
850841
unless (null errs) $
851-
throwE $ diagFromErrMsgs "parser" dflags (fmap pprError errs)
842+
throwE $ diagFromErrMsgs "parser" dflags errs
852843

853-
let warnings = diagFromErrMsgs "parser" dflags (fmap pprWarning warns)
844+
let warnings = diagFromErrMsgs "parser" dflags warns
854845
return (warnings, rdr_module)
855846

856847
-- | Given a buffer, flags, and file path, produce a
@@ -870,28 +861,15 @@ parseFileContents env customPreprocessor filename ms = do
870861
#if MIN_VERSION_ghc(8,10,0)
871862
PFailed pst -> throwE
872863
$ diagFromErrMsgs "parser" dflags
873-
#if MIN_VERSION_ghc(9,2,0)
874-
$ fmap pprError
875-
#endif
876-
$ getErrorMessages pst
877-
#if !MIN_VERSION_ghc(9,2,0)
878-
$ dflags
879-
#endif
864+
$ getErrorMessages' pst dflags
880865
#else
881866
PFailed _ locErr msgErr ->
882867
throwE $ diagFromErrMsg "parser" dflags $ mkPlainErrMsg dflags locErr msgErr
883868
#endif
884869
POk pst rdr_module ->
885870
let
886871
hpm_annotations = mkApiAnns pst
887-
(warns, errs) = id
888-
#if MIN_VERSION_ghc(9,2,0)
889-
$ bimap (fmap pprWarning) (fmap pprError)
890-
#endif
891-
$ getMessages pst
892-
#if !MIN_VERSION_ghc(9,2,0)
893-
$ dflags
894-
#endif
872+
(warns, errs) = getMessages' pst dflags
895873
in
896874
do
897875
-- Just because we got a `POk`, it doesn't mean there

ghcide/src/Development/IDE/GHC/Compat.hs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ module Development.IDE.GHC.Compat(
1616
upNameCache,
1717
disableWarningsAsErrors,
1818
reLoc,
19+
getErrorMessages',
20+
getMessages',
1921

2022
#if !MIN_VERSION_ghc(9,0,1)
2123
RefMap,
@@ -117,6 +119,7 @@ import Data.IORef
117119

118120
import qualified Data.Map as Map
119121
import Data.List (foldl')
122+
import Data.Bifunctor
120123

121124
#if MIN_VERSION_ghc(9,0,0)
122125
import qualified Data.Set as S
@@ -134,6 +137,31 @@ hPutStringBuffer hdl (StringBuffer buf len cur)
134137
hPutBuf hdl ptr len
135138
#endif
136139

140+
#if MIN_VERSION_ghc(9,2,0)
141+
type ErrMsg = MsgEnvelope DecoratedSDoc
142+
type WarnMsg = MsgEnvelope DecoratedSDoc
143+
#endif
144+
145+
getErrorMessages' :: PState -> DynFlags -> Bag ErrMsg
146+
getErrorMessages' pst dflags =
147+
#if MIN_VERSION_ghc(9,2,0)
148+
fmap pprError $
149+
#endif
150+
getErrorMessages pst
151+
#if !MIN_VERSION_ghc(9,2,0)
152+
dflags
153+
#endif
154+
155+
getMessages' :: PState -> DynFlags -> (Bag WarnMsg, Bag ErrMsg)
156+
getMessages' pst dflags =
157+
#if MIN_VERSION_ghc(9,2,0)
158+
bimap (fmap pprWarning) (fmap pprError) $
159+
#endif
160+
getMessages pst
161+
#if !MIN_VERSION_ghc(9,2,0)
162+
dflags
163+
#endif
164+
137165
supportsHieFiles :: Bool
138166
supportsHieFiles = True
139167

0 commit comments

Comments
 (0)