@@ -32,8 +32,7 @@ import Data.Data (Data)
32
32
import Data.Functor
33
33
import Data.Generics (listify )
34
34
import qualified Data.Map.Strict as Map
35
- import Data.Maybe (fromJust , isNothing ,
36
- mapMaybe )
35
+ import Data.Maybe (fromJust , isNothing , mapMaybe , fromMaybe )
37
36
import qualified Data.Text as T
38
37
import Development.IDE.GHC.Compat hiding (Annotation )
39
38
import Development.IDE.GHC.Error
@@ -50,8 +49,8 @@ import Data.Default
50
49
import GHC (AddEpAnn (.. ), AnnContext (.. ), AnnParen (.. ),
51
50
DeltaPos (SameLine ), EpAnn (.. ), EpaLocation (EpaDelta ),
52
51
IsUnicodeSyntax (NormalSyntax ),
53
- NameAdornment (NameParens ), NameAnn ( .. ), addAnns , ann , emptyComments ,
54
- reAnnL , AnnList (.. ), TrailingAnn (AddCommaAnn ), addTrailingAnnToA )
52
+ NameAdornment (NameParens ), addAnns , ann , emptyComments ,
53
+ reAnnL , AnnList (.. ), TrailingAnn (AddCommaAnn ))
55
54
#endif
56
55
import Language.LSP.Types
57
56
import Development.IDE.GHC.Util
@@ -377,7 +376,7 @@ extendImportTopLevel thing (L l it@ImportDecl{..})
377
376
transferAnn (L l' lies) (L l' [x]) id
378
377
return $ L l it{ideclHiding = Just (hide, L l' $ lies ++ [x])}
379
378
#else
380
- lies' <- addCommaInImportList lies x
379
+ let lies' = addCommaInImportList lies x
381
380
return $ L l it{ideclHiding = Just (hide, L l' lies')}
382
381
#endif
383
382
extendImportTopLevel _ _ = lift $ Left " Unable to extend the import list"
@@ -514,30 +513,44 @@ extendImportViaParent df parent child (L l it@ImportDecl{..})
514
513
listAnn = epAnn srcParent [AddEpAnn AnnOpenP (epl 1 ), AddEpAnn AnnCloseP (epl 0 )]
515
514
x :: LIE GhcPs = reLocA $ L l'' $ IEThingWith listAnn parentLIE NoIEWildcard [childLIE]
516
515
517
- lies' <- addCommaInImportList (reverse pre) x
516
+ lies' = addCommaInImportList (reverse pre) x
518
517
#endif
519
518
return $ L l it{ideclHiding = Just (hide, L l' lies')}
520
519
extendImportViaParent _ _ _ _ = lift $ Left " Unable to extend the import list via parent"
521
520
522
521
#if MIN_VERSION_ghc(9,2,0)
523
522
-- Add an item in an import list, taking care of adding comma if needed.
524
- addCommaInImportList :: Monad m =>
523
+ addCommaInImportList ::
525
524
-- | Initial list
526
525
[LocatedAn AnnListItem a ]
527
526
-- | Additionnal item
528
527
-> LocatedAn AnnListItem a
529
- -> m [LocatedAn AnnListItem a ]
530
- addCommaInImportList lies x = do
531
- let hasSibling = not (null lies)
532
- -- Add the space before the comma
533
- x <- pure $ setEntryDP x (SameLine $ if hasSibling then 1 else 0 )
534
-
535
- -- Add the comma (if needed)
536
- let
537
- fixLast = if hasSibling then first addComma else id
538
- lies' = over _last fixLast lies ++ [x]
539
-
540
- pure lies'
528
+ -> [LocatedAn AnnListItem a ]
529
+ addCommaInImportList lies x =
530
+ fixLast lies ++ [newItem]
531
+ where
532
+ isTrailingAnnComma :: TrailingAnn -> Bool
533
+ isTrailingAnnComma (AddCommaAnn _) = True
534
+ isTrailingAnnComma _ = False
535
+
536
+ -- check if there is an existing trailing comma
537
+ hasTrailingComma = fromMaybe False $ do
538
+ L lastItemSrcAnn _ <- lastMaybe lies
539
+ lastItemAnn <- case ann lastItemSrcAnn of
540
+ EpAnn _ lastItemAnn _ -> pure lastItemAnn
541
+ _ -> Nothing
542
+ pure . not $ any isTrailingAnnComma (lann_trailing lastItemAnn)
543
+
544
+ hasSibling = not . null $ lies
545
+
546
+ -- Setup the new item. It should have a preceding whitespace if it has siblings, and a trailing comma if the
547
+ -- preceding item already has one.
548
+ newItem = first (if hasTrailingComma then id else addComma) $
549
+ setEntryDP x (SameLine $ if hasSibling then 1 else 0 )
550
+
551
+ -- Add the comma (if needed)
552
+ fixLast :: [LocatedAn AnnListItem a ] -> [LocatedAn AnnListItem a ]
553
+ fixLast = over _last (first (if hasTrailingComma then addComma else id ))
541
554
#endif
542
555
543
556
unIEWrappedName :: IEWrappedName (IdP GhcPs ) -> String
0 commit comments