diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2016-07-23 10:44:25 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2016-07-23 10:44:25 +0200 |
commit | 7d0f24a3e3a01db3e6ff3858518a0fd715380c83 (patch) | |
tree | 3dda515c23aa02b0141ee3a7a925c443f39a4662 | |
parent | 82943538ba7570dfadd8b3deb501bd67537c57b0 (diff) | |
download | stylish-haskell-7d0f24a3e3a01db3e6ff3858518a0fd715380c83.tar.gz |
Move records step into simple_align
-rw-r--r-- | data/stylish-haskell.yaml | 4 | ||||
-rw-r--r-- | lib/Language/Haskell/Stylish.hs | 8 | ||||
-rw-r--r-- | lib/Language/Haskell/Stylish/Config.hs | 10 | ||||
-rw-r--r-- | lib/Language/Haskell/Stylish/Step/Records.hs | 41 | ||||
-rw-r--r-- | lib/Language/Haskell/Stylish/Step/SimpleAlign.hs | 27 | ||||
-rw-r--r-- | stylish-haskell.cabal | 3 | ||||
-rw-r--r-- | tests/Language/Haskell/Stylish/Step/Records/Tests.hs | 100 | ||||
-rw-r--r-- | tests/Language/Haskell/Stylish/Step/SimpleAlign/Tests.hs | 85 | ||||
-rw-r--r-- | tests/TestSuite.hs | 2 |
9 files changed, 110 insertions, 170 deletions
diff --git a/data/stylish-haskell.yaml b/data/stylish-haskell.yaml index cbeaee0..2398e6b 100644 --- a/data/stylish-haskell.yaml +++ b/data/stylish-haskell.yaml @@ -21,6 +21,7 @@ steps: - simple_align: cases: true top_level_patterns: true + records: true # Import cleanup - imports: @@ -135,9 +136,6 @@ steps: # is set to true, it will remove those redundant pragmas. Default: true. remove_redundant: true - # Align the types in record declarations - - records: {} - # Replace tabs by spaces. This is disabled by default. # - tabs: # # Number of spaces to use for each tab. Default: 8, as specified by the diff --git a/lib/Language/Haskell/Stylish.hs b/lib/Language/Haskell/Stylish.hs index 2dbf7fc..5b1e918 100644 --- a/lib/Language/Haskell/Stylish.hs +++ b/lib/Language/Haskell/Stylish.hs @@ -6,7 +6,6 @@ module Language.Haskell.Stylish , simpleAlign , imports , languagePragmas - , records , tabs , trailingWhitespace , unicodeSyntax @@ -35,7 +34,6 @@ import Language.Haskell.Stylish.Parse import Language.Haskell.Stylish.Step import qualified Language.Haskell.Stylish.Step.Imports as Imports import qualified Language.Haskell.Stylish.Step.LanguagePragmas as LanguagePragmas -import qualified Language.Haskell.Stylish.Step.Records as Records import qualified Language.Haskell.Stylish.Step.SimpleAlign as SimpleAlign import qualified Language.Haskell.Stylish.Step.Tabs as Tabs import qualified Language.Haskell.Stylish.Step.TrailingWhitespace as TrailingWhitespace @@ -68,12 +66,6 @@ languagePragmas = LanguagePragmas.step -------------------------------------------------------------------------------- -records :: Int -- ^ columns - -> Step -records = Records.step - - --------------------------------------------------------------------------------- tabs :: Int -- ^ number of spaces -> Step tabs = Tabs.step diff --git a/lib/Language/Haskell/Stylish/Config.hs b/lib/Language/Haskell/Stylish/Config.hs index 14f7a04..d14e1be 100644 --- a/lib/Language/Haskell/Stylish/Config.hs +++ b/lib/Language/Haskell/Stylish/Config.hs @@ -33,7 +33,6 @@ import qualified System.IO as IO (Newline import Language.Haskell.Stylish.Step import qualified Language.Haskell.Stylish.Step.Imports as Imports import qualified Language.Haskell.Stylish.Step.LanguagePragmas as LanguagePragmas -import qualified Language.Haskell.Stylish.Step.Records as Records import qualified Language.Haskell.Stylish.Step.SimpleAlign as SimpleAlign import qualified Language.Haskell.Stylish.Step.Tabs as Tabs import qualified Language.Haskell.Stylish.Step.TrailingWhitespace as TrailingWhitespace @@ -140,7 +139,6 @@ catalog :: Map String (Config -> A.Object -> A.Parser Step) catalog = M.fromList [ ("imports", parseImports) , ("language_pragmas", parseLanguagePragmas) - , ("records", parseRecords) , ("simple_align", parseSimpleAlign) , ("tabs", parseTabs) , ("trailing_whitespace", parseTrailingWhitespace) @@ -173,7 +171,8 @@ parseSimpleAlign c o = SimpleAlign.step <$> pure (configColumns c) <*> (SimpleAlign.Config <$> withDef SimpleAlign.cCases "cases" - <*> withDef SimpleAlign.cTopLevelPatterns "top_level_patterns") + <*> withDef SimpleAlign.cTopLevelPatterns "top_level_patterns" + <*> withDef SimpleAlign.cRecords "records") where withDef f k = fromMaybe (f SimpleAlign.defaultConfig) <$> (o A..:? k) @@ -228,11 +227,6 @@ parseLanguagePragmas config o = LanguagePragmas.step -------------------------------------------------------------------------------- -parseRecords :: Config -> A.Object -> A.Parser Step -parseRecords c _ = return (Records.step $ configColumns c) - - --------------------------------------------------------------------------------- parseTabs :: Config -> A.Object -> A.Parser Step parseTabs _ o = Tabs.step <$> o A..:? "spaces" A..!= 8 diff --git a/lib/Language/Haskell/Stylish/Step/Records.hs b/lib/Language/Haskell/Stylish/Step/Records.hs deleted file mode 100644 index bbfb895..0000000 --- a/lib/Language/Haskell/Stylish/Step/Records.hs +++ /dev/null @@ -1,41 +0,0 @@ --------------------------------------------------------------------------------- -module Language.Haskell.Stylish.Step.Records - ( step - ) where - - --------------------------------------------------------------------------------- -import qualified Language.Haskell.Exts.Annotated as H - - --------------------------------------------------------------------------------- -import Language.Haskell.Stylish.Align -import Language.Haskell.Stylish.Editor -import Language.Haskell.Stylish.Step - - --------------------------------------------------------------------------------- -records :: H.Module l -> [[Alignable l]] -records modu = - [ map fieldDeclToAlignable fields - | H.Module _ _ _ _ decls <- [modu] - , H.DataDecl _ _ _ _ cons _ <- decls - , H.QualConDecl _ _ _ (H.RecDecl _ _ fields) <- cons - ] - - --------------------------------------------------------------------------------- -fieldDeclToAlignable :: H.FieldDecl a -> Alignable a -fieldDeclToAlignable (H.FieldDecl ann names ty) = Alignable - { aContainer = ann - , aLeft = H.ann (last names) - , aRight = H.ann ty - , aRightLead = length ":: " - } - - --------------------------------------------------------------------------------- -step :: Int -> Step -step maxColumns = makeStep "Records" $ \ls (module', _) -> - let module'' = fmap H.srcInfoSpan module' in - applyChanges (records module'' >>= align maxColumns) ls diff --git a/lib/Language/Haskell/Stylish/Step/SimpleAlign.hs b/lib/Language/Haskell/Stylish/Step/SimpleAlign.hs index 68af224..c89e8a1 100644 --- a/lib/Language/Haskell/Stylish/Step/SimpleAlign.hs +++ b/lib/Language/Haskell/Stylish/Step/SimpleAlign.hs @@ -23,6 +23,7 @@ import Language.Haskell.Stylish.Util data Config = Config { cCases :: !Bool , cTopLevelPatterns :: !Bool + , cRecords :: !Bool } deriving (Show) @@ -31,6 +32,7 @@ defaultConfig :: Config defaultConfig = Config { cCases = True , cTopLevelPatterns = True + , cRecords = True } @@ -69,6 +71,26 @@ matchToAlignable (H.Match ann name pats rhs Nothing) = Just $ Alignable -------------------------------------------------------------------------------- +records :: H.Module l -> [[H.FieldDecl l]] +records modu = + [ fields + | H.Module _ _ _ _ decls <- [modu] + , H.DataDecl _ _ _ _ cons _ <- decls + , H.QualConDecl _ _ _ (H.RecDecl _ _ fields) <- cons + ] + + +-------------------------------------------------------------------------------- +fieldDeclToAlignable :: H.FieldDecl a -> Maybe (Alignable a) +fieldDeclToAlignable (H.FieldDecl ann names ty) = Just $ Alignable + { aContainer = ann + , aLeft = H.ann (last names) + , aRight = H.ann ty + , aRightLead = length ":: " + } + + +-------------------------------------------------------------------------------- step :: Int -> Config -> Step step maxColumns config = makeStep "Cases" $ \ls (module', _) -> let module'' = fmap H.srcInfoSpan module' @@ -80,7 +102,8 @@ step maxColumns config = makeStep "Cases" $ \ls (module', _) -> ] configured = concat $ - [changes cases altToAlignable | cCases config] ++ - [changes tlpats matchToAlignable | cTopLevelPatterns config] + [changes cases altToAlignable | cCases config] ++ + [changes tlpats matchToAlignable | cTopLevelPatterns config] ++ + [changes records fieldDeclToAlignable | cRecords config] in applyChanges configured ls diff --git a/stylish-haskell.cabal b/stylish-haskell.cabal index 3678315..1d33c76 100644 --- a/stylish-haskell.cabal +++ b/stylish-haskell.cabal @@ -39,7 +39,6 @@ Library Language.Haskell.Stylish.Step.SimpleAlign Language.Haskell.Stylish.Step.Imports Language.Haskell.Stylish.Step.LanguagePragmas - Language.Haskell.Stylish.Step.Records Language.Haskell.Stylish.Step.Tabs Language.Haskell.Stylish.Step.TrailingWhitespace Language.Haskell.Stylish.Step.UnicodeSyntax @@ -100,8 +99,6 @@ Test-suite stylish-haskell-tests Language.Haskell.Stylish.Step.Imports.Tests Language.Haskell.Stylish.Step.LanguagePragmas Language.Haskell.Stylish.Step.LanguagePragmas.Tests - Language.Haskell.Stylish.Step.Records - Language.Haskell.Stylish.Step.Records.Tests Language.Haskell.Stylish.Step.Tabs Language.Haskell.Stylish.Step.Tabs.Tests Language.Haskell.Stylish.Step.TrailingWhitespace diff --git a/tests/Language/Haskell/Stylish/Step/Records/Tests.hs b/tests/Language/Haskell/Stylish/Step/Records/Tests.hs deleted file mode 100644 index 5b24c2f..0000000 --- a/tests/Language/Haskell/Stylish/Step/Records/Tests.hs +++ /dev/null @@ -1,100 +0,0 @@ --------------------------------------------------------------------------------- -module Language.Haskell.Stylish.Step.Records.Tests - ( tests - ) where - - --------------------------------------------------------------------------------- -import Test.Framework (Test, testGroup) -import Test.Framework.Providers.HUnit (testCase) -import Test.HUnit (Assertion, (@=?)) - - --------------------------------------------------------------------------------- -import Language.Haskell.Stylish.Step.Records -import Language.Haskell.Stylish.Tests.Util - - --------------------------------------------------------------------------------- -tests :: Test -tests = testGroup "Language.Haskell.Stylish.Step.Records.Tests" - [ testCase "case 01" case01 - , testCase "case 02" case02 - , testCase "case 03" case03 - , testCase "case 04" case04 - ] - - --------------------------------------------------------------------------------- -case01 :: Assertion -case01 = expected @=? testStep (step 80) input - where - input = unlines - [ "data Foo = Foo" - , " { foo :: Int" - , " , barqux :: String" - , " } deriving (Show)" - ] - - expected = unlines - [ "data Foo = Foo" - , " { foo :: Int" - , " , barqux :: String" - , " } deriving (Show)" - ] - - --------------------------------------------------------------------------------- -case02 :: Assertion -case02 = input @=? testStep (step 80) input - where - -- Don't attempt to align this since a field spans multiple lines - input = unlines - [ "data Foo = Foo" - , " { foo :: Int" - , " , barqux" - , " :: String" - , " } deriving (Show)" - ] - - --------------------------------------------------------------------------------- -case03 :: Assertion -case03 = - -- 22 max columns is /just/ enough to align this stuff. - expected @=? testStep (step 22) input - where - input = unlines - [ "data Foo = Foo" - , " { foo :: String" - , " , barqux :: Int" - , " }" - ] - - expected = unlines - [ "data Foo = Foo" - , " { foo :: String" - , " , barqux :: Int" - , " }" - ] - - --------------------------------------------------------------------------------- -case04 :: Assertion -case04 = - -- 21 max columns is /just NOT/ enough to align this stuff. - expected @=? testStep (step 21) input - where - input = unlines - [ "data Foo = Foo" - , " { foo :: String" - , " , barqux :: Int" - , " }" - ] - - expected = unlines - [ "data Foo = Foo" - , " { foo :: String" - , " , barqux :: Int" - , " }" - ] diff --git a/tests/Language/Haskell/Stylish/Step/SimpleAlign/Tests.hs b/tests/Language/Haskell/Stylish/Step/SimpleAlign/Tests.hs index b36f5d9..a57e6e9 100644 --- a/tests/Language/Haskell/Stylish/Step/SimpleAlign/Tests.hs +++ b/tests/Language/Haskell/Stylish/Step/SimpleAlign/Tests.hs @@ -5,9 +5,9 @@ module Language.Haskell.Stylish.Step.SimpleAlign.Tests -------------------------------------------------------------------------------- -import Test.Framework (Test, testGroup) -import Test.Framework.Providers.HUnit (testCase) -import Test.HUnit (Assertion, (@=?)) +import Test.Framework (Test, testGroup) +import Test.Framework.Providers.HUnit (testCase) +import Test.HUnit (Assertion, (@=?)) -------------------------------------------------------------------------------- @@ -21,6 +21,10 @@ tests = testGroup "Language.Haskell.Stylish.Step.SimpleAlign.Tests" [ testCase "case 01" case01 , testCase "case 02" case02 , testCase "case 03" case03 + , testCase "case 04" case04 + , testCase "case 05" case05 + , testCase "case 06" case06 + , testCase "case 07" case07 ] @@ -69,3 +73,78 @@ case03 = expected @=? testStep (step 80 defaultConfig) input [ "heady def [] = def" , "heady _ (x : _) = x" ] + + +-------------------------------------------------------------------------------- +case04 :: Assertion +case04 = expected @=? testStep (step 80 defaultConfig) input + where + input = unlines + [ "data Foo = Foo" + , " { foo :: Int" + , " , barqux :: String" + , " } deriving (Show)" + ] + + expected = unlines + [ "data Foo = Foo" + , " { foo :: Int" + , " , barqux :: String" + , " } deriving (Show)" + ] + + +-------------------------------------------------------------------------------- +case05 :: Assertion +case05 = input @=? testStep (step 80 defaultConfig) input + where + -- Don't attempt to align this since a field spans multiple lines + input = unlines + [ "data Foo = Foo" + , " { foo :: Int" + , " , barqux" + , " :: String" + , " } deriving (Show)" + ] + + +-------------------------------------------------------------------------------- +case06 :: Assertion +case06 = + -- 22 max columns is /just/ enough to align this stuff. + expected @=? testStep (step 22 defaultConfig) input + where + input = unlines + [ "data Foo = Foo" + , " { foo :: String" + , " , barqux :: Int" + , " }" + ] + + expected = unlines + [ "data Foo = Foo" + , " { foo :: String" + , " , barqux :: Int" + , " }" + ] + + +-------------------------------------------------------------------------------- +case07 :: Assertion +case07 = + -- 21 max columns is /just NOT/ enough to align this stuff. + expected @=? testStep (step 21 defaultConfig) input + where + input = unlines + [ "data Foo = Foo" + , " { foo :: String" + , " , barqux :: Int" + , " }" + ] + + expected = unlines + [ "data Foo = Foo" + , " { foo :: String" + , " , barqux :: Int" + , " }" + ] diff --git a/tests/TestSuite.hs b/tests/TestSuite.hs index d739760..853126d 100644 --- a/tests/TestSuite.hs +++ b/tests/TestSuite.hs @@ -12,7 +12,6 @@ import Test.Framework (default import qualified Language.Haskell.Stylish.Parse.Tests import qualified Language.Haskell.Stylish.Step.Imports.Tests import qualified Language.Haskell.Stylish.Step.LanguagePragmas.Tests -import qualified Language.Haskell.Stylish.Step.Records.Tests import qualified Language.Haskell.Stylish.Step.SimpleAlign.Tests import qualified Language.Haskell.Stylish.Step.Tabs.Tests import qualified Language.Haskell.Stylish.Step.TrailingWhitespace.Tests @@ -25,7 +24,6 @@ main = defaultMain [ Language.Haskell.Stylish.Parse.Tests.tests , Language.Haskell.Stylish.Step.Imports.Tests.tests , Language.Haskell.Stylish.Step.LanguagePragmas.Tests.tests - , Language.Haskell.Stylish.Step.Records.Tests.tests , Language.Haskell.Stylish.Step.SimpleAlign.Tests.tests , Language.Haskell.Stylish.Step.Tabs.Tests.tests , Language.Haskell.Stylish.Step.TrailingWhitespace.Tests.tests |