summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMaxim Koltsov <kolmax94@gmail.com>2020-10-05 01:53:26 +0300
committerGitHub <noreply@github.com>2020-10-05 00:53:26 +0200
commit3f4edcce319c3dafd1d1309b281625beb854e8a7 (patch)
treed58e883d66e4a79612541d3199c2ff143783013a
parent20dbe3a444a79dc5a8e0bf564b987db5393d127b (diff)
downloadstylish-haskell-3f4edcce319c3dafd1d1309b281625beb854e8a7.tar.gz
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.
-rw-r--r--lib/Language/Haskell/Stylish/Module.hs9
-rw-r--r--tests/Language/Haskell/Stylish/Step/Imports/Tests.hs23
2 files changed, 29 insertions, 3 deletions
diff --git a/lib/Language/Haskell/Stylish/Module.hs b/lib/Language/Haskell/Stylish/Module.hs
index 3647f3c..2cc8f47 100644
--- a/lib/Language/Haskell/Stylish/Module.hs
+++ b/lib/Language/Haskell/Stylish/Module.hs
@@ -201,10 +201,13 @@ moduleImportGroups = go [] Nothing . moduleImports
-> [NonEmpty (Located Import)]
go acc _ [] = ne acc
go acc mbCurrentLine (imp : impRest) =
- let l2 = getStartLineUnsafe imp in
+ let
+ lStart = getStartLineUnsafe imp
+ lEnd = getEndLineUnsafe imp in
case mbCurrentLine of
- Just l1 | l1 + 1 < l2 -> ne acc ++ go [imp] (Just l2) impRest
- _ -> go (acc ++ [imp]) (Just l2) impRest
+ Just lPrevEnd | lPrevEnd + 1 < lStart
+ -> ne acc ++ go [imp] (Just lEnd) impRest
+ _ -> go (acc ++ [imp]) (Just lEnd) impRest
ne [] = []
ne (x : xs) = [x :| xs]
diff --git a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
index bb56bab..fea3b78 100644
--- a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
+++ b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
@@ -59,6 +59,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.Imports.Tests"
, testCase "case 26 (issue 185)" case26
, testCase "case 27" case27
, testCase "case 28" case28
+ , testCase "case 29" case29
]
@@ -911,3 +912,25 @@ case28 = expected @=? testSnippet (step (Just 80) $ fromImportAlign Global) inpu
, "import Data.Set (empty, intersect)"
, "import Data.Set (empty, nub)"
]
+
+
+--------------------------------------------------------------------------------
+case29 :: Assertion
+case29 = expected @=? testSnippet (step Nothing $ fromImportAlign Group) input'
+ where
+ -- Check that "Group" mode recognizes groups with multi-line imports
+ input' = Snippet
+ [ "import Foo (foo)"
+ , "import BarBar ( bar"
+ , " , kek)"
+ , "import Abcd ()"
+ , ""
+ , "import A (A)"
+ ]
+ expected = Snippet
+ [ "import Abcd ()"
+ , "import BarBar (bar, kek)"
+ , "import Foo (foo)"
+ , ""
+ , "import A (A)"
+ ] \ No newline at end of file