@@ -49,7 +49,7 @@ import GHC (AddEpAnn (..), AnnContext (..), AnnParen (..),
4949 DeltaPos (SameLine ), EpAnn (.. ), EpaLocation (EpaDelta ),
5050 IsUnicodeSyntax (NormalSyntax ),
5151 NameAdornment (NameParens ), NameAnn (.. ), addAnns , ann , emptyComments ,
52- reAnnL , AnnList (.. ))
52+ reAnnL , AnnList (.. ), TrailingAnn ( AddCommaAnn ), addTrailingAnnToA )
5353#endif
5454import Language.LSP.Types
5555import Development.IDE.GHC.Util
@@ -374,11 +374,7 @@ extendImportTopLevel thing (L l it@ImportDecl{..})
374374 transferAnn (L l' lies) (L l' [x]) id
375375 return $ L l it{ideclHiding = Just (hide, L l' $ lies ++ [x])}
376376#else
377-
378- x <- pure $ setEntryDP x (SameLine $ if hasSibling then 1 else 0 )
379-
380- let fixLast = if hasSibling then first addComma else id
381- lies' = over _last fixLast lies ++ [x]
377+ lies' <- addCommaInImportList lies x
382378 return $ L l it{ideclHiding = Just (hide, L l' lies')}
383379#endif
384380extendImportTopLevel _ _ = lift $ Left " Unable to extend the import list"
@@ -490,13 +486,39 @@ extendImportViaParent df parent child (L l it@ImportDecl{..})
490486 -- we need change the ann key from `[]` to `:` to keep parens and other anns.
491487 unless hasSibling $
492488 transferAnn (L l' $ reverse pre) (L l' [x]) id
489+
490+ let lies' = reverse pre ++ [x]
493491#else
494- x :: LIE GhcPs = reLocA $ L l'' $ IEThingWith listAnn parentLIE NoIEWildcard [childLIE]
495492 listAnn = epAnn srcParent [AddEpAnn AnnOpenP (epl 1 ), AddEpAnn AnnCloseP (epl 0 )]
493+ x :: LIE GhcPs = reLocA $ L l'' $ IEThingWith listAnn parentLIE NoIEWildcard [childLIE]
494+
495+ let hasSibling = not (null pre)
496+ lies' <- addCommaInImportList (reverse pre) x
496497#endif
497- return $ L l it{ideclHiding = Just (hide, L l' $ reverse pre ++ [x] )}
498+ return $ L l it{ideclHiding = Just (hide, L l' lies' )}
498499extendImportViaParent _ _ _ _ = lift $ Left " Unable to extend the import list via parent"
499500
501+ #if MIN_VERSION_ghc(9,2,0)
502+ -- Add an item in an import list, taking care of adding comma if needed.
503+ addCommaInImportList :: Monad m =>
504+ -- | Initial list
505+ [LocatedAn AnnListItem a ]
506+ -- | Additionnal item
507+ -> LocatedAn AnnListItem a
508+ -> m [LocatedAn AnnListItem a ]
509+ addCommaInImportList lies x = do
510+ let hasSibling = not (null lies)
511+ -- Add the space before the comma
512+ x <- pure $ setEntryDP x (SameLine $ if hasSibling then 1 else 0 )
513+
514+ -- Add the comma (if needed)
515+ let
516+ fixLast = if hasSibling then first addComma else id
517+ lies' = over _last fixLast lies ++ [x]
518+
519+ pure lies'
520+ #endif
521+
500522unIEWrappedName :: IEWrappedName (IdP GhcPs ) -> String
501523unIEWrappedName (occName -> occ) = showSDocUnsafe $ parenSymOcc occ (ppr occ)
502524
0 commit comments