summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorJasper Van der Jeugt <m@jaspervdj.be>2016-07-03 14:06:47 +0200
committerJasper Van der Jeugt <m@jaspervdj.be>2016-07-03 18:15:57 +0200
commitad677e6a22d22041d83162e714c5551c9a4d62d8 (patch)
tree2ea21ad20b33b8081453e7300fe39a7d5dd0ec38 /lib
parentfd5e7df4896845a113d03cefac9a710d2252aa19 (diff)
downloadstylish-haskell-ad677e6a22d22041d83162e714c5551c9a4d62d8.tar.gz
Record alignment takes max columns into account
Diffstat (limited to 'lib')
-rw-r--r--lib/Language/Haskell/Stylish.hs3
-rw-r--r--lib/Language/Haskell/Stylish/Config.hs2
-rw-r--r--lib/Language/Haskell/Stylish/Step/Records.hs36
3 files changed, 32 insertions, 9 deletions
diff --git a/lib/Language/Haskell/Stylish.hs b/lib/Language/Haskell/Stylish.hs
index 103306c..cfc7807 100644
--- a/lib/Language/Haskell/Stylish.hs
+++ b/lib/Language/Haskell/Stylish.hs
@@ -59,7 +59,8 @@ languagePragmas = LanguagePragmas.step
--------------------------------------------------------------------------------
-records :: Step
+records :: Int -- ^ columns
+ -> Step
records = Records.step
diff --git a/lib/Language/Haskell/Stylish/Config.hs b/lib/Language/Haskell/Stylish/Config.hs
index fee7594..2f72958 100644
--- a/lib/Language/Haskell/Stylish/Config.hs
+++ b/lib/Language/Haskell/Stylish/Config.hs
@@ -205,7 +205,7 @@ parseLanguagePragmas config o = LanguagePragmas.step
--------------------------------------------------------------------------------
parseRecords :: Config -> A.Object -> A.Parser Step
-parseRecords _ _ = return Records.step
+parseRecords c _ = return (Records.step $ configColumns c)
--------------------------------------------------------------------------------
diff --git a/lib/Language/Haskell/Stylish/Step/Records.hs b/lib/Language/Haskell/Stylish/Step/Records.hs
index c8f6d19..1c46024 100644
--- a/lib/Language/Haskell/Stylish/Step/Records.hs
+++ b/lib/Language/Haskell/Stylish/Step/Records.hs
@@ -31,6 +31,10 @@ data Alignable a = Alignable
{ aContainer :: !a
, aLeft :: !a
, aRight :: !a
+ -- | This is the minimal number of columns we need for the leading part not
+ -- included in our right string. For example, for datatype alignment, this
+ -- leading part is the string ":: " so we use 3.
+ , aRightLead :: !Int
} deriving (Show)
@@ -40,20 +44,38 @@ fieldDeclToAlignable (H.FieldDecl ann names ty) = Alignable
{ aContainer = ann
, aLeft = H.ann (last names)
, aRight = H.ann ty
+ , aRightLead = length ":: "
}
--------------------------------------------------------------------------------
-- | Align the type of a field
-align :: [Alignable H.SrcSpan] -> [Change String]
-align alignment = map align' alignment
+align :: Int -> [Alignable H.SrcSpan] -> [Change String]
+align maxColumns alignment
+ -- Do not make any change if we would go past the maximum number of columns.
+ | longestLeft + longestRight > maxColumns = info []
+ | otherwise = info $ map align' alignment
where
- longest = maximum $ map (H.srcSpanEndColumn . aLeft) alignment
+ info =
+ id
+ -- trace ("Alignable: " ++ show alignment) .
+ -- trace ("longestLeft: " ++ show longestLeft) .
+ -- trace ("longestRight: " ++ show longestRight)
+
+ -- The longest thing in the left column.
+ longestLeft = maximum $ map (H.srcSpanEndColumn . aLeft) alignment
+
+ -- The longest thing in the right column.
+ longestRight = maximum
+ [ H.srcSpanEndColumn (aRight a) - H.srcSpanStartColumn (aRight a)
+ + aRightLead a
+ | a <- alignment
+ ]
align' a = changeLine (H.srcSpanStartLine $ aContainer a) $ \str ->
let column = H.srcSpanEndColumn $ aLeft a
(pre, post) = splitAt column str
- in [padRight longest (trimRight pre) ++ trimLeft post]
+ in [padRight longestLeft (trimRight pre) ++ trimLeft post]
trimLeft = dropWhile isSpace
trimRight = reverse . trimLeft . reverse
@@ -72,8 +94,8 @@ fixable fields = all singleLine containers && nonOverlapping containers
--------------------------------------------------------------------------------
-step :: Step
-step = makeStep "Records" $ \ls (module', _) ->
+step :: Int -> Step
+step maxColumns = makeStep "Records" $ \ls (module', _) ->
let module'' = fmap H.srcInfoSpan module'
fixableRecords = filter fixable $ records module''
- in applyChanges (fixableRecords >>= align) ls
+ in applyChanges (fixableRecords >>= align maxColumns) ls