Skip to content

Commit 7af4e58

Browse files
committed
fix: handle comma in extend import list with ghc 9.2
The comma annotation was missing. Close #2662.
1 parent 6cdd5be commit 7af4e58

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

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

+14-3
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
@@ -490,11 +490,22 @@ extendImportViaParent df parent child (L l it@ImportDecl{..})
490490
-- we need change the ann key from `[]` to `:` to keep parens and other anns.
491491
unless hasSibling $
492492
transferAnn (L l' $ reverse pre) (L l' [x]) id
493+
494+
let lies' = reverse pre ++ [x]
493495
#else
494-
x :: LIE GhcPs = reLocA $ L l'' $ IEThingWith listAnn parentLIE NoIEWildcard [childLIE]
495496
listAnn = epAnn srcParent [AddEpAnn AnnOpenP (epl 1), AddEpAnn AnnCloseP (epl 0)]
497+
x :: LIE GhcPs = reLocA $ L l'' $ IEThingWith listAnn parentLIE NoIEWildcard [childLIE]
498+
499+
x <- pure $ setEntryDP x (SameLine $ if (not (null pre)) then 1 else 0)
500+
501+
let
502+
503+
fixLast = if not (null pre) then first addComma else id
504+
lies' = over _last fixLast lies ++ [x]
505+
lies = reverse pre
506+
496507
#endif
497-
return $ L l it{ideclHiding = Just (hide, L l' $ reverse pre ++ [x])}
508+
return $ L l it{ideclHiding = Just (hide, L l' lies')}
498509
extendImportViaParent _ _ _ _ = lift $ Left "Unable to extend the import list via parent"
499510

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

ghcide/test/exe/Main.hs

+2-2
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)