2
2
module Development.IDE.Main
3
3
(Arguments (.. )
4
4
,Command (.. )
5
+ ,IdeCommand (.. )
5
6
,isLSP
6
7
,commandP
7
8
,defaultMain
8
9
) where
9
10
import Control.Concurrent.Extra (newLock , readVar ,
10
11
withLock )
11
- import Control.Concurrent.STM
12
12
import Control.Exception.Safe (Exception (displayException ),
13
13
catchAny )
14
14
import Control.Monad.Extra (concatMapM , unless ,
15
15
when )
16
- import Control.Monad.IO.Class
17
16
import Data.Default (Default (def ))
18
- import Data.Foldable (toList )
19
17
import qualified Data.HashMap.Strict as HashMap
20
18
import Data.Hashable (hashed )
21
19
import Data.List.Extra (intercalate , isPrefixOf ,
22
20
nub , nubOrd , partition )
23
21
import Data.Maybe (catMaybes , fromMaybe ,
24
- isJust , isNothing )
22
+ isJust )
25
23
import qualified Data.Text as T
26
24
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' )
31
27
import Development.IDE.Core.Debouncer (Debouncer ,
32
28
newAsyncDebouncer )
33
29
import Development.IDE.Core.FileStore (makeVFSHandle )
@@ -44,11 +40,9 @@ import Development.IDE.Core.RuleTypes (GenerateCore (GenerateCo
44
40
import Development.IDE.Core.Rules (GhcSessionIO (GhcSessionIO ),
45
41
mainRule )
46
42
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 )
52
46
import Development.IDE.Core.Tracing (measureMemory )
53
47
import Development.IDE.LSP.LanguageServer (runLanguageServer )
54
48
import Development.IDE.Plugin (Plugin (pluginHandlers , pluginRules ))
@@ -98,13 +92,16 @@ import Text.Printf (printf)
98
92
99
93
data Command
100
94
= 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
103
95
| Db { projectRoot :: FilePath , hieOptions :: HieDb. Options , hieCommand :: HieDb. Command}
104
96
-- ^ Run a command in the hiedb
105
97
| LSP -- ^ Run the LSP server
98
+ | Custom { projectRoot :: FilePath , ideCommand :: IdeCommand } -- ^ User defined
106
99
deriving Show
107
100
101
+ newtype IdeCommand = IdeCommand (IdeState -> IO () )
102
+
103
+ instance Show IdeCommand where show _ = " <ide command>"
104
+
108
105
-- TODO move these to hiedb
109
106
deriving instance Show HieDb. Command
110
107
deriving instance Show HieDb. Options
@@ -116,15 +113,13 @@ isLSP _ = False
116
113
commandP :: Parser Command
117
114
commandP = hsubparser (command " typecheck" (info (Check <$> fileCmd) fileInfo)
118
115
<> command " hiedb" (info (Db " ." <$> HieDb. optParser " " True <*> HieDb. cmdParser <**> helper) hieInfo)
119
- <> command " index" (info (Index " ." <$> fileCmd) indexInfo)
120
116
<> command " lsp" (info (pure LSP <**> helper) lspInfo)
121
117
)
122
118
where
123
119
fileCmd = many (argument str (metavar " FILES/DIRS..." ))
124
120
lspInfo = fullDesc <> progDesc " Start talking to an LSP client"
125
121
fileInfo = fullDesc <> progDesc " Used as a test bed to check your IDE will work"
126
122
hieInfo = fullDesc <> progDesc " Query .hie files"
127
- indexInfo = fullDesc <> progDesc " Load the given files and index all the known targets"
128
123
129
124
130
125
data Arguments = Arguments
@@ -296,45 +291,26 @@ defaultMain Arguments{..} = do
296
291
measureMemory logger [keys] consoleObserver valuesRef
297
292
298
293
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
-
331
294
Db dir opts cmd -> do
332
295
dbLoc <- getHieDbLoc dir
333
296
hPutStrLn stderr $ " Using hiedb at: " ++ dbLoc
334
297
mlibdir <- setInitialDynFlags def
335
298
case mlibdir of
336
299
Nothing -> exitWith $ ExitFailure 1
337
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
338
314
339
315
{-# ANN defaultMain ("HLint: ignore Use nubOrd" :: String) #-}
340
316
0 commit comments