Skip to content

Commit 3f4edcc

Browse files
authored
Fix "group" import sort with multi-line imports
When some import line spans multuple lines, e.g. when import list is long, stylish-haskell breaks a group at this line, leading to bad result. This commits makes sure that import groups are recognized solely by empty lines.
1 parent 20dbe3a commit 3f4edcc

File tree

2 files changed

+29
-3
lines changed
  • lib/Language/Haskell/Stylish
  • tests/Language/Haskell/Stylish/Step/Imports

2 files changed

+29
-3
lines changed

lib/Language/Haskell/Stylish/Module.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,13 @@ moduleImportGroups = go [] Nothing . moduleImports
201201
-> [NonEmpty (Located Import)]
202202
go acc _ [] = ne acc
203203
go acc mbCurrentLine (imp : impRest) =
204-
let l2 = getStartLineUnsafe imp in
204+
let
205+
lStart = getStartLineUnsafe imp
206+
lEnd = getEndLineUnsafe imp in
205207
case mbCurrentLine of
206-
Just l1 | l1 + 1 < l2 -> ne acc ++ go [imp] (Just l2) impRest
207-
_ -> go (acc ++ [imp]) (Just l2) impRest
208+
Just lPrevEnd | lPrevEnd + 1 < lStart
209+
-> ne acc ++ go [imp] (Just lEnd) impRest
210+
_ -> go (acc ++ [imp]) (Just lEnd) impRest
208211

209212
ne [] = []
210213
ne (x : xs) = [x :| xs]

tests/Language/Haskell/Stylish/Step/Imports/Tests.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.Imports.Tests"
5959
, testCase "case 26 (issue 185)" case26
6060
, testCase "case 27" case27
6161
, testCase "case 28" case28
62+
, testCase "case 29" case29
6263
]
6364

6465

@@ -911,3 +912,25 @@ case28 = expected @=? testSnippet (step (Just 80) $ fromImportAlign Global) inpu
911912
, "import Data.Set (empty, intersect)"
912913
, "import Data.Set (empty, nub)"
913914
]
915+
916+
917+
--------------------------------------------------------------------------------
918+
case29 :: Assertion
919+
case29 = expected @=? testSnippet (step Nothing $ fromImportAlign Group) input'
920+
where
921+
-- Check that "Group" mode recognizes groups with multi-line imports
922+
input' = Snippet
923+
[ "import Foo (foo)"
924+
, "import BarBar ( bar"
925+
, " , kek)"
926+
, "import Abcd ()"
927+
, ""
928+
, "import A (A)"
929+
]
930+
expected = Snippet
931+
[ "import Abcd ()"
932+
, "import BarBar (bar, kek)"
933+
, "import Foo (foo)"
934+
, ""
935+
, "import A (A)"
936+
]

0 commit comments

Comments
 (0)