Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configure options for ghc-options layout. #66

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cabal-fmt.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ library cabal-fmt-internal
CabalFmt.Fields
CabalFmt.Fields.BuildDepends
CabalFmt.Fields.Extensions
CabalFmt.Fields.GhcOptions
CabalFmt.Fields.Modules
CabalFmt.Fields.SourceFiles
CabalFmt.Fields.TestedWith
Expand All @@ -91,6 +92,15 @@ library cabal-fmt-internal
RankNTypes
ScopedTypeVariables

ghc-options:
-Wall
-Wcompat
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wmissing-export-lists
-Wmissing-home-modules
-Wredundant-constraints

executable cabal-fmt
default-language: Haskell2010
hs-source-dirs: cli
Expand Down
8 changes: 8 additions & 0 deletions cli/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ optsP = (,)
, indentP
, tabularP
, noTabularP
, ghcOptionsFlowP
, ghcOptionsVertP
, cabalFileP
, noCabalFileP
, stdoutP
Expand All @@ -110,6 +112,12 @@ optsP = (,)
noTabularP = O.flag' (mkOptionsMorphism $ \opts -> opts { optTabular = False })
$ O.long "no-tabular"

ghcOptionsFlowP = O.flag' (mkOptionsMorphism $ \opts -> opts { optFlow = True })
$ O.long "ghc-options-flow" <> O.help "ghc-options: flow options (default)"

ghcOptionsVertP = O.flag' (mkOptionsMorphism $ \opts -> opts { optFlow = False })
$ O.long "ghc-options-vert" <> O.help "ghc-options: one option per line"

cabalFileP = O.flag' (mkOptionsMorphism $ \opts -> opts { optCabalFile = True })
$ O.long "cabal-file"

Expand Down
43 changes: 43 additions & 0 deletions fixtures/ghc-options-flow.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
cabal-version: 3.4
name: simple-pkg
version: 0.1.0.0

common warnings
ghc-options:
-Wall
-Wtabs
-Wincomplete-uni-patterns
-Werror=dodgy-imports
-Werror=missing-home-modules
-Werror=star-is-type
-Werror=unused-imports
-eventlog
-O0
+RTS -A32M -RTS
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you know of a way to keep +RTS -A32M -RTS as one line while options prefixed with -W as one per line then that could be very nice.


library
import: warnings
exposed-modules: MyLib
build-depends: base ^>=4.17.0.0
hs-source-dirs: src
default-language: GHC2021

executable simple-pkg
import: warnings
main-is: Main.hs
build-depends:
base ^>=4.17.0.0,
simple-pkg

hs-source-dirs: app
default-language: GHC2021

test-suite simple-pkg-test
import: warnings
default-language: GHC2021
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Main.hs
build-depends:
base ^>=4.17.0.0,
simple-pkg
36 changes: 36 additions & 0 deletions fixtures/ghc-options-flow.format
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cabal-version: 3.4
name: simple-pkg
version: 0.1.0.0

common warnings
ghc-options:
-Wall -Wtabs -Wincomplete-uni-patterns -Werror=dodgy-imports
-Werror=missing-home-modules -Werror=star-is-type
-Werror=unused-imports -eventlog -O0 +RTS -A32M -RTS

library
import: warnings
exposed-modules: MyLib
build-depends: base ^>=4.17.0.0
hs-source-dirs: src
default-language: GHC2021

executable simple-pkg
import: warnings
main-is: Main.hs
build-depends:
, base ^>=4.17.0.0
, simple-pkg

hs-source-dirs: app
default-language: GHC2021

test-suite simple-pkg-test
import: warnings
default-language: GHC2021
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Main.hs
build-depends:
, base ^>=4.17.0.0
, simple-pkg
36 changes: 36 additions & 0 deletions fixtures/ghc-options-vert.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cabal-version: 3.4
name: simple-pkg
version: 0.1.0.0

common warnings
ghc-options:
-Wall -Wtabs -Wincomplete-uni-patterns -Werror=dodgy-imports
-Werror=missing-home-modules -Werror=star-is-type
-Werror=unused-imports -eventlog -O0 +RTS -A32M -RTS

library
import: warnings
exposed-modules: MyLib
build-depends: base ^>=4.17.0.0
hs-source-dirs: src
default-language: GHC2021

executable simple-pkg
import: warnings
main-is: Main.hs
build-depends:
base ^>=4.17.0.0,
simple-pkg

hs-source-dirs: app
default-language: GHC2021

test-suite simple-pkg-test
import: warnings
default-language: GHC2021
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Main.hs
build-depends:
base ^>=4.17.0.0,
simple-pkg
45 changes: 45 additions & 0 deletions fixtures/ghc-options-vert.format
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
cabal-version: 3.4
name: simple-pkg
version: 0.1.0.0

common warnings
ghc-options:
-Wall
-Wtabs
-Wincomplete-uni-patterns
-Werror=dodgy-imports
-Werror=missing-home-modules
-Werror=star-is-type
-Werror=unused-imports
-eventlog
-O0
+RTS
-A32M
-RTS

library
import: warnings
exposed-modules: MyLib
build-depends: base ^>=4.17.0.0
hs-source-dirs: src
default-language: GHC2021

executable simple-pkg
import: warnings
main-is: Main.hs
build-depends:
, base ^>=4.17.0.0
, simple-pkg

hs-source-dirs: app
default-language: GHC2021

test-suite simple-pkg-test
import: warnings
default-language: GHC2021
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Main.hs
build-depends:
, base ^>=4.17.0.0
, simple-pkg
2 changes: 2 additions & 0 deletions src/CabalFmt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import CabalFmt.Comments
import CabalFmt.Fields
import CabalFmt.Fields.BuildDepends
import CabalFmt.Fields.Extensions
import CabalFmt.Fields.GhcOptions
import CabalFmt.Fields.Modules
import CabalFmt.Fields.SourceFiles
import CabalFmt.Fields.TestedWith
Expand Down Expand Up @@ -125,6 +126,7 @@ fieldDescrs opts
<> otherModulesF
<> testedWithF opts
<> mconcat sourceFilesF
<> (if optFlow opts then mempty else ghcOptionsF)
<> coerceFieldDescrs C.packageDescriptionFieldGrammar
<> coerceFieldDescrs C.buildInfoFieldGrammar

Expand Down
22 changes: 22 additions & 0 deletions src/CabalFmt/Fields/GhcOptions.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- |
-- License: GPL-3.0-or-later
-- Copyright: Leo Zhang
{-# LANGUAGE OverloadedStrings #-}
module CabalFmt.Fields.GhcOptions (ghcOptionsF) where

import qualified Distribution.FieldGrammar as C
import qualified Distribution.Parsec as C
import qualified Distribution.Pretty as C
import qualified Text.PrettyPrint as PP

import CabalFmt.Fields
import CabalFmt.Prelude

ghcOptionsF :: FieldDescrs () ()
ghcOptionsF = singletonF "ghc-options" pretty parse

parse :: C.CabalParsing m => m [String]
parse = unpack' (C.alaList' C.NoCommaFSep C.Token') <$> C.parsec

pretty :: [String] -> PP.Doc
pretty = PP.vcat . map C.showToken
2 changes: 2 additions & 0 deletions src/CabalFmt/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ data Options = Options
{ optError :: !Bool
, optIndent :: !Int
, optTabular :: !Bool
, optFlow :: !Bool
, optCabalFile :: !Bool
, optSpecVersion :: !C.CabalSpecVersion
, optMode :: !Mode
Expand All @@ -34,6 +35,7 @@ defaultOptions = Options
{ optError = False
, optIndent = 2
, optTabular = True
, optFlow = True
, optCabalFile = True
, optSpecVersion = C.cabalSpecLatest
, optMode = ModeStdout
Expand Down
38 changes: 21 additions & 17 deletions tests/Golden.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,31 @@ import qualified Data.Map as Map

import CabalFmt (cabalFmt)
import CabalFmt.Monad (runCabalFmt)
import CabalFmt.Options (defaultOptions)
import CabalFmt.Options (Options(..), defaultOptions)
import CabalFmt.Prelude

main :: IO ()
main = defaultMain $ testGroup "tests"
[ goldenTest' "cabal-fmt"
, goldenTest' "Cabal"
, goldenTest' "Cabal-notab"
, goldenTest' "simple-example"
, goldenTest' "tree-diff"

, goldenTest' "fragment-missing"
, goldenTest' "fragment-empty"
, goldenTest' "fragment-wrong-field"
, goldenTest' "fragment-wrong-type"
, goldenTest' "fragment-multiple"
, goldenTest' "fragment-section"
[ go "cabal-fmt"
, go "Cabal"
, go "Cabal-notab"
, go "simple-example"
, go "tree-diff"
, go "ghc-options-flow"
, goldenTest' (defaultOptions{optFlow = False}) "ghc-options-vert"

, go "fragment-missing"
, go "fragment-empty"
, go "fragment-wrong-field"
, go "fragment-wrong-type"
, go "fragment-multiple"
, go "fragment-section"
]
where
go = goldenTest' defaultOptions

goldenTest' :: String -> TestTree
goldenTest' n = goldenTest n readGolden makeTest cmp writeGolden
goldenTest' :: Options -> String -> TestTree
goldenTest' options n = goldenTest n readGolden makeTest cmp writeGolden
where
goldenPath = "fixtures" </> n -<.> "format"
inputPath = "fixtures" </> n -<.> "cabal"
Expand All @@ -43,11 +47,11 @@ goldenTest' n = goldenTest n readGolden makeTest cmp writeGolden

makeTest = do
contents <- BS.readFile inputPath
case runCabalFmt files defaultOptions $ cabalFmt inputPath contents of
case runCabalFmt files options $ cabalFmt inputPath contents of
Left err -> fail ("First pass: " ++ show err)
Right (output', ws) -> do
-- idempotent
case runCabalFmt files defaultOptions $ cabalFmt inputPath (toUTF8BS output') of
case runCabalFmt files options $ cabalFmt inputPath (toUTF8BS output') of
Left err -> fail ("Second pass: " ++ show err)
Right (output'', _) -> do
unless (output' == output'') $ fail "Output not idempotent"
Expand Down