1- module Development.IDE.Main (Arguments (.. ), defaultMain ) where
1+ {-# OPTIONS_GHC -Wno-orphans #-}
2+ module Development.IDE.Main
3+ (Arguments (.. )
4+ ,Command (.. )
5+ ,IdeCommand (.. )
6+ ,isLSP
7+ ,commandP
8+ ,defaultMain
9+ ) where
210import Control.Concurrent.Extra (newLock , readVar ,
311 withLock )
412import Control.Exception.Safe (Exception (displayException ),
@@ -57,6 +65,7 @@ import Development.Shake (action)
5765import GHC.IO.Encoding (setLocaleEncoding )
5866import GHC.IO.Handle (hDuplicate )
5967import HIE.Bios.Cradle (findCradle )
68+ import qualified HieDb.Run as HieDb
6069import Ide.Plugin.Config (CheckParents (NeverCheck ),
6170 Config ,
6271 getConfigFromNotification )
@@ -65,6 +74,7 @@ import Ide.PluginUtils (allLspCmdIds',
6574 pluginDescToIdePlugins )
6675import Ide.Types (IdePlugins )
6776import qualified Language.LSP.Server as LSP
77+ import Options.Applicative hiding (action )
6878import qualified System.Directory.Extra as IO
6979import System.Exit (ExitCode (ExitFailure ),
7080 exitWith )
@@ -80,9 +90,41 @@ import System.Time.Extra (offsetTime,
8090 showDuration )
8191import Text.Printf (printf )
8292
93+ data Command
94+ = Check [FilePath ] -- ^ Typecheck some paths and print diagnostics. Exit code is the number of failures
95+ | Db { projectRoot :: FilePath , hieOptions :: HieDb. Options , hieCommand :: HieDb. Command}
96+ -- ^ Run a command in the hiedb
97+ | LSP -- ^ Run the LSP server
98+ | Custom { projectRoot :: FilePath , ideCommand :: IdeCommand } -- ^ User defined
99+ deriving Show
100+
101+ newtype IdeCommand = IdeCommand (IdeState -> IO () )
102+
103+ instance Show IdeCommand where show _ = " <ide command>"
104+
105+ -- TODO move these to hiedb
106+ deriving instance Show HieDb. Command
107+ deriving instance Show HieDb. Options
108+
109+ isLSP :: Command -> Bool
110+ isLSP LSP = True
111+ isLSP _ = False
112+
113+ commandP :: Parser Command
114+ commandP = hsubparser (command " typecheck" (info (Check <$> fileCmd) fileInfo)
115+ <> command " hiedb" (info (Db " ." <$> HieDb. optParser " " True <*> HieDb. cmdParser <**> helper) hieInfo)
116+ <> command " lsp" (info (pure LSP <**> helper) lspInfo)
117+ )
118+ where
119+ fileCmd = many (argument str (metavar " FILES/DIRS..." ))
120+ lspInfo = fullDesc <> progDesc " Start talking to an LSP client"
121+ fileInfo = fullDesc <> progDesc " Used as a test bed to check your IDE will work"
122+ hieInfo = fullDesc <> progDesc " Query .hie files"
123+
124+
83125data Arguments = Arguments
84126 { argsOTMemoryProfiling :: Bool
85- , argFiles :: Maybe [ FilePath ] -- ^ Nothing: lsp server ; Just: typecheck and exit
127+ , argCommand :: Command
86128 , argsLogger :: IO Logger
87129 , argsRules :: Rules ()
88130 , argsHlsPlugins :: IdePlugins IdeState
@@ -100,7 +142,7 @@ data Arguments = Arguments
100142instance Default Arguments where
101143 def = Arguments
102144 { argsOTMemoryProfiling = False
103- , argFiles = Nothing
145+ , argCommand = LSP
104146 , argsLogger = stderrLogger
105147 , argsRules = mainRule >> action kick
106148 , argsGhcidePlugin = mempty
@@ -153,8 +195,8 @@ defaultMain Arguments{..} = do
153195 inH <- argsHandleIn
154196 outH <- argsHandleOut
155197
156- case argFiles of
157- Nothing -> do
198+ case argCommand of
199+ LSP -> do
158200 t <- offsetTime
159201 hPutStrLn stderr " Starting LSP server..."
160202 hPutStrLn stderr " If you are seeing this in a terminal, you probably should have run ghcide WITHOUT the --lsp option!"
@@ -188,7 +230,7 @@ defaultMain Arguments{..} = do
188230 vfs
189231 hiedb
190232 hieChan
191- Just argFiles -> do
233+ Check argFiles -> do
192234 dir <- IO. getCurrentDirectory
193235 dbLoc <- getHieDbLoc dir
194236 runWithDb dbLoc $ \ hiedb hieChan -> do
@@ -249,8 +291,30 @@ defaultMain Arguments{..} = do
249291 measureMemory logger [keys] consoleObserver valuesRef
250292
251293 unless (null failed) (exitWith $ ExitFailure (length failed))
294+ Db dir opts cmd -> do
295+ dbLoc <- getHieDbLoc dir
296+ hPutStrLn stderr $ " Using hiedb at: " ++ dbLoc
297+ mlibdir <- setInitialDynFlags def
298+ case mlibdir of
299+ Nothing -> exitWith $ ExitFailure 1
300+ Just libdir -> HieDb. runCommand libdir opts{HieDb. database = dbLoc} cmd
301+ Custom projectRoot (IdeCommand c) -> do
302+ dbLoc <- getHieDbLoc projectRoot
303+ runWithDb dbLoc $ \ hiedb hieChan -> do
304+ vfs <- makeVFSHandle
305+ sessionLoader <- loadSessionWithOptions argsSessionLoadingOptions " ."
306+ let options =
307+ (argsIdeOptions argsDefaultHlsConfig sessionLoader)
308+ { optCheckParents = pure NeverCheck ,
309+ optCheckProject = pure False
310+ }
311+ ide <- initialise argsDefaultHlsConfig rules Nothing logger debouncer options vfs hiedb hieChan
312+ registerIdeConfiguration (shakeExtras ide) $ IdeConfiguration mempty (hashed Nothing )
313+ c ide
314+
252315{-# ANN defaultMain ("HLint: ignore Use nubOrd" :: String) #-}
253316
317+
254318expandFiles :: [FilePath ] -> IO [FilePath ]
255319expandFiles = concatMapM $ \ x -> do
256320 b <- IO. doesFileExist x
0 commit comments