Skip to content

Support Custom Dialects #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions language-server/src/build-server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TextDocuments } from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";
import { removeMediaTypePlugin } from "@hyperjump/browser";

// Hyperjump
import "@hyperjump/json-schema/draft-2020-12";
Expand All @@ -22,6 +23,9 @@ import "@hyperjump/json-schema/draft-04";
* }} Feature
*/

removeMediaTypePlugin("http");
removeMediaTypePlugin("https");

/** @type (connection: Connection, features: Feature[]) => void */
export const buildServer = (connection, features) => {
const documents = new TextDocuments(TextDocument);
Expand Down
109 changes: 109 additions & 0 deletions language-server/src/features/custom-dialects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import {
DidChangeWatchedFilesNotification,
PublishDiagnosticsNotification,
WorkDoneProgress,
WorkDoneProgressCreateRequest
} from "vscode-languageserver";
import { resolveIri } from "@hyperjump/uri";
import { rm } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { TestClient } from "../test-client.js";
import documentSettings from "./document-settings.js";
import semanticTokens from "./semantic-tokens.js";
import schemaRegistry from "./schema-registry.js";
import workspace from "./workspace.js";
import validationErrorsFeature from "./validation-errors.js";
import { setupWorkspace, tearDownWorkspace } from "../test-utils.js";

import type { Diagnostic } from "vscode-languageserver";
import type { DocumentSettings } from "./document-settings.js";


describe("Feature - Custom Dialects", () => {
let client: TestClient<DocumentSettings>;
let workspaceFolder: string;
let documentUriB: string;
let documentUri: string;

beforeEach(async () => {
client = new TestClient([
workspace,
documentSettings,
semanticTokens,
schemaRegistry,
validationErrorsFeature
]);
workspaceFolder = await setupWorkspace({
"subjectB.schema.json": `{
"$id": "https://example.com/my-dialect",
"$schema": "https://json-schema.org/draft/2020-12/schema",

"$vocabulary": {
"https://json-schema.org/draft/2020-12/vocab/core": true,
"https://json-schema.org/draft/2020-12/vocab/applicator": true,
"https://json-schema.org/draft/2020-12/vocab/validation": true
}
}`,
"subject.schema.json": `{
"$schema": "https://example.com/my-dialect"
}`
});
documentUriB = resolveIri("./subjectB.schema.json", `${workspaceFolder}/`);
documentUri = resolveIri("./subject.schema.json", `${workspaceFolder}/`);

await client.start({
workspaceFolders: [
{
name: "root",
uri: workspaceFolder
}
]
});
});

afterEach(async () => {
await client.stop();
await tearDownWorkspace(workspaceFolder);
});

test("Registered dialect schema", async () => {
const diagnosticsPromise = new Promise<Diagnostic[]>((resolve) => {
client.onNotification(PublishDiagnosticsNotification.type, (params) => {
resolve(params.diagnostics);
});
});

await client.openDocument(documentUriB);
await client.openDocument(documentUri);

const diagnostics = await diagnosticsPromise;
expect(diagnostics).to.eql([]);
});

test("Unregister dialect schema", async () => {
const diagnosticsPromise = new Promise<Diagnostic[]>((resolve) => {
let diagnostics: Diagnostic[];

client.onRequest(WorkDoneProgressCreateRequest.type, ({ token }) => {
client.onProgress(WorkDoneProgress.type, token, ({ kind }) => {
if (kind === "end") {
resolve(diagnostics);
}
});
});

client.onNotification(PublishDiagnosticsNotification.type, (params) => {
diagnostics = params.diagnostics;
});
});

await rm(fileURLToPath(documentUriB));
await client.sendNotification(DidChangeWatchedFilesNotification.type, {
changes: []
});

const diagnostics = await diagnosticsPromise;
expect(diagnostics[0]?.message).to.eql("Unknown dialect");
});
});
4 changes: 2 additions & 2 deletions language-server/src/features/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe("Feature - Hover", () => {
["type", "\"object\""]
])("%s should have a message", async (keyword, value) => {
documentUri = await client.openDocument("./subject.schema.json", `{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",${keyword === "$vocabulary" ? `"$id": "https://example.com/schema",` : ""}
"${keyword}": ${value}
}`);

Expand Down Expand Up @@ -317,7 +317,7 @@ describe("Feature - Hover", () => {
["type", "\"object\""]
])("%s should have a message", async (keyword, value) => {
documentUri = await client.openDocument("./subject.schema.json", `{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$schema": "https://json-schema.org/draft/2019-09/schema",${keyword === "$vocabulary" ? `"$id": "https://example.com/schema",` : ""}
"${keyword}": ${value}
}`);

Expand Down
1 change: 1 addition & 0 deletions language-server/src/features/schema-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default {
},

onShutdown() {
clearSchemaDocuments();
}
};

Expand Down
9 changes: 4 additions & 5 deletions language-server/src/features/semantic-tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ describe("Feature - Semantic Tokens", () => {
await client.closeDocument(documentUri);
});


test.each([
// Applicators
["prefixItems", "[{}]", [1, 2, 9, 1, 0, 1, 2, 13, 1, 0]],
Expand Down Expand Up @@ -144,7 +143,7 @@ describe("Feature - Semantic Tokens", () => {
["$ref", "\"\"", [1, 2, 9, 1, 0, 1, 2, 6, 1, 0]],
["$dynamicRef", "\"\"", [1, 2, 9, 1, 0, 1, 2, 13, 1, 0]],
["$dynamicAnchor", "\"foo\"", [1, 2, 9, 1, 0, 1, 2, 16, 1, 0]],
["$vocabulary", "{}", [1, 2, 9, 1, 0, 1, 2, 13, 1, 0]],
["$vocabulary", "{}", [1, 2, 9, 1, 0, 0, 58, 5, 1, 0, 1, 2, 13, 1, 0]],
["$comment", "\"\"", [1, 2, 9, 1, 0, 1, 2, 14, 2, 0]],
["$defs", "{}", [1, 2, 9, 1, 0, 1, 2, 7, 1, 0]],

Expand Down Expand Up @@ -187,7 +186,7 @@ describe("Feature - Semantic Tokens", () => {
["type", "\"object\"", [1, 2, 9, 1, 0, 1, 2, 6, 1, 0]]
])("%s should be highlighted", async (keyword, value, expected) => {
documentUri = await client.openDocument("./subject.schema.json", `{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",${keyword === "$vocabulary" ? `"$id": "https://example.com/schema",` : ""}
"${keyword}": ${value}
}`);

Expand Down Expand Up @@ -261,7 +260,7 @@ describe("Feature - Semantic Tokens", () => {
["$ref", "\"\"", [1, 2, 9, 1, 0, 1, 2, 6, 1, 0]],
["$recursiveRef", "\"\"", [1, 2, 9, 1, 0, 1, 2, 15, 1, 0]],
["$recursiveAnchor", "true", [1, 2, 9, 1, 0, 1, 2, 18, 1, 0]],
["$vocabulary", "{}", [1, 2, 9, 1, 0, 1, 2, 13, 1, 0]],
["$vocabulary", "{}", [1, 2, 9, 1, 0, 0, 58, 5, 1, 0, 1, 2, 13, 1, 0]],
["$comment", "\"\"", [1, 2, 9, 1, 0, 1, 2, 14, 2, 0]],
["$defs", "{}", [1, 2, 9, 1, 0, 1, 2, 7, 1, 0]],

Expand Down Expand Up @@ -300,7 +299,7 @@ describe("Feature - Semantic Tokens", () => {
["type", "\"object\"", [1, 2, 9, 1, 0, 1, 2, 6, 1, 0]]
])("%s should be highlighted", async (keyword, value, expected) => {
documentUri = await client.openDocument("./subject.schema.json", `{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$schema": "https://json-schema.org/draft/2019-09/schema",${keyword === "$vocabulary" ? `"$id": "https://example.com/schema",` : ""}
"${keyword}": ${value}
}`);

Expand Down
63 changes: 53 additions & 10 deletions language-server/src/features/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ import {
} from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";
import { URI } from "vscode-uri";
import { registerSchema, unregisterSchema } from "@hyperjump/json-schema/draft-2020-12";
import { hasDialect } from "@hyperjump/json-schema/experimental";
import { toAbsoluteIri } from "@hyperjump/uri";
import picomatch from "picomatch";
import { publishAsync, subscribe, unsubscribe } from "../pubsub.js";
import * as SchemaNode from "../schema-node.js";
import { keywordNameFor } from "../util.js";
import { allSchemaDocuments, getSchemaDocument } from "./schema-registry.js";
import { getDocumentSettings } from "./document-settings.js";
import picomatch from "picomatch";

/**
* @import { FSWatcher, WatchEventType } from "node:fs"
Expand All @@ -33,30 +38,64 @@ import picomatch from "picomatch";
* tags?: DiagnosticTag[];
* }} ValidationDiagnostic
*/

let hasWorkspaceFolderCapability = false;
let hasWorkspaceWatchCapability = false;

/** @type string */
let subscriptionToken;

/** @type Set<string> */
const customDialects = new Set();

/** @type Feature */
export default {
load(connection, documents) {
subscriptionToken = subscribe("workspaceChanged", async (_message, _changes) => {
const reporter = await connection.window.createWorkDoneProgress();
reporter.begin("JSON Schema: Indexing workspace");

// Unregister all existing schemas
for (const dialectUri of customDialects) {
unregisterSchema(dialectUri);
}
customDialects.clear();

// Load all schemas
const settings = await getDocumentSettings(connection);
const schemaFilePatterns = settings.schemaFilePatterns;
for await (const uri of workspaceSchemas(schemaFilePatterns)) {
let textDocument = documents.get(uri);
if (!textDocument) {
const instanceJson = await readFile(fileURLToPath(uri), "utf8");
textDocument = TextDocument.create(uri, "json", -1, instanceJson);
const instanceJson = await readFile(fileURLToPath(uri), "utf8");
const textDocument = TextDocument.create(uri, "json", -1, instanceJson);

const schemaDocument = await getSchemaDocument(connection, textDocument);
for (const schemaResource of schemaDocument.schemaResources) {
const vocabToken = keywordNameFor("https://json-schema.org/keyword/vocabulary", schemaResource.dialectUri);
const vocabularyNode = vocabToken && SchemaNode.step(vocabToken, schemaResource);
if (vocabularyNode) {
registerSchema(SchemaNode.value(schemaResource), schemaResource.baseUri);
customDialects.add(schemaResource.baseUri);
}
}
}

await getSchemaDocument(connection, textDocument);
// Rebuild custom dialect schemas
for (const schemaDocument of allSchemaDocuments()) {
for (const error of schemaDocument.errors) {
try {
const dialectUri = toAbsoluteIri(SchemaNode.value(error.instanceNode));
if (error.keyword === "https://json-schema.org/keyword/schema" && hasDialect(dialectUri)) {
for (const schemaResource of schemaDocument.schemaResources) {
if (customDialects.has(schemaResource.baseUri)) {
unregisterSchema(schemaResource.baseUri);
}
}
await getSchemaDocument(connection, schemaDocument.textDocument);
}
} catch (error) {
// Ignore Invalid IRI for now
}
}
}

// Re/validate all schemas
Expand Down Expand Up @@ -174,9 +213,12 @@ export default {
},

onShutdown() {
for (const path in watchers) {
watchers[path].close();
removeWorkspaceFolders([...workspaceFolders]);

for (const dialectUri of customDialects) {
unregisterSchema(dialectUri);
}
customDialects.clear();

unsubscribe("workspaceChanged", subscriptionToken);
}
Expand Down Expand Up @@ -227,8 +269,9 @@ const addWorkspaceFolders = (folders) => {
/** @type (folders: WorkspaceFolder[]) => void */
const removeWorkspaceFolders = (folders) => {
for (const folder of folders) {
if (watchers[folder.uri]) {
watchers[folder.uri].close();
const folderPath = fileURLToPath(folder.uri);
if (watchers[folderPath]) {
watchers[folderPath].close();
}

workspaceFolders.delete(folder);
Expand Down
Loading