2020-04-30 14:39:51 +04:00
|
|
|
|
2020-04-30 21:46:37 +04:00
|
|
|
import Data.Foldable (for_)
|
|
|
|
|
2020-05-19 21:26:57 +04:00
|
|
|
import Control.Concurrent
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
import Control.Exception as E
|
|
|
|
import Control.Lens
|
|
|
|
import Control.Monad
|
2020-05-08 00:09:14 +04:00
|
|
|
|
2020-06-02 15:47:56 +04:00
|
|
|
import Data.Default
|
2020-05-19 21:26:57 +04:00
|
|
|
import qualified Data.Text as Text
|
|
|
|
import Data.String.Interpolate (i)
|
2020-04-30 14:39:51 +04:00
|
|
|
|
2020-05-19 21:26:57 +04:00
|
|
|
import qualified Language.Haskell.LSP.Control as CTRL
|
|
|
|
import qualified Language.Haskell.LSP.Core as Core
|
|
|
|
import Language.Haskell.LSP.Diagnostics
|
|
|
|
import Language.Haskell.LSP.Messages as Msg
|
|
|
|
import qualified Language.Haskell.LSP.Types as J
|
|
|
|
import qualified Language.Haskell.LSP.Types.Lens as J
|
|
|
|
import qualified Language.Haskell.LSP.Utility as U
|
|
|
|
import Language.Haskell.LSP.VFS
|
|
|
|
|
|
|
|
import System.Environment
|
|
|
|
import System.Exit
|
|
|
|
import qualified System.Log as L
|
|
|
|
|
|
|
|
import ParseTree
|
|
|
|
import Parser
|
|
|
|
import Range
|
2020-06-02 15:47:56 +04:00
|
|
|
import AST hiding (def)
|
2020-06-01 18:17:33 +04:00
|
|
|
import HasErrors
|
2020-05-19 21:26:57 +04:00
|
|
|
import Pretty
|
2020-04-30 14:39:51 +04:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2020-05-19 21:26:57 +04:00
|
|
|
errCode <- mainLoop
|
|
|
|
exit errCode
|
|
|
|
|
|
|
|
mainLoop :: IO Int
|
|
|
|
mainLoop = do
|
|
|
|
chan <- atomically newTChan :: IO (TChan FromClientMessage)
|
|
|
|
|
|
|
|
let
|
|
|
|
callbacks = Core.InitializeCallbacks
|
|
|
|
{ Core.onInitialConfiguration = const $ Right ()
|
|
|
|
, Core.onConfigurationChange = const $ Right ()
|
|
|
|
, Core.onStartup = \lFuns -> do
|
|
|
|
_ <- forkIO $ eventLoop lFuns chan
|
|
|
|
return Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
Core.setupLogger (Just "log.txt") [] L.INFO
|
2020-06-02 15:47:56 +04:00
|
|
|
CTRL.run callbacks (lspHandlers chan) lspOptions (Just "log.txt")
|
2020-05-19 21:26:57 +04:00
|
|
|
return 0
|
|
|
|
`catches`
|
|
|
|
[ Handler \(e :: SomeException) -> do
|
|
|
|
print e
|
|
|
|
return 1
|
|
|
|
]
|
|
|
|
|
2020-06-02 15:47:56 +04:00
|
|
|
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 :: Core.Options
|
2020-06-02 16:27:19 +04:00
|
|
|
lspOptions = def
|
|
|
|
{ Core.textDocumentSync = Just syncOptions
|
|
|
|
, Core.executeCommandCommands = Just ["lsp-hello-command"]
|
|
|
|
}
|
2020-06-02 15:47:56 +04:00
|
|
|
|
|
|
|
lspHandlers :: TChan FromClientMessage -> Core.Handlers
|
2020-06-02 16:27:19 +04:00
|
|
|
lspHandlers rin = def
|
|
|
|
{ Core.initializedHandler = Just $ passHandler rin NotInitialized
|
|
|
|
, Core.renameHandler = Just $ passHandler rin ReqRename
|
|
|
|
, Core.hoverHandler = Just $ passHandler rin ReqHover
|
|
|
|
, Core.didOpenTextDocumentNotificationHandler = Just $ passHandler rin NotDidOpenTextDocument
|
|
|
|
, Core.didSaveTextDocumentNotificationHandler = Just $ passHandler rin NotDidSaveTextDocument
|
|
|
|
, Core.didChangeTextDocumentNotificationHandler = Just $ passHandler rin NotDidChangeTextDocument
|
|
|
|
, Core.didCloseTextDocumentNotificationHandler = Just $ passHandler rin NotDidCloseTextDocument
|
|
|
|
, Core.cancelNotificationHandler = Just $ passHandler rin NotCancelRequestFromClient
|
|
|
|
, Core.responseHandler = Just $ responseHandlerCb rin
|
|
|
|
, Core.codeActionHandler = Just $ passHandler rin ReqCodeAction
|
|
|
|
, Core.executeCommandHandler = Just $ passHandler rin ReqExecuteCommand
|
|
|
|
}
|
2020-06-02 15:47:56 +04:00
|
|
|
|
|
|
|
passHandler :: TChan FromClientMessage -> (a -> FromClientMessage) -> Core.Handler a
|
|
|
|
passHandler rin c notification = do
|
|
|
|
atomically $ writeTChan rin (c notification)
|
|
|
|
|
|
|
|
responseHandlerCb :: TChan FromClientMessage -> Core.Handler J.BareResponseMessage
|
|
|
|
responseHandlerCb _rin resp = do
|
|
|
|
U.logs $ "******** got ResponseMessage, ignoring:" ++ show resp
|
|
|
|
|
2020-05-19 21:26:57 +04:00
|
|
|
send :: Core.LspFuncs () -> FromServerMessage -> IO ()
|
|
|
|
send = Core.sendFunc
|
|
|
|
|
|
|
|
nextID :: Core.LspFuncs () -> IO J.LspId
|
|
|
|
nextID = Core.getNextReqId
|
|
|
|
|
|
|
|
eventLoop :: Core.LspFuncs () -> TChan FromClientMessage -> IO ()
|
|
|
|
eventLoop funs chan = do
|
|
|
|
forever do
|
|
|
|
msg <- atomically (readTChan chan)
|
|
|
|
|
|
|
|
U.logs [i|Client: ${msg}|]
|
|
|
|
|
|
|
|
case msg of
|
|
|
|
RspFromClient {} -> do
|
|
|
|
return ()
|
|
|
|
|
|
|
|
NotInitialized _notif -> do
|
|
|
|
let
|
|
|
|
registration = J.Registration
|
|
|
|
"lsp-haskell-registered"
|
|
|
|
J.WorkspaceExecuteCommand
|
|
|
|
Nothing
|
|
|
|
registrations = J.RegistrationParams $ J.List [registration]
|
|
|
|
|
|
|
|
rid <- nextID funs
|
|
|
|
send funs
|
|
|
|
$ ReqRegisterCapability
|
|
|
|
$ fmServerRegisterCapabilityRequest rid registrations
|
|
|
|
|
|
|
|
NotDidOpenTextDocument notif -> do
|
2020-05-21 23:28:26 +04:00
|
|
|
let
|
|
|
|
doc = notif
|
|
|
|
^.J.params
|
|
|
|
.J.textDocument
|
|
|
|
.J.uri
|
|
|
|
|
|
|
|
ver = notif
|
|
|
|
^.J.params
|
|
|
|
.J.textDocument
|
|
|
|
.J.version
|
|
|
|
|
|
|
|
collectErrors funs
|
|
|
|
(J.toNormalizedUri doc)
|
|
|
|
(J.uriToFilePath doc)
|
|
|
|
(Just ver)
|
|
|
|
|
|
|
|
NotDidChangeTextDocument notif -> do
|
2020-05-19 21:26:57 +04:00
|
|
|
let
|
|
|
|
doc = notif
|
|
|
|
^.J.params
|
|
|
|
.J.textDocument
|
|
|
|
.J.uri
|
|
|
|
|
|
|
|
collectErrors funs
|
|
|
|
(J.toNormalizedUri doc)
|
|
|
|
(J.uriToFilePath doc)
|
|
|
|
(Just 0)
|
|
|
|
|
2020-06-02 15:47:56 +04:00
|
|
|
_ -> putStrLn "unknown msg"
|
|
|
|
|
2020-05-21 23:28:26 +04:00
|
|
|
|
2020-05-19 21:26:57 +04:00
|
|
|
collectErrors
|
|
|
|
:: Core.LspFuncs ()
|
|
|
|
-> J.NormalizedUri
|
|
|
|
-> Maybe FilePath
|
|
|
|
-> Maybe Int
|
|
|
|
-> IO ()
|
|
|
|
collectErrors funs uri path version = do
|
|
|
|
case path of
|
|
|
|
Just fin -> do
|
|
|
|
(tree, errs) <- runParser contract fin
|
|
|
|
Core.publishDiagnosticsFunc funs 100 uri version
|
|
|
|
$ partitionBySource
|
|
|
|
$ map errorToDiag (errs <> errors tree)
|
|
|
|
|
|
|
|
errorToDiag :: Error -> J.Diagnostic
|
|
|
|
errorToDiag (Expected what instead (Range (sl, sc, _) (el, ec, _))) =
|
|
|
|
J.Diagnostic
|
|
|
|
(J.Range begin end)
|
|
|
|
(Just J.DsError)
|
|
|
|
Nothing
|
|
|
|
(Just "ligo-lsp")
|
2020-06-01 10:28:47 +04:00
|
|
|
(Text.pack [i|Expected #{what}|])
|
2020-05-19 21:26:57 +04:00
|
|
|
(Just $ J.List[])
|
|
|
|
where
|
|
|
|
begin = J.Position (sl - 1) (sc - 1)
|
|
|
|
end = J.Position (el - 1) (ec - 1)
|
|
|
|
|
|
|
|
exit :: Int -> IO ()
|
|
|
|
exit 0 = exitSuccess
|
|
|
|
exit n = exitWith (ExitFailure n)
|