Compare commits
2 commits
c4d2770bae
...
2d0ae19026
| Author | SHA1 | Date | |
|---|---|---|---|
| 2d0ae19026 | |||
| 30f913906d |
11 changed files with 172 additions and 49 deletions
22
test/ast.xq
22
test/ast.xq
|
|
@ -1,10 +1,20 @@
|
|||
import module namespace syms="lsp/symbols" at "../webapp/lsp/ast/ast.xqm";
|
||||
import module namespace ast="lsp/ast" at "../webapp/lsp/ast/ast.xqm";
|
||||
|
||||
declare $file:="C:\Users\mrwhe\git\quodatum\basex-lsp\test\sample.docs\pdfbox.xqm";
|
||||
(: declare variable $file:="sample.docs/pdfbox.xqm"; :)
|
||||
declare variable $file:="sample.docs/simple.xq";
|
||||
|
||||
declare variable $A:=doc("C:\Users\mrwhe\git\quodatum\basex-lsp\test\sample.docs\parse-pdfbox.xml");
|
||||
$A update{
|
||||
for $e in .//element()
|
||||
declare variable $A:=doc("sample.docs/parse-pdfbox.xml");
|
||||
|
||||
|
||||
unparsed-text($file)
|
||||
|
||||
=>ast:build()
|
||||
|
||||
update{
|
||||
for $e in descendant-or-self::element()
|
||||
let $len:=string-length($e)
|
||||
return insert node attribute len { $len } into $e
|
||||
let $before:=$e/preceding-sibling::node()/string-length()=>sum()
|
||||
return (insert node attribute len { $len } into $e,
|
||||
insert node attribute before { $before } into $e
|
||||
)
|
||||
}
|
||||
|
|
|
|||
6
test/sample.docs/simple.xq
Normal file
6
test/sample.docs/simple.xq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(: simple parse test :)
|
||||
declare function local:print($src as xs:string, $dest as xs:string)
|
||||
{
|
||||
concat($src,"->", $dest)
|
||||
};
|
||||
local:print("sss","bbb")
|
||||
|
|
@ -5,14 +5,23 @@ module namespace ast="lsp/ast";
|
|||
(: import module namespace p="xq4" at "xq4.xqm"; :)
|
||||
import module namespace xq4="java:quodatum.parser.xq4";
|
||||
|
||||
(:~ build :)
|
||||
declare function ast:build($text as xs:string,$uri as xs:string:="")
|
||||
(:~ build
|
||||
$opts uri,
|
||||
abstract:true(),
|
||||
position: true()
|
||||
:)
|
||||
declare function ast:build($text as xs:string,$opts as map(*):={})
|
||||
as element(*){
|
||||
let $xml:= xq4:parseModule($text)=>prof:time("⏱️ p:parse-Module " || $uri)
|
||||
return ($xml,
|
||||
message(ast:report($xml),"REP"),
|
||||
message(ast:report(ast:flatten($xml)),"flat")
|
||||
)
|
||||
let $xml:= xq4:parseModule($text)=>prof:time("⏱️ parseModule " || $opts?uri)
|
||||
let $res:= if($opts?abstract)
|
||||
then ast:flatten($xml)=>prof:time("⏱️ abstract ")
|
||||
else $xml
|
||||
|
||||
let $res:=if($opts?position)
|
||||
then ast:annotate-with-positions($res)=>prof:time("⏱️ position ")
|
||||
else $res
|
||||
|
||||
return $res
|
||||
};
|
||||
|
||||
(:~
|
||||
|
|
@ -31,7 +40,82 @@ declare function ast:flatten($input as element()) as element() {
|
|||
}
|
||||
};
|
||||
|
||||
(:-------------------------------------------:)
|
||||
declare function ast:add-positions($nodes as node()*, $start-pos as xs:integer) as item()+ {
|
||||
if (empty($nodes)) then (
|
||||
$start-pos, ()
|
||||
) else (
|
||||
let $head := $nodes[1]
|
||||
let $tail := $nodes[position() > 1]
|
||||
return
|
||||
typeswitch($head)
|
||||
case element() return
|
||||
let $children := $head/node()
|
||||
let $child-result := ast:add-positions($children, $start-pos)
|
||||
let $child-end-pos := $child-result[1]
|
||||
let $processed-children := subsequence($child-result, 2)
|
||||
let $element-text := string-join($processed-children ! string(.), "")
|
||||
let $element-length := string-length($element-text)
|
||||
let $element-end-pos :=
|
||||
if ($element-length > 0) then $start-pos + $element-length - 1
|
||||
else $start-pos
|
||||
let $tail-result :=
|
||||
if (exists($tail)) then
|
||||
ast:add-positions($tail, $element-end-pos + 1)
|
||||
else (
|
||||
$element-end-pos + 1, ()
|
||||
)
|
||||
let $final-end-pos := $tail-result[1]
|
||||
let $processed-tail := subsequence($tail-result, 2)
|
||||
return (
|
||||
$final-end-pos,
|
||||
element {node-name($head)} {
|
||||
attribute start {$start-pos},
|
||||
attribute end {$element-end-pos},
|
||||
$processed-children
|
||||
},
|
||||
$processed-tail
|
||||
)
|
||||
case text() return
|
||||
let $text-length := string-length($head)
|
||||
let $text-end-pos :=
|
||||
if ($text-length > 0) then $start-pos + $text-length - 1
|
||||
else $start-pos
|
||||
let $tail-result :=
|
||||
if (exists($tail)) then
|
||||
ast:add-positions($tail, $text-end-pos + 1)
|
||||
else (
|
||||
$text-end-pos + 1, ()
|
||||
)
|
||||
let $final-end-pos := $tail-result[1]
|
||||
let $processed-tail := subsequence($tail-result, 2)
|
||||
return (
|
||||
$final-end-pos,
|
||||
$head,
|
||||
$processed-tail
|
||||
)
|
||||
default return
|
||||
let $tail-result :=
|
||||
if (exists($tail)) then
|
||||
ast:add-positions($tail, $start-pos)
|
||||
else (
|
||||
$start-pos, ()
|
||||
)
|
||||
let $final-end-pos := $tail-result[1]
|
||||
let $processed-tail := subsequence($tail-result, 2)
|
||||
return (
|
||||
$final-end-pos,
|
||||
$head,
|
||||
$processed-tail
|
||||
)
|
||||
)
|
||||
};
|
||||
|
||||
declare function ast:annotate-with-positions($xml as element()) as element() {
|
||||
let $result := ast:add-positions($xml, 1)
|
||||
return $result[2]
|
||||
};
|
||||
|
||||
(:-------reporting------------------------------------:)
|
||||
|
||||
declare function ast:report($el as element(*)) {
|
||||
{
|
||||
|
|
|
|||
|
|
@ -118,7 +118,7 @@ declare function docs:parse(
|
|||
)as map(*)?
|
||||
{
|
||||
let $text:=docs:get($socket,$file,"textDocument")?text
|
||||
let $xml:= ast:build($text, $file)
|
||||
let $xml:= ast:build($text, {"uri": $file, "abstract":false(), "position":true()})
|
||||
let $diags:=lsp-diags:list($file, $text, $xml)=>prof:time("⏱️ diags " || $file)
|
||||
return (
|
||||
ws:set($socket,docs:key($socket,$file,"parse"),$xml),
|
||||
|
|
|
|||
|
|
@ -7,10 +7,14 @@ module namespace hnd="lsp/handlers";
|
|||
(:~ structure returned by tree walkers :)
|
||||
declare record hnd:Result(
|
||||
result as item()*,
|
||||
skipchildren? as xs:boolean:=false()
|
||||
skipchildren? as xs:boolean:=false(),
|
||||
extras?
|
||||
);
|
||||
|
||||
declare type hnd:actionFn as fn($parse as element(),$state as hnd:Result ) as hnd:Result;
|
||||
declare type hnd:actionFn as fn(
|
||||
$parse as element(),
|
||||
$state as hnd:Result
|
||||
) as hnd:Result;
|
||||
|
||||
declare type hnd:actionMap as map(xs:string,hnd:actionFn);
|
||||
|
||||
|
|
|
|||
|
|
@ -70,7 +70,8 @@ declare
|
|||
function rpc:send($msg as map(*))
|
||||
as empty-sequence()
|
||||
{
|
||||
rpc:admin-log($msg,"⬅️"),ws:send($msg ,ws:id())
|
||||
rpc:admin-log($msg,"⬅️"),
|
||||
ws:send($msg ,ws:id())
|
||||
};
|
||||
|
||||
(:~ canned initialize response :)
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ function lsp-ws:message(
|
|||
$message as xs:string
|
||||
) as empty-sequence() {
|
||||
|
||||
let $json := rpc:parse($message)
|
||||
let $json := rpc:parse($message=>trace("IN: "))
|
||||
return if(exists($json))
|
||||
then rpc:reply($json)
|
||||
else message($message,"bad RPC: ")
|
||||
|
|
|
|||
|
|
@ -84,3 +84,14 @@ declare function pos:full-range($text as xs:string)
|
|||
as lspt:Range{
|
||||
lspt:Range(pos:toPosition($text,0), pos:toPosition($text, string-length($text)))
|
||||
};
|
||||
|
||||
(:~ range for $text from indices:)
|
||||
declare function pos:range-from-ast(
|
||||
$ast as element(*),
|
||||
$text as xs:string)
|
||||
as lspt:Range{
|
||||
lspt:Range(
|
||||
pos:toPosition($text,number($ast/@start)),
|
||||
pos:toPosition($text, number($ast/@end))
|
||||
)
|
||||
};
|
||||
|
|
|
|||
|
|
@ -51,8 +51,9 @@ declare function syms:FunctionDecl($parse as element(FunctionDecl),$state as hn
|
|||
as hnd:Result{
|
||||
let $name:=syms:localName($parse/UnreservedFunctionEQName)
|
||||
let $prev:=$state?result[$name eq ?name]
|
||||
let $range:=lspt:Range(lspt:Position(0,0),lspt:Position(0,3))
|
||||
let $sym:=lspt:DocumentSymbol($name,$lspt:SymbolKindMap('Method'),$range,$range,"FUN")
|
||||
let $range:=pos:range-from-ast($parse,$state?context?text)=>trace("POSSSS")
|
||||
let $full-range:=$range
|
||||
let $sym:=lspt:DocumentSymbol($name,$lspt:SymbolKindMap('Method'),$range,$full-range,"FUN")
|
||||
return ($state?result,$sym)=>hnd:Result(true())
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
<!doctype html>
|
||||
<html lang="en" class="quiet-cloak">
|
||||
<html lang="en" class="quiet-cloak "> <!-- also quiet-dark -->
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
|
|
@ -8,8 +8,14 @@
|
|||
<link rel="icon" type="image/png" href="../favicon.png" />
|
||||
|
||||
<!-- Quiet theme + autoloader -->
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@quietui/quiet-browser@1.4.0/dist/themes/quiet.css">
|
||||
<script type="module" src="https://cdn.jsdelivr.net/npm/@quietui/quiet-browser@1.4.0/dist/quiet.loader.js"></script>
|
||||
<!-- Default theme (if not already installed) -->
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@quietui/quiet-browser@1.6.1/dist/themes/quiet.css">
|
||||
|
||||
<!-- Quiet Restyle
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@quietui/quiet-browser@1.6.1/dist/themes/restyle.css">
|
||||
-->
|
||||
|
||||
<script type="module" src="https://cdn.jsdelivr.net/npm/@quietui/quiet-browser@1.6.1/dist/quiet.loader.js"></script>
|
||||
|
||||
|
||||
<link rel="stylesheet" href="grail.css" />
|
||||
|
|
@ -27,7 +33,7 @@
|
|||
<quiet-icon slot="checked" name="network" family="outline"></quiet-icon>
|
||||
</quiet-toggle-icon>
|
||||
|
||||
<quiet-button-group label="Links">
|
||||
<quiet-toolbar label="Links">
|
||||
<quiet-button href="#" variant="primary" aria-current="page" size="sm">
|
||||
Editor
|
||||
</quiet-button>
|
||||
|
|
@ -37,8 +43,8 @@
|
|||
<quiet-button href="/dba/logs" target="dba" rel="noreferrer noopener" size="sm">
|
||||
Dba
|
||||
</quiet-button>
|
||||
</quiet-button-group>
|
||||
<select id="load">
|
||||
</quiet-toolbar>
|
||||
<quiet-select id="load" style="width:20em;">
|
||||
<option selected value="">load..</option>
|
||||
<optgroup label="XQuery3">
|
||||
<option
|
||||
|
|
@ -61,7 +67,7 @@
|
|||
value="https://raw.githubusercontent.com/dnovatchev/Articles/refs/heads/main/Generators/Code/generator.xq">
|
||||
generator.xquery</option>
|
||||
</optgroup>
|
||||
</select>
|
||||
</quiet-select>
|
||||
|
||||
<button popovertarget="popAbout" type="button">
|
||||
<i class="codicon codicon-info"></i>
|
||||
|
|
@ -71,12 +77,7 @@
|
|||
|
||||
<main class="page-main" style="overflow: auto;">
|
||||
<quiet-toolbar>
|
||||
<quiet-button-group>
|
||||
<label for="file">File:</label>
|
||||
<input id="iFile" type="url" value="file:///some/file.xqm" />
|
||||
|
||||
<label for="symbols">Symbols:</label><select id="symbols" disabled="disabled"></select>
|
||||
</quiet-button-group>
|
||||
<quiet-button-group>
|
||||
|
||||
<button id="search" title="Search" type="button" class="btn btn-light"><i
|
||||
|
|
@ -155,25 +156,31 @@
|
|||
</aside>
|
||||
|
||||
<footer class="page-footer">
|
||||
|
||||
<select id="language">
|
||||
<div style="display:flex;">
|
||||
<select id="language" style="width:10em;display:inline-block;">
|
||||
<option selected>Language</option>
|
||||
<option value="plaintext">plaintext</option>
|
||||
<option value="xquery">xquery</option>
|
||||
<option value="xml">xml</option>
|
||||
</select>
|
||||
<div>
|
||||
<label for="iFile">File:</label>
|
||||
<input id="iFile" type="url" value="file:///some/file.xqm" style="width:10em;display:inline-block;" />
|
||||
|
||||
<label for="symbols">Symbols:</label>
|
||||
<select id="symbols" disabled="disabled" style="width:10em;display:inline-block;"></select>
|
||||
</div>
|
||||
</div>
|
||||
<quiet-relative-time live id="relative-time__live" style="width:10em;"></quiet-relative-time>
|
||||
<quiet-dropdown id="dropdown__checkboxes">
|
||||
<quiet-button slot="trigger"><i class='codicon codicon-kebab-vertical'></i></quiet-button>
|
||||
<quiet-dropdown-item type="checkbox" value="canvas" checked>Follow cursor</quiet-dropdown-item>
|
||||
|
||||
<quiet-divider></quiet-divider>
|
||||
<quiet-radio orientation="vertical" name="sortBy">
|
||||
<quiet-radio-item value="position">sort by: position</quiet-radio-item>
|
||||
<quiet-radio-item value="name">sort by: name</quiet-radio-item>
|
||||
<quiet-radio-item value="category">sort by: category</quiet-radio-item>
|
||||
<quiet-dropdown-item type="checkbox" value="position" checked>sort by: Position</quiet-dropdown-item>
|
||||
<quiet-dropdown-item type="checkbox" value="name">sort by: Name</quiet-dropdown-item>
|
||||
<quiet-dropdown-item type="checkbox" value="category">sort by: Category</quiet-dropdown-item>
|
||||
|
||||
</quiet-radio>
|
||||
|
||||
</quiet-dropdown>
|
||||
|
||||
|
|
|
|||
|
|
@ -151,7 +151,7 @@ function connect() {
|
|||
const file = $("iFile").value;
|
||||
lsp.simpleWebSocketTransport(server)
|
||||
.then(transport => {
|
||||
transport.socket.onclose = (event) => connectStatus(false);
|
||||
transport.socket.onclose = (event) => $("tConnect").checked=false;
|
||||
transport.socket.oneror = (event) => $("msg").innerText = "sock error!";
|
||||
transport.subscribe(incoming);
|
||||
client = new lsp.LSPClient({ extensions: lsp.languageServerExtensions() });
|
||||
|
|
@ -169,7 +169,6 @@ function connect() {
|
|||
.catch(e => {
|
||||
console.log(e);
|
||||
$("tConnect").checked=false;
|
||||
connectStatus(false);
|
||||
alert("connection failed: " + server)
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue