From e9a2d46272ee9cae01f4ba5aeb85b07384e6e33e Mon Sep 17 00:00:00 2001 From: Andy Bunce Date: Sun, 17 Aug 2025 22:59:08 +0100 Subject: [PATCH] [mod] --- compose.yaml | 2 +- package.json | 1 + webapp/lsp/jsonrpc.xqm | 11 +++++++++++ webapp/lsp/lsp-diags.xqm | 17 +++++++++-------- webapp/lsp/lsp-text.xqm | 15 +++++++-------- webapp/lsp/position.xqm | 10 ++++++++-- webapp/static/clients/codemirror/script.js | 9 ++++++--- 7 files changed, 43 insertions(+), 22 deletions(-) diff --git a/compose.yaml b/compose.yaml index 05bd149..46add6f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -18,7 +18,7 @@ services: # - ./jars:/srv/basex/lib/custom # - ./repo:/srv/basex/repo environment: - - "SERVER_OPTS= -d" + - "SERVER_OPTS= " volumes: basex-lsp: diff --git a/package.json b/package.json index 583e588..253874f 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "build-cm": "rollup bundles/src/lsp.js -m true -f iife -o webapp/static/clients/codemirror/lsp.bundle.js -p node-resolve,tla --output.name lsp", "min-cm": "cd webapp/static/clients/codemirror && npx minify lsp.bundle.js > lsp.bundle.min.js ", "min": "cd dist && npx minify cm6.bundle.js > cm6.bundle.min.js && npx minify lsp.bundle.js > lsp.bundle.min.js", + "javac":"cd bundles/grammar && javac -cp %BASEX12%\\BaseX.jar -d build xq4.java && cd build && jar cf ../xq4.jar . " } } diff --git a/webapp/lsp/jsonrpc.xqm b/webapp/lsp/jsonrpc.xqm index 11f41a2..a751e71 100644 --- a/webapp/lsp/jsonrpc.xqm +++ b/webapp/lsp/jsonrpc.xqm @@ -81,4 +81,15 @@ as map(*) "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 +} }; \ No newline at end of file diff --git a/webapp/lsp/lsp-diags.xqm b/webapp/lsp/lsp-diags.xqm index 5507639..eff59f5 100644 --- a/webapp/lsp/lsp-diags.xqm +++ b/webapp/lsp/lsp-diags.xqm @@ -33,7 +33,7 @@ declare function lsp-diags:publish( as map(*){ let $diagnostics:=if($xml/self::ERROR) then array{lsp-diags:parse-error($text, $xml)} - else array{lsp-diags:parse-xquery($text,$xml)} + else array{ lsp-diags:parse-xquery($text,$xml)} return {"jsonrpc": "2.0", "method":"textDocument/publishDiagnostics", @@ -55,21 +55,22 @@ lsp-diags:nostic(pos:Range(pos:toPosition($text, $xml/@b), lsp-diags:nostic(pos:Range(pos:toPosition($text, $xml/@e +1 ), pos:toPosition($text, string-length($text)-1)), $lsp-diags:severities('warning'), - "Previous parse error") + "Unparsed due to previous parser error.") }; (: test data :) declare function lsp-diags:parse-xquery($text as xs:string, $xml as element(Module)) as map(*)*{ - lsp-diags:nostic(pos:Range(pos:Position(0,0), pos:Position(0, 5)), - $lsp-diags:severities('error'),"111"), +(: lsp-diags:nostic(pos:Range(pos:Position(0,0), pos:Position(0, 5)), + $lsp-diags:severities('error'),"error"), lsp-diags:nostic(pos:Range(pos:Position(1,0), pos:Position(1,5)), - $lsp-diags:severities('warning'),"2222"), + $lsp-diags:severities('warning'),"warning"), lsp-diags:nostic(pos:Range(pos:Position(2,0), pos:Position(2, 5)), - $lsp-diags:severities('info'),"333"), + $lsp-diags:severities('info'),"info"), + +lsp-diags:nostic(pos:Range(pos:Position(3,0), pos:Position(3, 6)), + $lsp-diags:severities('hint'), "hint") :) -lsp-diags:nostic(pos:Range(pos:Position(3,0), pos:Position(3, 12)), - $lsp-diags:severities('hint'), "44") }; diff --git a/webapp/lsp/lsp-text.xqm b/webapp/lsp/lsp-text.xqm index c9e5003..d4d3e3f 100644 --- a/webapp/lsp/lsp-text.xqm +++ b/webapp/lsp/lsp-text.xqm @@ -2,8 +2,10 @@ : @author andy bunce :) module namespace lsp-text = 'lsp-text'; + import module namespace docs="lsp/docs" at "docs.xqm"; import module namespace rpc = 'rpc' at 'jsonrpc.xqm'; +import module namespace pos="lsp/position" at "position.xqm"; declare variable $lsp-text:methods:=map{ "textDocument/didOpen": lsp-text:didOpen#1, @@ -21,24 +23,21 @@ declare function lsp-text:hover($json as map(*)) as map(*) { - map{ - "jsonrpc": "2.0", - "id": $json?id, - "result":{ - "contents": [ + + let $r:= [ `markdown here, this is **bold**. A [link](http://google.com) The last line.` , - `A hover at \\n\\n{ json:serialize($json?params?position) } +`A hover at { pos:ln-col($json?params?position) } uri: {$json?params?textDocument?uri} ` ] -} - } +return rpc:response($r,$json) }; + declare function lsp-text:completion($json as map(*)) as map(*)? diff --git a/webapp/lsp/position.xqm b/webapp/lsp/position.xqm index 58acbfd..25553c1 100644 --- a/webapp/lsp/position.xqm +++ b/webapp/lsp/position.xqm @@ -23,12 +23,14 @@ declare record pos:Range( end as pos:Position ); +(:~ update $text with changes $chs from didChange +:) declare function pos:apply-changes($text as xs:string, $chs as array(*)) as xs:string{ array:fold-left($chs,$text,pos:apply-change#2) }; -(:~ text updated with $ch changes from didChange :) +(:~ text updated single change :) declare function pos:apply-change($text as xs:string, $ch as map(*)) as xs:string{ if(exists($ch?range)) @@ -83,4 +85,8 @@ as xs:integer fn($r){$r?max eq $r?min} ) return $s?max -}; \ No newline at end of file +}; + +declare function pos:ln-col($pos as pos:Position){ + `Ln { $pos?line}, Col { $pos?character}` +}; \ No newline at end of file diff --git a/webapp/static/clients/codemirror/script.js b/webapp/static/clients/codemirror/script.js index 8461bd7..a44da73 100644 --- a/webapp/static/clients/codemirror/script.js +++ b/webapp/static/clients/codemirror/script.js @@ -77,7 +77,7 @@ function connect() { function incoming(msg) { const rpc=JSON.parse(msg); - log(rpc.method); + log(rpc); switch (rpc.method) { case "textDocument/publishDiagnostics": diags(rpc.params); @@ -88,10 +88,13 @@ function incoming(msg) { } }; -function log(msg){ + +function log(rpc){ + if(rpc.id) return + const text=rpc.method; const li = document.createElement("li"); - li.appendChild(document.createTextNode(msg)); + li.appendChild(document.createTextNode(text)); const ol=document.getElementById("msg"); ol.insertBefore(li,ol.firstChild) };