15
15
{-# LANGUAGE TypeApplications #-}
16
16
{-# LANGUAGE TypeFamilies #-}
17
17
{-# LANGUAGE ViewPatterns #-}
18
+ {-# LANGUAGE PatternSynonyms #-}
18
19
{-# LANGUAGE MultiParamTypeClasses #-}
19
20
{-# LANGUAGE FlexibleInstances #-}
20
21
@@ -51,15 +52,18 @@ import Development.IDE.GHC.Compat.ExactPrint
51
52
import qualified Development.IDE.GHC.Compat.Util as Util
52
53
import Development.IDE.GHC.ExactPrint
53
54
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
54
60
import Ide.Plugin.Splice.Types
55
61
import Ide.Types
56
62
import Language.Haskell.GHC.ExactPrint (uniqueSrcSpanT )
57
63
import Language.LSP.Server
58
64
import Language.LSP.Types
59
65
import Language.LSP.Types.Capabilities
60
66
import qualified Language.LSP.Types.Lens as J
61
- import GHC.Hs (SrcSpanAnn' (.. ))
62
- import qualified GHC.Types.Error as Error
63
67
64
68
descriptor :: PluginId -> PluginDescriptor IdeState
65
69
descriptor plId =
@@ -264,11 +268,23 @@ adjustToRange uri ran (WorkspaceEdit mhult mlt x) =
264
268
J. range %~ \ r ->
265
269
if r == bad then ran else bad
266
270
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
+
267
283
findSubSpansDesc :: SrcSpan -> [(LHsExpr GhcTc , a )] -> [(SrcSpan , a )]
268
284
findSubSpansDesc srcSpan =
269
285
sortOn (Down . SubSpan . fst )
270
286
. mapMaybe
271
- ( \ (L (SrcSpanAnn {locA = spn} ) _, e) -> do
287
+ ( \ (L (AsSrcSpan spn) _, e) -> do
272
288
guard (spn `isSubspanOf` srcSpan)
273
289
pure (spn, e)
274
290
)
@@ -354,11 +370,11 @@ manualCalcEdit clientCapabilities reportEditor ran ps hscEnv typechkd srcSpan _e
354
370
Right y -> unRenamedE dflags y
355
371
_ -> pure Nothing
356
372
let (warns, errs) =
357
- # if __GLASGOW_HASKELL__ >= 902
373
+ #if __GLASGOW_HASKELL__ >= 902
358
374
(Error. getWarningMessages msgs, Error. getErrorMessages msgs)
359
- # else
375
+ #else
360
376
msgs
361
- # endif
377
+ #endif
362
378
pure $ (warns,) <$> fromMaybe (Left $ show errs) eresl
363
379
364
380
unless
@@ -382,9 +398,15 @@ unRenamedE ::
382
398
TransformT m (LocatedAn l (ast GhcPs ))
383
399
unRenamedE dflags expr = do
384
400
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 $
386
407
parseAST @ _ @ (ast GhcPs ) dflags uniq $
387
408
showSDoc dflags $ ppr expr
409
+ pure expr'
388
410
389
411
data SearchResult r =
390
412
Continue | Stop | Here r
@@ -423,13 +445,13 @@ codeAction state plId (CodeActionParams _ _ docId ran _) = liftIO $
423
445
GenericQ (SearchResult (RealSrcSpan , SpliceContext ))
424
446
detectSplice spn =
425
447
let
426
- spanIsRelevant x = RealSrcSpan spn Nothing `isSubspanOf ` x
448
+ realSpanIsRelevant x = spn `isRealSubspanOf ` x
427
449
in
428
450
mkQ
429
451
Continue
430
452
( \ 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 ->
433
455
case expr of
434
456
HsSpliceE {} -> Here (spLoc, Expr )
435
457
_ -> Continue
@@ -439,23 +461,23 @@ codeAction state plId (CodeActionParams _ _ docId ran _) = liftIO $
439
461
#if __GLASGOW_HASKELL__ == 808
440
462
(dL @ (Pat GhcPs ) -> L l@ (RealSrcSpan spLoc _) pat :: Located (Pat GhcPs ))
441
463
#else
442
- (L (SrcSpanAnn {locA = l @ (RealSrcSpan spLoc _)} ) pat :: LPat GhcPs )
464
+ (L (AsSrcSpan (RealSrcSpan spLoc _)) pat :: LPat GhcPs )
443
465
#endif
444
- | spanIsRelevant l ->
466
+ | realSpanIsRelevant spLoc ->
445
467
case pat of
446
468
SplicePat {} -> Here (spLoc, Pat )
447
469
_ -> Continue
448
470
_ -> Stop
449
471
`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 ->
452
474
case ty of
453
475
HsSpliceTy {} -> Here (spLoc, HsType )
454
476
_ -> Continue
455
477
_ -> Stop
456
478
`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 ->
459
481
case decl of
460
482
SpliceD {} -> Here (spLoc, HsDecl )
461
483
_ -> Continue
0 commit comments