1515{-# LANGUAGE TypeApplications #-}
1616{-# LANGUAGE TypeFamilies #-}
1717{-# LANGUAGE ViewPatterns #-}
18+ {-# LANGUAGE PatternSynonyms #-}
1819{-# LANGUAGE MultiParamTypeClasses #-}
1920{-# LANGUAGE FlexibleInstances #-}
2021
@@ -51,10 +52,13 @@ import Development.IDE.GHC.Compat.ExactPrint
5152import qualified Development.IDE.GHC.Compat.Util as Util
5253import Development.IDE.GHC.ExactPrint
5354import GHC.Exts
55+ #if __GLASGOW_HASKELL__ >= 902
56+ import GHC.Parser.Annotation (SrcSpanAnn' (.. ))
57+ import qualified GHC.Types.Error as Error
58+ #endif
5459import Ide.Plugin.Splice.Types
5560import Ide.Types
56- import Language.Haskell.GHC.ExactPrint (setPrecedingLines ,
57- uniqueSrcSpanT )
61+ import Language.Haskell.GHC.ExactPrint (uniqueSrcSpanT )
5862import Language.LSP.Server
5963import Language.LSP.Types
6064import Language.LSP.Types.Capabilities
@@ -135,7 +139,7 @@ expandTHSplice _eStyle ideState params@ExpandSpliceParams {..} = do
135139 graftSpliceWith ::
136140 forall ast .
137141 HasSplice AnnListItem ast =>
138- Maybe (SrcSpan , Located (ast GhcPs )) ->
142+ Maybe (SrcSpan , LocatedAn AnnListItem (ast GhcPs )) ->
139143 Maybe (Either String WorkspaceEdit )
140144 graftSpliceWith expandeds =
141145 expandeds <&> \ (_, expanded) ->
@@ -236,11 +240,11 @@ adjustToRange uri ran (WorkspaceEdit mhult mlt x) =
236240 where
237241 adjustTextEdits :: Traversable f => f TextEdit -> f TextEdit
238242 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
244248
245249 adjustATextEdits :: Traversable f => f (TextEdit |? AnnotatedTextEdit ) -> f (TextEdit |? AnnotatedTextEdit )
246250 adjustATextEdits = fmap $ \ case
@@ -263,11 +267,23 @@ adjustToRange uri ran (WorkspaceEdit mhult mlt x) =
263267 J. range %~ \ r ->
264268 if r == bad then ran else bad
265269
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+
266282findSubSpansDesc :: SrcSpan -> [(LHsExpr GhcTc , a )] -> [(SrcSpan , a )]
267283findSubSpansDesc srcSpan =
268284 sortOn (Down . SubSpan . fst )
269285 . mapMaybe
270- ( \ (L spn _, e) -> do
286+ ( \ (L ( AsSrcSpan spn) _, e) -> do
271287 guard (spn `isSubspanOf` srcSpan)
272288 pure (spn, e)
273289 )
@@ -321,7 +337,7 @@ manualCalcEdit ::
321337manualCalcEdit clientCapabilities reportEditor ran ps hscEnv typechkd srcSpan _eStyle ExpandSpliceParams {.. } = do
322338 (warns, resl) <-
323339 ExceptT $ do
324- ((warns, errs) , eresl) <-
340+ (msgs , eresl) <-
325341 initTcWithGbl hscEnv typechkd srcSpan $
326342 case classifyAST spliceContext of
327343 IsHsDecl -> fmap (fmap $ adjustToRange uri ran) $
@@ -348,8 +364,16 @@ manualCalcEdit clientCapabilities reportEditor ran ps hscEnv typechkd srcSpan _e
348364 Util. try @ _ @ SomeException $
349365 (fst <$> expandSplice astP spl)
350366 )
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
352370 _ -> pure Nothing
371+ let (warns, errs) =
372+ #if __GLASGOW_HASKELL__ >= 902
373+ (Error. getWarningMessages msgs, Error. getErrorMessages msgs)
374+ #else
375+ msgs
376+ #endif
353377 pure $ (warns,) <$> fromMaybe (Left $ show errs) eresl
354378
355379 unless
@@ -370,14 +394,17 @@ unRenamedE ::
370394 (Fail. MonadFail m , HasSplice l ast ) =>
371395 DynFlags ->
372396 ast GhcRn ->
373- TransformT m (Located (ast GhcPs ))
397+ TransformT m (LocatedAn l (ast GhcPs ))
374398unRenamedE dflags expr = do
375399 uniq <- show <$> uniqueSrcSpanT
376- (anns, expr') <-
400+ #if __GLASGOW_HASKELL__ >= 902
401+ expr' <-
402+ #else
403+ (_anns, expr') <-
404+ #endif
377405 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
381408 pure expr'
382409
383410data SearchResult r =
@@ -416,11 +443,14 @@ codeAction state plId (CodeActionParams _ _ docId ran _) = liftIO $
416443 RealSrcSpan ->
417444 GenericQ (SearchResult (RealSrcSpan , SpliceContext ))
418445 detectSplice spn =
446+ let
447+ spanIsRelevant x = RealSrcSpan spn Nothing `isSubspanOf` x
448+ in
419449 mkQ
420450 Continue
421451 ( \ 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 ->
424454 case expr of
425455 HsSpliceE {} -> Here (spLoc, Expr )
426456 _ -> Continue
@@ -430,23 +460,23 @@ codeAction state plId (CodeActionParams _ _ docId ran _) = liftIO $
430460#if __GLASGOW_HASKELL__ == 808
431461 (dL @ (Pat GhcPs ) -> L l@ (RealSrcSpan spLoc _) pat :: Located (Pat GhcPs ))
432462#else
433- (L l@ (RealSrcSpan spLoc _) pat :: LPat GhcPs )
463+ (L ( AsSrcSpan l@ (RealSrcSpan spLoc _) ) pat :: LPat GhcPs )
434464#endif
435- | RealSrcSpan spn Nothing `isSubspanOf` l ->
465+ | spanIsRelevant l ->
436466 case pat of
437467 SplicePat {} -> Here (spLoc, Pat )
438468 _ -> Continue
439469 _ -> Stop
440470 `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 ->
443473 case ty of
444474 HsSpliceTy {} -> Here (spLoc, HsType )
445475 _ -> Continue
446476 _ -> Stop
447477 `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 ->
450480 case decl of
451481 SpliceD {} -> Here (spLoc, HsDecl )
452482 _ -> Continue
0 commit comments