[fix] diag msg

This commit is contained in:
Andy Bunce 2025-08-19 15:20:44 +01:00
parent ba9a3594b0
commit 6829f17673
4 changed files with 36 additions and 28 deletions

View file

@ -1 +1 @@
{"cells":[{"kind":2,"language":"xquery","value":"(:<:)\r\n\r\nimport module namespace docs=\"lsp/docs\" at \"/srv/basex/webapp/lsp/docs.xqm\";"},{"kind":2,"language":"xquery","value":"ws:ids()"},{"kind":2,"language":"xquery","value":"let $sock:=foot(ws:ids())\r\nlet $f:=docs:list($sock)\r\nlet $t:=docs:get($sock,$f,\"textDocument\")\r\nreturn map:put($t,\"text\",substring($t?text,1,15))"},{"kind":2,"language":"xquery","value":"let $sock:=foot(ws:ids())\r\nlet $f:=docs:list($sock)\r\nlet $t:=docs:get($sock,$f,\"parse\")\r\nreturn $t/self::ERROR"},{"kind":2,"language":"xquery","value":"let $sock:=foot(ws:ids())\r\nlet $f:=docs:list($sock)\r\nlet $t:=docs:get($sock,$f,\"textDocument\")\r\nreturn $t"}]}
{"cells":[{"kind":2,"language":"xquery","value":"(:<:)\r\n\r\nimport module namespace docs=\"lsp/docs\" at \"/srv/basex/webapp/lsp/docs.xqm\";"},{"kind":2,"language":"xquery","value":"ws:ids()"},{"kind":2,"language":"xquery","value":"let $sock:=foot(ws:ids())\r\nlet $f:=docs:list($sock)\r\nlet $t:=docs:get($sock,$f,\"textDocument\")\r\nreturn $t"},{"kind":2,"language":"xquery","value":"let $sock:=foot(ws:ids())\r\nlet $f:=docs:list($sock)\r\nlet $t:=docs:get($sock,$f,\"parse\")\r\nreturn $t/self::ERROR"},{"kind":2,"language":"xquery","value":"let $sock:=foot(ws:ids())\r\nlet $f:=docs:list($sock)\r\nlet $t:=docs:get($sock,$f,\"textDocument\")\r\nreturn $t"}]}

View file

@ -17,7 +17,7 @@ declare record lsp-diags:nostic(
range as pos:Range,
severity as xs:integer, (: enum('error', 'hint', 'info', 'warning') :)
message as xs:string,
code as xs:string,
code? as xs:string,
source as xs:string:="xquery"
);
@ -33,11 +33,11 @@ declare function lsp-diags:publish(
$text as xs:string,
$xml as lsp-diags:ParseResult)
as map(*){
let $_:=trace($xml,"PABLISH ")
let $diagnostics:=if($xml/self::ERROR)
then array{lsp-diags:parse-error($text, $xml)}
else array{ lsp-diags:parse-xquery($text,$xml)}
let $_:=trace($diagnostics,"PIBLISH ")
return {"jsonrpc": "2.0",
"method":"textDocument/publishDiagnostics",
"params":{"uri": $uri, "diagnostics": $diagnostics}
@ -50,25 +50,30 @@ let $_:=trace($diagnostics,"PIBLISH ")
:)
declare function lsp-diags:parse-error($text as xs:string, $xml as element(ERROR))
as map(*)*{
let $dmesg:=$xml/string()=>trace("parse-error")
let $dmesg:=translate($dmesg,"&#10;",";")
let $b:=number($xml/@b)-1
let $e:= number($xml/@e)-1
let $last:= string-length($text)-1
return (
lsp-diags:nostic(pos:Range(pos:toPosition($text, $b),
pos:toPosition($text, min(($e,$last)))),
1,
$dmesg,'XPST0003'),
if($e ge string-length($text))
then ()
else lsp-diags:nostic(pos:Range(pos:toPosition($text, $e +1 ),
pos:toPosition($text, $last)),
2,
"Unparsed due to previous parser error.",
"XQLT0001")
)
if(string-length($text) gt 0)
then let $last:= max((0,string-length($text)-1))
let $dmesg:=$xml/string()=>trace("parse-error")
let $dmesg:=translate($dmesg,"&#10;",";")
let $b:=number($xml/@b)-1
let $e:= number($xml/@e)-1
return (
(: mark error :)
lsp-diags:nostic(pos:Range(pos:toPosition($text, $b),
pos:toPosition($text, min(($e,$last)))),
1,
$dmesg,'XPST0003'),
(:mark after error:)
if($e ge string-length($text))
then ()
else lsp-diags:nostic(pos:Range(pos:toPosition($text, $e +1 ),
pos:toPosition($text, $last)),
2,
"Unparsed due to previous parser error.",
"XQLT0001")
)
else ()
};
(: test data :)

View file

@ -1,5 +1,5 @@
(:~
text editing API
positions in text
:)
module namespace pos="lsp/position";
@ -89,6 +89,8 @@ as xs:integer
return $s?max
};
declare function pos:ln-col($pos as pos:Position){
`Ln { $pos?line}, Col { $pos?character}`
(:~ position text for display :)
declare function pos:ln-col($pos as pos:Position,$offset as xs:integer:=1)
{
`Ln { $pos?line + $offset}, Col { $pos?character + $offset}`
};

View file

@ -56,7 +56,8 @@ document.getElementById("load").onchange = e => {
insert: t
}
})
console.log("SYNC")
client.sync();
console.log("SYNC");
});
document.getElementById("load").value="";
};
@ -104,11 +105,11 @@ function diags(params){
const severities=["error","warning" ,"info","hint" ]
//
const diagnostics = params.diagnostics
.map(({ range, message, severity }) => ({
.map(({ range, message, severity,code }) => ({
from: plugin.fromPosition( range.start,view.state.doc),
to: plugin.fromPosition( range.end,view.state.doc),
severity: severities[severity -1],
message,
message: ((typeof code === 'undefined')?"":`[${code}] `)+message,
}))
.filter(
({ from, to }) =>