summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorOndřej Janošík <j.ondra14@gmail.com>2015-07-09 09:15:04 +0200
committerOndřej Janošík <j.ondra14@gmail.com>2015-07-09 09:15:04 +0200
commit8ad70736e2ac2cb19b03c471e115784476ee2ee0 (patch)
tree0d69ffbb0136c983a697e860a8e2072f4a02153b /src
parent9180fae2d5d358bdea257b8b033ee4a2a963ce05 (diff)
downloadstylish-haskell-8ad70736e2ac2cb19b03c471e115784476ee2ee0.tar.gz
Ensured compatibility with original formatting
Diffstat (limited to 'src')
-rw-r--r--src/Language/Haskell/Stylish/Config.hs7
-rw-r--r--src/Language/Haskell/Stylish/Step/Imports.hs15
-rw-r--r--src/Language/Haskell/Stylish/Util.hs28
3 files changed, 40 insertions, 10 deletions
diff --git a/src/Language/Haskell/Stylish/Config.hs b/src/Language/Haskell/Stylish/Config.hs
index dd37a60..9dcbb50 100644
--- a/src/Language/Haskell/Stylish/Config.hs
+++ b/src/Language/Haskell/Stylish/Config.hs
@@ -166,7 +166,7 @@ parseImports config o = Imports.step
<$> pure (configColumns config)
<*> (Imports.Align
<$> (o A..:? "align" >>= parseEnum aligns Imports.Global)
- <*> (o A..:? "list_align" >>= parseEnum listAligns Imports.SameLine)
+ <*> (o A..:? "list_align" >>= parseEnum listAligns Imports.AfterAlias)
<*> (o A..:? "long_list_align"
>>= parseEnum longListAligns Imports.Inline)
<*> (o A..:? "list_padding"
@@ -180,8 +180,9 @@ parseImports config o = Imports.step
]
listAligns =
- [ ("new line", Imports.NewLine)
- , ("same line", Imports.SameLine)
+ [ ("new line", Imports.NewLine)
+ , ("with alias", Imports.WithAlias)
+ , ("after alias", Imports.AfterAlias)
]
longListAligns =
diff --git a/src/Language/Haskell/Stylish/Step/Imports.hs b/src/Language/Haskell/Stylish/Step/Imports.hs
index f1fbf40..78c8878 100644
--- a/src/Language/Haskell/Stylish/Step/Imports.hs
+++ b/src/Language/Haskell/Stylish/Step/Imports.hs
@@ -42,8 +42,9 @@ data ImportAlign
deriving (Eq, Show)
data ListAlign
- = SameLine
- | NewLine
+ = NewLine
+ | WithAlias
+ | AfterAlias
deriving (Eq, Show)
data LongListAlign
@@ -132,8 +133,11 @@ prettyImport columns Align{..} padQualified padName longest imp =
$ specs
inlineWrapper = case listAlign of
- SameLine -> wrap columns inlineBase (inlineBaseLength + 1)
- NewLine -> (inlineBase :) . wrapRest columns listPadding
+ NewLine -> (inlineBase :) . wrapRest columns listPadding
+ WithAlias -> wrap columns inlineBase (inlineBaseLength + 1)
+ -- Add 1 extra space to ensure same padding as in original code.
+ AfterAlias -> withTail (' ' :)
+ . wrap columns inlineBase (afterAliasBaseLength + 1)
multilineWrap = multilineBase : (wrapRest 0 listPadding
$ (withHead ("( " ++)
@@ -163,6 +167,9 @@ prettyImport columns Align{..} padQualified padName longest imp =
inlineBaseLength = length $ base' (padImport $ importName imp) [] []
+ afterAliasBaseLength = length $ base' (padImport $ importName imp)
+ ["as " ++ as | H.ModuleName _ as <- maybeToList $ H.importAs imp] []
+
(hiding, importSpecs) = case H.importSpecs imp of
Just (H.ImportSpecList _ h l) -> (h, Just l)
_ -> (False, Nothing)
diff --git a/src/Language/Haskell/Stylish/Util.hs b/src/Language/Haskell/Stylish/Util.hs
index f94c356..48cc0d3 100644
--- a/src/Language/Haskell/Stylish/Util.hs
+++ b/src/Language/Haskell/Stylish/Util.hs
@@ -74,7 +74,29 @@ wrap maxWidth leading ind strs' = wrap' leading strs'
| otherwise = wrap' (ss ++ " " ++ str) strs
overflows ss str = (length ss + length str) >= maxWidth
-
+ && ind + length str <= maxWidth
+
+
+-- wrap :: Int -- ^ Maximum line width
+-- -> String -- ^ Leading string
+-- -> Int -- ^ Indentation
+-- -> [String] -- ^ Strings to add/wrap
+-- -> Lines -- ^ Resulting lines
+-- wrap maxWidth leading ind strs =
+-- let (ls, curr, _) = foldl step ([], leading, length leading) strs
+-- in ls ++ [curr]
+-- where
+-- -- TODO: In order to optimize this, use a difference list instead of a
+-- -- regular list for 'ls'.
+-- step (ls, curr, width) str
+-- | nextLine = (ls ++ [curr], indent ind str, ind + len)
+-- | otherwise = (ls, curr ++ " " ++ str, width')
+-- where
+-- -- Put it on the next line if it would make the current line too long,
+-- -- AND if it doesn't make the next line too long.
+-- nextLine = width' > maxWidth && ind + len <= maxWidth
+-- len = length str
+-- width' = width + 1 + len
--------------------------------------------------------------------------------
wrapRest :: Int
@@ -87,11 +109,11 @@ wrapRest maxWidth ind = reverse . wrapRest' [] ""
| null ss = ls
| otherwise = ss:ls
wrapRest' ls ss (str:strs)
- | overflows ss str = wrapRest' (ss:ls) "" (str:strs)
| null ss = wrapRest' ls (indent ind str) strs
+ | overflows ss str = wrapRest' (ss:ls) "" (str:strs)
| otherwise = wrapRest' ls (ss ++ " " ++ str) strs
- overflows ss str = (length ss + length str) >= maxWidth && length ss > ind
+ overflows ss str = (length ss + length str + 1) >= maxWidth
--------------------------------------------------------------------------------