2014-10-22 22:56:18 +00:00
|
|
|
{-# LANGUAGE ScopedTypeVariables, DeriveDataTypeable #-}
|
2010-06-14 06:38:56 +00:00
|
|
|
|
2010-01-06 05:38:06 +00:00
|
|
|
module Main where
|
|
|
|
|
2015-08-28 07:44:20 +00:00
|
|
|
import Control.Category
|
2014-09-18 08:05:47 +00:00
|
|
|
import Control.Applicative
|
2014-10-22 22:56:18 +00:00
|
|
|
import Control.Monad
|
2014-03-27 05:46:33 +00:00
|
|
|
import Data.Typeable (Typeable)
|
2014-09-18 08:05:47 +00:00
|
|
|
import Data.List
|
2014-10-22 22:56:18 +00:00
|
|
|
import Data.List.Split
|
2014-09-18 08:05:47 +00:00
|
|
|
import Data.Char (isSpace)
|
2015-08-10 08:10:33 +00:00
|
|
|
import Data.Maybe
|
2015-01-12 16:26:46 +00:00
|
|
|
import Exception
|
2013-05-17 01:00:01 +00:00
|
|
|
import Language.Haskell.GhcMod
|
2015-06-07 18:36:49 +00:00
|
|
|
import Language.Haskell.GhcMod.Internal hiding (MonadIO,liftIO)
|
2015-08-28 07:44:20 +00:00
|
|
|
import Language.Haskell.GhcMod.Types
|
2015-09-14 07:42:45 +00:00
|
|
|
import Language.Haskell.GhcMod.Monad
|
2015-08-10 08:10:33 +00:00
|
|
|
import System.FilePath ((</>))
|
|
|
|
import System.Directory (setCurrentDirectory, getAppUserDataDirectory,
|
|
|
|
removeDirectoryRecursive)
|
2015-12-05 20:55:12 +00:00
|
|
|
-- import System.Environment (getArgs)
|
2015-09-01 02:14:15 +00:00
|
|
|
import System.IO
|
2015-08-18 02:50:19 +00:00
|
|
|
import System.Exit
|
2015-12-05 20:55:12 +00:00
|
|
|
import Text.PrettyPrint hiding ((<>))
|
2015-08-28 07:44:20 +00:00
|
|
|
import Prelude hiding ((.))
|
2015-12-05 20:55:12 +00:00
|
|
|
import GHCMod.Options
|
2010-01-06 05:38:06 +00:00
|
|
|
|
2014-10-22 22:56:18 +00:00
|
|
|
import Misc
|
|
|
|
|
2015-04-29 16:44:46 +00:00
|
|
|
ghcModStyle :: Style
|
|
|
|
ghcModStyle = style { lineLength = 80, ribbonsPerLine = 1.2 }
|
|
|
|
|
2014-09-18 08:05:47 +00:00
|
|
|
----------------------------------------------------------------
|
2014-09-12 22:09:57 +00:00
|
|
|
|
2015-08-16 20:28:45 +00:00
|
|
|
{-
|
|
|
|
File map docs:
|
|
|
|
|
|
|
|
CLI options:
|
|
|
|
* `--map-file "file1.hs=file2.hs"` can be used to tell
|
|
|
|
ghc-mod that it should take source code for `file1.hs` from `file2.hs`.
|
|
|
|
`file1.hs` can be either full path, or path relative to project root.
|
2015-08-19 02:28:26 +00:00
|
|
|
`file2.hs` has to be either relative to project root,
|
2015-08-16 20:28:45 +00:00
|
|
|
or full path (preferred).
|
|
|
|
* `--map-file "file.hs"` can be used to tell ghc-mod that it should take
|
|
|
|
source code for `file.hs` from stdin. File end marker is `\EOT\n`,
|
|
|
|
i.e. `\x04\x0A`. `file.hs` may or may not exist, and should be
|
|
|
|
either full path, or relative to project root.
|
|
|
|
|
|
|
|
Interactive commands:
|
|
|
|
* `map-file file.hs` -- tells ghc-modi to read `file.hs` source from stdin.
|
|
|
|
Works the same as second form of `--map-file` CLI option.
|
|
|
|
* `unmap-file file.hs` -- unloads previously mapped file, so that it's
|
|
|
|
no longer mapped. `file.hs` can be full path or relative to
|
|
|
|
project root, either will work.
|
|
|
|
|
|
|
|
Exposed functions:
|
|
|
|
* `loadMappedFile :: FilePath -> FilePath -> GhcModT m ()` -- maps `FilePath`,
|
|
|
|
given as first argument to take source from `FilePath` given as second
|
|
|
|
argument. Works exactly the same as first form of `--map-file`
|
|
|
|
CLI option.
|
|
|
|
* `loadMappedFileSource :: FilePath -> String -> GhcModT m ()` -- maps
|
|
|
|
`FilePath`, given as first argument to have source as given
|
|
|
|
by second argument. Works exactly the same as second form of `--map-file`
|
|
|
|
CLI option, sans reading from stdin.
|
|
|
|
* `unloadMappedFile :: FilePath -> GhcModT m ()` -- unmaps `FilePath`, given as
|
|
|
|
first argument, and removes any temporary files created when file was
|
|
|
|
mapped. Works exactly the same as `unmap-file` interactive command
|
|
|
|
-}
|
2010-06-14 06:38:56 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-09-18 08:05:47 +00:00
|
|
|
data CmdError = UnknownCommand String
|
|
|
|
| NoSuchFileError String
|
|
|
|
| LibraryError GhcModError
|
2010-06-14 06:38:56 +00:00
|
|
|
|
2014-09-18 08:05:47 +00:00
|
|
|
deriving (Show, Typeable)
|
|
|
|
|
|
|
|
instance Exception CmdError
|
2010-01-06 05:38:06 +00:00
|
|
|
|
2014-09-18 08:05:47 +00:00
|
|
|
data InteractiveOptions = InteractiveOptions {
|
|
|
|
ghcModExtensions :: Bool
|
|
|
|
}
|
|
|
|
|
2015-08-14 03:57:33 +00:00
|
|
|
handler :: IOish m => GhcModT m a -> GhcModT m a
|
|
|
|
handler = flip gcatches $
|
|
|
|
[ GHandler $ \(FatalError msg) -> exitError msg
|
2015-08-18 02:50:19 +00:00
|
|
|
, GHandler $ \e@(ExitSuccess) -> throw e
|
|
|
|
, GHandler $ \e@(ExitFailure _) -> throw e
|
2015-08-14 03:57:33 +00:00
|
|
|
, GHandler $ \(SomeException e) -> exitError $ "ghc-mod: " ++ show e
|
2014-09-18 08:05:47 +00:00
|
|
|
]
|
|
|
|
|
2010-01-06 05:38:06 +00:00
|
|
|
main :: IO ()
|
2015-08-14 03:57:33 +00:00
|
|
|
main = do
|
2013-03-29 12:58:55 +00:00
|
|
|
hSetEncoding stdout utf8
|
2015-12-05 20:55:12 +00:00
|
|
|
parseArgs >>= \res@(globalOptions, _) ->
|
|
|
|
catches (progMain res) [
|
|
|
|
Handler $ \(e :: GhcModError) ->
|
|
|
|
runGmOutT globalOptions $ exitError $ renderStyle ghcModStyle (gmeDoc e)
|
|
|
|
]
|
2015-08-13 04:47:12 +00:00
|
|
|
|
2015-12-05 20:55:12 +00:00
|
|
|
progMain :: (Options, GhcModCommands) -> IO ()
|
|
|
|
progMain (globalOptions, commands) = runGmOutT globalOptions $
|
|
|
|
wrapGhcCommands globalOptions commands
|
2014-10-22 22:56:18 +00:00
|
|
|
|
2015-04-29 16:44:46 +00:00
|
|
|
-- ghc-modi
|
|
|
|
legacyInteractive :: IOish m => GhcModT m ()
|
2015-08-05 06:52:52 +00:00
|
|
|
legacyInteractive = do
|
2015-04-29 16:44:46 +00:00
|
|
|
opt <- options
|
2015-08-14 01:48:29 +00:00
|
|
|
prepareCabalHelper
|
2015-08-14 04:48:56 +00:00
|
|
|
tmpdir <- cradleTempDir <$> cradle
|
2015-09-14 07:42:45 +00:00
|
|
|
gmo <- gmoAsk
|
|
|
|
symdbreq <- liftIO $ newSymDbReq opt gmo tmpdir
|
2015-08-07 04:47:34 +00:00
|
|
|
world <- getCurrentWorld
|
2015-08-05 06:52:52 +00:00
|
|
|
legacyInteractiveLoop symdbreq world
|
2014-10-22 22:56:18 +00:00
|
|
|
|
2015-08-13 04:47:12 +00:00
|
|
|
bug :: IOish m => String -> GhcModT m ()
|
2014-10-22 22:56:18 +00:00
|
|
|
bug msg = do
|
2015-08-13 04:47:12 +00:00
|
|
|
gmPutStrLn $ notGood $ "BUG: " ++ msg
|
|
|
|
liftIO exitFailure
|
2014-10-22 22:56:18 +00:00
|
|
|
|
|
|
|
notGood :: String -> String
|
|
|
|
notGood msg = "NG " ++ escapeNewlines msg
|
|
|
|
|
|
|
|
escapeNewlines :: String -> String
|
|
|
|
escapeNewlines = replace "\n" "\\n" . replace "\\n" "\\\\n"
|
|
|
|
|
|
|
|
replace :: String -> String -> String -> String
|
|
|
|
replace needle replacement = intercalate replacement . splitOn needle
|
|
|
|
|
|
|
|
legacyInteractiveLoop :: IOish m
|
2015-08-05 06:52:52 +00:00
|
|
|
=> SymDbReq -> World -> GhcModT m ()
|
|
|
|
legacyInteractiveLoop symdbreq world = do
|
2014-10-22 22:56:18 +00:00
|
|
|
liftIO . setCurrentDirectory =<< cradleRootDir <$> cradle
|
|
|
|
|
|
|
|
-- blocking
|
2015-08-05 06:52:52 +00:00
|
|
|
cmdArg <- liftIO $ getLine
|
2014-10-22 22:56:18 +00:00
|
|
|
|
|
|
|
-- after blocking, we need to see if the world has changed.
|
|
|
|
|
2015-08-07 04:47:34 +00:00
|
|
|
changed <- didWorldChange world
|
2015-09-11 07:53:24 +00:00
|
|
|
|
|
|
|
world' <- if changed
|
|
|
|
then getCurrentWorld -- TODO: gah, we're hitting the fs twice
|
|
|
|
else return world
|
|
|
|
|
2014-10-22 22:56:18 +00:00
|
|
|
when changed $ do
|
2015-08-05 06:52:52 +00:00
|
|
|
dropSession
|
2014-10-22 22:56:18 +00:00
|
|
|
|
|
|
|
let (cmd':args') = split (keepDelimsR $ condense $ whenElt isSpace) cmdArg
|
|
|
|
arg = concat args'
|
|
|
|
cmd = dropWhileEnd isSpace cmd'
|
|
|
|
args = dropWhileEnd isSpace `map` args'
|
|
|
|
|
2015-08-14 07:47:45 +00:00
|
|
|
res <- flip gcatches interactiveHandlers $ case dropWhileEnd isSpace cmd of
|
2015-12-05 20:55:12 +00:00
|
|
|
"check" -> checkSyntax [arg]
|
2015-12-09 21:40:37 +00:00
|
|
|
"lint" -> lint defaultLintOpts arg
|
|
|
|
"find" -> do
|
2014-10-22 22:56:18 +00:00
|
|
|
db <- getDb symdbreq >>= checkDb symdbreq
|
|
|
|
lookupSymbol arg db
|
|
|
|
|
2015-12-05 20:55:12 +00:00
|
|
|
"info" -> info (head args) $ Expression $ concat $ tail args'
|
|
|
|
"type" -> locArgs types args
|
|
|
|
"split" -> locArgs splits args
|
2014-10-22 22:56:18 +00:00
|
|
|
|
2015-12-05 20:55:12 +00:00
|
|
|
"sig" -> locArgs sig args
|
|
|
|
"auto" -> locArgs auto args
|
|
|
|
"refine" -> locArgs' refine args
|
2014-10-22 22:56:18 +00:00
|
|
|
|
2015-12-05 20:55:12 +00:00
|
|
|
"boot" -> boot
|
2015-12-09 21:40:37 +00:00
|
|
|
"browse" -> concat <$> browse defaultBrowseOpts `mapM` args
|
2014-10-22 22:56:18 +00:00
|
|
|
|
2015-07-02 11:01:03 +00:00
|
|
|
"map-file" -> liftIO getFileSourceFromStdin
|
2015-08-16 20:20:00 +00:00
|
|
|
>>= loadMappedFileSource arg
|
2015-07-02 11:01:03 +00:00
|
|
|
>> return ""
|
2015-05-31 08:32:46 +00:00
|
|
|
|
2015-07-02 11:01:03 +00:00
|
|
|
"unmap-file" -> unloadMappedFile arg
|
|
|
|
>> return ""
|
2015-05-31 08:32:46 +00:00
|
|
|
|
2014-10-22 22:56:18 +00:00
|
|
|
"quit" -> liftIO $ exitSuccess
|
|
|
|
"" -> liftIO $ exitSuccess
|
|
|
|
_ -> fatalError $ "unknown command: `" ++ cmd ++ "'"
|
|
|
|
|
2015-08-13 04:47:12 +00:00
|
|
|
gmPutStr res >> gmPutStrLn "OK" >> liftIO (hFlush stdout)
|
2015-09-11 07:53:24 +00:00
|
|
|
legacyInteractiveLoop symdbreq world'
|
2015-08-14 07:47:45 +00:00
|
|
|
where
|
2015-12-05 20:55:12 +00:00
|
|
|
interactiveHandlers =
|
2015-08-14 07:47:45 +00:00
|
|
|
[ GHandler $ \e@(FatalError _) -> throw e
|
2015-08-18 02:50:19 +00:00
|
|
|
, GHandler $ \e@(ExitSuccess) -> throw e
|
|
|
|
, GHandler $ \e@(ExitFailure _) -> throw e
|
2015-08-14 07:47:45 +00:00
|
|
|
, GHandler $ \(SomeException e) -> gmErrStrLn (show e) >> return ""
|
|
|
|
]
|
2015-12-06 16:47:11 +00:00
|
|
|
locArgs a [f,l,c] = a f (read l) (read c)
|
|
|
|
locArgs _ args = throw $ InvalidCommandLine $ Left $ unwords args
|
|
|
|
locArgs' a (f:l:c:xs) = a f (read l) (read c) (Expression $ unwords xs)
|
|
|
|
locArgs' _ args = throw $ InvalidCommandLine $ Left $ unwords args
|
2014-10-22 22:56:18 +00:00
|
|
|
|
2015-10-03 14:00:46 +00:00
|
|
|
getFileSourceFromStdin :: IO String
|
2015-07-02 11:01:03 +00:00
|
|
|
getFileSourceFromStdin = do
|
2015-10-01 06:54:13 +00:00
|
|
|
linesIn <- readStdin'
|
|
|
|
return (intercalate "\n" linesIn)
|
|
|
|
where
|
|
|
|
readStdin' = do
|
|
|
|
x <- getLine
|
|
|
|
if x/="\EOT"
|
|
|
|
then fmap (x:) readStdin'
|
|
|
|
else return []
|
2015-07-02 11:01:03 +00:00
|
|
|
|
2015-09-14 07:42:45 +00:00
|
|
|
-- Someone please already rewrite the cmdline parsing code *weep* :'(
|
2015-12-05 20:55:12 +00:00
|
|
|
wrapGhcCommands :: (IOish m, GmOut m) => Options -> GhcModCommands -> m ()
|
|
|
|
wrapGhcCommands _opts CmdRoot = gmPutStr =<< rootInfo
|
|
|
|
wrapGhcCommands opts cmd = do
|
2015-09-14 07:42:45 +00:00
|
|
|
handleGmError $ runGhcModT opts $ handler $ do
|
|
|
|
forM_ (reverse $ optFileMappings opts) $
|
|
|
|
uncurry loadMMappedFiles
|
|
|
|
|
2015-12-05 20:55:12 +00:00
|
|
|
gmPutStr =<< ghcCommands cmd
|
2015-09-14 07:42:45 +00:00
|
|
|
where
|
|
|
|
handleGmError action = do
|
|
|
|
(e, _l) <- liftIO . evaluate =<< action
|
|
|
|
case e of
|
|
|
|
Right _ ->
|
|
|
|
return ()
|
|
|
|
Left ed ->
|
|
|
|
exitError $ renderStyle ghcModStyle (gmeDoc ed)
|
|
|
|
|
|
|
|
loadMMappedFiles from (Just to) = loadMappedFile from to
|
|
|
|
loadMMappedFiles from (Nothing) = do
|
|
|
|
src <- liftIO getFileSourceFromStdin
|
|
|
|
loadMappedFileSource from src
|
|
|
|
|
|
|
|
|
2015-12-05 20:55:12 +00:00
|
|
|
ghcCommands :: IOish m => GhcModCommands -> GhcModT m String
|
|
|
|
-- ghcCommands cmd = action args
|
|
|
|
ghcCommands (CmdLang) = languages
|
|
|
|
ghcCommands (CmdFlag) = flags
|
|
|
|
ghcCommands (CmdDebug) = debugInfo
|
|
|
|
ghcCommands (CmdDebugComponent ts) = componentInfo ts
|
|
|
|
ghcCommands (CmdBoot) = boot
|
|
|
|
-- ghcCommands (CmdNukeCaches) = nukeCaches >> return ""
|
|
|
|
-- ghcCommands (CmdRoot) = undefined -- handled in wrapGhcCommands
|
|
|
|
ghcCommands (CmdLegacyInteractive) = legacyInteractive >> return ""
|
|
|
|
ghcCommands (CmdModules detail) = modules detail
|
|
|
|
ghcCommands (CmdDumpSym tmpdir) = dumpSymbol tmpdir
|
|
|
|
ghcCommands (CmdFind symb) = findSymbol symb
|
|
|
|
ghcCommands (CmdDoc m) = pkgDoc m
|
|
|
|
ghcCommands (CmdLint opts file) = lint opts file
|
|
|
|
ghcCommands (CmdBrowse opts ms) = concat <$> browse opts `mapM` ms
|
|
|
|
ghcCommands (CmdCheck files) = checkSyntax files
|
|
|
|
ghcCommands (CmdExpand files) = expandTemplate files
|
|
|
|
ghcCommands (CmdInfo file symb) = info file $ Expression symb
|
|
|
|
ghcCommands (CmdType file (line, col)) = types file line col
|
|
|
|
ghcCommands (CmdSplit file (line, col)) = splits file line col
|
|
|
|
ghcCommands (CmdSig file (line, col)) = sig file line col
|
|
|
|
ghcCommands (CmdAuto file (line, col)) = auto file line col
|
|
|
|
ghcCommands (CmdRefine file (line, col) expr) = refine file line col $ Expression expr
|
|
|
|
ghcCommands _ = fatalError "Unknown command"
|
2014-09-18 08:05:47 +00:00
|
|
|
|
|
|
|
newtype FatalError = FatalError String deriving (Show, Typeable)
|
|
|
|
instance Exception FatalError
|
|
|
|
|
|
|
|
newtype InvalidCommandLine = InvalidCommandLine (Either String String)
|
|
|
|
deriving (Show, Typeable)
|
|
|
|
instance Exception InvalidCommandLine
|
|
|
|
|
2015-09-14 07:42:45 +00:00
|
|
|
exitError :: (MonadIO m, GmOut m) => String -> m a
|
2015-08-14 03:57:33 +00:00
|
|
|
exitError msg = gmErrStrLn (dropWhileEnd (=='\n') msg) >> liftIO exitFailure
|
2014-09-18 08:05:47 +00:00
|
|
|
|
|
|
|
fatalError :: String -> a
|
2015-04-29 16:44:46 +00:00
|
|
|
fatalError s = throw $ FatalError $ "ghc-mod: " ++ s
|
2014-09-18 08:05:47 +00:00
|
|
|
|
2015-01-12 16:26:46 +00:00
|
|
|
catchArgs :: (Monad m, ExceptionMonad m) => String -> m a -> m a
|
|
|
|
catchArgs cmd action =
|
|
|
|
action `gcatch` \(PatternMatchFail _) ->
|
|
|
|
throw $ InvalidCommandLine (Left cmd)
|
|
|
|
|
2015-08-10 08:10:33 +00:00
|
|
|
nukeCaches :: IOish m => GhcModT m ()
|
|
|
|
nukeCaches = do
|
|
|
|
chdir <- liftIO $ (</> "cabal-helper") <$> getAppUserDataDirectory "ghc-mod"
|
|
|
|
c <- cradle
|
|
|
|
|
2015-09-11 01:48:52 +00:00
|
|
|
when (isCabalHelperProject $ cradleProject c) $ do
|
2015-08-10 08:10:33 +00:00
|
|
|
let root = cradleRootDir c
|
2015-08-18 12:55:45 +00:00
|
|
|
let dist = cradleDistDir c
|
|
|
|
liftIO $ (trySome . removeDirectoryRecursive) `mapM_` [chdir, root </> dist]
|
2015-08-10 08:10:33 +00:00
|
|
|
|
|
|
|
trySome :: IO a -> IO (Either SomeException a)
|
|
|
|
trySome = try
|