Skip to content

Commit 3f9c1c2

Browse files
committed
check + expand: Allow passing in multiple files
1 parent 4949444 commit 3f9c1c2

5 files changed

Lines changed: 37 additions & 14 deletions

File tree

Check.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ import Types
1212

1313
----------------------------------------------------------------
1414

15-
checkSyntax :: Options -> Cradle -> String -> IO String
16-
checkSyntax opt cradle file = unlines <$> check opt cradle file
15+
checkSyntax :: Options -> Cradle -> [String] -> IO String
16+
checkSyntax opt cradle files = unlines <$> check opt cradle files
1717

1818
----------------------------------------------------------------
1919

20-
check :: Options -> Cradle -> String -> IO [String]
21-
check opt cradle fileName = withGHC fileName $ checkIt `gcatch` handleErrMsg
20+
check :: Options -> Cradle -> [String] -> IO [String]
21+
check _ _ [] = error "ghc-mod: check: No files given"
22+
check opt cradle fileNames = withGHC (concat fileNames) $ checkIt `gcatch` handleErrMsg
2223
where
2324
checkIt = do
2425
readLog <- initializeFlagsWithCradle opt cradle options True
25-
setTargetFile fileName
26+
setTargetFiles fileNames
2627
checkSlowAndSet
2728
void $ load LoadAllTargets
2829
liftIO readLog

Debug.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ debug opt cradle ver fileName = do
2424
return (ghcOpts opt, [], [])
2525
[fast] <- withGHC fileName $ do
2626
void $ initializeFlagsWithCradle opt cradle gopts True
27-
setTargetFile fileName
27+
setTargetFiles [fileName]
2828
pure . canCheckFast <$> depanal [] False
2929
return [
3030
"GHC version: " ++ ver

GHCApi.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module GHCApi (
33
, withGHCDummyFile
44
, initializeFlags
55
, initializeFlagsWithCradle
6-
, setTargetFile
6+
, setTargetFiles
77
, getDynamicFlags
88
, setSlowDynFlags
99
, checkSlowAndSet
@@ -150,10 +150,11 @@ modifyFlagsWithOpts dflags cmdOpts =
150150

151151
----------------------------------------------------------------
152152

153-
setTargetFile :: (GhcMonad m) => String -> m ()
154-
setTargetFile file = do
155-
target <- guessTarget file Nothing
156-
setTargets [target]
153+
setTargetFiles :: (GhcMonad m) => [String] -> m ()
154+
setTargetFiles [] = error "ghc-mod: setTargetFiles: No target files given"
155+
setTargetFiles files = do
156+
targets <- forM files $ \file -> guessTarget file Nothing
157+
setTargets targets
157158

158159
----------------------------------------------------------------
159160

GHCMod.hs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import CabalApi
77
import Check
88
import Control.Applicative
99
import Control.Exception
10+
import Control.Monad
1011
import Cradle
1112
import Data.Typeable
1213
import Data.Version
@@ -108,8 +109,8 @@ main = flip catches handlers $ do
108109
res <- case cmdArg0 of
109110
"browse" -> concat <$> mapM (browseModule opt) remainingArgs
110111
"list" -> listModules opt
111-
"check" -> nArgs 1 $ withFile (checkSyntax opt cradle) cmdArg1
112-
"expand" -> nArgs 1 $ withFile (checkSyntax opt { expandSplice = True } cradle) cmdArg1
112+
"check" -> withFiles (checkSyntax opt cradle) remainingArgs
113+
"expand" -> withFiles (checkSyntax opt { expandSplice = True } cradle) remainingArgs
113114
"debug" -> nArgs 1 $ withFile (debugInfo opt cradle strVer) cmdArg1
114115
"type" -> nArgs 4 $ withFile (typeExpr opt cradle cmdArg2 (read cmdArg3) (read cmdArg4)) cmdArg1
115116
"info" -> nArgs 3 $ withFile (infoExpr opt cradle cmdArg2 cmdArg3) cmdArg1
@@ -148,6 +149,11 @@ main = flip catches handlers $ do
148149
if exist
149150
then cmd file
150151
else throw (FileNotExist file)
152+
withFiles cmd files = do
153+
missing <- findM ((not <$>) . doesFileExist) files
154+
case missing of
155+
Just file -> throw (FileNotExist file)
156+
Nothing -> cmd files
151157
xs !. idx
152158
| length xs <= idx = throw SafeList
153159
| otherwise = xs !! idx
@@ -159,6 +165,21 @@ main = flip catches handlers $ do
159165
where
160166
mPkgConf = cradlePackageConf cradle
161167

168+
-- | Returns the first Just produced, if any.
169+
findJust :: (Monad m) => [m (Maybe a)] -> m (Maybe a)
170+
findJust [] = return Nothing
171+
findJust (mma:mmas) = do
172+
m <- mma
173+
case m of Nothing -> findJust mmas
174+
just -> return just
175+
176+
-- | Returns the first result that fulfills the check.
177+
findM :: (Monad m) => (a -> m Bool) -> [a] -> m (Maybe a)
178+
findM f xs = findJust $ map justIfTrue xs
179+
where
180+
justIfTrue x = (\b -> if b then Just x else Nothing) `liftM` f x
181+
182+
162183
----------------------------------------------------------------
163184

164185
preBrowsedModules :: [String]

Info.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ inModuleContext cmd opt cradle fileName modstr action errmsg =
148148
valid = do
149149
void $ initializeFlagsWithCradle opt cradle ["-w:"] False
150150
when (cmd == Info) setSlowDynFlags
151-
setTargetFile fileName
151+
setTargetFiles [fileName]
152152
checkSlowAndSet
153153
void $ load LoadAllTargets
154154
doif setContextFromTarget action

0 commit comments

Comments
 (0)