@@ -25,6 +25,7 @@ module Development.IDE.GHC.ExactPrint
2525 Annotate ,
2626 setPrecedingLinesT,
2727#else
28+ setPrecedingLines,
2829 addParens,
2930 addParensToCtxt,
3031 modifyAnns,
@@ -56,6 +57,7 @@ import Control.Monad.Trans.Except
5657import Control.Monad.Zip
5758import Data.Bifunctor
5859import Data.Bool (bool )
60+ import Data.Default (Default )
5961import qualified Data.DList as DL
6062import Data.Either.Extra (mapLeft )
6163import Data.Foldable (Foldable (fold ))
@@ -101,7 +103,13 @@ import GHC (EpAnn (..),
101103 spanAsAnchor )
102104import GHC.Parser.Annotation (AnnContext (.. ),
103105 DeltaPos (SameLine ),
104- EpaLocation (EpaDelta ))
106+ EpaLocation (EpaDelta ),
107+ deltaPos )
108+ #endif
109+
110+ #if MIN_VERSION_ghc(9,2,0)
111+ setPrecedingLines :: Default t => LocatedAn t a -> Int -> Int -> LocatedAn t a
112+ setPrecedingLines ast n c = setEntryDP ast (deltaPos n c)
105113#endif
106114
107115------------------------------------------------------------------------------
@@ -114,10 +122,10 @@ instance Pretty Log where
114122
115123instance Show (Annotated ParsedSource ) where
116124 show _ = " <Annotated ParsedSource>"
117-
125+
118126instance NFData (Annotated ParsedSource ) where
119127 rnf = rwhnf
120-
128+
121129data GetAnnotatedParsedSource = GetAnnotatedParsedSource
122130 deriving (Eq , Show , Typeable , GHC.Generic )
123131
@@ -374,7 +382,7 @@ graftWithM dst trans = Graft $ \dflags a -> do
374382#if MIN_VERSION_ghc(9,2,0)
375383 val'' <-
376384 hoistTransform (either Fail. fail pure ) $
377- annotate dflags True $ maybeParensAST val'
385+ annotate dflags False $ maybeParensAST val'
378386 pure val''
379387#else
380388 (anns, val'') <-
@@ -468,7 +476,7 @@ graftDeclsWithM dst toDecls = Graft $ \dflags a -> do
468476 modifyDeclsT (fmap DL. toList . go) a
469477
470478
471- class (Data ast , Typeable l , Outputable l , Outputable ast ) => ASTElement l ast | ast -> l where
479+ class (Data ast , Default l , Typeable l , Outputable l , Outputable ast ) => ASTElement l ast | ast -> l where
472480 parseAST :: Parser (LocatedAn l ast )
473481 maybeParensAST :: LocatedAn l ast -> LocatedAn l ast
474482 {- | Construct a 'Graft', replacing the node at the given 'SrcSpan' with
@@ -520,6 +528,7 @@ fixAnns ParsedModule {..} =
520528
521529------------------------------------------------------------------------------
522530
531+
523532-- | Given an 'LHSExpr', compute its exactprint annotations.
524533-- Note that this function will throw away any existing annotations (and format)
525534annotate :: (ASTElement l ast , Outputable l )
@@ -533,7 +542,7 @@ annotate dflags needs_space ast = do
533542 let rendered = render dflags ast
534543#if MIN_VERSION_ghc(9,2,0)
535544 expr' <- lift $ mapLeft show $ parseAST dflags uniq rendered
536- pure expr'
545+ pure $ setPrecedingLines expr' 0 (bool 0 1 needs_space)
537546#else
538547 (anns, expr') <- lift $ mapLeft show $ parseAST dflags uniq rendered
539548 let anns' = setPrecedingLines expr' 0 (bool 0 1 needs_space) anns
@@ -542,6 +551,7 @@ annotate dflags needs_space ast = do
542551
543552-- | Given an 'LHsDecl', compute its exactprint annotations.
544553annotateDecl :: DynFlags -> LHsDecl GhcPs -> TransformT (Either String ) (LHsDecl GhcPs )
554+ #if !MIN_VERSION_ghc(9,2,0)
545555-- The 'parseDecl' function fails to parse 'FunBind' 'ValD's which contain
546556-- multiple matches. To work around this, we split the single
547557-- 'FunBind'-of-multiple-'Match'es into multiple 'FunBind's-of-one-'Match',
@@ -554,17 +564,6 @@ annotateDecl dflags
554564 let set_matches matches =
555565 ValD ext fb { fun_matches = mg { mg_alts = L alt_src matches }}
556566
557- #if MIN_VERSION_ghc(9,2,0)
558- alts' <- for alts $ \ alt -> do
559- uniq <- show <$> uniqueSrcSpanT
560- let rendered = render dflags $ set_matches [alt]
561- lift (mapLeft show $ parseDecl dflags uniq rendered) >>= \ case
562- (L _ (ValD _ FunBind { fun_matches = MG { mg_alts = L _ [alt']}}))
563- -> pure alt'
564- _ -> lift $ Left " annotateDecl: didn't parse a single FunBind match"
565-
566- pure $ L src $ set_matches alts'
567- #else
568567 (anns', alts') <- fmap unzip $ for alts $ \ alt -> do
569568 uniq <- show <$> uniqueSrcSpanT
570569 let rendered = render dflags $ set_matches [alt]
@@ -580,7 +579,8 @@ annotateDecl dflags ast = do
580579 uniq <- show <$> uniqueSrcSpanT
581580 let rendered = render dflags ast
582581#if MIN_VERSION_ghc(9,2,0)
583- lift $ mapLeft show $ parseDecl dflags uniq rendered
582+ expr' <- lift $ mapLeft show $ parseDecl dflags uniq rendered
583+ pure $ setPrecedingLines expr' 1 0
584584#else
585585 (anns, expr') <- lift $ mapLeft show $ parseDecl dflags uniq rendered
586586 let anns' = setPrecedingLines expr' 1 0 anns
0 commit comments