From ce69c61b6c0593d0022358272f1b02c948c44e3e Mon Sep 17 00:00:00 2001 From: Andy Bunce Date: Sun, 3 Aug 2025 22:47:54 +0100 Subject: [PATCH] [mod] refactor --- webapp/lsp/docs.xqm | 45 +++++++++++++++++++++++++++++++++++++++++++++ webapp/lsp/parse.xq | 29 +++++++++++++---------------- 2 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 webapp/lsp/docs.xqm diff --git a/webapp/lsp/docs.xqm b/webapp/lsp/docs.xqm new file mode 100644 index 0000000..d2c9caf --- /dev/null +++ b/webapp/lsp/docs.xqm @@ -0,0 +1,45 @@ +(: Store for XQuery document data , type, text,uri + on save parse is created and stored + implementation: data is stored in webSocket +@author Andy Bunce +:) +module namespace docs="lsp/docs"; +import module namespace p="xq4" at "xq4.xqm"; + +(: document info :) +declare type docs:property as enum("textDocument","parse"); + +(: get $property for $file from session $socket :) +declare function docs:get( + $socket as xs:string, + $file as xs:string, + $property as docs:property +) +{ + let $key:=ws:get($socket,"files")($file)($property) + return ws:get($socket,$key) +}; + +(: save $textDocument data as session $socket properties :) +declare function docs:save( + $socket as xs:string, + $textDocument as map(*) +) +{ +let $text as xs:string:=$textDocument?text +let $uri:=$textDocument?uri +let $files:=ws:get($socket,"files",{}) +let $files:=if(map:contains($files,$uri)) + then $files + else map:put($files,$uri,{ + "textDocument":random:uuid(), + "parse":random:uuid() }) +let $keys:=$files($uri) +let $xml:=p:parse-Module($text) + +return ( + ws:set($socket,"files",$files), + ws:set($socket,$keys?textDocument,$textDocument), + ws:set($socket,$keys?parse,$xml) +) +}; \ No newline at end of file diff --git a/webapp/lsp/parse.xq b/webapp/lsp/parse.xq index ba1eaf7..e2c3b56 100644 --- a/webapp/lsp/parse.xq +++ b/webapp/lsp/parse.xq @@ -1,24 +1,21 @@ -(: parse a didOpen :) -import module namespace p="xq4" at "xq4.xqm"; +(: Save an XQuery doc and it's parse :) +import module namespace docs="lsp/docs" at "docs.xqm"; declare variable $textDocument external; declare variable $webSocket as xs:string external; -let $text as xs:string:=$textDocument?text -let $uri:=$textDocument?uri -let $files:=ws:get($webSocket,"files",{}) -let $files:=if(map:contains($files,$uri)) - then $files - else map:put($files,$uri,{"textDocument":random:uuid(),"parse":random:uuid() }) -let $keys:=$files($uri) -let $xml:=p:parse-Module($text) +docs:save( + $webSocket, + $textDocument +), -return ( - ws:set($webSocket,"files",$files), - ws:set($webSocket,$keys?textDocument,$textDocument), - ws:set($webSocket,$keys?parse,$xml), - ws:send({"jsonrpc": "2.0","method":"window/logMessage", - "params":{"type":1,"message":"TODO"}},$webSocket) +ws:send( + {"jsonrpc": "2.0", + "method":"window/logMessage", + "params":{"type":1,"message":"TODO"} + }, + $webSocket ) + \ No newline at end of file