diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-06-06 19:51:19 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-06-06 19:51:19 +0200 |
commit | 9ae500c6d51325251db2198c0b36183a132f226a (patch) | |
tree | e1b31415c57125b8f8798ad944159e0eddbb2067 | |
parent | e0fd7da365624f8f80237b0fdce13b8c21ac1d27 (diff) | |
download | stylish-haskell-9ae500c6d51325251db2198c0b36183a132f226a.tar.gz |
Add a add_language_pragma option to UnicodeSyntax
-rw-r--r-- | .stylish-haskell.yaml | 14 | ||||
-rw-r--r-- | src/StylishHaskell/Config.hs | 3 | ||||
-rw-r--r-- | src/StylishHaskell/Step/LanguagePragmas.hs | 6 | ||||
-rw-r--r-- | src/StylishHaskell/Step/UnicodeSyntax.hs | 11 | ||||
-rw-r--r-- | tests/StylishHaskell/Step/UnicodeSyntax/Tests.hs | 7 |
5 files changed, 26 insertions, 15 deletions
diff --git a/.stylish-haskell.yaml b/.stylish-haskell.yaml index 288d307..bfa8595 100644 --- a/.stylish-haskell.yaml +++ b/.stylish-haskell.yaml @@ -1,7 +1,19 @@ +# stylish-haskell configuration file +# ================================== +# +# The stylish-haskell tool is mainly configured by specifying steps. These steps +# are a list, so they have an order, and one specific step may appear more than +# once (if needed). Each file is processed by these steps in the given order. steps: # Convert some ASCII sequences to their Unicode equivalents. This is disabled # by default. - # - unicode_syntax: {} + # - unicode_syntax: + # # In order to make this work, we also need to insert the UnicodeSyntax + # # language pragma. If this flag is set to true, we insert it when it's + # # not already present. You may want to disable it if you configure + # # language extensions using some other method than pragmas. Default: + # # true. + # add_language_pragma: true # Import cleanup - imports: diff --git a/src/StylishHaskell/Config.hs b/src/StylishHaskell/Config.hs index a2daaab..f305cc3 100644 --- a/src/StylishHaskell/Config.hs +++ b/src/StylishHaskell/Config.hs @@ -171,4 +171,5 @@ parseTrailingWhitespace _ = return TrailingWhitespace.step -------------------------------------------------------------------------------- parseUnicodeSyntax :: A.Object -> A.Parser Step -parseUnicodeSyntax _ = return UnicodeSyntax.step +parseUnicodeSyntax o = UnicodeSyntax.step + <$> o A..:? "add_language_pragma" A..!= True diff --git a/src/StylishHaskell/Step/LanguagePragmas.hs b/src/StylishHaskell/Step/LanguagePragmas.hs index ed6d3b5..1f3ccb2 100644 --- a/src/StylishHaskell/Step/LanguagePragmas.hs +++ b/src/StylishHaskell/Step/LanguagePragmas.hs @@ -88,9 +88,9 @@ step' style removeRedundant ls (module', _) -------------------------------------------------------------------------------- -- | Add a LANGUAGE pragma to a module if it is not present already. addLanguagePragma :: String -> H.Module H.SrcSpanInfo -> [Change String] -addLanguagePragma pragma modu - | pragma `elem` present = [] - | otherwise = [insert line ["{-# LANGUAGE " ++ pragma ++ "#-}"]] +addLanguagePragma prag modu + | prag `elem` present = [] + | otherwise = [insert line ["{-# LANGUAGE " ++ prag ++ " #-}"]] where pragmas' = pragmas (fmap linesFromSrcSpan modu) present = concatMap snd pragmas' diff --git a/src/StylishHaskell/Step/UnicodeSyntax.hs b/src/StylishHaskell/Step/UnicodeSyntax.hs index a43e4f5..c3290c7 100644 --- a/src/StylishHaskell/Step/UnicodeSyntax.hs +++ b/src/StylishHaskell/Step/UnicodeSyntax.hs @@ -119,15 +119,16 @@ between (startRow, startCol) (endRow, endCol) needle = -------------------------------------------------------------------------------- -step :: Step -step = makeStep "UnicodeSyntax" step' +step :: Bool -> Step +step = makeStep "UnicodeSyntax" . step' -------------------------------------------------------------------------------- -step' :: Lines -> Module -> Lines -step' ls (module', _) = applyChanges changes ls +step' :: Bool -> Lines -> Module -> Lines +step' alp ls (module', _) = applyChanges changes ls where - changes = addLanguagePragma "UnicodeSyntax" module' ++ replaceAll perLine ls + changes = (if alp then addLanguagePragma "UnicodeSyntax" module' else []) ++ + replaceAll perLine ls perLine = sort $ groupPerLine $ typeSigs module' ls ++ contexts module' ls ++ diff --git a/tests/StylishHaskell/Step/UnicodeSyntax/Tests.hs b/tests/StylishHaskell/Step/UnicodeSyntax/Tests.hs index 9f53b58..f7bd80a 100644 --- a/tests/StylishHaskell/Step/UnicodeSyntax/Tests.hs +++ b/tests/StylishHaskell/Step/UnicodeSyntax/Tests.hs @@ -24,18 +24,15 @@ tests = testGroup "StylishHaskell.Step.UnicodeSyntax.Tests" -------------------------------------------------------------------------------- case01 :: Test -case01 = testCase "case 01" $ expected @=? testStep step input +case01 = testCase "case 01" $ expected @=? testStep (step True) input where input = unlines - [ "{-# LANGUAGE UnicodeSyntax #-}" -- TODO: remove - , "" - , "sort :: Ord a => [a] -> [a]" + [ "sort :: Ord a => [a] -> [a]" , "sort _ = []" ] expected = unlines [ "{-# LANGUAGE UnicodeSyntax #-}" - , "" , "sort ∷ Ord a ⇒ [a] → [a]" , "sort _ = []" ] |