Skip to content

Commit edc7160

Browse files
committed
Restore eval plugin build for GHC 9.2
The eval plugin is building again and working partially. The codelens (and evaluation) only happen for code in module comment, code appearing in the top level comments / haddock are ignored. I need to walk the AST to locate them.
1 parent 9cd1fdd commit edc7160

File tree

5 files changed

+75
-21
lines changed

5 files changed

+75
-21
lines changed

cabal-ghc921.project

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ constraints:
4747
-alternateNumberFormat
4848
-brittany
4949
-class
50-
-eval
5150
-haddockComments
5251
-hlint
5352
-retrie

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ import System.IO.Extra (fixIO, newTempFileWithin)
9595

9696
-- GHC API imports
9797
-- GHC API imports
98+
#if MIN_VERSION_ghc(9,2,0)
99+
import GHC (Anchor (anchor),
100+
EpaComment (EpaComment),
101+
EpaCommentTok (EpaBlockComment, EpaLineComment),
102+
epAnnComments,
103+
priorComments)
104+
import GHC.Hs (LEpaComment)
105+
#endif
98106
import GHC (GetDocsFailure (..),
99107
mgModSummaries,
100108
parsedSource)
@@ -873,7 +881,12 @@ parseFileContents env customPreprocessor filename ms = do
873881
PFailedWithErrorMessages msgs -> throwE $ diagFromErrMsgs "parser" dflags $ msgs dflags
874882
POk pst rdr_module ->
875883
let
884+
#if MIN_VERSION_ghc(9,2,1)
885+
-- TODO: we need to export the annotations here
886+
hpm_annotations = ()
887+
#else
876888
hpm_annotations = mkApiAnns pst
889+
#endif
877890
(warns, errs) = getMessages' pst dflags
878891
in
879892
do

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,18 @@ import qualified GHC.Parser.Annotation as Anno
5151
import qualified GHC.Parser.Lexer as Lexer
5252
import GHC.Types.SrcLoc (PsSpan (..))
5353
#if MIN_VERSION_ghc(9,2,0)
54-
import GHC (pm_extra_src_files,
54+
import GHC (Anchor (anchor),
55+
EpAnnComments (priorComments),
56+
EpaComment (EpaComment),
57+
EpaCommentTok (..),
58+
epAnnComments,
59+
pm_extra_src_files,
5560
pm_mod_summary,
5661
pm_parsed_source)
5762
import qualified GHC
5863
import qualified GHC.Driver.Config as Config
59-
import GHC.Hs (hpm_module, hpm_src_files)
64+
import GHC.Hs (LEpaComment, hpm_module,
65+
hpm_src_files)
6066
import GHC.Parser.Lexer hiding (initParserState)
6167
#endif
6268
#else
@@ -100,6 +106,8 @@ initParserState =
100106
#endif
101107

102108
#if MIN_VERSION_ghc(9,2,0)
109+
-- GHC 9.2 does not have ApiAnns anymore packaged in ParsedModule. Now the
110+
-- annotations are found in the ast.
103111
type ApiAnns = ()
104112
#else
105113
type ApiAnns = Anno.ApiAnns

plugins/hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,20 @@ import GHC (ClsInst,
7272
getInteractiveDynFlags,
7373
isImport, isStmt, load,
7474
parseName, pprFamInst,
75-
pprInstance, setLogAction,
76-
setTargets, typeKind)
75+
pprInstance, setTargets,
76+
typeKind)
77+
#if MIN_VERSION_ghc(9,2,0)
78+
import GHC (Fixity, pushLogHookM)
79+
#else
80+
import GHC (setLogAction)
81+
#endif
7782
import qualified GHC.LanguageExtensions.Type as LangExt (Extension (..))
7883

7984
import Development.IDE.Core.FileStore (setSomethingModified)
8085
import Development.IDE.Types.Shake (toKey)
86+
#if MIN_VERSION_ghc(9,2,0)
87+
import GHC.Types.SrcLoc (UnhelpfulSpanReason (UnhelpfulInteractive))
88+
#endif
8189
import Ide.Plugin.Eval.Code (Statement, asStatements,
8290
evalSetup, myExecStmt,
8391
propSetup, resultRange,
@@ -103,7 +111,8 @@ import System.FilePath (takeFileName)
103111
import System.IO (hClose)
104112
import UnliftIO.Temporary (withSystemTempFile)
105113

106-
#if MIN_VERSION_ghc(9,0,0)
114+
#if MIN_VERSION_ghc(9,2,0)
115+
#elif MIN_VERSION_ghc(9,0,0)
107116
import GHC.Driver.Session (unitDatabases, unitState)
108117
import GHC.Types.SrcLoc (UnhelpfulSpanReason (UnhelpfulInteractive))
109118
#else
@@ -244,13 +253,8 @@ runEvalCmd st EvalParams{..} =
244253
$ idflags
245254
setInteractiveDynFlags $ df'
246255
#if MIN_VERSION_ghc(9,0,0)
247-
{ unitState =
248-
unitState
249-
df
250-
, unitDatabases =
251-
unitDatabases
252-
df
253-
, packageFlags =
256+
{
257+
packageFlags =
254258
packageFlags
255259
df
256260
, useColor = Never
@@ -272,7 +276,11 @@ runEvalCmd st EvalParams{..} =
272276
#endif
273277

274278
-- set up a custom log action
275-
#if MIN_VERSION_ghc(9,0,0)
279+
#if MIN_VERSION_ghc(9,2,0)
280+
pushLogHookM . const $ \_df _wr _sev _span _doc ->
281+
defaultLogActionHPutStrDoc _df True logHandle _doc
282+
-- TODO: check the True
283+
#elif MIN_VERSION_ghc(9,0,0)
276284
setLogAction $ \_df _wr _sev _span _doc ->
277285
defaultLogActionHPutStrDoc _df logHandle _doc
278286
#else
@@ -683,7 +691,9 @@ doTypeCmd dflags arg = do
683691

684692
parseExprMode :: Text -> (TcRnExprMode, T.Text)
685693
parseExprMode rawArg = case T.break isSpace rawArg of
694+
#if !MIN_VERSION_ghc(9,0,0)
686695
("+v", rest) -> (TM_NoInst, T.strip rest)
696+
#endif
687697
("+d", rest) -> (TM_Default, T.strip rest)
688698
_ -> (TM_Inst, rawArg)
689699

plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Rules.hs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ import Development.IDE.GHC.Compat
3636
import qualified Development.IDE.GHC.Compat as SrcLoc
3737
import qualified Development.IDE.GHC.Compat.Util as FastString
3838
import Development.IDE.Graph (alwaysRerun)
39+
#if MIN_VERSION_ghc(9,2,0)
40+
import GHC.Parser.Annotation
41+
#endif
3942
import Ide.Plugin.Eval.Types
4043

4144

@@ -53,22 +56,34 @@ queueForEvaluation ide nfp = do
5356
EvaluatingVar var <- getIdeGlobalState ide
5457
modifyIORef var (Set.insert nfp)
5558

56-
#if MIN_VERSION_ghc(9,0,0)
59+
#if MIN_VERSION_ghc(9,2,0)
60+
getAnnotations :: Development.IDE.GHC.Compat.Located HsModule -> [LEpaComment]
61+
getAnnotations (L _ (HsModule { hsmodAnn = anns'})) = priorComments $ epAnnComments anns'
62+
63+
apiAnnComments' :: ParsedModule -> [SrcLoc.RealLocated EpaCommentTok]
64+
apiAnnComments' pm = do
65+
L span' (EpaComment c span) <- getAnnotations $ pm_parsed_source pm
66+
pure (L (anchor span') c)
67+
68+
pattern RealSrcSpanAlready :: SrcLoc.RealSrcSpan -> SrcLoc.RealSrcSpan
69+
pattern RealSrcSpanAlready x = x
70+
#elif MIN_VERSION_ghc(9,0,0)
71+
apiAnnComments' :: ParsedModule -> [SrcLoc.RealLocated AnnotationComment]
72+
apiAnnComments' = apiAnnRogueComments . pm_annotations
73+
5774
pattern RealSrcSpanAlready :: SrcLoc.RealSrcSpan -> SrcLoc.RealSrcSpan
5875
pattern RealSrcSpanAlready x = x
59-
apiAnnComments' :: SrcLoc.ApiAnns -> [SrcLoc.RealLocated AnnotationComment]
60-
apiAnnComments' = apiAnnRogueComments
6176
#else
62-
apiAnnComments' :: SrcLoc.ApiAnns -> [SrcLoc.Located AnnotationComment]
63-
apiAnnComments' = concat . Map.elems . snd
77+
apiAnnComments' :: ParsedModule -> [SrcLoc.Located AnnotationComment]
78+
apiAnnComments' = concat . Map.elems . snd . pm_annotations
6479

6580
pattern RealSrcSpanAlready :: SrcLoc.RealSrcSpan -> SrcSpan
6681
pattern RealSrcSpanAlready x = SrcLoc.RealSrcSpan x Nothing
6782
#endif
6883

6984
evalParsedModuleRule :: Rules ()
7085
evalParsedModuleRule = defineEarlyCutoff $ RuleNoDiagnostics $ \GetEvalComments nfp -> do
71-
(ParsedModule{..}, posMap) <- useWithStale_ GetParsedModuleWithComments nfp
86+
(pm, posMap) <- useWithStale_ GetParsedModuleWithComments nfp
7287
let comments = foldMap (\case
7388
L (RealSrcSpanAlready real) bdy
7489
| FastString.unpackFS (srcSpanFile real) ==
@@ -79,15 +94,24 @@ evalParsedModuleRule = defineEarlyCutoff $ RuleNoDiagnostics $ \GetEvalComments
7994

8095
-- since Haddock parsing is unset explicitly in 'getParsedModuleWithComments',
8196
-- we can concentrate on these two
97+
#if MIN_VERSION_ghc(9,2,0)
98+
case bdy of
99+
EpaLineComment cmt ->
100+
mempty { lineComments = Map.singleton curRan (RawLineComment cmt) }
101+
EpaBlockComment cmt ->
102+
mempty { blockComments = Map.singleton curRan $ RawBlockComment cmt }
103+
_ -> mempty
104+
#else
82105
case bdy of
83106
AnnLineComment cmt ->
84107
mempty { lineComments = Map.singleton curRan (RawLineComment cmt) }
85108
AnnBlockComment cmt ->
86109
mempty { blockComments = Map.singleton curRan $ RawBlockComment cmt }
87110
_ -> mempty
111+
#endif
88112
_ -> mempty
89113
)
90-
$ apiAnnComments' pm_annotations
114+
$ apiAnnComments' pm
91115
-- we only care about whether the comments are null
92116
-- this is valid because the only dependent is NeedsCompilation
93117
fingerPrint = fromString $ if nullComments comments then "" else "1"

0 commit comments

Comments
 (0)