diff options
author | Oleg Grenrus <oleg.grenrus@iki.fi> | 2016-08-03 12:51:36 +0300 |
---|---|---|
committer | Oleg Grenrus <oleg.grenrus@iki.fi> | 2016-08-03 12:51:36 +0300 |
commit | 694aea9872be69ce77e168ac87e5109c830b14f7 (patch) | |
tree | bdbc2fb7b963b9e61a4ba268cf75272a3874c945 /lib | |
parent | ab625813a8ebf69defe83745a71c7c085632ec23 (diff) | |
download | stylish-haskell-694aea9872be69ce77e168ac87e5109c830b14f7.tar.gz |
list_padding: module_name
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Language/Haskell/Stylish/Config.hs | 3 | ||||
-rw-r--r-- | lib/Language/Haskell/Stylish/Step/Imports.hs | 36 |
2 files changed, 33 insertions, 6 deletions
diff --git a/lib/Language/Haskell/Stylish/Config.hs b/lib/Language/Haskell/Stylish/Config.hs index e551d71..5092ed9 100644 --- a/lib/Language/Haskell/Stylish/Config.hs +++ b/lib/Language/Haskell/Stylish/Config.hs @@ -189,7 +189,7 @@ parseImports config o = Imports.step -- Note that padding has to be at least 1. Default is 4. <*> (o A..:? "empty_list_align" >>= parseEnum emptyListAligns Imports.Inherit) - <*> (maybe 4 (max 1) <$> o A..:? "list_padding") + <*> o A..:? "list_padding" A..!= Imports.LPConstant 4 <*> o A..:? "separate_lists" A..!= True) where aligns = @@ -217,7 +217,6 @@ parseImports config o = Imports.step , ("right_after", Imports.RightAfter) ] - -------------------------------------------------------------------------------- parseLanguagePragmas :: Config -> A.Object -> A.Parser Step parseLanguagePragmas config o = LanguagePragmas.step diff --git a/lib/Language/Haskell/Stylish/Step/Imports.hs b/lib/Language/Haskell/Stylish/Step/Imports.hs index 00ca9b7..d0519ef 100644 --- a/lib/Language/Haskell/Stylish/Step/Imports.hs +++ b/lib/Language/Haskell/Stylish/Step/Imports.hs @@ -1,4 +1,5 @@ {-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE OverloadedStrings #-} -------------------------------------------------------------------------------- module Language.Haskell.Stylish.Step.Imports ( Align (..) @@ -6,6 +7,7 @@ module Language.Haskell.Stylish.Step.Imports , ListAlign (..) , LongListAlign (..) , EmptyListAlign (..) + , ListPadding (..) , step ) where @@ -18,6 +20,8 @@ import Data.List (intercalate, sortBy) import Data.Maybe (isJust, maybeToList) import Data.Ord (comparing) import qualified Language.Haskell.Exts as H +import qualified Data.Aeson as A +import qualified Data.Aeson.Types as A -------------------------------------------------------------------------------- @@ -32,11 +36,16 @@ data Align = Align , listAlign :: ListAlign , longListAlign :: LongListAlign , emptyListAlign :: EmptyListAlign - , listPadding :: Int + , listPadding :: ListPadding , separateLists :: Bool } deriving (Eq, Show) +data ListPadding + = LPConstant Int + | LPModuleName + deriving (Eq, Show) + data ImportAlign = Global | File @@ -153,6 +162,11 @@ prettyImport columns Align{..} padQualified padName longest imp Multiline -> longListWrapper inlineWrap multilineWrap where emptyImportSpec = Just (H.ImportSpecList () False []) + -- "import" + space + qualifiedLength has space in it. + listPadding' = listPaddingValue (6 + 1 + qualifiedLength) listPadding + where + qualifiedLength = + if null qualified then 0 else 1 + sum (map length qualified) longListWrapper shortWrap longWrap | listAlign == NewLine @@ -172,13 +186,13 @@ prettyImport columns Align{..} padQualified padName longest imp . withLast (++ ")") inlineWrapper = case listAlign of - NewLine -> (paddedNoSpecBase :) . wrapRest columns listPadding + NewLine -> (paddedNoSpecBase :) . wrapRest columns listPadding' WithAlias -> wrap columns paddedBase (inlineBaseLength + 1) -- Add 1 extra space to ensure same padding as in original code. AfterAlias -> withTail (' ' :) . wrap columns paddedBase (afterAliasBaseLength + 1) - inlineWithBreakWrap = paddedNoSpecBase : wrapRest columns listPadding + inlineWithBreakWrap = paddedNoSpecBase : wrapRest columns listPadding' ( mapSpecs $ withInit (++ ",") . withHead ("(" ++) @@ -191,7 +205,7 @@ prettyImport columns Align{..} padQualified padName longest imp | otherwise = inlineWithBreakWrap -- 'wrapRest 0' ensures that every item of spec list is on new line. - multilineWrap = paddedNoSpecBase : wrapRest 0 listPadding + multilineWrap = paddedNoSpecBase : wrapRest 0 listPadding' ( mapSpecs ( withHead ("( " ++) . withTail (", " ++)) @@ -288,3 +302,17 @@ step' columns align ls (module', _) = applyChanges fileAlign = case importAlign align of File -> any H.importQualified imps _ -> False + +-------------------------------------------------------------------------------- +listPaddingValue :: Int -> ListPadding -> Int +listPaddingValue _ (LPConstant n) = n +listPaddingValue n LPModuleName = n + +-------------------------------------------------------------------------------- + +instance A.FromJSON ListPadding where + parseJSON (A.String "module_name") = return LPModuleName + parseJSON (A.Number n) | n' >= 1 = return $ LPConstant n' + where + n' = truncate n + parseJSON v = A.typeMismatch "'module_name' or >=1 number" v |