@@ -13,6 +13,7 @@ import Control.Exception.Safe (Exception (displayExcept
1313 catchAny )
1414import Control.Monad.Extra (concatMapM , unless ,
1515 when )
16+ import qualified Data.Aeson.Encode.Pretty as A
1617import Data.Default (Default (def ))
1718import Data.Foldable (traverse_ )
1819import qualified Data.HashMap.Strict as HashMap
@@ -22,6 +23,8 @@ import Data.List.Extra (intercalate, isPrefixOf,
2223import Data.Maybe (catMaybes , isJust )
2324import qualified Data.Text as T
2425import qualified Data.Text.IO as T
26+ import Data.Text.Lazy.Encoding (decodeUtf8 )
27+ import qualified Data.Text.Lazy.IO as LT
2528import Development.IDE (Action , Rules ,
2629 hDuplicateTo' )
2730import Development.IDE.Core.Debouncer (Debouncer ,
@@ -71,10 +74,16 @@ import qualified HieDb.Run as HieDb
7174import Ide.Plugin.Config (CheckParents (NeverCheck ),
7275 Config ,
7376 getConfigFromNotification )
77+ import Ide.Plugin.ConfigUtils (pluginsToDefaultConfig ,
78+ pluginsToVSCodeExtensionSchema )
7479import Ide.PluginUtils (allLspCmdIds' ,
7580 getProcessID ,
7681 pluginDescToIdePlugins )
77- import Ide.Types (IdePlugins )
82+ import Ide.Types (IdeCommand (IdeCommand ),
83+ IdePlugins ,
84+ PluginDescriptor (PluginDescriptor , pluginCli ),
85+ PluginId (PluginId ),
86+ ipMap )
7887import qualified Language.LSP.Server as LSP
7988import Options.Applicative hiding (action )
8089import qualified System.Directory.Extra as IO
@@ -97,12 +106,11 @@ data Command
97106 | Db { projectRoot :: FilePath , hieOptions :: HieDb. Options , hieCommand :: HieDb. Command}
98107 -- ^ Run a command in the hiedb
99108 | LSP -- ^ Run the LSP server
100- | Custom { projectRoot :: FilePath , ideCommand :: IdeCommand } -- ^ User defined
109+ | PrintExtensionSchema
110+ | PrintDefaultConfig
111+ | Custom { projectRoot :: FilePath , ideCommand :: IdeCommand IdeState } -- ^ User defined
101112 deriving Show
102113
103- newtype IdeCommand = IdeCommand (IdeState -> IO () )
104-
105- instance Show IdeCommand where show _ = " <ide command>"
106114
107115-- TODO move these to hiedb
108116deriving instance Show HieDb. Command
@@ -112,16 +120,31 @@ isLSP :: Command -> Bool
112120isLSP LSP = True
113121isLSP _ = False
114122
115- commandP :: Parser Command
116- commandP = hsubparser (command " typecheck" (info (Check <$> fileCmd) fileInfo)
117- <> command " hiedb" (info (Db " ." <$> HieDb. optParser " " True <*> HieDb. cmdParser <**> helper) hieInfo)
118- <> command " lsp" (info (pure LSP <**> helper) lspInfo)
119- )
123+ commandP :: IdePlugins IdeState -> Parser Command
124+ commandP plugins =
125+ hsubparser(command " typecheck" (info (Check <$> fileCmd) fileInfo)
126+ <> command " hiedb" (info (Db " ." <$> HieDb. optParser " " True <*> HieDb. cmdParser <**> helper) hieInfo)
127+ <> command " lsp" (info (pure LSP <**> helper) lspInfo)
128+ <> command " vscode-extension-schema" extensionSchemaCommand
129+ <> command " generate-default-config" generateDefaultConfigCommand
130+ <> pluginCommands
131+ )
120132 where
121133 fileCmd = many (argument str (metavar " FILES/DIRS..." ))
122134 lspInfo = fullDesc <> progDesc " Start talking to an LSP client"
123135 fileInfo = fullDesc <> progDesc " Used as a test bed to check your IDE will work"
124136 hieInfo = fullDesc <> progDesc " Query .hie files"
137+ extensionSchemaCommand =
138+ info (pure PrintExtensionSchema )
139+ (fullDesc <> progDesc " Print generic config schema for plugins (used in the package.json of haskell vscode extension)" )
140+ generateDefaultConfigCommand =
141+ info (pure PrintDefaultConfig )
142+ (fullDesc <> progDesc " Print config supported by the server with default values" )
143+
144+ pluginCommands = mconcat
145+ [ command (T. unpack pId) (Custom " ." <$> p)
146+ | (PluginId pId, PluginDescriptor {pluginCli = Just p}) <- ipMap plugins
147+ ]
125148
126149
127150data Arguments = Arguments
@@ -198,6 +221,10 @@ defaultMain Arguments{..} = do
198221 outH <- argsHandleOut
199222
200223 case argCommand of
224+ PrintExtensionSchema ->
225+ LT. putStrLn $ decodeUtf8 $ A. encodePretty $ pluginsToVSCodeExtensionSchema argsHlsPlugins
226+ PrintDefaultConfig ->
227+ LT. putStrLn $ decodeUtf8 $ A. encodePretty $ pluginsToDefaultConfig argsHlsPlugins
201228 LSP -> do
202229 t <- offsetTime
203230 hPutStrLn stderr " Starting LSP server..."
@@ -310,6 +337,7 @@ defaultMain Arguments{..} = do
310337 case mlibdir of
311338 Nothing -> exitWith $ ExitFailure 1
312339 Just libdir -> HieDb. runCommand libdir opts{HieDb. database = dbLoc} cmd
340+
313341 Custom projectRoot (IdeCommand c) -> do
314342 dbLoc <- getHieDbLoc projectRoot
315343 runWithDb dbLoc $ \ hiedb hieChan -> do
0 commit comments