Skip to content

Commit 61ae4fb

Browse files
committed
Use SchemaNode instead of JsonNode
1 parent 164a290 commit 61ae4fb

File tree

5 files changed

+59
-49
lines changed

5 files changed

+59
-49
lines changed

language-server/src/features/deprecated.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DiagnosticSeverity, DiagnosticTag } from "vscode-languageserver";
2-
import * as JsonNode from "../json-node.js";
2+
import * as SchemaNode from "../schema-node.js";
33
import { subscribe } from "../pubsub.js";
44

55

@@ -12,12 +12,12 @@ export default {
1212

1313
onInitialized() {
1414
subscribe("diagnostics", async (_message, { schemaDocument, diagnostics }) => {
15-
for (const { schemaResource } of schemaDocument.schemaResources) {
16-
for (const deprecated of JsonNode.annotatedWith(schemaResource, "deprecated", annotationDialectUri)) {
17-
if (JsonNode.annotation(deprecated, "deprecated", annotationDialectUri).some((deprecated) => deprecated)) {
15+
for (const schemaResource of schemaDocument.schemaResources) {
16+
for (const deprecated of SchemaNode.annotatedWith(schemaResource, "deprecated", annotationDialectUri)) {
17+
if (SchemaNode.annotation(deprecated, "deprecated", annotationDialectUri).some((deprecated) => deprecated)) {
1818
diagnostics.push({
1919
instance: deprecated.parent,
20-
message: JsonNode.annotation(deprecated, "x-deprecationMessage", annotationDialectUri).join("\n") || "deprecated",
20+
message: SchemaNode.annotation(deprecated, "x-deprecationMessage", annotationDialectUri).join("\n") || "deprecated",
2121
severity: DiagnosticSeverity.Warning,
2222
tags: [DiagnosticTag.Deprecated]
2323
});

language-server/src/features/hover.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { MarkupKind } from "vscode-languageserver";
22
import * as SchemaDocument from "../schema-document.js";
3-
import * as JsonNode from "../json-node.js";
3+
import * as SchemaNode from "../schema-node.js";
44
import { getSchemaDocument } from "./schema-registry.js";
55

66

@@ -22,11 +22,11 @@ export default {
2222

2323
// This is a little wierd because we want the hover to be on the keyword, but
2424
// the annotation is actually on the value not the keyword.
25-
if (keyword.parent && JsonNode.typeOf(keyword.parent) === "property" && keyword.parent.children[0] === keyword) {
25+
if (keyword.parent && SchemaNode.typeOf(keyword.parent) === "property" && keyword.parent.children[0] === keyword) {
2626
return {
2727
contents: {
2828
kind: MarkupKind.Markdown,
29-
value: JsonNode.annotation(keyword.parent.children[1], "description", annotationDialectUri).join("\n")
29+
value: SchemaNode.annotation(keyword.parent.children[1], "description", annotationDialectUri).join("\n")
3030
},
3131
range: {
3232
start: document.positionAt(keyword.offset),

language-server/src/features/semantic-tokens.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SemanticTokensBuilder } from "vscode-languageserver";
22
import { getKeywordId } from "@hyperjump/json-schema/experimental";
3-
import * as JsonNode from "../json-node.js";
3+
import * as SchemaNode from "../schema-node.js";
44
import { getSchemaDocument } from "./schema-registry.js";
55
import { toAbsoluteUri } from "../util.js";
66
import { isMatchedFile } from "./workspace.js";
@@ -127,22 +127,22 @@ const sortSemanticTokens = (semanticTokens, textDocument) => {
127127
};
128128

129129
const getSemanticTokens = function* (schemaDocument) {
130-
for (const { schemaResource, dialectUri } of schemaDocument.schemaResources) {
131-
yield* schemaHandler(schemaResource, dialectUri);
130+
for (const schemaResource of schemaDocument.schemaResources) {
131+
yield* schemaHandler(schemaResource);
132132
}
133133
};
134134

135-
const schemaHandler = function* (schemaResource, dialectUri) {
136-
for (const [keyNode, valueNode] of JsonNode.entries(schemaResource)) {
137-
const keywordName = JsonNode.value(keyNode);
138-
const keywordId = keywordIdFor(keywordName, dialectUri);
135+
const schemaHandler = function* (schemaResource) {
136+
for (const [keyNode, valueNode] of SchemaNode.entries(schemaResource)) {
137+
const keywordName = SchemaNode.value(keyNode);
138+
const keywordId = keywordIdFor(keywordName, schemaResource.dialectUri);
139139

140140
if (keywordId) {
141141
if (keywordId === "https://json-schema.org/keyword/comment") {
142142
yield { keywordInstance: keyNode.parent, tokenType: "comment" };
143143
} else if (toAbsoluteUri(keywordId) !== "https://json-schema.org/keyword/unknown") {
144144
yield { keywordInstance: keyNode, tokenType: "keyword" };
145-
yield* getKeywordHandler(keywordId)(valueNode, dialectUri);
145+
yield* getKeywordHandler(keywordId)(valueNode);
146146
}
147147
}
148148
}
@@ -158,15 +158,15 @@ const keywordIdFor = (keywordName, dialectUri) => {
158158
}
159159
};
160160

161-
const schemaMapHandler = function* (schemaResource, dialectUri) {
162-
for (const schemaNode of JsonNode.values(schemaResource)) {
163-
yield* schemaHandler(schemaNode, dialectUri);
161+
const schemaMapHandler = function* (schemaResource) {
162+
for (const schemaNode of SchemaNode.values(schemaResource)) {
163+
yield* schemaHandler(schemaNode);
164164
}
165165
};
166166

167-
const schemaArrayHandler = function* (schemaResource, dialectUri) {
168-
for (const schemaNode of JsonNode.iter(schemaResource)) {
169-
yield* schemaHandler(schemaNode, dialectUri);
167+
const schemaArrayHandler = function* (schemaResource) {
168+
for (const schemaNode of SchemaNode.iter(schemaResource)) {
169+
yield* schemaHandler(schemaNode);
170170
}
171171
};
172172

language-server/src/schema-document.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getSchema, compile, interpret, getKeywordName, hasDialect, BASIC } from
22
import * as JsonPointer from "@hyperjump/json-pointer";
33
import { resolveIri, toAbsoluteIri } from "@hyperjump/uri";
44
import { getNodeValue, parseTree } from "jsonc-parser";
5-
import * as JsonNode from "./json-node.js";
5+
import * as SchemaNode from "./schema-node.js";
66
import { uriFragment } from "./util.js";
77

88

@@ -27,16 +27,16 @@ export const fromTextDocument = async (textDocument, contextDialectUri) => {
2727

2828
buildSchemaResources(document, root, textDocument.uri, contextDialectUri);
2929

30-
for (const { dialectUri, schemaResource } of document.schemaResources) {
31-
if (!hasDialect(dialectUri)) {
32-
const $schema = JsonNode.get("#/$schema", schemaResource);
33-
if ($schema && JsonNode.typeOf($schema) === "string") {
30+
for (const schemaResource of document.schemaResources) {
31+
if (!hasDialect(schemaResource.dialectUri)) {
32+
const $schema = SchemaNode.get("#/$schema", schemaResource);
33+
if ($schema && SchemaNode.typeOf($schema) === "string") {
3434
document.errors.push({
3535
keyword: "https://json-schema.org/keyword/schema",
3636
instanceNode: $schema,
3737
message: "Unknown dialect"
3838
});
39-
} else if (dialectUri) {
39+
} else if (schemaResource.dialectUri) {
4040
document.errors.push({
4141
keyword: "https://json-schema.org/keyword/schema",
4242
instanceNode: schemaResource,
@@ -53,15 +53,15 @@ export const fromTextDocument = async (textDocument, contextDialectUri) => {
5353
continue;
5454
}
5555

56-
const schema = await getSchema(dialectUri);
56+
const schema = await getSchema(schemaResource.dialectUri);
5757
const compiled = await compile(schema);
5858
const output = interpret(compiled, schemaResource, BASIC);
5959
if (!output.valid) {
6060
for (const error of output.errors) {
6161
document.errors.push({
6262
keyword: error.keyword,
6363
keywordNode: await getSchema(error.absoluteKeywordLocation),
64-
instanceNode: JsonNode.get(error.instanceLocation, schemaResource)
64+
instanceNode: SchemaNode.get(error.instanceLocation, schemaResource)
6565
});
6666
}
6767
}
@@ -72,13 +72,13 @@ export const fromTextDocument = async (textDocument, contextDialectUri) => {
7272
};
7373

7474
const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer = "", parent = undefined, anchors = {}) => {
75-
const jsonNode = JsonNode.cons(uri, pointer, getNodeValue(node), node.type, [], parent, node.offset, node.length);
75+
const schemaNode = SchemaNode.cons(uri, pointer, getNodeValue(node), node.type, [], parent, node.offset, node.length, dialectUri, anchors);
7676

7777
switch (node.type) {
7878
case "array":
79-
jsonNode.children = node.children.map((child, index) => {
79+
schemaNode.children = node.children.map((child, index) => {
8080
const itemPointer = JsonPointer.append(index, pointer);
81-
return buildSchemaResources(document, child, uri, dialectUri, itemPointer, jsonNode, anchors);
81+
return buildSchemaResources(document, child, uri, dialectUri, itemPointer, schemaNode, anchors);
8282
});
8383
break;
8484

@@ -89,6 +89,7 @@ const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer
8989
if ($schema?.type === "string") {
9090
try {
9191
dialectUri = toAbsoluteIri(getNodeValue($schema));
92+
schemaNode.dialectUri = dialectUri;
9293
} catch (error) {
9394
// Ignore
9495
}
@@ -98,7 +99,7 @@ const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer
9899
const $idNode = idToken && nodeStep(node, idToken);
99100
if ($idNode) {
100101
uri = toAbsoluteIri(resolveIri(getNodeValue($idNode), uri));
101-
jsonNode.baseUri = uri;
102+
schemaNode.baseUri = uri;
102103
}
103104

104105
const legacyIdToken = keywordNameFor("https://json-schema.org/keyword/draft-04/id", dialectUri);
@@ -107,7 +108,7 @@ const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer
107108
const legacy$id = getNodeValue(legacy$idNode);
108109
if (legacy$id[0] !== "#") {
109110
uri = toAbsoluteIri(resolveIri(legacy$id, uri));
110-
jsonNode.baseUri = uri;
111+
schemaNode.baseUri = uri;
111112
}
112113
}
113114
} else {
@@ -116,7 +117,7 @@ const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer
116117
if (embeddedDialectUri) {
117118
buildSchemaResources(document, node, uri, embeddedDialectUri);
118119

119-
return JsonNode.cons(uri, pointer, true, "boolean", [], parent, node.offset, node.length);
120+
return SchemaNode.cons(uri, pointer, true, "boolean", [], parent, node.offset, node.length, dialectUri, anchors);
120121
}
121122
}
122123

@@ -138,10 +139,10 @@ const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer
138139

139140
for (const child of node.children) {
140141
const propertyPointer = JsonPointer.append(getNodeValue(child.children[0]), pointer);
141-
const propertyNode = buildSchemaResources(document, child, uri, dialectUri, propertyPointer, jsonNode, anchors);
142+
const propertyNode = buildSchemaResources(document, child, uri, dialectUri, propertyPointer, schemaNode, anchors);
142143

143144
if (propertyNode) {
144-
jsonNode.children.push(propertyNode);
145+
schemaNode.children.push(propertyNode);
145146
}
146147
}
147148
break;
@@ -151,22 +152,17 @@ const buildSchemaResources = (document, node, uri = "", dialectUri = "", pointer
151152
return;
152153
}
153154

154-
jsonNode.children = node.children.map((child) => {
155-
return buildSchemaResources(document, child, uri, dialectUri, pointer, jsonNode, anchors);
155+
schemaNode.children = node.children.map((child) => {
156+
return buildSchemaResources(document, child, uri, dialectUri, pointer, schemaNode, anchors);
156157
});
157158
break;
158159
}
159160

160-
if (jsonNode.pointer === "") {
161-
document.schemaResources.push({
162-
schemaResource: jsonNode,
163-
dialectUri: dialectUri,
164-
baseUri: uri,
165-
anchors: anchors
166-
});
161+
if (schemaNode.pointer === "") {
162+
document.schemaResources.push(schemaNode);
167163
}
168164

169-
return jsonNode;
165+
return schemaNode;
170166
};
171167

172168
const getEmbeddedDialectUri = (node, dialectUri) => {
@@ -194,7 +190,7 @@ const getEmbeddedDialectUri = (node, dialectUri) => {
194190
};
195191

196192
export const findNodeAtOffset = (document, offset) => {
197-
for (const { schemaResource } of document.schemaResources) {
193+
for (const schemaResource of document.schemaResources) {
198194
const node = _findNodeAtOffset(schemaResource, offset);
199195
if (node) {
200196
return node;

language-server/src/schema-node.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as JsonNode from "./json-node.js";
2+
3+
4+
// eslint-disable-next-line import/export
5+
export const cons = (uri, pointer, value, type, children, parent, offset, textLength, dialectUri, anchors) => {
6+
const node = JsonNode.cons(uri, pointer, value, type, children, parent, offset, textLength);
7+
node.dialectUri = dialectUri;
8+
node.anchors = anchors;
9+
10+
return node;
11+
};
12+
13+
// eslint-disable-next-line import/export
14+
export * from "./json-node.js";

0 commit comments

Comments
 (0)