module Neovim.Main
where
import Neovim.Config
import qualified Neovim.Context.Internal as Internal
import Neovim.Log
import qualified Neovim.Plugin as P
import Neovim.RPC.Common as RPC
import Neovim.RPC.EventHandler
import Neovim.RPC.SocketReader
import Neovim.Util (oneLineErrorMessage)
import Control.Concurrent
import Control.Concurrent.STM (atomically, putTMVar)
import Control.Monad
import Data.Default
import Data.Maybe
import Data.Monoid
import Options.Applicative
import System.IO (stdin, stdout)
import UnliftIO.Async (Async, async)
import Prelude
logger :: String
logger = "Neovim.Main"
data CommandLineOptions =
Opt { providerName :: Maybe String
, hostPort :: Maybe (String, Int)
, unix :: Maybe FilePath
, envVar :: Bool
, logOpts :: Maybe (FilePath, Priority)
}
instance Default CommandLineOptions where
def = Opt
{ providerName = Nothing
, hostPort = Nothing
, unix = Nothing
, envVar = False
, logOpts = Nothing
}
optParser :: Parser CommandLineOptions
optParser = Opt
<$> optional (strArgument
(metavar "NAME"
<> help (unlines
[ "Name that associates the plugin provider with neovim."
, "This option has only an effect if you start nvim-hs"
, "with rpcstart()/jobstart() and use the factory method approach."
, "Since it is extremely hard to figure that out inside"
, "nvim-hs, this option is assumed to used if the input"
, "and output is tied to standard in and standard out."
])))
<*> optional ((,)
<$> strOption
(long "host"
<> short 'a'
<> metavar "HOSTNAME"
<> help "Connect to the specified host. (requires -p)")
<*> option auto
(long "port"
<> short 'p'
<> metavar "PORT"
<> help "Connect to the specified port. (requires -a)"))
<*> optional (strOption
(long "unix"
<> short 'u'
<> help "Connect to the given unix domain socket."))
<*> switch
( long "environment"
<> short 'e'
<> help "Read connection information from $NVIM_LISTEN_ADDRESS.")
<*> optional ((,)
<$> strOption
(long "log-file"
<> short 'l'
<> help "File to log to.")
<*> option auto
(long "log-level"
<> short 'v'
<> help ("Log level. Must be one of: " ++ (unwords . map show) logLevels)))
where
logLevels :: [Priority]
logLevels = [ DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, ALERT, EMERGENCY ]
opts :: ParserInfo CommandLineOptions
opts = info (helper <*> optParser)
(fullDesc
<> header "Start a neovim plugin provider for Haskell plugins."
<> progDesc "This is still work in progress. Feel free to contribute.")
neovim :: NeovimConfig -> IO ()
neovim = realMain standalone
type TransitionHandler a = [Async ()] -> Internal.Config RPCConfig -> IO a
realMain :: TransitionHandler a
-> NeovimConfig
-> IO ()
realMain transitionHandler cfg = do
os <- execParser opts
maybe disableLogger (uncurry withLogger) (logOpts os <|> logOptions cfg) $ do
debugM logger "Starting up neovim haskell plguin provider"
void $ runPluginProvider os (Just cfg) transitionHandler
runPluginProvider
:: CommandLineOptions
-> Maybe NeovimConfig
-> TransitionHandler a
-> IO a
runPluginProvider os mcfg transitionHandler = case (hostPort os, unix os) of
(Just (h,p), _) ->
createHandle (TCP p h) >>= \s -> run s s
(_, Just fp) ->
createHandle (UnixSocket fp) >>= \s -> run s s
_ | envVar os ->
createHandle Environment >>= \s -> run s s
_ ->
run stdout stdin
where
run evHandlerHandle sockreaderHandle = do
let allPlugins = maybe [] plugins mcfg
conf <- Internal.newConfig (pure (providerName os)) newRPCConfig
ehTid <- async $ runEventHandler
evHandlerHandle
conf { Internal.pluginSettings = Nothing }
srTid <- async $ runSocketReader sockreaderHandle conf
let startupConf = Internal.retypeConfig () conf
P.startPluginThreads startupConf allPlugins >>= \case
Left e -> do
errorM logger $ "Error initializing plugins: " <> show (oneLineErrorMessage e)
putMVar (Internal.transitionTo conf) $ Internal.Failure e
transitionHandler [ehTid, srTid] conf
Right (funMapEntries, pluginTids) -> do
atomically $ putTMVar
(Internal.globalFunctionMap conf)
(Internal.mkFunctionMap funMapEntries)
putMVar (Internal.transitionTo conf) $ Internal.InitSuccess
transitionHandler (srTid:ehTid:pluginTids) conf
standalone :: TransitionHandler ()
standalone threads cfg = takeMVar (Internal.transitionTo cfg) >>= \case
Internal.InitSuccess -> do
debugM logger "Initialization Successful"
standalone threads cfg
Internal.Restart -> do
errorM logger "Cannot restart"
standalone threads cfg
Internal.Failure e ->
errorM logger . show $ oneLineErrorMessage e
Internal.Quit ->
return ()