module Dhall.LSP.Server(run) where
import Control.Concurrent.MVar
import Data.Default
import qualified Language.Haskell.LSP.Control as LSP.Control
import qualified Language.Haskell.LSP.Core as LSP.Core
import qualified Language.Haskell.LSP.Types as J
import Data.Text (Text)
import qualified System.Log.Logger
import Dhall.LSP.State
import Dhall.LSP.Handlers (nullHandler, wrapHandler, hoverHandler,
didOpenTextDocumentNotificationHandler, didSaveTextDocumentNotificationHandler,
executeCommandHandler, documentFormattingHandler, documentLinkHandler)
run :: Maybe FilePath -> IO ()
run mlog = do
setupLogger mlog
state <- newEmptyMVar
_ <- LSP.Control.run (makeConfig, initCallback state) (lspHandlers state)
lspOptions Nothing
return ()
where
initCallback
:: MVar ServerState
-> LSP.Core.LspFuncs ()
-> IO (Maybe J.ResponseError)
initCallback state lsp = do
putMVar state (initialState lsp)
return Nothing
makeConfig :: J.DidChangeConfigurationNotification -> Either Text ()
makeConfig _ = Right ()
setupLogger :: Maybe FilePath -> IO ()
setupLogger Nothing = pure ()
setupLogger (Just "[OUTPUT]") = LSP.Core.setupLogger Nothing [] System.Log.Logger.DEBUG
setupLogger file = LSP.Core.setupLogger file [] System.Log.Logger.DEBUG
syncOptions :: J.TextDocumentSyncOptions
syncOptions = J.TextDocumentSyncOptions
{ J._openClose = Just True
, J._change = Just J.TdSyncIncremental
, J._willSave = Just False
, J._willSaveWaitUntil = Just False
, J._save = Just $ J.SaveOptions $ Just False
}
lspOptions :: LSP.Core.Options
lspOptions = def { LSP.Core.textDocumentSync = Just syncOptions
, LSP.Core.executeCommandProvider =
Just (J.ExecuteCommandOptions
(J.List ["dhall.server.lint",
"dhall.server.annotateLet",
"dhall.server.freezeImport",
"dhall.server.freezeAllImports"]))
, LSP.Core.documentLinkProvider =
Just (J.DocumentLinkOptions { _resolveProvider = Just False })
}
lspHandlers :: MVar ServerState -> LSP.Core.Handlers
lspHandlers state
= def { LSP.Core.initializedHandler = Just $ wrapHandler state nullHandler
, LSP.Core.hoverHandler = Just $ wrapHandler state hoverHandler
, LSP.Core.didOpenTextDocumentNotificationHandler = Just $ wrapHandler state didOpenTextDocumentNotificationHandler
, LSP.Core.didChangeTextDocumentNotificationHandler = Just $ wrapHandler state nullHandler
, LSP.Core.didSaveTextDocumentNotificationHandler = Just $ wrapHandler state didSaveTextDocumentNotificationHandler
, LSP.Core.didCloseTextDocumentNotificationHandler = Just $ wrapHandler state nullHandler
, LSP.Core.cancelNotificationHandler = Just $ wrapHandler state nullHandler
, LSP.Core.responseHandler = Just $ wrapHandler state nullHandler
, LSP.Core.executeCommandHandler = Just $ wrapHandler state executeCommandHandler
, LSP.Core.documentFormattingHandler = Just $ wrapHandler state documentFormattingHandler
, LSP.Core.documentLinkHandler = Just $ wrapHandler state documentLinkHandler
}