Skip to content

Commit

Permalink
Add flag to format not .cabal file
Browse files Browse the repository at this point in the history
Like cabal.project or cabal.haskell-ci
  • Loading branch information
phadej committed Aug 30, 2020
1 parent a2b2365 commit 49ac522
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ self-test :
self-test-2 :
cabal v2-run cabal-fmt -- --no-tabular cabal-fmt.cabal

self-test-3 :
cabal v2-run cabal-fmt -- --no-cabal-file cabal.project

self-test-raw :
$$(cabal-plan list-bin cabal-fmt) cabal-fmt.cabal

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

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

noCabalFileP = O.flag' (mkOptionsMorphism $ \opts -> opts { optCabalFile = False })
$ O.short 'n' <> O.long "no-cabal-file" <> O.help "Don't parse as .cabal file"

stdoutP = O.flag' (mkOptionsMorphism $ \opts -> opts { optMode = ModeStdout })
$ O.long "stdout" <> O.help "Write output to stdout (default)"

Expand Down
22 changes: 14 additions & 8 deletions src/CabalFmt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import CabalFmt.Refactoring

cabalFmt :: MonadCabalFmt r m => FilePath -> BS.ByteString -> m String
cabalFmt filepath contents = do
gpd <- parseGpd filepath contents
inputFields' <- parseFields contents
let (inputFieldsC, endComments) = attachComments contents inputFields'

Expand All @@ -62,16 +61,23 @@ cabalFmt filepath contents = do
inputFieldsR <- foldM (&) inputFieldsP refactorings

-- options morphisms
let pragmas = foldMap (foldMap snd) inputFieldsR <> endCommentsPragmas
let pragmas :: [Pragma]
pragmas = foldMap (foldMap snd) inputFieldsR <> endCommentsPragmas

optsEndo :: OptionsMorphism
optsEndo = foldMap pragmaToOM pragmas

let v = C.cabalSpecFromVersionDigits
$ C.versionNumbers
$ C.specVersion
$ C.packageDescription gpd

local (over options $ \o -> runOptionsMorphism optsEndo $ o { optSpecVersion = v }) $ do
cabalFile <- asks (optCabalFile . view options)
csv <- case cabalFile of
False -> return C.cabalSpecLatest
True -> do
gpd <- parseGpd filepath contents
return $ C.cabalSpecFromVersionDigits
$ C.versionNumbers
$ C.specVersion
$ C.packageDescription gpd

local (over options $ \o -> runOptionsMorphism optsEndo $ o { optSpecVersion = csv }) $ do
indentWith <- asks (optIndent . view options)
let inputFields = fmap (fmap fst) inputFieldsR

Expand Down
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
, optCabalFile :: !Bool
, optSpecVersion :: !C.CabalSpecVersion
, optMode :: !Mode
}
Expand All @@ -33,6 +34,7 @@ defaultOptions = Options
{ optError = False
, optIndent = 2
, optTabular = True
, optCabalFile = True
, optSpecVersion = C.cabalSpecLatest
, optMode = ModeStdout
}
Expand Down

0 comments on commit 49ac522

Please sign in to comment.