diff --git a/test/position.xq b/test/position.xq index 1d4e1a0..e695a7f 100644 --- a/test/position.xq +++ b/test/position.xq @@ -1,6 +1,6 @@ import module namespace pos="lsp/position" at "../webapp/lsp/position.xqm"; import module namespace xq4="java:quodatum.parser.xq4"; - let $text:="1+2 a" + let $text:="1+" let $xml:=xq4:parseModule($text)=>trace("£ ") let $b:=number($xml/@b)-1 let $e:= number($xml/@e)-1 diff --git a/webapp/lsp/lsp-text.xqm b/webapp/lsp/lsp-text.xqm index 322f0d8..ffaca73 100644 --- a/webapp/lsp/lsp-text.xqm +++ b/webapp/lsp/lsp-text.xqm @@ -13,17 +13,15 @@ declare variable $lsp-text:methods:=map{ "textDocument/didClose" : lsp-text:method-unknown#1, "textDocument/hover": lsp-text:hover#1, - "textDocument/completion": lsp-text:completion#1 + "textDocument/completion": lsp-text:completion#1, + "textDocument/formatting" : lsp-text:format#1 }; -(:~ hover - -:) +(:~ hover :) declare function lsp-text:hover($json as map(*)) as map(*) { - let $r:= [ `markdown here, this is **bold**. @@ -49,15 +47,29 @@ as map(*)? } }; +declare +function lsp-text:format($json as map(*)) +as map(*)? +{ + let $uri:=$json?params?textDocument?uri + let $text:=docs:get(ws:id(), $uri, "textDocument")?text + return map{ + "jsonrpc": "2.0", + "id": $json?id, + "result":{ + "range":pos:full-range($text), + "newText":"formated" + } + } +}; + (:~ didOpen method response :) declare function lsp-text:didOpen($json as map(*)) as map(*)? { let $uri:=docs:open(ws:id(),$json?params)=>prof:time("⏱️ doc:save ") - return docs:parse(ws:id(),$uri) - - + return docs:parse(ws:id(),$uri) }; (:~ didChange method response :) diff --git a/webapp/lsp/position.xqm b/webapp/lsp/position.xqm index 76c724b..ce34391 100644 --- a/webapp/lsp/position.xqm +++ b/webapp/lsp/position.xqm @@ -24,22 +24,6 @@ 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 single change :) -declare function pos:apply-change($text as xs:string, $ch as map(*)) -as xs:string{ - if(exists($ch?range)) - then substring($text,1, pos:resolvePosition($text, $ch?range?start)) - || $ch?text || - substring($text,pos:resolvePosition($text, $ch?range?end)) - else $ch?text -}; (:~ find index from Position :) declare function pos:resolvePosition($text as xs:string, $pos as pos:Position) @@ -94,4 +78,27 @@ as xs:integer declare function pos:ln-col($pos as pos:Position,$offset as xs:integer:=1) { `Ln { $pos?line + $offset}, Col { $pos?character + $offset}` -}; \ No newline at end of file +}; + +(:~ 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 single change :) +declare function pos:apply-change($text as xs:string, $ch as map(*)) +as xs:string{ + if(exists($ch?range)) + then substring($text,1, pos:resolvePosition($text, $ch?range?start)) + || $ch?text || + substring($text,pos:resolvePosition($text, $ch?range?end)) + else $ch?text +}; + +(:~ full range for $text :) +declare function pos:full-range($text as xs:string) +as pos:Range{ + pos:Range(pos:toPosition($text,0), pos:toPosition($text, string-length($text)-1)) +}; diff --git a/webapp/static/clients/codemirror/script.js b/webapp/static/clients/codemirror/script.js index 7cd2b50..c37aacd 100644 --- a/webapp/static/clients/codemirror/script.js +++ b/webapp/static/clients/codemirror/script.js @@ -40,7 +40,7 @@ $("sync").onclick = e => { client.sync(); console.log("XXXsync");}; $("format").onclick = e => { client.sync(); - console.log("FMT",lsp.formatDocument()); + console.log("FMT",lsp.formatDocument(view)); }; $("load").onchange = e => {