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,10 +52,13 @@ 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
+ #if __GLASGOW_HASKELL__ >= 902
56
+ import GHC.Parser.Annotation (SrcSpanAnn' (.. ))
57
+ import qualified GHC.Types.Error as Error
58
+ #endif
54
59
import Ide.Plugin.Splice.Types
55
60
import Ide.Types
56
- import Language.Haskell.GHC.ExactPrint (setPrecedingLines ,
57
- uniqueSrcSpanT )
61
+ import Language.Haskell.GHC.ExactPrint (uniqueSrcSpanT )
58
62
import Language.LSP.Server
59
63
import Language.LSP.Types
60
64
import Language.LSP.Types.Capabilities
@@ -135,7 +139,7 @@ expandTHSplice _eStyle ideState params@ExpandSpliceParams {..} = do
135
139
graftSpliceWith ::
136
140
forall ast .
137
141
HasSplice AnnListItem ast =>
138
- Maybe (SrcSpan , Located (ast GhcPs )) ->
142
+ Maybe (SrcSpan , LocatedAn AnnListItem (ast GhcPs )) ->
139
143
Maybe (Either String WorkspaceEdit )
140
144
graftSpliceWith expandeds =
141
145
expandeds <&> \ (_, expanded) ->
@@ -236,11 +240,11 @@ adjustToRange uri ran (WorkspaceEdit mhult mlt x) =
236
240
where
237
241
adjustTextEdits :: Traversable f => f TextEdit -> f TextEdit
238
242
adjustTextEdits eds =
239
- let Just minStart =
240
- L. fold
241
- ( L. premap (view J. range) L. minimum )
242
- eds
243
- in adjustLine minStart <$> eds
243
+ let minStart =
244
+ case L. fold ( L. premap (view J. range) L. minimum ) eds of
245
+ Nothing -> error " impossible "
246
+ Just v -> v
247
+ in adjustLine minStart <$> eds
244
248
245
249
adjustATextEdits :: Traversable f => f (TextEdit |? AnnotatedTextEdit ) -> f (TextEdit |? AnnotatedTextEdit )
246
250
adjustATextEdits = fmap $ \ case
@@ -263,11 +267,23 @@ adjustToRange uri ran (WorkspaceEdit mhult mlt x) =
263
267
J. range %~ \ r ->
264
268
if r == bad then ran else bad
265
269
270
+ -- Define a pattern to get hold of a `SrcSpan` from the location part of a
271
+ -- `GenLocated`. In GHC >= 9.2 this will be a SrcSpanAnn', with annotations;
272
+ -- earlier it will just be a plain `SrcSpan`.
273
+ {-# COMPLETE AsSrcSpan #-}
274
+ #if __GLASGOW_HASKELL__ >= 902
275
+ pattern AsSrcSpan :: SrcSpan -> SrcSpanAnn' a
276
+ pattern AsSrcSpan locA <- SrcSpanAnn {locA}
277
+ #else
278
+ pattern AsSrcSpan :: SrcSpan -> SrcSpan
279
+ pattern AsSrcSpan loc <- loc
280
+ #endif
281
+
266
282
findSubSpansDesc :: SrcSpan -> [(LHsExpr GhcTc , a )] -> [(SrcSpan , a )]
267
283
findSubSpansDesc srcSpan =
268
284
sortOn (Down . SubSpan . fst )
269
285
. mapMaybe
270
- ( \ (L spn _, e) -> do
286
+ ( \ (L ( AsSrcSpan spn) _, e) -> do
271
287
guard (spn `isSubspanOf` srcSpan)
272
288
pure (spn, e)
273
289
)
@@ -321,7 +337,7 @@ manualCalcEdit ::
321
337
manualCalcEdit clientCapabilities reportEditor ran ps hscEnv typechkd srcSpan _eStyle ExpandSpliceParams {.. } = do
322
338
(warns, resl) <-
323
339
ExceptT $ do
324
- ((warns, errs) , eresl) <-
340
+ (msgs , eresl) <-
325
341
initTcWithGbl hscEnv typechkd srcSpan $
326
342
case classifyAST spliceContext of
327
343
IsHsDecl -> fmap (fmap $ adjustToRange uri ran) $
@@ -348,8 +364,16 @@ manualCalcEdit clientCapabilities reportEditor ran ps hscEnv typechkd srcSpan _e
348
364
Util. try @ _ @ SomeException $
349
365
(fst <$> expandSplice astP spl)
350
366
)
351
- Just <$> either (pure . L _spn) (unRenamedE dflags) eExpr
367
+ Just <$> case eExpr of
368
+ Left x -> pure $ L _spn x
369
+ Right y -> unRenamedE dflags y
352
370
_ -> pure Nothing
371
+ let (warns, errs) =
372
+ #if __GLASGOW_HASKELL__ >= 902
373
+ (Error. getWarningMessages msgs, Error. getErrorMessages msgs)
374
+ #else
375
+ msgs
376
+ #endif
353
377
pure $ (warns,) <$> fromMaybe (Left $ show errs) eresl
354
378
355
379
unless
@@ -370,14 +394,17 @@ unRenamedE ::
370
394
(Fail. MonadFail m , HasSplice l ast ) =>
371
395
DynFlags ->
372
396
ast GhcRn ->
373
- TransformT m (Located (ast GhcPs ))
397
+ TransformT m (LocatedAn l (ast GhcPs ))
374
398
unRenamedE dflags expr = do
375
399
uniq <- show <$> uniqueSrcSpanT
376
- (anns, expr') <-
400
+ #if __GLASGOW_HASKELL__ >= 902
401
+ expr' <-
402
+ #else
403
+ (_anns, expr') <-
404
+ #endif
377
405
either (fail . show ) pure $
378
- parseAST @ _ @ (ast GhcPs ) dflags uniq $
379
- showSDoc dflags $ ppr expr
380
- let _anns' = setPrecedingLines expr' 0 1 anns
406
+ parseAST @ _ @ (ast GhcPs ) dflags uniq $
407
+ showSDoc dflags $ ppr expr
381
408
pure expr'
382
409
383
410
data SearchResult r =
@@ -416,11 +443,14 @@ codeAction state plId (CodeActionParams _ _ docId ran _) = liftIO $
416
443
RealSrcSpan ->
417
444
GenericQ (SearchResult (RealSrcSpan , SpliceContext ))
418
445
detectSplice spn =
446
+ let
447
+ spanIsRelevant x = RealSrcSpan spn Nothing `isSubspanOf` x
448
+ in
419
449
mkQ
420
450
Continue
421
451
( \ case
422
- (L l@ (RealSrcSpan spLoc _) expr :: LHsExpr GhcPs )
423
- | RealSrcSpan spn Nothing `isSubspanOf` l ->
452
+ (L ( AsSrcSpan l@ (RealSrcSpan spLoc _) ) expr :: LHsExpr GhcPs )
453
+ | spanIsRelevant l ->
424
454
case expr of
425
455
HsSpliceE {} -> Here (spLoc, Expr )
426
456
_ -> Continue
@@ -430,23 +460,23 @@ codeAction state plId (CodeActionParams _ _ docId ran _) = liftIO $
430
460
#if __GLASGOW_HASKELL__ == 808
431
461
(dL @ (Pat GhcPs ) -> L l@ (RealSrcSpan spLoc _) pat :: Located (Pat GhcPs ))
432
462
#else
433
- (L l@ (RealSrcSpan spLoc _) pat :: LPat GhcPs )
463
+ (L ( AsSrcSpan l@ (RealSrcSpan spLoc _) ) pat :: LPat GhcPs )
434
464
#endif
435
- | RealSrcSpan spn Nothing `isSubspanOf` l ->
465
+ | spanIsRelevant l ->
436
466
case pat of
437
467
SplicePat {} -> Here (spLoc, Pat )
438
468
_ -> Continue
439
469
_ -> Stop
440
470
`extQ` \ case
441
- (L l@ (RealSrcSpan spLoc _) ty :: LHsType GhcPs )
442
- | RealSrcSpan spn Nothing `isSubspanOf` l ->
471
+ (L ( AsSrcSpan l@ (RealSrcSpan spLoc _) ) ty :: LHsType GhcPs )
472
+ | spanIsRelevant l ->
443
473
case ty of
444
474
HsSpliceTy {} -> Here (spLoc, HsType )
445
475
_ -> Continue
446
476
_ -> Stop
447
477
`extQ` \ case
448
- (L l@ (RealSrcSpan spLoc _) decl :: LHsDecl GhcPs )
449
- | RealSrcSpan spn Nothing `isSubspanOf` l ->
478
+ (L ( AsSrcSpan l@ (RealSrcSpan spLoc _) ) decl :: LHsDecl GhcPs )
479
+ | spanIsRelevant l ->
450
480
case decl of
451
481
SpliceD {} -> Here (spLoc, HsDecl )
452
482
_ -> Continue
0 commit comments