xq-lsp 1.8.2 → 1.8.3-next.430
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -9
- package/builtins/builtins-fn.xq +1 -1
- package/dist/analyzer.d.ts +1 -0
- package/dist/analyzer.js +43 -3
- package/dist/ast-nodes.d.ts +38 -0
- package/dist/ast-nodes.js +71 -0
- package/dist/diagnostics.js +2 -0
- package/dist/functioncall-diagnostics.d.ts +6 -3
- package/dist/functioncall-diagnostics.js +76 -17
- package/dist/inlay-hints.js +28 -82
- package/dist/module-siblings.d.ts +11 -0
- package/dist/module-siblings.js +16 -0
- package/dist/predeclared-namespaces.js +1 -0
- package/dist/prolog-diagnostics.d.ts +10 -0
- package/dist/prolog-diagnostics.js +113 -0
- package/dist/qt4-worker.js +5 -3
- package/dist/runtimes/fonto/fonto.xq +95 -0
- package/dist/semantic-tokens.d.ts +29 -0
- package/dist/semantic-tokens.js +254 -0
- package/dist/server.js +40 -7
- package/dist/typechecker.d.ts +48 -1
- package/dist/typechecker.js +1215 -122
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
- package/src/analyzer.ts +45 -3
- package/src/ast-nodes.ts +85 -0
- package/src/diagnostics.ts +2 -0
- package/src/functioncall-diagnostics.ts +93 -15
- package/src/inlay-hints.ts +42 -93
- package/src/module-siblings.ts +17 -0
- package/src/predeclared-namespaces.ts +1 -0
- package/src/prolog-diagnostics.ts +137 -0
- package/src/qt4-worker.ts +6 -4
- package/src/runtimes/fonto/fonto.xq +95 -0
- package/src/semantic-tokens.ts +289 -0
- package/src/server.ts +40 -6
- package/src/typechecker.ts +1265 -123
- package/src/types.ts +2 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import type { Node } from "xq-parser";
|
|
2
|
+
import type { TypeDiagnostic, FileAnalysis } from "./types.ts";
|
|
3
|
+
import { qnameKey } from "./types.ts";
|
|
4
|
+
import { findAll } from "./analyzer.ts";
|
|
5
|
+
import {
|
|
6
|
+
asVersionDecl,
|
|
7
|
+
asOptionDecl,
|
|
8
|
+
asNamespaceDecl,
|
|
9
|
+
asDefaultNamespaceDecl,
|
|
10
|
+
asDecimalFormatDecl,
|
|
11
|
+
asFunctionDecl,
|
|
12
|
+
asInlineFunctionExpr,
|
|
13
|
+
} from "./ast-nodes.ts";
|
|
14
|
+
|
|
15
|
+
/** The second and later items sharing a key with an earlier item. */
|
|
16
|
+
function repeats<T>(items: T[], key: (item: T) => string): T[] {
|
|
17
|
+
const seen = new Set<string>();
|
|
18
|
+
return items.filter((item) => {
|
|
19
|
+
const k = key(item);
|
|
20
|
+
if (seen.has(k)) return true;
|
|
21
|
+
seen.add(k);
|
|
22
|
+
return false;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function diagnostic(node: Node, code: string, message: string): TypeDiagnostic {
|
|
27
|
+
const start = node.start ?? 0;
|
|
28
|
+
return { message, code, offset: start, length: (node.end ?? start) - start };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Setter declarations that may appear at most once per prolog. */
|
|
32
|
+
const SINGLE_USE_DECLS: Array<{ type: string; code: string; what: string }> = [
|
|
33
|
+
{ type: "BaseURIDecl", code: "XQST0032", what: "base URI" },
|
|
34
|
+
{ type: "DefaultCollationDecl", code: "XQST0038", what: "default collation" },
|
|
35
|
+
{ type: "OrderingModeDecl", code: "XQST0065", what: "ordering mode" },
|
|
36
|
+
{ type: "ConstructionDecl", code: "XQST0067", what: "construction mode" },
|
|
37
|
+
{ type: "BoundarySpaceDecl", code: "XQST0068", what: "boundary-space policy" },
|
|
38
|
+
{ type: "EmptyOrderDecl", code: "XQST0069", what: "default empty order" },
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
const XMLNS_OUTPUT = "http://www.w3.org/2010/xslt-xquery-serialization";
|
|
42
|
+
|
|
43
|
+
/** Serialization parameters that may be set with `declare option output:*` (use-character-maps may not). */
|
|
44
|
+
const PROLOG_SERIALIZATION_PARAMS = new Set([
|
|
45
|
+
"allow-duplicate-names",
|
|
46
|
+
"byte-order-mark",
|
|
47
|
+
"cdata-section-elements",
|
|
48
|
+
"doctype-public",
|
|
49
|
+
"doctype-system",
|
|
50
|
+
"encoding",
|
|
51
|
+
"escape-uri-attributes",
|
|
52
|
+
"html-version",
|
|
53
|
+
"include-content-type",
|
|
54
|
+
"indent",
|
|
55
|
+
"item-separator",
|
|
56
|
+
"json-node-output-method",
|
|
57
|
+
"media-type",
|
|
58
|
+
"method",
|
|
59
|
+
"normalization-form",
|
|
60
|
+
"omit-xml-declaration",
|
|
61
|
+
"parameter-document",
|
|
62
|
+
"standalone",
|
|
63
|
+
"suppress-indentation",
|
|
64
|
+
"undeclare-prefixes",
|
|
65
|
+
"version",
|
|
66
|
+
]);
|
|
67
|
+
|
|
68
|
+
const SUPPORTED_XQUERY_VERSIONS = new Set(["1.0", "3.0", "3.1", "4.0"]);
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Report an unsupported `xquery version` (XQST0031), serialization option
|
|
72
|
+
* misuse (XQST0108/0109/0110) and prolog components that the spec allows only once: setters such as
|
|
73
|
+
* `declare base-uri`, namespace prefixes, default element/function namespaces,
|
|
74
|
+
* decimal formats, and duplicate function parameter names.
|
|
75
|
+
* Only the second and later occurrences are flagged.
|
|
76
|
+
*/
|
|
77
|
+
export function checkDuplicatePrologDecls(ast: Node, analysis: FileAnalysis): TypeDiagnostic[] {
|
|
78
|
+
const out: TypeDiagnostic[] = [];
|
|
79
|
+
|
|
80
|
+
for (const node of findAll(ast, "VersionDecl")) {
|
|
81
|
+
const decl = asVersionDecl(node);
|
|
82
|
+
if (decl && !SUPPORTED_XQUERY_VERSIONS.has(decl.version))
|
|
83
|
+
out.push(diagnostic(decl.versionNode, "XQST0031", `XQuery version '${decl.version}' is not supported`));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const isLibraryModule = findAll(ast, "LibraryModule").length > 0;
|
|
87
|
+
const outputOptions = findAll(ast, "OptionDecl").flatMap((node) => {
|
|
88
|
+
const decl = asOptionDecl(node, analysis);
|
|
89
|
+
return decl?.qname.namespaceUri === XMLNS_OUTPUT ? [{ node, name: decl.qname.localName }] : [];
|
|
90
|
+
});
|
|
91
|
+
for (const { node, name } of outputOptions) {
|
|
92
|
+
if (isLibraryModule)
|
|
93
|
+
out.push(diagnostic(node, "XQST0108", "Serialization parameters may only be declared in a main module"));
|
|
94
|
+
else if (!PROLOG_SERIALIZATION_PARAMS.has(name))
|
|
95
|
+
out.push(diagnostic(node, "XQST0109", `'${name}' is not a serialization parameter that can be declared in the prolog`));
|
|
96
|
+
}
|
|
97
|
+
for (const { node, name } of repeats(outputOptions, (o) => o.name))
|
|
98
|
+
out.push(diagnostic(node, "XQST0110", `Serialization parameter '${name}' is declared more than once`));
|
|
99
|
+
|
|
100
|
+
for (const { type, code, what } of SINGLE_USE_DECLS)
|
|
101
|
+
for (const node of repeats(findAll(ast, type), () => type))
|
|
102
|
+
out.push(diagnostic(node, code, `The ${what} is declared more than once`));
|
|
103
|
+
|
|
104
|
+
const namespaceDecls = findAll(ast, "NamespaceDecl").flatMap((node) => {
|
|
105
|
+
const decl = asNamespaceDecl(node);
|
|
106
|
+
return decl ? [{ node, ...decl }] : [];
|
|
107
|
+
});
|
|
108
|
+
for (const { node, prefix } of repeats(namespaceDecls, (d) => d.prefix))
|
|
109
|
+
out.push(diagnostic(node, "XQST0033", `Namespace prefix '${prefix}' is declared more than once`));
|
|
110
|
+
|
|
111
|
+
const defaultNsDecls = findAll(ast, "DefaultNamespaceDecl").flatMap((node) => {
|
|
112
|
+
const decl = asDefaultNamespaceDecl(node);
|
|
113
|
+
return decl ? [{ node, ...decl }] : [];
|
|
114
|
+
});
|
|
115
|
+
for (const { node, kind } of repeats(defaultNsDecls, (d) => d.kind))
|
|
116
|
+
out.push(diagnostic(node, "XQST0066", `The default ${kind} namespace is declared more than once`));
|
|
117
|
+
|
|
118
|
+
const decimalFormats = findAll(ast, "DecimalFormatDecl").flatMap((node) => {
|
|
119
|
+
const decl = asDecimalFormatDecl(node, analysis);
|
|
120
|
+
return decl ? [{ node, ...decl }] : [];
|
|
121
|
+
});
|
|
122
|
+
for (const { node } of repeats(decimalFormats, (d) => d.key))
|
|
123
|
+
out.push(diagnostic(node, "XQST0111", "This decimal format is declared more than once"));
|
|
124
|
+
for (const { properties } of decimalFormats)
|
|
125
|
+
for (const { nameNode, name } of repeats(properties, (p) => p.name))
|
|
126
|
+
out.push(diagnostic(nameNode, "XQST0114", `Decimal-format property '${name}' is specified more than once`));
|
|
127
|
+
|
|
128
|
+
const paramLists = [
|
|
129
|
+
...findAll(ast, "FunctionDecl").map((n) => asFunctionDecl(n, analysis)?.params),
|
|
130
|
+
...findAll(ast, "InlineFunctionExpr").map((n) => asInlineFunctionExpr(n, analysis)?.params),
|
|
131
|
+
];
|
|
132
|
+
for (const params of paramLists)
|
|
133
|
+
for (const { nameNode, qname } of repeats(params ?? [], (p) => qnameKey(p.qname)))
|
|
134
|
+
out.push(diagnostic(nameNode, "XQST0039", `Parameter '$${qname.localName}' is declared more than once`));
|
|
135
|
+
|
|
136
|
+
return out;
|
|
137
|
+
}
|
package/src/qt4-worker.ts
CHANGED
|
@@ -92,14 +92,16 @@ const batch = workerData as TestInput[];
|
|
|
92
92
|
const results: TestOutput[] = batch.map((tc) => {
|
|
93
93
|
const got = collectCodes(tc.query, tc.envNamespaces, tc.envVariables, tc.moduleCatalog);
|
|
94
94
|
const hasError = got.length > 0;
|
|
95
|
-
|
|
95
|
+
// A static analyzer may detect a dynamic error early: when the test expects a
|
|
96
|
+
// dynamic error (e.g. XPTY0004) and that is exactly what we reported, it's a pass.
|
|
97
|
+
const outcome: TestOutput["outcome"] =
|
|
96
98
|
tc.expected === "static-error"
|
|
97
99
|
? hasError
|
|
98
100
|
? "pass"
|
|
99
101
|
: "false-negative"
|
|
100
|
-
: hasError
|
|
101
|
-
? "
|
|
102
|
-
: "
|
|
102
|
+
: !hasError || got.every((c) => c === tc.expectedCode)
|
|
103
|
+
? "pass"
|
|
104
|
+
: "false-positive";
|
|
103
105
|
return { testSetSlug: tc.testSetSlug, testCase: tc.testCase, outcome, expectedCode: tc.expectedCode, got };
|
|
104
106
|
});
|
|
105
107
|
|
|
@@ -162,3 +162,98 @@ declare function fonto:document($remoteDocumentId as xs:string?) as node()? exte
|
|
|
162
162
|
: @see https://documentation.fontoxml.com/latest/fonto-remote-document-state-03df124b223a
|
|
163
163
|
:)
|
|
164
164
|
declare function fonto:remote-document-state($remoteDocumentId as xs:string) as map(*)? external;
|
|
165
|
+
|
|
166
|
+
(:~
|
|
167
|
+
: Returns the remoteDocumentId of the document that contains the given node.
|
|
168
|
+
: Throws an error when the node is not related to a document that has a remote document id.
|
|
169
|
+
: @param $node The node of which the remote document id should be looked up.
|
|
170
|
+
: @see https://documentation.fontoxml.com/latest/fonto-remote-document-id-c4f2b7d3ca21
|
|
171
|
+
:)
|
|
172
|
+
declare function fonto:remote-document-id($node as node()) as xs:string external;
|
|
173
|
+
|
|
174
|
+
(:~
|
|
175
|
+
: Returns the Fonto NodeId of the given node. Do not use this in operations.
|
|
176
|
+
: @param $node The node.
|
|
177
|
+
: @see https://documentation.fontoxml.com/latest/fonto-node-id-7b60a9382817
|
|
178
|
+
:)
|
|
179
|
+
declare function fonto:node-id($node as node()) as xs:string external;
|
|
180
|
+
|
|
181
|
+
(:~
|
|
182
|
+
: Returns true if the given node is read-only for any reason.
|
|
183
|
+
: @param $node The node to check.
|
|
184
|
+
: @see https://documentation.fontoxml.com/latest/fonto-is-node-read-only-72ca327d3704
|
|
185
|
+
:)
|
|
186
|
+
declare function fonto:is-node-read-only($node as node()) as xs:boolean external;
|
|
187
|
+
|
|
188
|
+
(:~
|
|
189
|
+
: Returns the hierarchy source node for the given hierarchy node id.
|
|
190
|
+
: @param $hierarchyNodeId The hierarchy node id.
|
|
191
|
+
: @see https://documentation.fontoxml.com/latest/fonto-hierarchy-source-node-346968934801
|
|
192
|
+
:)
|
|
193
|
+
declare function fonto:hierarchy-source-node($hierarchyNodeId as xs:string?) as node()? external;
|
|
194
|
+
|
|
195
|
+
(:~
|
|
196
|
+
: Evaluates the configured hierarchyChildNodesQuery for the given node.
|
|
197
|
+
: Returns the empty sequence if no node is given or no query is configured.
|
|
198
|
+
: @param $node The node for which to retrieve the hierarchy child nodes.
|
|
199
|
+
: @see https://documentation.fontoxml.com/latest/fonto-hierarchy-child-nodes-e46c9fb8f2c0
|
|
200
|
+
:)
|
|
201
|
+
declare function fonto:hierarchy-child-nodes($node as node()?) as node()* external;
|
|
202
|
+
|
|
203
|
+
(:~
|
|
204
|
+
: Returns the contents of the "title" of the given node, as configured with titleQuery.
|
|
205
|
+
: @param $node The node to resolve the contents of the title of.
|
|
206
|
+
: @see https://documentation.fontoxml.com/latest/fonto-title-content-1acac0f6c610
|
|
207
|
+
:)
|
|
208
|
+
declare function fonto:title-content($node as node()) as xs:string? external;
|
|
209
|
+
|
|
210
|
+
(:~
|
|
211
|
+
: Returns the direction of the "title" of the given node, as configured with titleDirectionQuery.
|
|
212
|
+
: @param $node The node to resolve the direction of the title of.
|
|
213
|
+
: @see https://documentation.fontoxml.com/latest/fonto-title-content-direction-975b104b0a82
|
|
214
|
+
:)
|
|
215
|
+
declare function fonto:title-content-direction($node as node()) as xs:string external;
|
|
216
|
+
|
|
217
|
+
(:~
|
|
218
|
+
: Returns the effective text direction ('ltr' or 'rtl') for the given node.
|
|
219
|
+
: Returns 'ltr' when no direction is configured or no node is passed.
|
|
220
|
+
: @param $node The node for which to get the effective direction.
|
|
221
|
+
: @see https://documentation.fontoxml.com/latest/fonto-direction-193c5dccadfc
|
|
222
|
+
:)
|
|
223
|
+
declare function fonto:direction($node as node()?) as xs:string external;
|
|
224
|
+
|
|
225
|
+
(:~
|
|
226
|
+
: Returns true if the given node is a table according to any table definition.
|
|
227
|
+
: @param $node The node to check.
|
|
228
|
+
: @see https://documentation.fontoxml.com/latest/fonto-is-table-a4faf751893d
|
|
229
|
+
:)
|
|
230
|
+
declare function fonto:is-table($node as node()) as xs:boolean external;
|
|
231
|
+
|
|
232
|
+
(:~
|
|
233
|
+
: Returns true if the given node is a table cell according to any table definition.
|
|
234
|
+
: @param $node The node to check.
|
|
235
|
+
: @see https://documentation.fontoxml.com/latest/fonto-is-table-cell-7d82e5b1f409
|
|
236
|
+
:)
|
|
237
|
+
declare function fonto:is-table-cell($node as node()) as xs:boolean external;
|
|
238
|
+
|
|
239
|
+
(:~
|
|
240
|
+
: Returns true for any CALS table element (tgroup) or CALS table figure element (table).
|
|
241
|
+
: @param $node The node to check.
|
|
242
|
+
: @see https://documentation.fontoxml.com/latest/fonto-is-cals-table-fd98acb9a49f
|
|
243
|
+
:)
|
|
244
|
+
declare function fonto:is-cals-table($node as node()?) as xs:boolean external;
|
|
245
|
+
|
|
246
|
+
(:~
|
|
247
|
+
: Returns true for any XHTML table element.
|
|
248
|
+
: @param $node The node to check.
|
|
249
|
+
: @see https://documentation.fontoxml.com/latest/fonto-is-xhtml-table-5e4d4e55a9df
|
|
250
|
+
:)
|
|
251
|
+
declare function fonto:is-xhtml-table($node as node()?) as xs:boolean external;
|
|
252
|
+
|
|
253
|
+
(:~
|
|
254
|
+
: Returns the column specification.
|
|
255
|
+
: @param $columnSpecifications The column specifications node.
|
|
256
|
+
: @param $columnIdentifier The column identifier.
|
|
257
|
+
: @see https://documentation.fontoxml.com/latest/fonto-column-spec-0d3684963eb6
|
|
258
|
+
:)
|
|
259
|
+
declare function fonto:column-spec($columnSpecifications as node(), $columnIdentifier as xs:string) as item()? external;
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import type { Node, NonTerminal } from "xq-parser";
|
|
2
|
+
import type { FileAnalysis, QName } from "./types.ts";
|
|
3
|
+
import { isTerminal, directChildOf, directChildrenOf, firstTerminalValue, BUILTIN_PREFIXES } from "./analyzer.ts";
|
|
4
|
+
import { asFunctionCall, asNamedFunctionRef, asFunctionDecl, asVarDecl } from "./ast-nodes.ts";
|
|
5
|
+
import { checkUnused } from "./unused-diagnostics.ts";
|
|
6
|
+
|
|
7
|
+
// ── Legend ────────────────────────────────────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
export const TOKEN_TYPES = ["namespace", "function", "parameter", "variable"] as const;
|
|
10
|
+
export const TOKEN_MODIFIERS = ["defaultLibrary", "unused"] as const;
|
|
11
|
+
|
|
12
|
+
export type SemanticTokenType = (typeof TOKEN_TYPES)[number];
|
|
13
|
+
export type SemanticTokenModifier = (typeof TOKEN_MODIFIERS)[number];
|
|
14
|
+
|
|
15
|
+
export const SEMANTIC_TOKENS_LEGEND = {
|
|
16
|
+
tokenTypes: [...TOKEN_TYPES],
|
|
17
|
+
tokenModifiers: [...TOKEN_MODIFIERS],
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const TOKEN_TYPE_INDEX = Object.fromEntries(TOKEN_TYPES.map((t, i) => [t, i])) as Record<SemanticTokenType, number>;
|
|
21
|
+
const TOKEN_MODIFIER_BIT = Object.fromEntries(TOKEN_MODIFIERS.map((m, i) => [m, 1 << i])) as Record<SemanticTokenModifier, number>;
|
|
22
|
+
|
|
23
|
+
function modifierBitmask(mods: SemanticTokenModifier[]): number {
|
|
24
|
+
return mods.reduce((acc, m) => acc | TOKEN_MODIFIER_BIT[m], 0);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ── Delta encoding ────────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
export interface EncodedToken {
|
|
30
|
+
line: number;
|
|
31
|
+
character: number;
|
|
32
|
+
length: number;
|
|
33
|
+
tokenType: number;
|
|
34
|
+
tokenModifiers: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Delta-encode already-positioned tokens into the flat array required by
|
|
39
|
+
* `SemanticTokens.data` (see the LSP spec's `textDocument/semanticTokens/full`).
|
|
40
|
+
* Tokens are sorted by (line, character) first since the format is cumulative.
|
|
41
|
+
*/
|
|
42
|
+
export function encodeSemanticTokens(tokens: EncodedToken[]): number[] {
|
|
43
|
+
const sorted = [...tokens].sort((a, b) => a.line - b.line || a.character - b.character);
|
|
44
|
+
const data: number[] = [];
|
|
45
|
+
let prevLine = 0;
|
|
46
|
+
let prevChar = 0;
|
|
47
|
+
for (const t of sorted) {
|
|
48
|
+
const deltaLine = t.line - prevLine;
|
|
49
|
+
const deltaChar = deltaLine === 0 ? t.character - prevChar : t.character;
|
|
50
|
+
data.push(deltaLine, deltaChar, t.length, t.tokenType, t.tokenModifiers);
|
|
51
|
+
prevLine = t.line;
|
|
52
|
+
prevChar = t.character;
|
|
53
|
+
}
|
|
54
|
+
return data;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// ── Offset → line/character ──────────────────────────────────────────────────
|
|
58
|
+
|
|
59
|
+
function computeLineStarts(text: string): number[] {
|
|
60
|
+
const starts = [0];
|
|
61
|
+
for (let i = 0; i < text.length; i++) if (text[i] === "\n") starts.push(i + 1);
|
|
62
|
+
return starts;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function offsetToPosition(lineStarts: number[], offset: number): { line: number; character: number } {
|
|
66
|
+
let lo = 0;
|
|
67
|
+
let hi = lineStarts.length - 1;
|
|
68
|
+
while (lo < hi) {
|
|
69
|
+
const mid = (lo + hi + 1) >> 1;
|
|
70
|
+
if (lineStarts[mid] <= offset) lo = mid;
|
|
71
|
+
else hi = mid - 1;
|
|
72
|
+
}
|
|
73
|
+
return { line: lo, character: offset - lineStarts[lo] };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ── Classification helpers ────────────────────────────────────────────────────
|
|
77
|
+
|
|
78
|
+
interface RawToken {
|
|
79
|
+
offset: number;
|
|
80
|
+
length: number;
|
|
81
|
+
type: SemanticTokenType;
|
|
82
|
+
modifiers: SemanticTokenModifier[];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Mirrors resolvePrefix's precedence order (analyzer.ts) without re-resolving the URI. */
|
|
86
|
+
function isDefaultLibraryPrefix(prefix: string, analysis: FileAnalysis): boolean {
|
|
87
|
+
if (analysis.imports.some((i) => i.prefix === prefix)) return false;
|
|
88
|
+
if (prefix === analysis.modulePrefix && analysis.moduleNamespaceUri) return false;
|
|
89
|
+
const nd = analysis.namespaceDecls.find((n) => n.prefix === prefix);
|
|
90
|
+
if (nd) return nd.offset === -1; // runtime-injected predeclared namespace, e.g. eXist-db
|
|
91
|
+
return prefix in BUILTIN_PREFIXES;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const builtinKey = (q: QName) => `${q.namespaceUri}|${q.localName}`;
|
|
95
|
+
|
|
96
|
+
function buildBuiltinKeys(analysis: FileAnalysis, importedAnalyses: Map<string, FileAnalysis>): Set<string> {
|
|
97
|
+
const keys = new Set<string>();
|
|
98
|
+
for (const a of [analysis, ...importedAnalyses.values()])
|
|
99
|
+
for (const f of a.functions) if (f.sourceUri.startsWith("builtin:")) keys.add(builtinKey(f.qname));
|
|
100
|
+
return keys;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Splits a raw EQName/VarName/FunctionEQName node's text into its prefix and local-name spans. */
|
|
104
|
+
function splitPrefixedName(node: Node): { prefix?: { offset: number; length: number }; local: { offset: number; length: number } } | null {
|
|
105
|
+
const raw = firstTerminalValue(node);
|
|
106
|
+
if (!raw) return null;
|
|
107
|
+
if (raw.startsWith("Q{")) {
|
|
108
|
+
const close = raw.indexOf("}");
|
|
109
|
+
if (close < 0) return null;
|
|
110
|
+
return { local: { offset: node.start + close + 1, length: raw.length - close - 1 } };
|
|
111
|
+
}
|
|
112
|
+
const colon = raw.indexOf(":");
|
|
113
|
+
if (colon <= 0) return { local: { offset: node.start, length: raw.length } };
|
|
114
|
+
return {
|
|
115
|
+
prefix: { offset: node.start, length: colon },
|
|
116
|
+
local: { offset: node.start + colon + 1, length: raw.length - colon - 1 },
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function pushNameTokens(
|
|
121
|
+
node: Node,
|
|
122
|
+
kind: SemanticTokenType,
|
|
123
|
+
analysis: FileAnalysis,
|
|
124
|
+
modifiers: SemanticTokenModifier[],
|
|
125
|
+
out: RawToken[],
|
|
126
|
+
): void {
|
|
127
|
+
const split = splitPrefixedName(node);
|
|
128
|
+
if (!split) return;
|
|
129
|
+
if (split.prefix) {
|
|
130
|
+
const raw = firstTerminalValue(node) ?? "";
|
|
131
|
+
const prefixText = raw.slice(0, split.prefix.length);
|
|
132
|
+
const nsModifiers: SemanticTokenModifier[] = isDefaultLibraryPrefix(prefixText, analysis) ? ["defaultLibrary"] : [];
|
|
133
|
+
out.push({ offset: split.prefix.offset, length: split.prefix.length, type: "namespace", modifiers: nsModifiers });
|
|
134
|
+
}
|
|
135
|
+
out.push({ offset: split.local.offset, length: split.local.length, type: kind, modifiers });
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Element/attribute constructors: only the namespace-prefix part is classifiable (no "element" token type). */
|
|
139
|
+
function pushNamespaceOnly(node: Node | undefined, analysis: FileAnalysis, out: RawToken[]): void {
|
|
140
|
+
if (!node) return;
|
|
141
|
+
const raw = firstTerminalValue(node);
|
|
142
|
+
if (!raw || raw.startsWith("Q{") || raw.startsWith("xmlns")) return;
|
|
143
|
+
const colon = raw.indexOf(":");
|
|
144
|
+
if (colon <= 0) return;
|
|
145
|
+
const modifiers: SemanticTokenModifier[] = isDefaultLibraryPrefix(raw.slice(0, colon), analysis) ? ["defaultLibrary"] : [];
|
|
146
|
+
out.push({ offset: node.start, length: colon, type: "namespace", modifiers });
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function buildUnusedOffsets(ast: Node, analysis: FileAnalysis): Set<number> {
|
|
150
|
+
const offsets = new Set<number>();
|
|
151
|
+
for (const d of checkUnused(ast, analysis))
|
|
152
|
+
if (d.code === "xq-lsp:unused-function" || d.code === "xq-lsp:unused-variable") offsets.add(d.offset);
|
|
153
|
+
return offsets;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ── AST walk ──────────────────────────────────────────────────────────────────
|
|
157
|
+
|
|
158
|
+
function walk(node: Node, analysis: FileAnalysis, builtinKeys: Set<string>, unusedOffsets: Set<number>, out: RawToken[]): void {
|
|
159
|
+
if (isTerminal(node)) return;
|
|
160
|
+
|
|
161
|
+
const { children } = node as NonTerminal;
|
|
162
|
+
|
|
163
|
+
switch (node.type) {
|
|
164
|
+
case "FunctionCall": {
|
|
165
|
+
const call = asFunctionCall(node, analysis);
|
|
166
|
+
const eqname = directChildOf(node, "FunctionEQName");
|
|
167
|
+
if (call && eqname) {
|
|
168
|
+
const mods: SemanticTokenModifier[] = builtinKeys.has(builtinKey(call.qname)) ? ["defaultLibrary"] : [];
|
|
169
|
+
pushNameTokens(eqname, "function", analysis, mods, out);
|
|
170
|
+
}
|
|
171
|
+
for (const c of children) walk(c, analysis, builtinKeys, unusedOffsets, out);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
case "NamedFunctionRef": {
|
|
176
|
+
const ref = asNamedFunctionRef(node, analysis);
|
|
177
|
+
const eqname = directChildOf(node, "EQName");
|
|
178
|
+
if (ref && eqname) {
|
|
179
|
+
const mods: SemanticTokenModifier[] = builtinKeys.has(builtinKey(ref.qname)) ? ["defaultLibrary"] : [];
|
|
180
|
+
pushNameTokens(eqname, "function", analysis, mods, out);
|
|
181
|
+
}
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
case "VarRef": {
|
|
186
|
+
const varNameNode = directChildOf(node, "VarName");
|
|
187
|
+
if (varNameNode) pushNameTokens(varNameNode, "variable", analysis, [], out);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
case "AnnotatedDecl": {
|
|
192
|
+
const fnDeclNode = directChildOf(node, "FunctionDecl");
|
|
193
|
+
if (fnDeclNode) {
|
|
194
|
+
const fn = asFunctionDecl(fnDeclNode, analysis);
|
|
195
|
+
if (fn) {
|
|
196
|
+
const mods: SemanticTokenModifier[] = unusedOffsets.has(fn.nameNode.start) ? ["unused"] : [];
|
|
197
|
+
pushNameTokens(fn.nameNode, "function", analysis, mods, out);
|
|
198
|
+
const paramList = directChildOf(fnDeclNode, "ParamList");
|
|
199
|
+
for (const p of paramList ? directChildrenOf(paramList, "Param") : []) {
|
|
200
|
+
const peq = directChildOf(p, "EQName");
|
|
201
|
+
if (peq) pushNameTokens(peq, "parameter", analysis, [], out);
|
|
202
|
+
}
|
|
203
|
+
if (fn.body) walk(fn.body, analysis, builtinKeys, unusedOffsets, out);
|
|
204
|
+
}
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const varDeclNode = directChildOf(node, "VarDecl");
|
|
208
|
+
if (varDeclNode) {
|
|
209
|
+
const v = asVarDecl(varDeclNode, analysis);
|
|
210
|
+
if (v) {
|
|
211
|
+
const mods: SemanticTokenModifier[] = unusedOffsets.has(v.nameNode.start) ? ["unused"] : [];
|
|
212
|
+
pushNameTokens(v.nameNode, "variable", analysis, mods, out);
|
|
213
|
+
}
|
|
214
|
+
for (const c of (varDeclNode as NonTerminal).children) walk(c, analysis, builtinKeys, unusedOffsets, out);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
for (const c of children) walk(c, analysis, builtinKeys, unusedOffsets, out);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
case "LetBinding":
|
|
222
|
+
case "ForBinding": {
|
|
223
|
+
const varNameNode = directChildOf(node, "VarName");
|
|
224
|
+
if (varNameNode) pushNameTokens(varNameNode, "variable", analysis, [], out);
|
|
225
|
+
for (const c of children) if (c !== varNameNode) walk(c, analysis, builtinKeys, unusedOffsets, out);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
case "InlineFunctionExpr": {
|
|
230
|
+
const paramList = directChildOf(node, "ParamList");
|
|
231
|
+
for (const p of paramList ? directChildrenOf(paramList, "Param") : []) {
|
|
232
|
+
const peq = directChildOf(p, "EQName");
|
|
233
|
+
if (peq) pushNameTokens(peq, "parameter", analysis, [], out);
|
|
234
|
+
}
|
|
235
|
+
const body = directChildOf(node, "FunctionBody");
|
|
236
|
+
if (body) walk(body, analysis, builtinKeys, unusedOffsets, out);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
case "DirElemConstructor":
|
|
241
|
+
pushNamespaceOnly(directChildOf(node, "QName"), analysis, out);
|
|
242
|
+
for (const c of children) walk(c, analysis, builtinKeys, unusedOffsets, out);
|
|
243
|
+
return;
|
|
244
|
+
|
|
245
|
+
case "CompElemConstructor":
|
|
246
|
+
case "CompAttrConstructor":
|
|
247
|
+
pushNamespaceOnly(directChildOf(node, "EQName"), analysis, out);
|
|
248
|
+
for (const c of children) walk(c, analysis, builtinKeys, unusedOffsets, out);
|
|
249
|
+
return;
|
|
250
|
+
|
|
251
|
+
default:
|
|
252
|
+
for (const c of children) walk(c, analysis, builtinKeys, unusedOffsets, out);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// ── Public entry point ────────────────────────────────────────────────────────
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Walk the AST once and produce the delta-encoded `data` array for
|
|
260
|
+
* `textDocument/semanticTokens/full`. Returns `[]` when `ast` is null
|
|
261
|
+
* (the file failed to parse — the regex-fallback analysis has no AST to walk).
|
|
262
|
+
*/
|
|
263
|
+
export function getSemanticTokensData(
|
|
264
|
+
ast: Node | null,
|
|
265
|
+
text: string,
|
|
266
|
+
analysis: FileAnalysis,
|
|
267
|
+
importedAnalyses: Map<string, FileAnalysis>,
|
|
268
|
+
): number[] {
|
|
269
|
+
if (!ast) return [];
|
|
270
|
+
|
|
271
|
+
const builtinKeys = buildBuiltinKeys(analysis, importedAnalyses);
|
|
272
|
+
const unusedOffsets = buildUnusedOffsets(ast, analysis);
|
|
273
|
+
const rawTokens: RawToken[] = [];
|
|
274
|
+
walk(ast, analysis, builtinKeys, unusedOffsets, rawTokens);
|
|
275
|
+
|
|
276
|
+
const lineStarts = computeLineStarts(text);
|
|
277
|
+
const encoded: EncodedToken[] = rawTokens.map((t) => {
|
|
278
|
+
const pos = offsetToPosition(lineStarts, t.offset);
|
|
279
|
+
return {
|
|
280
|
+
line: pos.line,
|
|
281
|
+
character: pos.character,
|
|
282
|
+
length: t.length,
|
|
283
|
+
tokenType: TOKEN_TYPE_INDEX[t.type],
|
|
284
|
+
tokenModifiers: modifierBitmask(t.modifiers),
|
|
285
|
+
};
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
return encodeSemanticTokens(encoded);
|
|
289
|
+
}
|
package/src/server.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
ProposedFeatures,
|
|
6
6
|
TextDocumentSyncKind,
|
|
7
7
|
DiagnosticSeverity,
|
|
8
|
+
DocumentDiagnosticReportKind,
|
|
8
9
|
CodeAction,
|
|
9
10
|
CodeActionKind,
|
|
10
11
|
TextEdit,
|
|
@@ -44,6 +45,8 @@ import {
|
|
|
44
45
|
import { buildOrganizeImportsEdit, buildExtractVariableEdit, buildExtractFunctionEdit } from "./refactor-actions.ts";
|
|
45
46
|
import type { OffsetEdit } from "./refactor-actions.ts";
|
|
46
47
|
import { getWorkspaceSymbols } from "./workspace-symbol.ts";
|
|
48
|
+
import { siblingModuleAnalysis } from "./module-siblings.ts";
|
|
49
|
+
import { getSemanticTokensData, SEMANTIC_TOKENS_LEGEND } from "./semantic-tokens.ts";
|
|
47
50
|
|
|
48
51
|
const connection = createConnection(ProposedFeatures.all);
|
|
49
52
|
const documents = new TextDocuments(TextDocument);
|
|
@@ -257,6 +260,12 @@ function resolveContext(
|
|
|
257
260
|
result.set(imp.namespaceUri, imported);
|
|
258
261
|
}
|
|
259
262
|
}
|
|
263
|
+
// Other files implementing the same module namespace are in scope without an import.
|
|
264
|
+
if (rawAnalysis.moduleNamespaceUri) {
|
|
265
|
+
const merged = globAnalyses.get(rawAnalysis.moduleNamespaceUri);
|
|
266
|
+
const siblings = merged && siblingModuleAnalysis(merged, currentUri);
|
|
267
|
+
if (siblings) result.set(rawAnalysis.moduleNamespaceUri, siblings);
|
|
268
|
+
}
|
|
260
269
|
// Add pre-declared runtime analyses directly — available without explicit import
|
|
261
270
|
for (const nd of predeclaredNs) {
|
|
262
271
|
if (!result.has(nd.namespaceUri)) {
|
|
@@ -302,12 +311,13 @@ connection.onInitialize((params) => {
|
|
|
302
311
|
documentLinkProvider: { resolveProvider: false },
|
|
303
312
|
callHierarchyProvider: true,
|
|
304
313
|
inlayHintProvider: true,
|
|
314
|
+
diagnosticProvider: { interFileDependencies: true, workspaceDiagnostics: false },
|
|
315
|
+
semanticTokensProvider: { legend: SEMANTIC_TOKENS_LEGEND, full: true },
|
|
305
316
|
},
|
|
306
317
|
};
|
|
307
318
|
});
|
|
308
319
|
|
|
309
|
-
|
|
310
|
-
const doc = change.document;
|
|
320
|
+
function computeDiagnostics(doc: TextDocument) {
|
|
311
321
|
const { analysis: rawAnalysis, parseDiagnostics: parseDiags, hasAst, ast } = analyzeDocumentFull(doc);
|
|
312
322
|
const { analysis, imported: resolvedImported } = resolveContext(doc.uri, rawAnalysis);
|
|
313
323
|
|
|
@@ -335,10 +345,26 @@ documents.onDidChangeContent((change) => {
|
|
|
335
345
|
const errorDiags = errorDiagRaw.filter((d) => d.code !== "XQST0081").map((d) => toLsp(d, DiagnosticSeverity.Error));
|
|
336
346
|
const hintDiags = hintDiagRaw.map((d) => toLsp(d, DiagnosticSeverity.Hint));
|
|
337
347
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
348
|
+
return [...parseDiags, ...nsDiags, ...errorDiags, ...hintDiags];
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
documents.onDidChangeContent((change) => {
|
|
352
|
+
connection.sendDiagnostics({ uri: change.document.uri, diagnostics: computeDiagnostics(change.document) });
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
// LSP 3.17 pull diagnostics. Falls back to reading the file from disk so clients that edit
|
|
356
|
+
// files outside an editor session (no didOpen) can still ask "does this file have errors?".
|
|
357
|
+
connection.languages.diagnostics.on((params) => {
|
|
358
|
+
let doc = documents.get(params.textDocument.uri);
|
|
359
|
+
if (!doc) {
|
|
360
|
+
try {
|
|
361
|
+
const text = fs.readFileSync(uriToPath(params.textDocument.uri), "utf-8");
|
|
362
|
+
doc = TextDocument.create(params.textDocument.uri, "xquery", 0, text);
|
|
363
|
+
} catch {
|
|
364
|
+
return { kind: DocumentDiagnosticReportKind.Full, items: [] };
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return { kind: DocumentDiagnosticReportKind.Full, items: computeDiagnostics(doc) };
|
|
342
368
|
});
|
|
343
369
|
|
|
344
370
|
connection.onCompletion((params) => {
|
|
@@ -541,6 +567,14 @@ function toEditRange(doc: TextDocument, edit: OffsetEdit) {
|
|
|
541
567
|
return { range: { start: doc.positionAt(edit.start), end: doc.positionAt(edit.end) }, newText: edit.newText };
|
|
542
568
|
}
|
|
543
569
|
|
|
570
|
+
connection.languages.semanticTokens.on((params) => {
|
|
571
|
+
const doc = documents.get(params.textDocument.uri);
|
|
572
|
+
if (!doc) return { data: [] };
|
|
573
|
+
const { analysis: rawAnalysis, ast } = analyzeDocumentFull(doc);
|
|
574
|
+
const { analysis, imported } = resolveContext(doc.uri, rawAnalysis);
|
|
575
|
+
return { data: getSemanticTokensData(ast, doc.getText(), analysis, imported) };
|
|
576
|
+
});
|
|
577
|
+
|
|
544
578
|
connection.onCodeAction((params) => {
|
|
545
579
|
const doc = documents.get(params.textDocument.uri);
|
|
546
580
|
if (!doc) return [];
|