diff --git a/cm6/lsp.js b/bundles/cm6/lsp.js similarity index 100% rename from cm6/lsp.js rename to bundles/cm6/lsp.js diff --git a/cm6/try.js b/bundles/cm6/try.js similarity index 100% rename from cm6/try.js rename to bundles/cm6/try.js diff --git a/bundles/grammar/rex.xq b/bundles/grammar/rex.xq index d703427..1b88b7b 100644 --- a/bundles/grammar/rex.xq +++ b/bundles/grammar/rex.xq @@ -1,3 +1,4 @@ +(: parse xq and save result:) import module namespace p="xq4" at "C:\Users\mrwhe\git\quodatum\basex-lsp\webapp\lsp\xq4.xqm"; declare variable $generator.xpath :="https://raw.githubusercontent.com/dnovatchev/Articles/refs/heads/main/Generators/Code/generator.xq"; @@ -5,8 +6,8 @@ declare variable $pdfbox-good.xqm :="../../test/sample.docs/pdfbox.xqm"; declare variable $dest:="C:\Users\mrwhe\git\quodatum\basex-lsp\test\sample.docs\parse-pdfbox.xml"; let $parse:= $pdfbox-good.xqm -=>fetch:text() -=>lazy:cache() -=>p:parse-Module() + =>fetch:text() + =>lazy:cache() + =>p:parse-Module() return file:write($dest,$parse) \ No newline at end of file diff --git a/webapp/lsp/jsonrpc.xqm b/webapp/lsp/jsonrpc.xqm index a751e71..232e4dc 100644 --- a/webapp/lsp/jsonrpc.xqm +++ b/webapp/lsp/jsonrpc.xqm @@ -49,11 +49,8 @@ declare function rpc:method-initialize($json as map(*)) as map(*) { -{ - "jsonrpc": "2.0", - "id": $json?id, - "result": json:doc("etc/capabilities.json",{"format":"w3"}) -} + json:doc("etc/capabilities.json",{"format":"w3"}) + =>rpc:result($json) }; (:~ initialized response :) @@ -84,7 +81,7 @@ as map(*) }; (:~ rpc response to $json msg :) -declare function rpc:response($result,$json as map(*)) +declare function rpc:result($result,$json as map(*)) as map(*) { map{ @@ -92,4 +89,20 @@ map{ "id": $json?id, "result": $result } +}; + +(:~ response when error :) +declare function rpc:error($message as xs:string,$json as map(*)) +{ + { + "jsonrpc": "2.0", + "id": $json?id, + "error": { + "code": -32803, + "message": $message, + "data": { "documentUri": "file:///example.txt", + "reason": "Syntax block" + } + } +} }; \ No newline at end of file diff --git a/webapp/lsp/lsp-text.xqm b/webapp/lsp/lsp-text.xqm index 9440e94..5a1c05a 100644 --- a/webapp/lsp/lsp-text.xqm +++ b/webapp/lsp/lsp-text.xqm @@ -31,7 +31,7 @@ The last line.` , `A hover at { pos:ln-col($json?params?position) } uri: {$json?params?textDocument?uri} ` ] -return rpc:response({"contents":$r},$json) +return rpc:result({"contents":$r},$json) }; @@ -53,16 +53,17 @@ as map(*)? { let $uri:=$json?params?textDocument?uri let $text:=docs:get(ws:id(), $uri, "textDocument")?text + let $xml:=docs:get(ws:id(), $uri, "parse") + return if($xml/self::ERROR) + then rpc:error("Syntax errors found.",$json) + else + let $xml:=$xml update {lsp-text:tidy(.)} let $fmt:=`(: formatting to do :) ` - return map{ - "jsonrpc": "2.0", - "id": $json?id, - "result":[{ + return rpc:result([{ "range":pos:full-range($text), - "newText": $fmt || $text - }] - } + "newText": $fmt || string($xml) + }],$json) }; (:~ didOpen method response :) @@ -92,4 +93,19 @@ as map(*)? { let $_:=trace($json?method,"unknown") return () +}; + +(:~ ensure spaces around := :) +declare %updating function lsp-text:tidy($doc){ + for $a in $doc//TOKEN[.=":="] + let $before:= $a/preceding-sibling::node()[1] + let $after:= $a/following-sibling::node()[1] + + return ( + if($before instance of element(*)) + then insert node " " before $a else (), + + if($after instance of element(*)) + then insert node " " after $a else () + ) }; \ No newline at end of file