Skip to content

Commit 4542c09

Browse files
committed
Generalized custom commands
1 parent 85bacb1 commit 4542c09

File tree

1 file changed

+25
-49
lines changed

1 file changed

+25
-49
lines changed

ghcide/src/Development/IDE/Main.hs

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,28 @@
22
module Development.IDE.Main
33
(Arguments(..)
44
,Command(..)
5+
,IdeCommand(..)
56
,isLSP
67
,commandP
78
,defaultMain
89
) where
910
import Control.Concurrent.Extra (newLock, readVar,
1011
withLock)
11-
import Control.Concurrent.STM
1212
import Control.Exception.Safe (Exception (displayException),
1313
catchAny)
1414
import Control.Monad.Extra (concatMapM, unless,
1515
when)
16-
import Control.Monad.IO.Class
1716
import Data.Default (Default (def))
18-
import Data.Foldable (toList)
1917
import qualified Data.HashMap.Strict as HashMap
2018
import Data.Hashable (hashed)
2119
import Data.List.Extra (intercalate, isPrefixOf,
2220
nub, nubOrd, partition)
2321
import Data.Maybe (catMaybes, fromMaybe,
24-
isJust, isNothing)
22+
isJust)
2523
import qualified Data.Text as T
2624
import qualified Data.Text.IO as T
27-
import Development.IDE (Action,
28-
GetKnownTargets (GetKnownTargets),
29-
GetModIfaceFromDiskAndIndex (GetModIfaceFromDiskAndIndex),
30-
Rules, hDuplicateTo')
25+
import Development.IDE (Action, Rules,
26+
hDuplicateTo')
3127
import Development.IDE.Core.Debouncer (Debouncer,
3228
newAsyncDebouncer)
3329
import Development.IDE.Core.FileStore (makeVFSHandle)
@@ -44,11 +40,9 @@ import Development.IDE.Core.RuleTypes (GenerateCore (GenerateCo
4440
import Development.IDE.Core.Rules (GhcSessionIO (GhcSessionIO),
4541
mainRule)
4642
import Development.IDE.Core.Service (initialise, runAction)
47-
import Development.IDE.Core.Shake (HieDbWriter (indexPending),
48-
IdeState (shakeExtras),
49-
ShakeExtras (hiedbWriter, state),
50-
toKnownFiles,
51-
useNoFile_, uses)
43+
import Development.IDE.Core.Shake (IdeState (shakeExtras),
44+
ShakeExtras (state),
45+
uses)
5246
import Development.IDE.Core.Tracing (measureMemory)
5347
import Development.IDE.LSP.LanguageServer (runLanguageServer)
5448
import Development.IDE.Plugin (Plugin (pluginHandlers, pluginRules))
@@ -98,13 +92,16 @@ import Text.Printf (printf)
9892

9993
data Command
10094
= Check [FilePath] -- ^ Typecheck some paths and print diagnostics. Exit code is the number of failures
101-
| Index {projectRoot :: FilePath, targetsToLoad :: [FilePath]}
102-
-- ^ Index all the targets and print the path to the database
10395
| Db {projectRoot :: FilePath, hieOptions :: HieDb.Options, hieCommand :: HieDb.Command}
10496
-- ^ Run a command in the hiedb
10597
| LSP -- ^ Run the LSP server
98+
| Custom {projectRoot :: FilePath, ideCommand :: IdeCommand} -- ^ User defined
10699
deriving Show
107100

101+
newtype IdeCommand = IdeCommand (IdeState -> IO ())
102+
103+
instance Show IdeCommand where show _ = "<ide command>"
104+
108105
-- TODO move these to hiedb
109106
deriving instance Show HieDb.Command
110107
deriving instance Show HieDb.Options
@@ -116,15 +113,13 @@ isLSP _ = False
116113
commandP :: Parser Command
117114
commandP = hsubparser (command "typecheck" (info (Check <$> fileCmd) fileInfo)
118115
<> command "hiedb" (info (Db "." <$> HieDb.optParser "" True <*> HieDb.cmdParser <**> helper) hieInfo)
119-
<> command "index" (info (Index "." <$> fileCmd) indexInfo)
120116
<> command "lsp" (info (pure LSP <**> helper) lspInfo)
121117
)
122118
where
123119
fileCmd = many (argument str (metavar "FILES/DIRS..."))
124120
lspInfo = fullDesc <> progDesc "Start talking to an LSP client"
125121
fileInfo = fullDesc <> progDesc "Used as a test bed to check your IDE will work"
126122
hieInfo = fullDesc <> progDesc "Query .hie files"
127-
indexInfo = fullDesc <> progDesc "Load the given files and index all the known targets"
128123

129124

130125
data Arguments = Arguments
@@ -296,45 +291,26 @@ defaultMain Arguments{..} = do
296291
measureMemory logger [keys] consoleObserver valuesRef
297292

298293
unless (null failed) (exitWith $ ExitFailure (length failed))
299-
Index{..} -> do
300-
dbLoc <- getHieDbLoc projectRoot
301-
files <- expandFiles (targetsToLoad ++ [projectRoot | null targetsToLoad])
302-
runWithDb dbLoc $ \hiedb hieChan -> do
303-
vfs <- makeVFSHandle
304-
sessionLoader <- loadSessionWithOptions argsSessionLoadingOptions "."
305-
let options = (argsIdeOptions argsDefaultHlsConfig sessionLoader)
306-
{ optCheckParents = pure NeverCheck
307-
, optCheckProject = pure False
308-
}
309-
ide <- initialise argsDefaultHlsConfig rules Nothing logger debouncer options vfs hiedb hieChan
310-
registerIdeConfiguration (shakeExtras ide) $ IdeConfiguration mempty (hashed Nothing)
311-
let fois = map toNormalizedFilePath' files
312-
setFilesOfInterest ide $ HashMap.fromList $ map (,OnDisk) fois
313-
results <- runAction "Index" ide $ do
314-
_ <- uses GetModIfaceFromDiskAndIndex fois
315-
allKnownTargets <- toKnownFiles <$> useNoFile_ GetKnownTargets
316-
liftIO $ hPutStrLn stderr $ "Indexing " <> show(length allKnownTargets) <> " targets"
317-
uses GetModIfaceFromDiskAndIndex $ toList allKnownTargets
318-
319-
hPutStrLn stderr "Writing index... "
320-
321-
let !nfailures = length $ filter isNothing results
322-
let !pending = indexPending $ hiedbWriter $ shakeExtras ide
323-
324-
atomically $ do
325-
n <- readTVar pending
326-
unless (HashMap.size n == 0) retry
327-
328-
putStrLn dbLoc
329-
unless (nfailures == 0) $ exitWith $ ExitFailure nfailures
330-
331294
Db dir opts cmd -> do
332295
dbLoc <- getHieDbLoc dir
333296
hPutStrLn stderr $ "Using hiedb at: " ++ dbLoc
334297
mlibdir <- setInitialDynFlags def
335298
case mlibdir of
336299
Nothing -> exitWith $ ExitFailure 1
337300
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
338314

339315
{-# ANN defaultMain ("HLint: ignore Use nubOrd" :: String) #-}
340316

0 commit comments

Comments
 (0)