Skip to content

Commit 78b6224

Browse files
committed
Fmt and fixes
1 parent fc1d332 commit 78b6224

File tree

6 files changed

+115
-120
lines changed

6 files changed

+115
-120
lines changed

Setup.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import Distribution.Simple
2+
23
main = defaultMain

app/Main.hs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@ module Main where
44

55
import Control.Monad
66
import Control.Monad.Trans.Maybe
7-
import Data.Attoparsec.Text
8-
import Data.List
97
import Data.Maybe
10-
import qualified Data.Text as T
11-
import qualified Data.Text.IO as T
12-
import Hie.Cabal.Parser
138
import Hie.Locate
149
import Hie.Yaml
1510
import System.Directory
16-
import System.Directory.Internal
17-
import System.FilePath.Posix
1811
import System.Environment
12+
import System.FilePath.Posix
1913

2014
main :: IO ()
2115
main = do
@@ -24,10 +18,11 @@ main = do
2418
cfs <- runMaybeT $ case name of
2519
"cabal" -> cabalPkgs pwd
2620
_ -> stackYamlPkgs pwd
27-
when (null cfs) $ error $
28-
"No .cabal files found under"
29-
<> pwd
30-
<> "\n You may need to run stack build."
21+
when (null cfs) $
22+
error $
23+
"No .cabal files found under"
24+
<> pwd
25+
<> "\n You may need to run stack build."
3126
pkgs <- catMaybes <$> mapM (nestedPkg pwd) (concat cfs)
3227
putStr <$> hieYaml name $ fmtPkgs name pkgs
3328

@@ -37,12 +32,12 @@ resolveName pwd = do
3732
files <- listDirectory pwd
3833
let fileNames = map takeFileName files
3934
name =
40-
if | "--cabal" `elem` args -> "cabal"
35+
if
36+
| "--cabal" `elem` args -> "cabal"
4137
| "--stack" `elem` args -> "stack"
4238
| "dist-newstyle" `elem` fileNames -> "cabal"
4339
| ".stack-work" `elem` fileNames -> "stack"
4440
| "cabal.project" `elem` fileNames -> "cabal"
4541
| "stack.yaml" `elem` fileNames -> "stack"
4642
| otherwise -> "cabal"
4743
return name
48-

src/Hie/Cabal/Parser.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ optSkipToNextLine i = do
102102
skipMany $ satisfy (\c -> isSpace c && not (isEndOfLine c))
103103
mChar <- peekChar
104104
case mChar of
105-
Just c | isEndOfLine c ->
106-
char c *> indent i $> ()
105+
Just c
106+
| isEndOfLine c ->
107+
char c *> indent i $> ()
107108
_ -> pure ()
108109

109110
-- | Comma or space separated list, with optional new lines.

src/Hie/Locate.hs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ module Hie.Locate
88
)
99
where
1010

11-
import Control.Applicative
1211
import Control.Exception
13-
import Control.Monad
1412
import Control.Monad.IO.Class
1513
import Control.Monad.Trans.Maybe
1614
import Data.Attoparsec.Text (parseOnly)
@@ -20,9 +18,7 @@ import Data.Maybe
2018
import qualified Data.Text as T
2119
import qualified Data.Text.IO as T
2220
import Data.Yaml
23-
import GHC.Generics
2421
import Hie.Cabal.Parser
25-
import Hie.Yaml
2622
import System.Directory
2723
import System.FilePath.Posix
2824
import System.FilePattern.Directory (getDirectoryFiles)
@@ -35,22 +31,24 @@ instance FromJSON Pkgs where
3531
parseJSON _ = fail "could not read packages from stack.yaml"
3632

3733
stackYamlPkgs :: FilePath -> MaybeT IO [FilePath]
38-
stackYamlPkgs p = liftIO $
39-
decodeFileEither (p </> "stack.yaml") >>= \case
40-
Right (Pkgs f) ->
41-
liftIO $
42-
map (p </>)
43-
<$> getDirectoryFiles p (map (</> "*.cabal") f)
44-
Left e -> fail $ show e
34+
stackYamlPkgs p =
35+
liftIO $
36+
decodeFileEither (p </> "stack.yaml") >>= \case
37+
Right (Pkgs f) ->
38+
liftIO $
39+
map (p </>)
40+
<$> getDirectoryFiles p (map (</> "*.cabal") f)
41+
Left e -> fail $ show e
4542

4643
cabalPkgs :: FilePath -> MaybeT IO [FilePath]
4744
cabalPkgs p = do
4845
cp <- cabalP "cabal.project"
4946
cl <- cabalP "cabal.project.local"
5047
case concat . rights $ map (parseOnly extractPkgs) $ rights [cp, cl] of
51-
[] -> liftIO (cfs p) >>= \case
52-
[] -> fail "no cabal files found"
53-
h : _ -> pure [p </> h]
48+
[] ->
49+
liftIO (cfs p) >>= \case
50+
[] -> fail "no cabal files found"
51+
h : _ -> pure [p </> h]
5452
xs -> do
5553
cd <- liftIO $ map (p </>) <$> getDirectoryFiles p (map (matchDirs . T.unpack) xs)
5654
cf <-
@@ -71,9 +69,10 @@ nestedPkg parrent child = do
7169
case parsePackage' f' of
7270
Right (Package n cs) -> do
7371
let dir =
74-
fromJust $ stripPrefix (splitDirectories parrent)
75-
$ splitDirectories
76-
$ fst (splitFileName child)
72+
fromJust $
73+
stripPrefix (splitDirectories parrent) $
74+
splitDirectories $
75+
fst (splitFileName child)
7776
pkg =
7877
Package n $
7978
map

stack.yaml.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
packages: []
77
snapshots:
88
- completed:
9-
size: 492027
10-
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/15/9.yaml
11-
sha256: 11394dc975e96c2fea90f7f2b3229630d46351a092ebcec78f0a56403930b429
12-
original: lts-15.9
9+
size: 496111
10+
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/15/14.yaml
11+
sha256: c442d702d66b8c129b3473a32c609050aabf1dc2f8e8502402c143271a8fb141
12+
original: lts-15.14

test/Spec.hs

Lines changed: 83 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{-# LANGUAGE OverloadedStrings #-}
22

33
import Data.Attoparsec.Text
4-
import qualified Data.Text as T
54
import Data.Text (Text)
65
import qualified Data.Text.IO as T
76
import Hie.Cabal.Parser
@@ -14,89 +13,89 @@ main = hspec spec
1413

1514
spec :: Spec
1615
spec = do
17-
describe "Should Succeed"
18-
$ it "successfully parses executable section"
19-
$ exeSection ~> parseExe 0
20-
`shouldParse` [Comp Exe "gen-hie" "app/Main.hs"]
21-
describe "Should Succeed"
22-
$ it "successfully parses test section"
23-
$ testSection ~> parseTestSuite 0
24-
`shouldParse` [Comp Test "implicit-hie-test" "test"]
25-
describe "Should Succeed"
26-
$ it "successfully parses library section"
27-
$ libSection ~> parseLib 0
28-
`shouldParse` [Comp Lib "" "src"]
29-
describe "Should Succeed"
30-
$ it "successfully parses library section with 2 hs-source-dirs"
31-
$ libSection2 ~> parseLib 0
32-
`shouldParse` [Comp Lib "" "src", Comp Lib "" "src2"]
33-
describe "Should Succeed"
34-
$ it "successfully parses library section with 2 paths under hs-source-dirs"
35-
$ libSection3 ~> parseLib 0
36-
`shouldParse` [Comp Lib "" "src", Comp Lib "" "src2"]
37-
describe "Should Succeed"
38-
$ it "successfully parses bench section"
39-
$ do
40-
bs <- T.readFile "test/benchSection"
41-
bs ~> parseBench 0
42-
`shouldParse` [Comp Bench "folds" "benchmarks/folds.hs"]
43-
describe "Should Succeed"
44-
$ it "successfully parses package"
45-
$ do
46-
cf <- T.readFile "implicit-hie.cabal"
47-
cf ~> parsePackage
48-
`shouldParse` Package
49-
"implicit-hie"
50-
[ Comp Lib "" "src",
51-
Comp Exe "gen-hie" "app/Main.hs",
52-
Comp Test "implicit-hie-test" "test"
53-
]
54-
describe "Should Succeed"
55-
$ it
16+
describe "Should Succeed" $
17+
it "successfully parses executable section" $
18+
exeSection ~> parseExe 0
19+
`shouldParse` [Comp Exe "gen-hie" "app/Main.hs"]
20+
describe "Should Succeed" $
21+
it "successfully parses test section" $
22+
testSection ~> parseTestSuite 0
23+
`shouldParse` [Comp Test "implicit-hie-test" "test"]
24+
describe "Should Succeed" $
25+
it "successfully parses library section" $
26+
libSection ~> parseLib 0
27+
`shouldParse` [Comp Lib "" "src"]
28+
describe "Should Succeed" $
29+
it "successfully parses library section with 2 hs-source-dirs" $
30+
libSection2 ~> parseLib 0
31+
`shouldParse` [Comp Lib "" "src", Comp Lib "" "src2"]
32+
describe "Should Succeed" $
33+
it "successfully parses library section with 2 paths under hs-source-dirs" $
34+
libSection3 ~> parseLib 0
35+
`shouldParse` [Comp Lib "" "src", Comp Lib "" "src2"]
36+
describe "Should Succeed" $
37+
it "successfully parses bench section" $
38+
do
39+
bs <- T.readFile "test/benchSection"
40+
bs ~> parseBench 0
41+
`shouldParse` [Comp Bench "folds" "benchmarks/folds.hs"]
42+
describe "Should Succeed" $
43+
it "successfully parses package" $
44+
do
45+
cf <- T.readFile "implicit-hie.cabal"
46+
cf ~> parsePackage
47+
`shouldParse` Package
48+
"implicit-hie"
49+
[ Comp Lib "" "src",
50+
Comp Exe "gen-hie" "app/Main.hs",
51+
Comp Test "implicit-hie-test" "test"
52+
]
53+
describe "Should Succeed" $
54+
it
5655
"skips to end of block section"
57-
$ let r = "test\n"
58-
in (libSection <> r) ~?> parseLib 0
59-
`leavesUnconsumed` r
60-
describe "Should Succeed"
61-
$ it "successfully generates stack hie.yaml"
62-
$ do
63-
sf <- readFile "test/stackHie.yaml"
64-
cf <- T.readFile "implicit-hie.cabal"
65-
(hieYaml "stack" . fmtPkgs "stack" . (: []) <$> parseOnly parsePackage cf)
66-
`shouldBe` Right sf
67-
describe "Should Succeed"
68-
$ it "successfully generates cabal hie.yaml for haskell-language-server"
69-
$ do
70-
f <- T.readFile "test/haskell-language-server-cabal"
71-
o <- readFile "test/hie.yaml.cbl"
72-
(hieYaml "cabal" . fmtPkgs "cabal" . (: []) <$> parseOnly parsePackage f)
73-
`shouldBe` Right o
74-
describe "Should Succeed"
75-
$ it "successfully parses comma list"
76-
$ ("one, two" :: Text) ~> parseList 1 `shouldParse` ["one", "two"]
77-
describe "Should Succeed"
78-
$ it "successfully parses newline list"
79-
$ ("one\n two \n three3" :: Text) ~> parseList 1
80-
`shouldParse` ["one", "two", "three3"]
81-
describe "Should Succeed"
82-
$ it "successfully parses newline comma list"
83-
$ ("one\n two, three3" :: Text) ~> parseList 1
84-
`shouldParse` ["one", "two", "three3"]
85-
describe "Should Succeed"
86-
$ it "quoted list"
87-
$ ("\"one\"\n two\n three3" :: Text) ~> parseList 1
88-
`shouldParse` ["one", "two", "three3"]
89-
describe "Should Succeed"
90-
$ it "list with leading commas"
91-
$ ("one\n , two\n , three3" :: Text) ~> parseList 1
92-
`shouldParse` ["one", "two", "three3"]
93-
describe "Should Succeed"
94-
$ it "succesfully parses exe component with other-modules containing dots"
95-
$ exeSection2 ~> parseExe 0
96-
`shouldParse` [ Comp Exe "gen-hie" "app/Main.hs"
97-
, Comp Exe "gen-hie" "app/Hie/Executable/Helper.hs"
98-
, Comp Exe "gen-hie" "app/Hie/Executable/Utils.hs"
99-
]
56+
$ let r = "test\n"
57+
in (libSection <> r) ~?> parseLib 0
58+
`leavesUnconsumed` r
59+
describe "Should Succeed" $
60+
it "successfully generates stack hie.yaml" $
61+
do
62+
sf <- readFile "test/stackHie.yaml"
63+
cf <- T.readFile "implicit-hie.cabal"
64+
(hieYaml "stack" . fmtPkgs "stack" . (: []) <$> parseOnly parsePackage cf)
65+
`shouldBe` Right sf
66+
describe "Should Succeed" $
67+
it "successfully generates cabal hie.yaml for haskell-language-server" $
68+
do
69+
f <- T.readFile "test/haskell-language-server-cabal"
70+
o <- readFile "test/hie.yaml.cbl"
71+
(hieYaml "cabal" . fmtPkgs "cabal" . (: []) <$> parseOnly parsePackage f)
72+
`shouldBe` Right o
73+
describe "Should Succeed" $
74+
it "successfully parses comma list" $
75+
("one, two" :: Text) ~> parseList 1 `shouldParse` ["one", "two"]
76+
describe "Should Succeed" $
77+
it "successfully parses newline list" $
78+
("one\n two \n three3" :: Text) ~> parseList 1
79+
`shouldParse` ["one", "two", "three3"]
80+
describe "Should Succeed" $
81+
it "successfully parses newline comma list" $
82+
("one\n two, three3" :: Text) ~> parseList 1
83+
`shouldParse` ["one", "two", "three3"]
84+
describe "Should Succeed" $
85+
it "quoted list" $
86+
("\"one\"\n two\n three3" :: Text) ~> parseList 1
87+
`shouldParse` ["one", "two", "three3"]
88+
describe "Should Succeed" $
89+
it "list with leading commas" $
90+
("one\n , two\n , three3" :: Text) ~> parseList 1
91+
`shouldParse` ["one", "two", "three3"]
92+
describe "Should Succeed" $
93+
it "succesfully parses exe component with other-modules containing dots" $
94+
exeSection2 ~> parseExe 0
95+
`shouldParse` [ Comp Exe "gen-hie" "app/Main.hs",
96+
Comp Exe "gen-hie" "app/Hie/Executable/Helper.hs",
97+
Comp Exe "gen-hie" "app/Hie/Executable/Utils.hs"
98+
]
10099

101100
exeSection :: Text
102101
exeSection =
@@ -191,4 +190,4 @@ exeSection2 =
191190
\ Hie.Executable.Utils\n\
192191
\ hs-source-dirs:\n\
193192
\ app\n\
194-
\ main-is: Main.hs \n"
193+
\ main-is: Main.hs \n"

0 commit comments

Comments
 (0)