packages feed

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

module LSP.Util where

import Control.Monad (void)
import qualified Data.Text as T
import qualified Language.LSP.Protocol.Message as LSP
import qualified Language.LSP.Protocol.Types as LSP
import Language.LSP.Server (LspM, sendNotification)
import Zwirn.Language.Location

type LSP = LspM ()

sendError :: T.Text -> LSP ()
sendError err =
  void $ sendNotification
      LSP.SMethod_WindowShowMessage
      ( LSP.ShowMessageParams
          LSP.MessageType_Error
          err
      )

sendInfo :: T.Text -> LSP ()
sendInfo msg =
  void $ sendNotification
      LSP.SMethod_WindowShowMessage
      ( LSP.ShowMessageParams
          LSP.MessageType_Info
          msg
      )

fromLSP :: T.Text -> LSP.Range -> RealSrcLoc
fromLSP t (LSP.Range (LSP.Position lst cst) (LSP.Position len cen)) = RealSrcLoc t (fromIntegral lst) (fromIntegral cst + 1) (fromIntegral len) (fromIntegral cen + 1)

toLSP :: RealSrcLoc -> LSP.Range
toLSP (RealSrcLoc _ lst cst len cen) = LSP.Range (LSP.Position (fromIntegral lst) (fromIntegral cst - 1)) (LSP.Position (fromIntegral len) (fromIntegral cen - 1))

fromLSPPos :: LSP.Position -> Position
fromLSPPos (LSP.Position l c) = Position (fromIntegral l) (fromIntegral c + 1)

toLSPPos :: Position -> LSP.Position
toLSPPos (Position l c) = LSP.Position (fromIntegral l) (fromIntegral c - 1)