packages feed

zwirn-0.2.2.0: app/zwirnzi/LSP/Main.hs

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}

module LSP.Main where

import Control.Concurrent.MVar
import Control.Monad.IO.Class
import LSP.Handlers.Action
import LSP.Handlers.Command
import LSP.Handlers.Completion
import LSP.Handlers.File
import LSP.Handlers.Hover
import LSP.Handlers.InlayHint
import LSP.Util
import qualified Language.LSP.Protocol.Message as LSP
import Language.LSP.Protocol.Types ()
import qualified Language.LSP.Protocol.Types as LSP
import Language.LSP.Server
import Zwirn.Language.Compiler

main :: MVar Environment -> IO Int
main envMV = runServer def
  where
    def :: ServerDefinition ()
    def =
      ServerDefinition
        { parseConfig = \oldEnv _ -> Right oldEnv,
          onConfigChange = const $ pure (),
          defaultConfig = (),
          configSection = "demo",
          doInitialize = \env _req -> pure $ Right env,
          staticHandlers = \_caps -> handlers envMV,
          interpretHandler = \env -> Iso (runLspT env) liftIO,
          options = lspOptions
        }

syncOptions :: LSP.TextDocumentSyncOptions
syncOptions =
  LSP.TextDocumentSyncOptions
    { LSP._openClose = Just True,
      LSP._change = Just LSP.TextDocumentSyncKind_Incremental,
      LSP._willSave = Just False,
      LSP._willSaveWaitUntil = Just False,
      LSP._save = Just $ LSP.InR $ LSP.SaveOptions $ Just False
    }

lspOptions :: Options
lspOptions =
  defaultOptions
    { optTextDocumentSync = Just syncOptions,
      optExecuteCommandCommands = Just ["zwirn-eval"]
    }

handlers :: MVar Environment -> Handlers LSP
handlers envMV =
  mconcat
    [ notificationHandler LSP.SMethod_Initialized $ \_not -> debug "Initialising server.",
      notificationHandler LSP.SMethod_WorkspaceDidChangeConfiguration $ \_ -> return (),
      codeActionHandler,
      -- codeLensHandler,
      -- codeLensResolveHandler,
      inlayHintHandler envMV,
      execCommandHandler envMV,
      didSaveHandler envMV,
      didOpenHandler envMV,
      didCloseHandler,
      didChangeHandler envMV,
      hoverHandler envMV,
      completionHandler envMV,
      cancelationHandler
    ]

-- zed doesn't support showing code lenses in the document yet
-- but they might be a good way to display different available commands
-- codeLensHandler :: Handlers LSP
-- codeLensHandler = requestHandler LSP.SMethod_TextDocumentCodeLens $ \_ _ -> return ()

-- codeLensResolveHandler :: Handlers LSP
-- codeLensResolveHandler = requestHandler LSP.SMethod_CodeLensResolve $ \_ _ -> return ()