summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSvyatoslav Gryaznov <nightuser@ya.ru>2016-07-05 16:45:29 +0300
committerSvyatoslav Gryaznov <nightuser@ya.ru>2016-07-05 16:45:29 +0300
commit637872f6ea4a37d2a4162c95d7cb0304efc41d19 (patch)
tree8e4fb29fa1537392bc4a41405c253d538b65bb95
parentfd5e7df4896845a113d03cefac9a710d2252aa19 (diff)
downloadstylish-haskell-637872f6ea4a37d2a4162c95d7cb0304efc41d19.tar.gz
Add newline format option
-rw-r--r--data/stylish-haskell.yaml3
-rw-r--r--lib/Language/Haskell/Stylish/Config.hs10
-rw-r--r--src/Main.hs7
3 files changed, 19 insertions, 1 deletions
diff --git a/data/stylish-haskell.yaml b/data/stylish-haskell.yaml
index fb12606..f74b00d 100644
--- a/data/stylish-haskell.yaml
+++ b/data/stylish-haskell.yaml
@@ -144,6 +144,9 @@ steps:
# to. Different steps take this into account. Default: 80.
columns: 80
+# Newline format for output files
+newline: native
+
# Sometimes, language extensions are specified in a cabal file or from the
# command line instead of using language pragmas in the file. stylish-haskell
# needs to be aware of these, so it can parse the file correctly.
diff --git a/lib/Language/Haskell/Stylish/Config.hs b/lib/Language/Haskell/Stylish/Config.hs
index fee7594..d921b09 100644
--- a/lib/Language/Haskell/Stylish/Config.hs
+++ b/lib/Language/Haskell/Stylish/Config.hs
@@ -24,6 +24,8 @@ import System.Directory
import System.FilePath (joinPath,
splitPath,
(</>))
+import qualified System.IO as IO (Newline (..),
+ nativeNewline)
--------------------------------------------------------------------------------
@@ -47,6 +49,7 @@ data Config = Config
{ configSteps :: [Step]
, configColumns :: Int
, configLanguageExtensions :: [String]
+ , configNewline :: IO.Newline
}
@@ -115,11 +118,18 @@ parseConfig (A.Object o) = do
<$> pure []
<*> (o A..:? "columns" A..!= 80)
<*> (o A..:? "language_extensions" A..!= [])
+ <*> (o A..:? "newline" >>= parseEnum newlines IO.nativeNewline)
-- Then fill in the steps based on the partial config we already have
stepValues <- o A..: "steps" :: A.Parser [A.Value]
steps <- mapM (parseSteps config) stepValues
return config {configSteps = concat steps}
+ where
+ newlines =
+ [ ("native", IO.nativeNewline)
+ , ("lf", IO.LF)
+ , ("crlf", IO.CRLF)
+ ]
parseConfig _ = mzero
diff --git a/src/Main.hs b/src/Main.hs
index e64b291..fdaf0de 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -123,7 +123,12 @@ file sa conf mfp = do
write old new = case mfp of
Nothing -> putStr new
Just _ | not (saInPlace sa) -> putStr new
- Just path | not (null new) && old /= new -> writeFile path new
+ Just path | not (null new) && old /= new ->
+ IO.withFile path IO.WriteMode $ \h -> do
+ let nl = configNewline conf
+ let mode = IO.NewlineMode nl nl
+ IO.hSetNewlineMode h mode
+ IO.hPutStr h new
_ -> return ()
readUTF8File :: FilePath -> IO String