95 lines
No EOL
1.8 KiB
Text
95 lines
No EOL
1.8 KiB
Text
(:~
|
|
: jsonrpc
|
|
: @author andy bunce
|
|
:)
|
|
module namespace rpc = 'rpc';
|
|
import module namespace lsp-text = 'lsp-text' at "lsp-text.xqm";
|
|
|
|
(: map methods to functions :)
|
|
declare variable $rpc:Methods:=map:merge((
|
|
map{
|
|
"initialize" : rpc:method-initialize#1,
|
|
"initialized" : rpc:method-initialized#1,
|
|
"workspace/didChangeConfiguration" :rpc:method-unknown#1
|
|
},
|
|
$lsp-text:methods
|
|
));
|
|
|
|
(:~ return map if $msg is jsonrpc else empty :)
|
|
declare
|
|
function rpc:parse($msg as xs:string)
|
|
as map(*)?
|
|
{
|
|
try {
|
|
let $json:=parse-json($msg)
|
|
return if($json?jsonrpc="2.0" and exists($json?method))
|
|
then $json
|
|
else ()
|
|
} catch *{
|
|
()
|
|
}
|
|
|
|
};
|
|
|
|
(:~ send reply to $json message
|
|
get functions for methods
|
|
evaluate function with message
|
|
send any responses
|
|
:)
|
|
declare
|
|
function rpc:reply($json as map(*))
|
|
as empty-sequence() {
|
|
let $f :=$rpc:Methods?($json?method)
|
|
let $response := $f!.($json)
|
|
return $response!ws:send(.=>trace("REPLY: "),ws:id())
|
|
};
|
|
|
|
(:~ canned initialize response :)
|
|
declare
|
|
function rpc:method-initialize($json as map(*))
|
|
as map(*)
|
|
{
|
|
{
|
|
"jsonrpc": "2.0",
|
|
"id": $json?id,
|
|
"result": json:doc("etc/capabilities.json",{"format":"w3"})
|
|
}
|
|
};
|
|
|
|
(:~ initialized response :)
|
|
declare
|
|
function rpc:method-initialized($json as map(*))
|
|
as empty-sequence()
|
|
{
|
|
ws:set(ws:id(),"initialized", true())
|
|
};
|
|
|
|
(:~ unknown method response :)
|
|
declare
|
|
function rpc:method-unknown($json as map(*))
|
|
as map(*)?
|
|
{
|
|
let $_:=trace($json?method,"unknown")
|
|
return ()
|
|
};
|
|
|
|
(:~ rpc log message :)
|
|
declare function rpc:log($msg as xs:string)
|
|
as map(*)
|
|
{
|
|
{"jsonrpc": "2.0",
|
|
"method":"window/logMessage",
|
|
"params":{"type":1, "message": $msg}
|
|
}
|
|
};
|
|
|
|
(:~ rpc response to $json msg :)
|
|
declare function rpc:response($result,$json as map(*))
|
|
as map(*)
|
|
{
|
|
map{
|
|
"jsonrpc": "2.0",
|
|
"id": $json?id,
|
|
"result": $result
|
|
}
|
|
}; |