Skip to content

Commit 3fc1062

Browse files
author
kokobd
committed
handle trailing comma in import list properly
1 parent 445192e commit 3fc1062

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ import Data.Data (Data)
3232
import Data.Functor
3333
import Data.Generics (listify)
3434
import qualified Data.Map.Strict as Map
35-
import Data.Maybe (fromJust, isNothing,
36-
mapMaybe)
35+
import Data.Maybe (fromJust, isNothing, mapMaybe, fromMaybe )
3736
import qualified Data.Text as T
3837
import Development.IDE.GHC.Compat hiding (Annotation)
3938
import Development.IDE.GHC.Error
@@ -50,8 +49,8 @@ import Data.Default
5049
import GHC (AddEpAnn (..), AnnContext (..), AnnParen (..),
5150
DeltaPos (SameLine), EpAnn (..), EpaLocation (EpaDelta),
5251
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))
5554
#endif
5655
import Language.LSP.Types
5756
import Development.IDE.GHC.Util
@@ -377,7 +376,7 @@ extendImportTopLevel thing (L l it@ImportDecl{..})
377376
transferAnn (L l' lies) (L l' [x]) id
378377
return $ L l it{ideclHiding = Just (hide, L l' $ lies ++ [x])}
379378
#else
380-
lies' <- addCommaInImportList lies x
379+
let lies' = addCommaInImportList lies x
381380
return $ L l it{ideclHiding = Just (hide, L l' lies')}
382381
#endif
383382
extendImportTopLevel _ _ = lift $ Left "Unable to extend the import list"
@@ -514,30 +513,44 @@ extendImportViaParent df parent child (L l it@ImportDecl{..})
514513
listAnn = epAnn srcParent [AddEpAnn AnnOpenP (epl 1), AddEpAnn AnnCloseP (epl 0)]
515514
x :: LIE GhcPs = reLocA $ L l'' $ IEThingWith listAnn parentLIE NoIEWildcard [childLIE]
516515

517-
lies' <- addCommaInImportList (reverse pre) x
516+
lies' = addCommaInImportList (reverse pre) x
518517
#endif
519518
return $ L l it{ideclHiding = Just (hide, L l' lies')}
520519
extendImportViaParent _ _ _ _ = lift $ Left "Unable to extend the import list via parent"
521520

522521
#if MIN_VERSION_ghc(9,2,0)
523522
-- Add an item in an import list, taking care of adding comma if needed.
524-
addCommaInImportList :: Monad m =>
523+
addCommaInImportList ::
525524
-- | Initial list
526525
[LocatedAn AnnListItem a]
527526
-- | Additionnal item
528527
-> 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))
541554
#endif
542555

543556
unIEWrappedName :: IEWrappedName (IdP GhcPs) -> String

ghcide/test/exe/Main.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,28 @@ extendImportTests = testGroup "extend import actions"
18821882
, " )"
18831883
, "main = print (stuffA, stuffB)"
18841884
])
1885+
, testSession "extend multi line import with trailing comma" $ template
1886+
[("ModuleA.hs", T.unlines
1887+
[ "module ModuleA where"
1888+
, "stuffA :: Double"
1889+
, "stuffA = 0.00750"
1890+
, "stuffB :: Integer"
1891+
, "stuffB = 123"
1892+
])]
1893+
("ModuleB.hs", T.unlines
1894+
[ "module ModuleB where"
1895+
, "import ModuleA (stuffB,"
1896+
, " )"
1897+
, "main = print (stuffA, stuffB)"
1898+
])
1899+
(Range (Position 3 17) (Position 3 18))
1900+
["Add stuffA to the import list of ModuleA"]
1901+
(T.unlines
1902+
[ "module ModuleB where"
1903+
, "import ModuleA (stuffB, stuffA,"
1904+
, " )"
1905+
, "main = print (stuffA, stuffB)"
1906+
])
18851907
, testSession "extend single line import with method within class" $ template
18861908
[("ModuleA.hs", T.unlines
18871909
[ "module ModuleA where"

0 commit comments

Comments
 (0)