Skip to content

Commit 131b2b4

Browse files
author
kokobd
committed
handle trailing comma in import list properly
1 parent 445192e commit 131b2b4

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,12 +529,16 @@ addCommaInImportList :: Monad m =>
529529
-> m [LocatedAn AnnListItem a]
530530
addCommaInImportList lies x = do
531531
let hasSibling = not (null lies)
532+
(EpAnn _ lastItem _) = let (L item _) = last lies in ann (item)
533+
isTrailingCommaAnn (AddCommaAnn _) = True
534+
isTrailingCommaAnn _ = False
535+
lastItemHasTrailingComma = any isTrailingCommaAnn (lann_trailing lastItem)
532536
-- Add the space before the comma
533537
x <- pure $ setEntryDP x (SameLine $ if hasSibling then 1 else 0)
534538

535539
-- Add the comma (if needed)
536540
let
537-
fixLast = if hasSibling then first addComma else id
541+
fixLast = if hasSibling && not lastItemHasTrailingComma then first addComma else id
538542
lies' = over _last fixLast lies ++ [x]
539543

540544
pure lies'

ghcide/test/exe/Main.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,28 @@ extendImportTests = testGroup "extend import actions"
18821882
, " )"
18831883
, "main = print (stuffA, stuffB)"
18841884
])
1885+
, testSession "extend multi line import with trailing comma" $ template
1886+
[("ModuleA.hs", T.unlines
1887+
[ "module ModuleA where"
1888+
, "stuffA :: Double"
1889+
, "stuffA = 0.00750"
1890+
, "stuffB :: Integer"
1891+
, "stuffB = 123"
1892+
])]
1893+
("ModuleB.hs", T.unlines
1894+
[ "module ModuleB where"
1895+
, "import ModuleA (stuffB,"
1896+
, " )"
1897+
, "main = print (stuffA, stuffB)"
1898+
])
1899+
(Range (Position 3 17) (Position 3 18))
1900+
["Add stuffA to the import list of ModuleA"]
1901+
(T.unlines
1902+
[ "module ModuleB where"
1903+
, "import ModuleA (stuffB, stuffA"
1904+
, " )"
1905+
, "main = print (stuffA, stuffB)"
1906+
])
18851907
, testSession "extend single line import with method within class" $ template
18861908
[("ModuleA.hs", T.unlines
18871909
[ "module ModuleA where"

0 commit comments

Comments
 (0)