Skip to content

Commit

Permalink
fix: handle comma in extend import list with ghc 9.2
Browse files Browse the repository at this point in the history
The comma annotation was missing.

Close #2662.
  • Loading branch information
guibou committed Feb 8, 2022
1 parent 6cdd5be commit 3396477
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 0 additions & 1 deletion configuration-ghc-921.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ let
"-f-hlint"
"-f-haddockComments"
"-f-alternateNumberFormat"
"-f-eval"
]) { };

# YOLO
Expand Down
17 changes: 14 additions & 3 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 @@ -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
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

0 comments on commit 3396477

Please sign in to comment.