From 7af4e587fe5696524821390dd381bd024a8ca14b Mon Sep 17 00:00:00 2001 From: Guillaume Bouchard Date: Tue, 8 Feb 2022 16:01:56 +0100 Subject: [PATCH 1/3] fix: handle comma in extend import list with ghc 9.2 The comma annotation was missing. Close #2662. --- .../IDE/Plugin/CodeAction/ExactPrint.hs | 17 ++++++++++++++--- ghcide/test/exe/Main.hs | 4 ++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs b/ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs index 9810cf98d5..2a90539dfb 100644 --- a/ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs +++ b/ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs @@ -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 @@ -490,11 +490,22 @@ 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] + + x <- pure $ setEntryDP x (SameLine $ if (not (null pre)) then 1 else 0) + + let + + fixLast = if not (null pre) then first addComma else id + lies' = over _last fixLast lies ++ [x] + lies = reverse pre + #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" unIEWrappedName :: IEWrappedName (IdP GhcPs) -> String diff --git a/ghcide/test/exe/Main.hs b/ghcide/test/exe/Main.hs index af46d337ad..18a348b7c1 100644 --- a/ghcide/test/exe/Main.hs +++ b/ghcide/test/exe/Main.hs @@ -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" @@ -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" From c6d98bc49ca91ed2998934514ec984e23653c841 Mon Sep 17 00:00:00 2001 From: Guillaume Bouchard Date: Tue, 8 Feb 2022 20:39:39 +0100 Subject: [PATCH 2/3] Update ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs Co-authored-by: Pepe Iborra --- ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs b/ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs index 2a90539dfb..158958d008 100644 --- a/ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs +++ b/ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs @@ -496,11 +496,12 @@ extendImportViaParent df parent child (L l it@ImportDecl{..}) listAnn = epAnn srcParent [AddEpAnn AnnOpenP (epl 1), AddEpAnn AnnCloseP (epl 0)] x :: LIE GhcPs = reLocA $ L l'' $ IEThingWith listAnn parentLIE NoIEWildcard [childLIE] - x <- pure $ setEntryDP x (SameLine $ if (not (null pre)) then 1 else 0) + let hasSibling = not (null pre) + x <- pure $ setEntryDP x (SameLine $ if hasSibling then 1 else 0) let - fixLast = if not (null pre) then first addComma else id + fixLast = if hasSibling then first addComma else id lies' = over _last fixLast lies ++ [x] lies = reverse pre From 0cc8668c591a5e4e0e4cdaaa27d9b0ba0bc8bb59 Mon Sep 17 00:00:00 2001 From: Guillaume Bouchard Date: Tue, 8 Feb 2022 21:14:23 +0100 Subject: [PATCH 3/3] refactor: extract addCommaInImportList --- .../IDE/Plugin/CodeAction/ExactPrint.hs | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs b/ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs index 158958d008..22c8e66901 100644 --- a/ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs +++ b/ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs @@ -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" @@ -497,18 +493,32 @@ extendImportViaParent df parent child (L l it@ImportDecl{..}) x :: LIE GhcPs = reLocA $ L l'' $ IEThingWith listAnn parentLIE NoIEWildcard [childLIE] let hasSibling = not (null pre) - 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 = reverse pre - + lies' <- addCommaInImportList (reverse pre) x #endif 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)