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

Format all files in src/ by cabal run -- src/**/*.hs #599

Closed
wants to merge 6 commits into from
Closed
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
357 changes: 175 additions & 182 deletions src/HIndent.hs

Large diffs are not rendered by default.

104 changes: 52 additions & 52 deletions src/HIndent/CabalFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,67 +26,68 @@ import System.Directory
import System.FilePath
import Text.Read

data Stanza = MkStanza
{ _stanzaBuildInfo :: BuildInfo
, stanzaIsSourceFilePath :: FilePath -> Bool
}
data Stanza =
MkStanza
{ _stanzaBuildInfo :: BuildInfo
, stanzaIsSourceFilePath :: FilePath -> Bool
}

-- | Find the relative path of a child path in a parent, if it is a child
toRelative :: FilePath -> FilePath -> Maybe FilePath
toRelative parent child = let
rel = makeRelative parent child
in if rel == child
then Nothing
else Just rel
toRelative parent child =
let rel = makeRelative parent child
in if rel == child
then Nothing
else Just rel

-- | Create a Stanza from `BuildInfo` and names of modules and paths
mkStanza :: BuildInfo -> [ModuleName] -> [FilePath] -> Stanza
mkStanza bi mnames fpaths =
MkStanza bi $ \path -> let
modpaths = fmap toFilePath $ otherModules bi ++ mnames
inDir dir =
case toRelative dir path of
Nothing -> False
Just relpath ->
any (equalFilePath $ dropExtension relpath) modpaths ||
any (equalFilePath relpath) fpaths
in any inDir $ hsSourceDirs' bi
where
MkStanza bi $ \path ->
let modpaths = fmap toFilePath $ otherModules bi ++ mnames
inDir dir =
case toRelative dir path of
Nothing -> False
Just relpath ->
any (equalFilePath $ dropExtension relpath) modpaths ||
any (equalFilePath relpath) fpaths
in any inDir $ hsSourceDirs' bi
where

#if MIN_VERSION_Cabal(3, 6, 0)
hsSourceDirs' = (map getSymbolicPath) . hsSourceDirs
hsSourceDirs' = (map getSymbolicPath) . hsSourceDirs
#else
hsSourceDirs' = hsSourceDirs
#endif

-- | Extract `Stanza`s from a package
packageStanzas :: PackageDescription -> [Stanza]
packageStanzas pd = let
libStanza :: Library -> Stanza
libStanza lib = mkStanza (libBuildInfo lib) (exposedModules lib) []
exeStanza :: Executable -> Stanza
exeStanza exe = mkStanza (buildInfo exe) [] [modulePath exe]
testStanza :: TestSuite -> Stanza
testStanza ts =
mkStanza
(testBuildInfo ts)
(case testInterface ts of
TestSuiteLibV09 _ mname -> [mname]
_ -> [])
(case testInterface ts of
TestSuiteExeV10 _ path -> [path]
_ -> [])
benchStanza :: Benchmark -> Stanza
benchStanza bn =
mkStanza (benchmarkBuildInfo bn) [] $
case benchmarkInterface bn of
BenchmarkExeV10 _ path -> [path]
_ -> []
in mconcat
[ maybeToList $ fmap libStanza $ library pd
, fmap exeStanza $ executables pd
, fmap testStanza $ testSuites pd
, fmap benchStanza $ benchmarks pd
]
packageStanzas pd =
let libStanza :: Library -> Stanza
libStanza lib = mkStanza (libBuildInfo lib) (exposedModules lib) []
exeStanza :: Executable -> Stanza
exeStanza exe = mkStanza (buildInfo exe) [] [modulePath exe]
testStanza :: TestSuite -> Stanza
testStanza ts =
mkStanza
(testBuildInfo ts)
(case testInterface ts of
TestSuiteLibV09 _ mname -> [mname]
_ -> [])
(case testInterface ts of
TestSuiteExeV10 _ path -> [path]
_ -> [])
benchStanza :: Benchmark -> Stanza
benchStanza bn =
mkStanza (benchmarkBuildInfo bn) [] $
case benchmarkInterface bn of
BenchmarkExeV10 _ path -> [path]
_ -> []
in mconcat
[ maybeToList $ fmap libStanza $ library pd
, fmap exeStanza $ executables pd
, fmap testStanza $ testSuites pd
, fmap benchStanza $ benchmarks pd
]

-- | Find cabal files that are "above" the source path
findCabalFiles :: FilePath -> FilePath -> IO (Maybe ([FilePath], FilePath))
Expand All @@ -103,16 +104,15 @@ findCabalFiles dir rel = do
getGenericPackageDescription :: FilePath -> IO (Maybe GenericPackageDescription)
#if MIN_VERSION_Cabal(2, 2, 0)
getGenericPackageDescription cabalPath = do
cabaltext <- BS.readFile cabalPath
return $ parseGenericPackageDescriptionMaybe cabaltext
cabaltext <- BS.readFile cabalPath
return $ parseGenericPackageDescriptionMaybe cabaltext
#else
getGenericPackageDescription cabalPath = do
cabaltext <- readFile cabalPath
case parsePackageDescription cabaltext of
ParseOk _ gpd -> return $ Just gpd
_ -> return Nothing
_ -> return Nothing
#endif

-- | Find the `Stanza` that refers to this source path
getCabalStanza :: FilePath -> IO (Maybe Stanza)
getCabalStanza srcpath = do
Expand Down
19 changes: 14 additions & 5 deletions src/HIndent/CodeBlock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import Data.Monoid

-- | A block of code.
data CodeBlock
= Shebang ByteString
| HaskellSource Int ByteString
= Shebang ByteString
| HaskellSource Int ByteString
-- ^ Includes the starting line (indexed from 0) for error reporting
| CPPDirectives ByteString
deriving (Show, Eq)
| CPPDirectives ByteString
deriving (Show, Eq)

-- | Break a Haskell code string into chunks, using CPP as a delimiter.
-- Lines that start with '#if', '#end', or '#else' are their own chunks, and
Expand Down Expand Up @@ -57,7 +57,16 @@ cppSplitBlocks inp =
cppLine src =
any
(`S8.isPrefixOf` src)
["#if", "#end", "#else", "#define", "#undef", "#elif", "#include", "#error", "#warning"]
[ "#if"
, "#end"
, "#else"
, "#define"
, "#undef"
, "#elif"
, "#include"
, "#error"
, "#warning"
]
-- Note: #ifdef and #ifndef are handled by #if
hasEscapedTrailingNewline :: ByteString -> Bool
hasEscapedTrailingNewline src = "\\" `S8.isSuffixOf` src
Expand Down
Loading