@@ -49,7 +49,7 @@ import GHC (AddEpAnn (..), AnnContext (..), AnnParen (..),
49
49
DeltaPos (SameLine ), EpAnn (.. ), EpaLocation (EpaDelta ),
50
50
IsUnicodeSyntax (NormalSyntax ),
51
51
NameAdornment (NameParens ), NameAnn (.. ), addAnns , ann , emptyComments ,
52
- reAnnL , AnnList (.. ))
52
+ reAnnL , AnnList (.. ), TrailingAnn ( AddCommaAnn ), addTrailingAnnToA )
53
53
#endif
54
54
import Language.LSP.Types
55
55
import Development.IDE.GHC.Util
@@ -374,11 +374,7 @@ extendImportTopLevel thing (L l it@ImportDecl{..})
374
374
transferAnn (L l' lies) (L l' [x]) id
375
375
return $ L l it{ideclHiding = Just (hide, L l' $ lies ++ [x])}
376
376
#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
382
378
return $ L l it{ideclHiding = Just (hide, L l' lies')}
383
379
#endif
384
380
extendImportTopLevel _ _ = lift $ Left " Unable to extend the import list"
@@ -490,13 +486,39 @@ extendImportViaParent df parent child (L l it@ImportDecl{..})
490
486
-- we need change the ann key from `[]` to `:` to keep parens and other anns.
491
487
unless hasSibling $
492
488
transferAnn (L l' $ reverse pre) (L l' [x]) id
489
+
490
+ let lies' = reverse pre ++ [x]
493
491
#else
494
- x :: LIE GhcPs = reLocA $ L l'' $ IEThingWith listAnn parentLIE NoIEWildcard [childLIE]
495
492
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
496
497
#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' )}
498
499
extendImportViaParent _ _ _ _ = lift $ Left " Unable to extend the import list via parent"
499
500
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
+
500
522
unIEWrappedName :: IEWrappedName (IdP GhcPs ) -> String
501
523
unIEWrappedName (occName -> occ) = showSDocUnsafe $ parenSymOcc occ (ppr occ)
502
524
0 commit comments