[mod] lsprpc->rpc

This commit is contained in:
Andy Bunce 2025-08-07 23:06:42 +01:00
parent 4cf201e71a
commit 49b4b1529a
6 changed files with 51 additions and 36 deletions

View file

@ -11,4 +11,5 @@ cccccc`;
(: return pos:resolvePosition($text,$p) :) (: return pos:resolvePosition($text,$p) :)
pos:apply-changes($text,$msg?params?contentChanges) pos:apply-changes($text,$msg?params?contentChanges)
(: $msg?params?contentChanges :) (: $msg?params?contentChanges :)
,$msg

View file

@ -6,6 +6,7 @@
module namespace docs="lsp/docs"; module namespace docs="lsp/docs";
import module namespace p="xq4" at "xq4.xqm"; import module namespace p="xq4" at "xq4.xqm";
import module namespace pos="lsp/position" at "position.xqm"; import module namespace pos="lsp/position" at "position.xqm";
import module namespace rpc = 'rpc' at 'jsonrpc.xqm';
(: save $textDocument data as session $socket properties (: save $textDocument data as session $socket properties
@return uri @return uri
@ -52,9 +53,9 @@ let $text:=if($ver eq 1+ $file?version)
else error("bad ver") else error("bad ver")
let $file:=$file=>map:put("version",$ver)=>map:put("text",$text) let $file:=$file=>map:put("version",$ver)=>map:put("text",$text)
return (ws:set($socket,$key,$file), return ( ws:set($socket,$key,$file)
`{$uri} V{ $ver} text: { $text}` , $uri)
)
}; };
(:~ close a file :) (:~ close a file :)
@ -91,5 +92,17 @@ declare function docs:key(
) as xs:string? ) as xs:string?
{ {
ws:get($socket,"files")($file)($property) ws:get($socket,"files")($file)($property)
};
declare function docs:parse(
$socket as xs:string,
$file as xs:string
)as map(*)?
{
let $text:=docs:key($socket,$file,"textDocument")
let $xml:= p:parse-Module($text)=>prof:time("⏱️ p:parse-Module " || $file)
return (
ws:set($socket,docs:key($socket,$file,"parse"),$xml),
rpc:log(`done {$xml/name(.)}`)
)
}; };

View file

@ -6,7 +6,7 @@
"triggerCharacters": [ "\"", ":" ], "triggerCharacters": [ "\"", ":" ],
"documentSelector": [{ "language": "xquery" }] "documentSelector": [{ "language": "xquery" }]
}, },
"hoverProvider": true, "hoverProvider": false,
"documentSymbolProvider": true, "documentSymbolProvider": true,
"documentRangeFormattingProvider": false, "documentRangeFormattingProvider": false,
"colorProvider": {}, "colorProvider": {},

View file

@ -2,22 +2,22 @@
: jsonrpc : jsonrpc
: @author andy bunce : @author andy bunce
:) :)
module namespace lsprpc = 'lsprpc'; module namespace rpc = 'rpc';
import module namespace lsp-text = 'lsp-text' at "lsp-text.xqm"; import module namespace lsp-text = 'lsp-text' at "lsp-text.xqm";
(: map methods to functions :) (: map methods to functions :)
declare variable $lsprpc:methods:=map:merge(( declare variable $rpc:methods:=map:merge((
map{ map{
"initialize" : lsprpc:method-initialize#1, "initialize" : rpc:method-initialize#1,
"initialized" : lsprpc:method-initialized#1, "initialized" : rpc:method-initialized#1,
"workspace/didChangeConfiguration" :lsprpc:method-unknown#1 "workspace/didChangeConfiguration" :rpc:method-unknown#1
}, },
$lsp-text:methods $lsp-text:methods
)); ));
(:~ return map if $msg is jsonrpc else empty :) (:~ return map if $msg is jsonrpc else empty :)
declare declare
function lsprpc:parse($msg as xs:string) function rpc:parse($msg as xs:string)
as map(*)? as map(*)?
{ {
try { try {
@ -33,17 +33,17 @@ as map(*)?
(:~ send replay to $json :) (:~ send replay to $json :)
declare declare
function lsprpc:reply($json as map(*)) function rpc:reply($json as map(*))
as empty-sequence() { as empty-sequence() {
let $method := $json?method let $method := $json?method
let $f :=$lsprpc:methods?($method) let $f :=$rpc:methods?($method)
let $response := $f!.($json) let $response := $f!.($json)
return if(exists($response)) then ws:send($response=>trace("REPLY: "),ws:id()) return if(exists($response)) then ws:send($response=>trace("REPLY: "),ws:id())
}; };
(:~ canned initialize response :) (:~ canned initialize response :)
declare declare
function lsprpc:method-initialize($json as map(*)) function rpc:method-initialize($json as map(*))
as map(*) as map(*)
{ {
{ {
@ -55,7 +55,7 @@ as map(*)
(:~ initialized response :) (:~ initialized response :)
declare declare
function lsprpc:method-initialized($json as map(*)) function rpc:method-initialized($json as map(*))
as empty-sequence() as empty-sequence()
{ {
ws:set(ws:id(),"initialized", true()) ws:set(ws:id(),"initialized", true())
@ -63,10 +63,19 @@ as empty-sequence()
(:~ unknown method response :) (:~ unknown method response :)
declare declare
function lsprpc:method-unknown($json as map(*)) function rpc:method-unknown($json as map(*))
as map(*)? as map(*)?
{ {
let $_:=trace($json?method,"unknown") let $_:=trace($json?method,"unknown")
return () 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}
}
};

View file

@ -1,8 +1,9 @@
(:~ handle text messages (:~ handle textDocument messages
: @author andy bunce : @author andy bunce
:) :)
module namespace lsp-text = 'lsp-text'; module namespace lsp-text = 'lsp-text';
import module namespace docs="lsp/docs" at "docs.xqm"; import module namespace docs="lsp/docs" at "docs.xqm";
import module namespace rpc = 'rpc' at 'jsonrpc.xqm';
declare variable $lsp-text:methods:=map{ declare variable $lsp-text:methods:=map{
"textDocument/didOpen": lsp-text:didOpen#1, "textDocument/didOpen": lsp-text:didOpen#1,
@ -53,7 +54,7 @@ as map(*)?
(:~ didOpen method response :) (:~ didOpen method response :)
declare declare
function lsp-text:didOpen($json as map(*)) function lsp-text:didOpen($json as map(*))
as empty-sequence() as map(*)?
{ {
let $uri:=docs:open(ws:id(),$json?params)=>prof:time("⏱️ doc:save ") let $uri:=docs:open(ws:id(),$json?params)=>prof:time("⏱️ doc:save ")
@ -65,13 +66,9 @@ as empty-sequence()
{ 'cache': true() } { 'cache': true() }
) )
:) :)
return ws:send( return rpc:log("saved " || $uri)
{"jsonrpc": "2.0",
"method":"window/logMessage",
"params":{"type":1,"message":"saved " || $uri}
},
ws:id()
)
}; };
(:~ didChange method response :) (:~ didChange method response :)
@ -79,15 +76,10 @@ declare
function lsp-text:didChange($json as map(*)) function lsp-text:didChange($json as map(*))
as map(*)? as map(*)?
{ {
let $msg:=docs:change(ws:id(),$json?params)=>prof:time("⏱️ doc:change ") let $uri:=docs:change(ws:id(),$json?params)=>prof:time("⏱️ doc:change ")
return docs:parse(ws:id(),$uri)
return ws:send(
{"jsonrpc": "2.0",
"method":"window/logMessage",
"params":{"type":1,"message":"changed " || $msg}
},
ws:id()
)
}; };

View file

@ -4,7 +4,7 @@
:) :)
module namespace lsp-ws = 'lsp-ws'; module namespace lsp-ws = 'lsp-ws';
import module namespace lsprpc = 'lsprpc' at 'jsonrpc.xqm'; import module namespace rpc = 'rpc' at 'jsonrpc.xqm';
declare declare
@ -36,9 +36,9 @@ function lsp-ws:message(
$message as xs:string $message as xs:string
) as empty-sequence() { ) as empty-sequence() {
let $json := lsprpc:parse($message=>trace("MSG ")) let $json := rpc:parse($message=>trace("MSG "))
return if(exists($json)) return if(exists($json))
then lsprpc:reply($json) then rpc:reply($json)
else message($message,"bad RPC: ") else message($message,"bad RPC: ")
}; };