[mod] improve formatting

This commit is contained in:
Andy Bunce 2025-09-11 18:18:56 +01:00
parent 0508901f72
commit 877e7b8d13
5 changed files with 47 additions and 17 deletions

View file

@ -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"; 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"; 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"; declare variable $dest:="C:\Users\mrwhe\git\quodatum\basex-lsp\test\sample.docs\parse-pdfbox.xml";
let $parse:= $pdfbox-good.xqm let $parse:= $pdfbox-good.xqm
=>fetch:text() =>fetch:text()
=>lazy:cache() =>lazy:cache()
=>p:parse-Module() =>p:parse-Module()
return file:write($dest,$parse) return file:write($dest,$parse)

View file

@ -49,11 +49,8 @@ declare
function rpc:method-initialize($json as map(*)) function rpc:method-initialize($json as map(*))
as map(*) as map(*)
{ {
{ json:doc("etc/capabilities.json",{"format":"w3"})
"jsonrpc": "2.0", =>rpc:result($json)
"id": $json?id,
"result": json:doc("etc/capabilities.json",{"format":"w3"})
}
}; };
(:~ initialized response :) (:~ initialized response :)
@ -84,7 +81,7 @@ as map(*)
}; };
(:~ rpc response to $json msg :) (:~ rpc response to $json msg :)
declare function rpc:response($result,$json as map(*)) declare function rpc:result($result,$json as map(*))
as map(*) as map(*)
{ {
map{ map{
@ -93,3 +90,19 @@ map{
"result": $result "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"
}
}
}
};

View file

@ -31,7 +31,7 @@ The last line.`
, ,
`A hover at { pos:ln-col($json?params?position) } uri: {$json?params?textDocument?uri} ` `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 $uri:=$json?params?textDocument?uri
let $text:=docs:get(ws:id(), $uri, "textDocument")?text 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 :) let $fmt:=`(: formatting to do :)
` `
return map{ return rpc:result([{
"jsonrpc": "2.0",
"id": $json?id,
"result":[{
"range":pos:full-range($text), "range":pos:full-range($text),
"newText": $fmt || $text "newText": $fmt || string($xml)
}] }],$json)
}
}; };
(:~ didOpen method response :) (:~ didOpen method response :)
@ -93,3 +94,18 @@ as map(*)?
let $_:=trace($json?method,"unknown") let $_:=trace($json?method,"unknown")
return () 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 ()
)
};