{-# LANGUAGE DataKinds #-}

-- | The handlers exposed by the language server.
module Futhark.LSP.Handlers (handlers) where

import Colog.Core (logStringStderr, (<&))
import Control.Lens ((^.))
import Data.Aeson.Types (Value (Array, String))
import Data.IORef
import Data.Proxy (Proxy (..))
import Data.Vector qualified as V
import Futhark.LSP.Compile (tryReCompile, tryTakeStateFromIORef)
import Futhark.LSP.State (State (..))
import Futhark.LSP.Tool (findDefinitionRange, getHoverInfoFromState)
import Language.LSP.Protocol.Lens (HasUri (uri))
import Language.LSP.Protocol.Message
import Language.LSP.Protocol.Types
import Language.LSP.Server (Handlers, LspM, notificationHandler, requestHandler)

onInitializeHandler :: Handlers (LspM ())
onInitializeHandler :: Handlers (LspT () IO)
onInitializeHandler = forall (m :: Method 'ClientToServer 'Notification) (f :: * -> *).
SMethod m -> Handler f m -> Handlers f
notificationHandler SMethod 'Method_Initialized
SMethod_Initialized forall a b. (a -> b) -> a -> b
$ \TNotificationMessage 'Method_Initialized
_msg ->
  forall (m :: * -> *). MonadIO m => LogAction m String
logStringStderr forall (m :: * -> *) msg. LogAction m msg -> msg -> m ()
<& String
"Initialized"

onHoverHandler :: IORef State -> Handlers (LspM ())
onHoverHandler :: IORef State -> Handlers (LspT () IO)
onHoverHandler IORef State
state_mvar =
  forall (m :: Method 'ClientToServer 'Request) (f :: * -> *).
SMethod m -> Handler f m -> Handlers f
requestHandler SMethod 'Method_TextDocumentHover
SMethod_TextDocumentHover forall a b. (a -> b) -> a -> b
$ \TRequestMessage 'Method_TextDocumentHover
req Either ResponseError (Hover |? Null) -> LspT () IO ()
responder -> do
    let TRequestMessage Text
_ LspId 'Method_TextDocumentHover
_ SMethod 'Method_TextDocumentHover
_ (HoverParams TextDocumentIdentifier
doc Position
pos Maybe ProgressToken
_workDone) = TRequestMessage 'Method_TextDocumentHover
req
        Position UInt
l UInt
c = Position
pos
        file_path :: Maybe String
file_path = Uri -> Maybe String
uriToFilePath forall a b. (a -> b) -> a -> b
$ TextDocumentIdentifier
doc forall s a. s -> Getting a s a -> a
^. forall s a. HasUri s a => Lens' s a
uri
    forall (m :: * -> *). MonadIO m => LogAction m String
logStringStderr forall (m :: * -> *) msg. LogAction m msg -> msg -> m ()
<& (String
"Got hover request: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show (Maybe String
file_path, Position
pos))
    State
state <- IORef State -> Maybe String -> LspT () IO State
tryTakeStateFromIORef IORef State
state_mvar Maybe String
file_path
    Either ResponseError (Hover |? Null) -> LspT () IO ()
responder forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. b -> a |? b
InR Null
Null) forall a b. a -> a |? b
InL forall a b. (a -> b) -> a -> b
$ State -> Maybe String -> Int -> Int -> Maybe Hover
getHoverInfoFromState State
state Maybe String
file_path (forall a. Enum a => a -> Int
fromEnum UInt
l forall a. Num a => a -> a -> a
+ Int
1) (forall a. Enum a => a -> Int
fromEnum UInt
c forall a. Num a => a -> a -> a
+ Int
1)

onDocumentFocusHandler :: IORef State -> Handlers (LspM ())
onDocumentFocusHandler :: IORef State -> Handlers (LspT () IO)
onDocumentFocusHandler IORef State
state_mvar =
  forall (m :: Method 'ClientToServer 'Notification) (f :: * -> *).
SMethod m -> Handler f m -> Handlers f
notificationHandler (forall {f :: MessageDirection} {t :: MessageKind} (s :: Symbol).
KnownSymbol s =>
Proxy s -> SMethod ('Method_CustomMethod s)
SMethod_CustomMethod (forall {k} (t :: k). Proxy t
Proxy @"custom/onFocusTextDocument")) forall a b. (a -> b) -> a -> b
$ \TNotificationMessage
  ('Method_CustomMethod "custom/onFocusTextDocument")
msg -> do
    forall (m :: * -> *). MonadIO m => LogAction m String
logStringStderr forall (m :: * -> *) msg. LogAction m msg -> msg -> m ()
<& String
"Got custom request: onFocusTextDocument"
    let TNotificationMessage Text
_ SMethod ('Method_CustomMethod "custom/onFocusTextDocument")
_ (Array Array
vector_param) = TNotificationMessage
  ('Method_CustomMethod "custom/onFocusTextDocument")
msg
        String Text
focused_uri = forall a. Vector a -> a
V.head Array
vector_param -- only one parameter passed from the client
    IORef State -> Maybe String -> LspT () IO ()
tryReCompile IORef State
state_mvar (Uri -> Maybe String
uriToFilePath (Text -> Uri
Uri Text
focused_uri))

goToDefinitionHandler :: IORef State -> Handlers (LspM ())
goToDefinitionHandler :: IORef State -> Handlers (LspT () IO)
goToDefinitionHandler IORef State
state_mvar =
  forall (m :: Method 'ClientToServer 'Request) (f :: * -> *).
SMethod m -> Handler f m -> Handlers f
requestHandler SMethod 'Method_TextDocumentDefinition
SMethod_TextDocumentDefinition forall a b. (a -> b) -> a -> b
$ \TRequestMessage 'Method_TextDocumentDefinition
req Either ResponseError (Definition |? ([DefinitionLink] |? Null))
-> LspT () IO ()
responder -> do
    let TRequestMessage Text
_ LspId 'Method_TextDocumentDefinition
_ SMethod 'Method_TextDocumentDefinition
_ (DefinitionParams TextDocumentIdentifier
doc Position
pos Maybe ProgressToken
_workDone Maybe ProgressToken
_partial) = TRequestMessage 'Method_TextDocumentDefinition
req
        Position UInt
l UInt
c = Position
pos
        file_path :: Maybe String
file_path = Uri -> Maybe String
uriToFilePath forall a b. (a -> b) -> a -> b
$ TextDocumentIdentifier
doc forall s a. s -> Getting a s a -> a
^. forall s a. HasUri s a => Lens' s a
uri
    forall (m :: * -> *). MonadIO m => LogAction m String
logStringStderr forall (m :: * -> *) msg. LogAction m msg -> msg -> m ()
<& (String
"Got goto definition: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show (Maybe String
file_path, Position
pos))
    State
state <- IORef State -> Maybe String -> LspT () IO State
tryTakeStateFromIORef IORef State
state_mvar Maybe String
file_path
    case State -> Maybe String -> Int -> Int -> Maybe Location
findDefinitionRange State
state Maybe String
file_path (forall a. Enum a => a -> Int
fromEnum UInt
l forall a. Num a => a -> a -> a
+ Int
1) (forall a. Enum a => a -> Int
fromEnum UInt
c forall a. Num a => a -> a -> a
+ Int
1) of
      Maybe Location
Nothing -> Either ResponseError (Definition |? ([DefinitionLink] |? Null))
-> LspT () IO ()
responder forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall a b. b -> a |? b
InR forall a b. (a -> b) -> a -> b
$ forall a b. b -> a |? b
InR Null
Null
      Just Location
loc -> Either ResponseError (Definition |? ([DefinitionLink] |? Null))
-> LspT () IO ()
responder forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall a b. a -> a |? b
InL forall a b. (a -> b) -> a -> b
$ (Location |? [Location]) -> Definition
Definition forall a b. (a -> b) -> a -> b
$ forall a b. a -> a |? b
InL Location
loc

onDocumentSaveHandler :: IORef State -> Handlers (LspM ())
onDocumentSaveHandler :: IORef State -> Handlers (LspT () IO)
onDocumentSaveHandler IORef State
state_mvar =
  forall (m :: Method 'ClientToServer 'Notification) (f :: * -> *).
SMethod m -> Handler f m -> Handlers f
notificationHandler SMethod 'Method_TextDocumentDidSave
SMethod_TextDocumentDidSave forall a b. (a -> b) -> a -> b
$ \TNotificationMessage 'Method_TextDocumentDidSave
msg -> do
    let TNotificationMessage Text
_ SMethod 'Method_TextDocumentDidSave
_ (DidSaveTextDocumentParams TextDocumentIdentifier
doc Maybe Text
_text) = TNotificationMessage 'Method_TextDocumentDidSave
msg
        file_path :: Maybe String
file_path = Uri -> Maybe String
uriToFilePath forall a b. (a -> b) -> a -> b
$ TextDocumentIdentifier
doc forall s a. s -> Getting a s a -> a
^. forall s a. HasUri s a => Lens' s a
uri
    forall (m :: * -> *). MonadIO m => LogAction m String
logStringStderr forall (m :: * -> *) msg. LogAction m msg -> msg -> m ()
<& (String
"Saved document: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show TextDocumentIdentifier
doc)
    IORef State -> Maybe String -> LspT () IO ()
tryReCompile IORef State
state_mvar Maybe String
file_path

onDocumentChangeHandler :: IORef State -> Handlers (LspM ())
onDocumentChangeHandler :: IORef State -> Handlers (LspT () IO)
onDocumentChangeHandler IORef State
state_mvar =
  forall (m :: Method 'ClientToServer 'Notification) (f :: * -> *).
SMethod m -> Handler f m -> Handlers f
notificationHandler SMethod 'Method_TextDocumentDidChange
SMethod_TextDocumentDidChange forall a b. (a -> b) -> a -> b
$ \TNotificationMessage 'Method_TextDocumentDidChange
msg -> do
    let TNotificationMessage Text
_ SMethod 'Method_TextDocumentDidChange
_ (DidChangeTextDocumentParams VersionedTextDocumentIdentifier
doc [TextDocumentContentChangeEvent]
_content) = TNotificationMessage 'Method_TextDocumentDidChange
msg
        file_path :: Maybe String
file_path = Uri -> Maybe String
uriToFilePath forall a b. (a -> b) -> a -> b
$ VersionedTextDocumentIdentifier
doc forall s a. s -> Getting a s a -> a
^. forall s a. HasUri s a => Lens' s a
uri
    IORef State -> Maybe String -> LspT () IO ()
tryReCompile IORef State
state_mvar Maybe String
file_path

-- Some clients (Eglot) sends open/close events whether we want them
-- or not, so we better be prepared to ignore them.
onDocumentOpenHandler :: Handlers (LspM ())
onDocumentOpenHandler :: Handlers (LspT () IO)
onDocumentOpenHandler = forall (m :: Method 'ClientToServer 'Notification) (f :: * -> *).
SMethod m -> Handler f m -> Handlers f
notificationHandler SMethod 'Method_TextDocumentDidOpen
SMethod_TextDocumentDidOpen forall a b. (a -> b) -> a -> b
$ \TNotificationMessage 'Method_TextDocumentDidOpen
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

onDocumentCloseHandler :: Handlers (LspM ())
onDocumentCloseHandler :: Handlers (LspT () IO)
onDocumentCloseHandler = forall (m :: Method 'ClientToServer 'Notification) (f :: * -> *).
SMethod m -> Handler f m -> Handlers f
notificationHandler SMethod 'Method_TextDocumentDidClose
SMethod_TextDocumentDidClose forall a b. (a -> b) -> a -> b
$ \TNotificationMessage 'Method_TextDocumentDidClose
_msg -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

-- Sent by Eglot when first connecting - not sure when else it might
-- be sent.
onWorkspaceDidChangeConfiguration :: IORef State -> Handlers (LspM ())
onWorkspaceDidChangeConfiguration :: IORef State -> Handlers (LspT () IO)
onWorkspaceDidChangeConfiguration IORef State
_state_mvar =
  forall (m :: Method 'ClientToServer 'Notification) (f :: * -> *).
SMethod m -> Handler f m -> Handlers f
notificationHandler SMethod 'Method_WorkspaceDidChangeConfiguration
SMethod_WorkspaceDidChangeConfiguration forall a b. (a -> b) -> a -> b
$ \TNotificationMessage 'Method_WorkspaceDidChangeConfiguration
_ ->
    forall (m :: * -> *). MonadIO m => LogAction m String
logStringStderr forall (m :: * -> *) msg. LogAction m msg -> msg -> m ()
<& String
"WorkspaceDidChangeConfiguration"

-- | Given an 'IORef' tracking the state, produce a set of handlers.
-- When we want to add more features to the language server, this is
-- the thing to change.
handlers :: IORef State -> ClientCapabilities -> Handlers (LspM ())
handlers :: IORef State -> ClientCapabilities -> Handlers (LspT () IO)
handlers IORef State
state_mvar ClientCapabilities
_ =
  forall a. Monoid a => [a] -> a
mconcat
    [ Handlers (LspT () IO)
onInitializeHandler,
      Handlers (LspT () IO)
onDocumentOpenHandler,
      Handlers (LspT () IO)
onDocumentCloseHandler,
      IORef State -> Handlers (LspT () IO)
onDocumentSaveHandler IORef State
state_mvar,
      IORef State -> Handlers (LspT () IO)
onDocumentChangeHandler IORef State
state_mvar,
      IORef State -> Handlers (LspT () IO)
onDocumentFocusHandler IORef State
state_mvar,
      IORef State -> Handlers (LspT () IO)
goToDefinitionHandler IORef State
state_mvar,
      IORef State -> Handlers (LspT () IO)
onHoverHandler IORef State
state_mvar,
      IORef State -> Handlers (LspT () IO)
onWorkspaceDidChangeConfiguration IORef State
state_mvar
    ]