summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2018-04-26 20:52:24 +0200
committerGitHub <noreply@github.com>2018-04-26 20:52:24 +0200
commit8447f67e7d16c0a8f84759c72833e14cab5611e7 (patch)
tree32f2e7aa97d98582ac3ec4602686e396f4933a8a /lib
parentb9c11413b17b5169fc7749f234ffef8fdbdb31aa (diff)
downloadstylish-haskell-8447f67e7d16c0a8f84759c72833e14cab5611e7.tar.gz
Support alignment of cases with a single guard
Diffstat (limited to 'lib')
-rw-r--r--lib/Language/Haskell/Stylish/Step/SimpleAlign.hs41
1 files changed, 30 insertions, 11 deletions
diff --git a/lib/Language/Haskell/Stylish/Step/SimpleAlign.hs b/lib/Language/Haskell/Stylish/Step/SimpleAlign.hs
index c83c482..924d6c5 100644
--- a/lib/Language/Haskell/Stylish/Step/SimpleAlign.hs
+++ b/lib/Language/Haskell/Stylish/Step/SimpleAlign.hs
@@ -8,6 +8,7 @@ module Language.Haskell.Stylish.Step.SimpleAlign
--------------------------------------------------------------------------------
import Data.Data (Data)
+import Data.List (foldl')
import Data.Maybe (maybeToList)
import qualified Language.Haskell.Exts as H
@@ -42,14 +43,30 @@ cases modu = [alts | H.Case _ _ alts <- everything modu]
--------------------------------------------------------------------------------
-altToAlignable :: H.Alt l -> Maybe (Alignable l)
-altToAlignable (H.Alt _ _ _ (Just _)) = Nothing
-altToAlignable (H.Alt ann pat rhs Nothing) = Just $ Alignable
- { aContainer = ann
- , aLeft = H.ann pat
- , aRight = H.ann rhs
- , aRightLead = length "-> "
- }
+-- | For this to work well, we require a way to merge annotations. This merge
+-- operation should follow the semigroup laws.
+altToAlignable :: (l -> l -> l) -> H.Alt l -> Maybe (Alignable l)
+altToAlignable _ (H.Alt _ _ _ (Just _)) = Nothing
+altToAlignable _ (H.Alt ann pat rhs@(H.UnGuardedRhs _ _) Nothing) = Just $
+ Alignable
+ { aContainer = ann
+ , aLeft = H.ann pat
+ , aRight = H.ann rhs
+ , aRightLead = length "-> "
+ }
+altToAlignable
+ merge
+ (H.Alt ann pat (H.GuardedRhss _ [H.GuardedRhs _ guards rhs]) Nothing) =
+ -- We currently only support the case where an alternative has a single
+ -- guarded RHS. If there are more, we would need to return multiple
+ -- `Alignable`s from this function, which would be a significant change.
+ Just $ Alignable
+ { aContainer = ann
+ , aLeft = foldl' merge (H.ann pat) (map H.ann guards)
+ , aRight = H.ann rhs
+ , aRightLead = length "-> "
+ }
+altToAlignable _ _ = Nothing
--------------------------------------------------------------------------------
@@ -101,9 +118,11 @@ step maxColumns config = makeStep "Cases" $ \ls (module', _) ->
, change_ <- align maxColumns aligns
]
- configured = concat $
- [changes cases altToAlignable | cCases config] ++
- [changes tlpats matchToAlignable | cTopLevelPatterns config] ++
+ configured = concat $
+ [ changes cases (altToAlignable H.mergeSrcSpan)
+ | cCases config
+ ] ++
+ [changes tlpats matchToAlignable | cTopLevelPatterns config] ++
[changes records fieldDeclToAlignable | cRecords config]
in applyChanges configured ls