Skip to content

Commit b501e5c

Browse files
Expose "format" function in Sylish.hs (#259)
* Expose "format" function in Sylish.hs It's going to be needed for the haskell-ide integration * Update tests/Language/Haskell/StylishSpec.hs Co-Authored-By: Jasper Van der Jeugt <[email protected]> * Remove empty line Co-authored-by: Jasper Van der Jeugt <[email protected]>
1 parent 5eb4902 commit b501e5c

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

lib/Language/Haskell/Stylish.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ module Language.Haskell.Stylish
1616
-- * Misc
1717
, module Language.Haskell.Stylish.Verbose
1818
, version
19+
, format
20+
, ConfigPath(..)
1921
, Lines
2022
, Step
2123
) where
@@ -91,3 +93,13 @@ runStep exts mfp ls step =
9193
runSteps :: Extensions -> Maybe FilePath -> [Step] -> Lines
9294
-> Either String Lines
9395
runSteps exts mfp steps ls = foldM (runStep exts mfp) ls steps
96+
97+
newtype ConfigPath = ConfigPath { unConfigPath :: FilePath }
98+
99+
-- |Formats given contents optionally using the config provided as first param.
100+
-- The second file path is the location from which the contents were read.
101+
-- If provided, it's going to be printed out in the error message.
102+
format :: Maybe ConfigPath -> Maybe FilePath -> String -> IO (Either String Lines)
103+
format maybeConfigPath maybeFilePath contents = do
104+
conf <- loadConfig (makeVerbose True) (fmap unConfigPath maybeConfigPath)
105+
pure $ runSteps (configLanguageExtensions conf) maybeFilePath (configSteps conf) $ lines contents

stylish-haskell.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Test-suite stylish-haskell-tests
9696
Type: exitcode-stdio-1.0
9797

9898
Other-modules:
99+
Language.Haskell.StylishSpec
99100
Language.Haskell.Stylish.Align
100101
Language.Haskell.Stylish.Block
101102
Language.Haskell.Stylish.Config

testdata/test-config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
steps:
2+
- records: {}
3+
indent: 2

tests/Language/Haskell/StylishSpec.hs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module Language.Haskell.StylishSpec where
2+
3+
--------------------------------------------------------------------------------
4+
import Test.Framework (Test, testGroup)
5+
import Test.Framework.Providers.HUnit (testCase)
6+
import Test.HUnit (Assertion, (@?=))
7+
8+
--------------------------------------------------------------------------------
9+
import Language.Haskell.Stylish
10+
11+
--------------------------------------------------------------------------------
12+
import System.IO.Unsafe
13+
--------------------------------------------------------------------------------
14+
tests :: Test
15+
tests = testGroup "Language.Haskell.Stylish.Step.Tabs.Tests"
16+
[ testCase "case 01" case01
17+
, testCase "case 02" case02
18+
, testCase "case 03" case03
19+
]
20+
21+
--------------------------------------------------------------------------------
22+
case01 :: Assertion
23+
case01 = (@?=) result (unsafePerformIO $ format Nothing Nothing input)
24+
where
25+
input = "module Herp where\n data Foo = Bar | Baz"
26+
result = Right [ "module Herp where"
27+
, "data Foo = Bar"
28+
, " | Baz"
29+
]
30+
31+
case02 :: Assertion
32+
case02 = (@?=) result (unsafePerformIO $ format (Just configLocation) Nothing input)
33+
where
34+
configLocation = ConfigPath "testdata/test-config.yaml"
35+
input = "module Herp where\n data Foo = Bar | Baz"
36+
result = Right [ "module Herp where"
37+
, "data Foo = Bar"
38+
, " | Baz"
39+
]
40+
41+
case03 :: Assertion
42+
case03 = do
43+
actual <- format Nothing (Just fileLocation) input
44+
actual @?= result
45+
where
46+
fileLocation = "directory/File.hs"
47+
input = "module Herp"
48+
result = Left $
49+
"Language.Haskell.Stylish.Parse.parseModule: could not parse " <>
50+
fileLocation <>
51+
": ParseFailed (SrcLoc \"<unknown>.hs\" 2 1) \"Parse error: EOF\""

tests/TestSuite.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ import qualified Language.Haskell.Stylish.Step.Squash.Tests
1919
import qualified Language.Haskell.Stylish.Step.Tabs.Tests
2020
import qualified Language.Haskell.Stylish.Step.TrailingWhitespace.Tests
2121
import qualified Language.Haskell.Stylish.Step.UnicodeSyntax.Tests
22+
import qualified Language.Haskell.StylishSpec
2223

2324

2425
--------------------------------------------------------------------------------
2526
main :: IO ()
2627
main = defaultMain
27-
[ Language.Haskell.Stylish.Parse.Tests.tests
28+
[ Language.Haskell.StylishSpec.tests
29+
, Language.Haskell.Stylish.Parse.Tests.tests
2830
, Language.Haskell.Stylish.Config.Tests.tests
2931
, Language.Haskell.Stylish.Step.Data.Tests.tests
3032
, Language.Haskell.Stylish.Step.Imports.Tests.tests

0 commit comments

Comments
 (0)