-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cabal-fmt: expand <dir> functionality
- Loading branch information
Showing
9 changed files
with
185 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
self-test : | ||
cabal new-run --enable-tests cabal-fmt -- cabal-fmt.cabal | ||
build : | ||
cabal v2-build | ||
|
||
self-test-Cabal : | ||
cabal new-run --enable-tests cabal-fmt -- Cabal/Cabal.cabal | ||
self-test : | ||
cabal v2-run cabal-fmt -- cabal-fmt.cabal | ||
|
||
golden : | ||
cabal new-run --enable-tests golden | ||
cabal v2-run golden | ||
|
||
golden-accept : | ||
cabal new-run --enable-tests golden -- --accept | ||
cabal v2-run golden -- --accept |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
packages: . | ||
tests: true | ||
|
||
package cabal-fmt | ||
ghc-options: -Wall |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module CabalFmt.Refactoring ( | ||
Refactoring, | ||
refactoringExpandExposedModules, | ||
) where | ||
|
||
import Data.List (intercalate, stripPrefix) | ||
import Data.Maybe (catMaybes) | ||
import System.FilePath (dropExtension, splitDirectories) | ||
|
||
import qualified Distribution.Compat.CharParsing as C | ||
import qualified Distribution.Fields as C | ||
import qualified Distribution.Parsec as C | ||
import qualified Distribution.Parsec.FieldLineStream as C | ||
import qualified Distribution.Simple.Utils as C | ||
|
||
import CabalFmt.Comments | ||
import CabalFmt.Options | ||
|
||
------------------------------------------------------------------------------- | ||
-- Refactoring type | ||
------------------------------------------------------------------------------- | ||
|
||
type Refactoring = Options -> [C.Field Comments] -> [C.Field Comments] | ||
|
||
------------------------------------------------------------------------------- | ||
-- Expand exposed-modules | ||
------------------------------------------------------------------------------- | ||
|
||
refactoringExpandExposedModules :: Refactoring | ||
refactoringExpandExposedModules opts = overField refact where | ||
refact name@(C.Name c n) fls | ||
| n == "exposed-modules" | ||
, definitions <- parse c = | ||
let newModules :: [C.FieldLine Comments] | ||
newModules = catMaybes | ||
[ do rest <- stripPrefix prefix fp | ||
return $ C.FieldLine mempty $ C.toUTF8BS $ intercalate "." rest | ||
| prefix <- definitions | ||
, fp <- fileList | ||
] | ||
in (name, newModules ++ fls) | ||
| otherwise = (name, fls) | ||
|
||
fileList :: [[FilePath]] | ||
fileList = map (splitDirectories . dropExtension) (optFileList opts) | ||
|
||
parse :: Comments -> [[FilePath]] | ||
parse (Comments bss) = catMaybes | ||
[ either (const Nothing) Just | ||
$ C.runParsecParser parser "<input>" $ C.fieldLineStreamFromBS bs | ||
| bs <- bss | ||
] | ||
|
||
parser :: C.ParsecParser [FilePath] | ||
parser = do | ||
_ <- C.string "--" | ||
C.spaces | ||
_ <- C.string "cabal-fmt:" | ||
C.spaces | ||
_ <- C.string "expand" | ||
C.spaces | ||
dir <- C.parsecToken | ||
return (splitDirectories dir) | ||
|
||
------------------------------------------------------------------------------- | ||
-- Tools | ||
------------------------------------------------------------------------------- | ||
|
||
overField :: (C.Name Comments -> [C.FieldLine Comments] -> (C.Name Comments, [C.FieldLine Comments])) | ||
-> [C.Field Comments] -> [C.Field Comments] | ||
overField f = goMany where | ||
goMany = map go | ||
|
||
go (C.Field name fls) = let ~(name', fls') = f name fls in C.Field name' fls' | ||
go (C.Section name args fs) = C.Section name args (goMany fs) |