Skip to content

Commit 4b8f9d5

Browse files
guiboupepeiborramergify[bot]
authored
fix: handle comma in extend import list with ghc 9.2 (#2697)
* fix: handle comma in extend import list with ghc 9.2 The comma annotation was missing. Close #2662. * Update ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs Co-authored-by: Pepe Iborra <pepeiborra@gmail.com> * refactor: extract addCommaInImportList Co-authored-by: Pepe Iborra <pepeiborra@gmail.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 434678e commit 4b8f9d5

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
5454
import Language.LSP.Types
5555
import 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
384380
extendImportTopLevel _ _ = 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')}
498499
extendImportViaParent _ _ _ _ = 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+
500522
unIEWrappedName :: IEWrappedName (IdP GhcPs) -> String
501523
unIEWrappedName (occName -> occ) = showSDocUnsafe $ parenSymOcc occ (ppr occ)
502524

ghcide/test/exe/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,7 @@ extendImportTests = testGroup "extend import actions"
15201520
, "import ModuleA as A (stuffB, (.*))"
15211521
, "main = print (stuffB .* stuffB)"
15221522
])
1523-
, knownBrokenForGhcVersions [GHC92] "missing comma. #2662" $ testSession "extend single line import with infix constructor" $ template
1523+
, testSession "extend single line import with infix constructor" $ template
15241524
[]
15251525
("ModuleB.hs", T.unlines
15261526
[ "module ModuleB where"
@@ -1534,7 +1534,7 @@ extendImportTests = testGroup "extend import actions"
15341534
, "import Data.List.NonEmpty (fromList, NonEmpty ((:|)))"
15351535
, "main = case (fromList []) of _ :| _ -> pure ()"
15361536
])
1537-
, knownBrokenForGhcVersions [GHC92] "missing comma. #2662" $ testSession "extend single line import with prefix constructor" $ template
1537+
, testSession "extend single line import with prefix constructor" $ template
15381538
[]
15391539
("ModuleB.hs", T.unlines
15401540
[ "module ModuleB where"

0 commit comments

Comments
 (0)