Skip to content

Commit 0026002

Browse files
committed
Fix cabal + named libraries
1 parent 6ae40b0 commit 0026002

File tree

4 files changed

+43
-37
lines changed

4 files changed

+43
-37
lines changed

hie.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
cradle:
2-
stack:
2+
cabal:
33
- path: "src"
4-
component: "implicit-hie:lib"
5-
- path: "app"
4+
component: "lib:implicit-hie"
5+
- path: "app/Main.hs"
66
component: "implicit-hie:exe:gen-hie"
77
- path: "test"
88
component: "implicit-hie:test:implicit-hie-test"

src/Hie/Cabal/Parser.hs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@ type Indent = Int
1818
data Package = Package Name [Component]
1919
deriving (Show, Eq, Ord)
2020

21-
data Component = Lib Path | Exe Name Path | Test Name Path
21+
data Component = Lib Name Path | Exe Name Path | Test Name Path
2222
deriving (Show, Eq, Ord)
2323

24-
parseName :: Parser Text
25-
parseName = "name" >> skipSpace >> char ':' >> parseString
26-
2724
parsePackage :: Parser Package
2825
parsePackage =
2926
( do
30-
n <- parseName
27+
n <- field 0 "name"
3128
(Package _ t) <- parsePackage
3229
pure $ Package n t
3330
)
@@ -39,39 +36,38 @@ parsePackage =
3936
<|> (skipToNextLine >> parsePackage)
4037
<|> pure (Package "" [])
4138

39+
component :: Indent -> Text -> Parser Name
40+
component i t = do
41+
indent i
42+
_ <- asciiCI t
43+
skipMany tabOrSpace
44+
n <- parseString <|> pure ""
45+
skipToNextLine
46+
pure n
47+
4248
parseComponent :: Indent -> Parser Component
4349
parseComponent i =
4450
parseLib i
4551
<|> parseExe i
4652
<|> parseNamed i "test-suite" Test
4753

4854
parseLib :: Indent -> Parser Component
49-
parseLib i =
50-
indent i
51-
>> asciiCI "library"
52-
>> skipToNextLine
53-
>> Lib <$> extractPath (i + 1)
55+
parseLib i = do
56+
n <- component i "library"
57+
Lib n <$> extractPath (i + 1)
5458

5559
parseQuoted :: Parser Text
5660
parseQuoted = do
5761
q <- char '"' <|> char '\''
5862
takeTill (== q)
5963

6064
parseString :: Parser Name
61-
parseString = do
62-
skipSpace
63-
parseQuoted <|> takeWhile1 (not . (\c -> isSpace c || c == ','))
65+
parseString = parseQuoted <|> takeWhile1 (not . (\c -> isSpace c || c == ','))
6466

6567
parseExe :: Indent -> Parser Component
66-
parseExe i =
67-
do
68-
indent i
69-
_ <- asciiCI "executable"
70-
_ <- skipSpace
71-
n <- parseString <?> "Exe Name"
72-
skipToNextLine
73-
Exe n <$> pathMain (i + 1) "." ""
74-
<?> T.unpack "parseExe"
68+
parseExe i = do
69+
n <- component i "executable"
70+
Exe n <$> pathMain (i + 1) "." ""
7571

7672
pathMain :: Indent -> Text -> Text -> Parser Text
7773
pathMain i p m =
@@ -85,7 +81,7 @@ parseNamed i compType compCon =
8581
do
8682
indent i
8783
_ <- asciiCI compType <?> "asciiCI " <> T.unpack compType
88-
_ <- skipSpace <?> "skipSpace"
84+
skipMany tabOrSpace
8985
n <- parseString <?> "N"
9086
skipToNextLine
9187
compCon n <$> extractPath (i + 1)
@@ -113,6 +109,7 @@ field i f =
113109
_ <- asciiCI f
114110
skipSpace
115111
_ <- char ':'
112+
skipSpace
116113
p <- parseString
117114
skipToNextLine
118115
pure p

src/Hie/Yaml.hs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ indentT :: T.Text -> T.Text
2323
indentT = T.unlines . map (" " <>) . T.lines
2424

2525
cabalComponent :: Name -> Component -> T.Text
26-
cabalComponent n (Lib p) = component p $ "lib:" <> n
27-
cabalComponent _ (Exe p cn) = component p $ "exe:" <> cn
28-
cabalComponent _ (Test p cn) = component p $ "test:" <> cn
26+
cabalComponent n (Lib "" p) = comp p $ "lib:" <> n
27+
cabalComponent n (Lib cn p) = comp p $ "lib:" <> n <> ":" <> cn
28+
cabalComponent n (Exe cn p) = comp p $ n <> ":exe:" <> cn
29+
cabalComponent n (Test cn p) = comp p $ n <> ":test:" <> cn
2930

3031
stackComponent :: Name -> Component -> T.Text
31-
stackComponent n (Lib p) = component p $ n <> ":lib"
32-
stackComponent n (Exe cn p) = component p $ n <> ":exe:" <> cn
33-
stackComponent n (Test cn p) = component p $ n <> ":test:" <> cn
32+
stackComponent n (Lib "" p) = comp p $ n <> ":lib"
33+
stackComponent n (Lib cn p) = comp p $ n <> ":lib:" <> cn
34+
stackComponent n (Exe cn p) = comp p $ n <> ":exe:" <> cn
35+
stackComponent n (Test cn p) = comp p $ n <> ":test:" <> cn
3436

35-
component :: T.Text -> T.Text -> T.Text
36-
component p c =
37+
comp :: T.Text -> T.Text -> T.Text
38+
comp p c =
3739
"- path: "
3840
<> dQuote p
3941
<> "\n "

test/Spec.hs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import Data.Attoparsec.Text
44
import qualified Data.Text as T
55
import Data.Text (Text)
6+
import qualified Data.Text.IO as T
67
import Hie.Cabal.Parser
78
import Hie.Yaml
89
import Test.Hspec
@@ -24,13 +25,13 @@ spec = do
2425
describe "Should Succeed"
2526
$ it "successfully parses library section"
2627
$ libSection ~> parseLib 0
27-
`shouldParse` Lib "src"
28+
`shouldParse` Lib "" "src"
2829
describe "Should Succeed"
2930
$ it "successfully parses package"
3031
$ fullFile ~> parsePackage
3132
`shouldParse` Package
3233
"implicit-hie"
33-
[ Lib "src",
34+
[ Lib "" "src",
3435
Exe "implicit-hie-exe" "app/Main.hs",
3536
Test "implicit-hie-test" "test"
3637
]
@@ -43,6 +44,12 @@ spec = do
4344
describe "Should Succeed"
4445
$ it "successfully generates stack hie.yaml"
4546
$ (stackHieYaml <$> parseOnly parsePackage fullFile) `shouldBe` Right stackHie
47+
describe "Should Succeed"
48+
$ it "successfully generates stack hie.yaml"
49+
$ do
50+
f <- T.readFile "test/haskell-language-server-cabal"
51+
o <- T.readFile "test/hie.yaml.cbl"
52+
(cabalHieYaml <$> parseOnly parsePackage f) `shouldBe` Right o
4653

4754
fullFile :: Text
4855
fullFile = "name: implicit-hie\n" <> libSection <> exeSection <> testSection
@@ -78,7 +85,7 @@ testSection =
7885

7986
libSection :: Text
8087
libSection =
81-
"library\
88+
"library\n\
8289
\ exposed-modules:\n\
8390
\ Lib\n\
8491
\ other-modules:\n\

0 commit comments

Comments
 (0)