Skip to content

fix: handle comma in extend import list with ghc 9.2 #2697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import GHC (AddEpAnn (..), AnnContext (..), AnnParen (..),
DeltaPos (SameLine), EpAnn (..), EpaLocation (EpaDelta),
IsUnicodeSyntax (NormalSyntax),
NameAdornment (NameParens), NameAnn (..), addAnns, ann, emptyComments,
reAnnL, AnnList (..))
reAnnL, AnnList (..), TrailingAnn (AddCommaAnn), addTrailingAnnToA)
#endif
import Language.LSP.Types
import Development.IDE.GHC.Util
Expand Down Expand Up @@ -374,11 +374,7 @@ extendImportTopLevel thing (L l it@ImportDecl{..})
transferAnn (L l' lies) (L l' [x]) id
return $ L l it{ideclHiding = Just (hide, L l' $ lies ++ [x])}
#else

x <- pure $ setEntryDP x (SameLine $ if hasSibling then 1 else 0)

let fixLast = if hasSibling then first addComma else id
lies' = over _last fixLast lies ++ [x]
lies' <- addCommaInImportList lies x
return $ L l it{ideclHiding = Just (hide, L l' lies')}
#endif
extendImportTopLevel _ _ = lift $ Left "Unable to extend the import list"
Expand Down Expand Up @@ -490,13 +486,39 @@ extendImportViaParent df parent child (L l it@ImportDecl{..})
-- we need change the ann key from `[]` to `:` to keep parens and other anns.
unless hasSibling $
transferAnn (L l' $ reverse pre) (L l' [x]) id

let lies' = reverse pre ++ [x]
#else
x :: LIE GhcPs = reLocA $ L l'' $ IEThingWith listAnn parentLIE NoIEWildcard [childLIE]
listAnn = epAnn srcParent [AddEpAnn AnnOpenP (epl 1), AddEpAnn AnnCloseP (epl 0)]
x :: LIE GhcPs = reLocA $ L l'' $ IEThingWith listAnn parentLIE NoIEWildcard [childLIE]

let hasSibling = not (null pre)
lies' <- addCommaInImportList (reverse pre) x
#endif
return $ L l it{ideclHiding = Just (hide, L l' $ reverse pre ++ [x])}
return $ L l it{ideclHiding = Just (hide, L l' lies')}
extendImportViaParent _ _ _ _ = lift $ Left "Unable to extend the import list via parent"

#if MIN_VERSION_ghc(9,2,0)
-- Add an item in an import list, taking care of adding comma if needed.
addCommaInImportList :: Monad m =>
-- | Initial list
[LocatedAn AnnListItem a]
-- | Additionnal item
-> LocatedAn AnnListItem a
-> m [LocatedAn AnnListItem a]
addCommaInImportList lies x = do
let hasSibling = not (null lies)
-- Add the space before the comma
x <- pure $ setEntryDP x (SameLine $ if hasSibling then 1 else 0)

-- Add the comma (if needed)
let
fixLast = if hasSibling then first addComma else id
lies' = over _last fixLast lies ++ [x]

pure lies'
#endif

unIEWrappedName :: IEWrappedName (IdP GhcPs) -> String
unIEWrappedName (occName -> occ) = showSDocUnsafe $ parenSymOcc occ (ppr occ)

Expand Down
4 changes: 2 additions & 2 deletions ghcide/test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@ extendImportTests = testGroup "extend import actions"
, "import ModuleA as A (stuffB, (.*))"
, "main = print (stuffB .* stuffB)"
])
, knownBrokenForGhcVersions [GHC92] "missing comma. #2662" $ testSession "extend single line import with infix constructor" $ template
, testSession "extend single line import with infix constructor" $ template
[]
("ModuleB.hs", T.unlines
[ "module ModuleB where"
Expand All @@ -1534,7 +1534,7 @@ extendImportTests = testGroup "extend import actions"
, "import Data.List.NonEmpty (fromList, NonEmpty ((:|)))"
, "main = case (fromList []) of _ :| _ -> pure ()"
])
, knownBrokenForGhcVersions [GHC92] "missing comma. #2662" $ testSession "extend single line import with prefix constructor" $ template
, testSession "extend single line import with prefix constructor" $ template
[]
("ModuleB.hs", T.unlines
[ "module ModuleB where"
Expand Down