Skip to content

Commit 07421fa

Browse files
committed
Add back-compat for GHC 9.0
1 parent f811c7f commit 07421fa

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

plugins/hls-refactor-plugin/src/Development/IDE/GHC/ExactPrint.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,17 @@ graftDeclsWithM dst toDecls = Graft $ \dflags a -> do
476476
modifyDeclsT (fmap DL.toList . go) a
477477

478478

479-
class (Data ast, Default l, Typeable l, Outputable l, Outputable ast) => ASTElement l ast | ast -> l where
479+
-- In 9.2+, we need `Default l` to do `setPrecedingLines` on annotated elements.
480+
-- In older versions, we pass around annotations explicitly, so the instance isn't needed.
481+
class
482+
( Data ast
483+
, Typeable l
484+
, Outputable l
485+
, Outputable ast
486+
#if MIN_VERSION_ghc(9,2,0)
487+
, Default l
488+
#endif
489+
) => ASTElement l ast | ast -> l where
480490
parseAST :: Parser (LocatedAn l ast)
481491
maybeParensAST :: LocatedAn l ast -> LocatedAn l ast
482492
{- | Construct a 'Graft', replacing the node at the given 'SrcSpan' with

plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
{-# LANGUAGE TypeApplications #-}
1616
{-# LANGUAGE TypeFamilies #-}
1717
{-# LANGUAGE ViewPatterns #-}
18+
{-# LANGUAGE PatternSynonyms #-}
1819
{-# LANGUAGE MultiParamTypeClasses #-}
1920
{-# LANGUAGE FlexibleInstances #-}
2021

@@ -51,15 +52,18 @@ import Development.IDE.GHC.Compat.ExactPrint
5152
import qualified Development.IDE.GHC.Compat.Util as Util
5253
import Development.IDE.GHC.ExactPrint
5354
import GHC.Exts
55+
import GHC.Types.SrcLoc (isRealSubspanOf)
56+
#if __GLASGOW_HASKELL__ >= 902
57+
import GHC.Parser.Annotation (SrcSpanAnn'(..))
58+
import qualified GHC.Types.Error as Error
59+
#endif
5460
import Ide.Plugin.Splice.Types
5561
import Ide.Types
5662
import Language.Haskell.GHC.ExactPrint (uniqueSrcSpanT)
5763
import Language.LSP.Server
5864
import Language.LSP.Types
5965
import Language.LSP.Types.Capabilities
6066
import qualified Language.LSP.Types.Lens as J
61-
import GHC.Hs (SrcSpanAnn'(..))
62-
import qualified GHC.Types.Error as Error
6367

6468
descriptor :: PluginId -> PluginDescriptor IdeState
6569
descriptor plId =
@@ -264,11 +268,23 @@ adjustToRange uri ran (WorkspaceEdit mhult mlt x) =
264268
J.range %~ \r ->
265269
if r == bad then ran else bad
266270

271+
-- Define a pattern to get hold of a `SrcSpan` from the location part of a
272+
-- `GenLocated`. In GHC >= 9.2 this will be a SrcSpanAnn', with annotations;
273+
-- earlier it will just be a plain `SrcSpan`.
274+
{-# COMPLETE AsSrcSpan #-}
275+
#if __GLASGOW_HASKELL__ >= 902
276+
pattern AsSrcSpan :: SrcSpan -> SrcSpanAnn' a
277+
pattern AsSrcSpan locA <- SrcSpanAnn {locA}
278+
#else
279+
pattern AsSrcSpan :: SrcSpan -> SrcSpan
280+
pattern AsSrcSpan loc <- loc
281+
#endif
282+
267283
findSubSpansDesc :: SrcSpan -> [(LHsExpr GhcTc, a)] -> [(SrcSpan, a)]
268284
findSubSpansDesc srcSpan =
269285
sortOn (Down . SubSpan . fst)
270286
. mapMaybe
271-
( \(L (SrcSpanAnn {locA = spn}) _, e) -> do
287+
( \(L (AsSrcSpan spn) _, e) -> do
272288
guard (spn `isSubspanOf` srcSpan)
273289
pure (spn, e)
274290
)
@@ -354,11 +370,11 @@ manualCalcEdit clientCapabilities reportEditor ran ps hscEnv typechkd srcSpan _e
354370
Right y -> unRenamedE dflags y
355371
_ -> pure Nothing
356372
let (warns, errs) =
357-
#if __GLASGOW_HASKELL__ >= 902
373+
#if __GLASGOW_HASKELL__ >= 902
358374
(Error.getWarningMessages msgs, Error.getErrorMessages msgs)
359-
#else
375+
#else
360376
msgs
361-
#endif
377+
#endif
362378
pure $ (warns,) <$> fromMaybe (Left $ show errs) eresl
363379

364380
unless
@@ -382,9 +398,15 @@ unRenamedE ::
382398
TransformT m (LocatedAn l (ast GhcPs))
383399
unRenamedE dflags expr = do
384400
uniq <- show <$> uniqueSrcSpanT
385-
either (fail . show) pure $
401+
#if __GLASGOW_HASKELL__ >= 902
402+
expr' <-
403+
#else
404+
(_anns, expr') <-
405+
#endif
406+
either (fail . show) pure $
386407
parseAST @_ @(ast GhcPs) dflags uniq $
387408
showSDoc dflags $ ppr expr
409+
pure expr'
388410

389411
data SearchResult r =
390412
Continue | Stop | Here r
@@ -423,13 +445,13 @@ codeAction state plId (CodeActionParams _ _ docId ran _) = liftIO $
423445
GenericQ (SearchResult (RealSrcSpan, SpliceContext))
424446
detectSplice spn =
425447
let
426-
spanIsRelevant x = RealSrcSpan spn Nothing `isSubspanOf` x
448+
realSpanIsRelevant x = spn `isRealSubspanOf` x
427449
in
428450
mkQ
429451
Continue
430452
( \case
431-
(L (SrcSpanAnn {locA = l@(RealSrcSpan spLoc _)}) expr :: LHsExpr GhcPs)
432-
| spanIsRelevant l ->
453+
(L (AsSrcSpan (RealSrcSpan spLoc _)) expr :: LHsExpr GhcPs)
454+
| realSpanIsRelevant spLoc ->
433455
case expr of
434456
HsSpliceE {} -> Here (spLoc, Expr)
435457
_ -> Continue
@@ -439,23 +461,23 @@ codeAction state plId (CodeActionParams _ _ docId ran _) = liftIO $
439461
#if __GLASGOW_HASKELL__ == 808
440462
(dL @(Pat GhcPs) -> L l@(RealSrcSpan spLoc _) pat :: Located (Pat GhcPs))
441463
#else
442-
(L (SrcSpanAnn {locA = l@(RealSrcSpan spLoc _)}) pat :: LPat GhcPs)
464+
(L (AsSrcSpan (RealSrcSpan spLoc _)) pat :: LPat GhcPs)
443465
#endif
444-
| spanIsRelevant l ->
466+
| realSpanIsRelevant spLoc ->
445467
case pat of
446468
SplicePat{} -> Here (spLoc, Pat)
447469
_ -> Continue
448470
_ -> Stop
449471
`extQ` \case
450-
(L (SrcSpanAnn {locA = l@(RealSrcSpan spLoc _)}) ty :: LHsType GhcPs)
451-
| spanIsRelevant l ->
472+
(L (AsSrcSpan (RealSrcSpan spLoc _)) ty :: LHsType GhcPs)
473+
| realSpanIsRelevant spLoc ->
452474
case ty of
453475
HsSpliceTy {} -> Here (spLoc, HsType)
454476
_ -> Continue
455477
_ -> Stop
456478
`extQ` \case
457-
(L (SrcSpanAnn {locA = l@(RealSrcSpan spLoc _)}) decl :: LHsDecl GhcPs)
458-
| spanIsRelevant l ->
479+
(L (AsSrcSpan (RealSrcSpan spLoc _)) decl :: LHsDecl GhcPs)
480+
| realSpanIsRelevant spLoc ->
459481
case decl of
460482
SpliceD {} -> Here (spLoc, HsDecl)
461483
_ -> Continue

0 commit comments

Comments
 (0)