Skip to content
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

Default dialect when no schema specified #25

Closed
Show file tree
Hide file tree
Changes from 11 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
64 changes: 57 additions & 7 deletions language-server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DiagnosticTag,
DidChangeWatchedFilesNotification,
ProposedFeatures,
DidChangeConfigurationNotification,
SemanticTokensBuilder,
TextDocuments,
TextDocumentSyncKind
Expand Down Expand Up @@ -38,9 +39,13 @@ connection.console.log("Starting JSON Schema service ...");

let hasWorkspaceFolderCapability = false;
let hasWorkspaceWatchCapability = false;
let hasConfigurationCapability = false;

connection.onInitialize(({ capabilities, workspaceFolders }) => {
connection.console.log("Initializing JSON Schema service ...");
hasConfigurationCapability = !!(
capabilities.workspace && !!capabilities.workspace.configuration
);

if (workspaceFolders) {
addWorkspaceFolders(workspaceFolders);
Expand Down Expand Up @@ -72,6 +77,10 @@ connection.onInitialize(({ capabilities, workspaceFolders }) => {
});

connection.onInitialized(async () => {
if (hasConfigurationCapability) {
connection.client.register(DidChangeConfigurationNotification.type);
}

if (hasWorkspaceWatchCapability) {
connection.client.register(DidChangeWatchedFilesNotification.type, {
watchers: [
Expand Down Expand Up @@ -135,6 +144,37 @@ connection.listen();

const documents = new TextDocuments(TextDocument);

// CONFIGURATION

const documentSettings = new Map();
let globalSettings = {};

async function getDocumentSettings(resource) {
if (!hasConfigurationCapability) {
return globalSettings;
}

if (!documentSettings.has(resource)) {
const result = await connection.workspace.getConfiguration({
scopeUri: resource,
section: "jsonSchemaLanguageServer"
});
documentSettings.set(resource, result ?? globalSettings);
}

return documentSettings.get(resource);
}

connection.onDidChangeConfiguration((change) => {
if (hasConfigurationCapability) {
documentSettings.clear();
} else {
globalSettings = change.settings.jsonSchemaLanguageServer || globalSettings;
diyaayay marked this conversation as resolved.
Show resolved Hide resolved
}

validateWorkspace();
});

// INLINE ERRORS

documents.onDidChangeContent(async ({ document }) => {
Expand All @@ -149,13 +189,14 @@ documents.onDidChangeContent(async ({ document }) => {
const validateSchema = async (document) => {
const diagnostics = [];

const settings = await getDocumentSettings(document.uri);
const instance = JsoncInstance.fromTextDocument(document);
if (instance.typeOf() === "undefined") {
return;
}

const $schema = instance.get("#/$schema");
const contextDialectUri = $schema.value();
const contextDialectUri = $schema.value() ?? settings.defaultDialect;
const schemaResources = decomposeSchemaDocument(instance, contextDialectUri);
for (const { dialectUri, schemaInstance } of schemaResources) {
if (!hasDialect(dialectUri)) {
Expand Down Expand Up @@ -259,9 +300,16 @@ const getTokenBuilder = (uri) => {
return result;
};

const buildTokens = (builder, document) => {
const buildTokens = (builder, document, settings) => {
const instance = JsoncInstance.fromTextDocument(document);
const dialectUri = instance.get("#/$schema").value();
const $schema = instance.get("#/$schema");
let dialectUri;
try {
dialectUri = $schema?.value() ?? settings.defaultDialect;
} catch (error) {
// SKIP
}
diyaayay marked this conversation as resolved.
Show resolved Hide resolved

const schemaResources = decomposeSchemaDocument(instance, dialectUri);
for (const { keywordInstance, tokenType, tokenModifier } of getSemanticTokens(schemaResources)) {
const startPosition = keywordInstance.startPosition();
Expand All @@ -275,31 +323,33 @@ const buildTokens = (builder, document) => {
}
};

connection.languages.semanticTokens.on(({ textDocument }) => {
connection.languages.semanticTokens.on(async ({ textDocument }) => {
connection.console.log(`semanticTokens.on: ${textDocument.uri}`);

if (isSchema(textDocument.uri)) {
const builder = getTokenBuilder(textDocument.uri);
const document = documents.get(textDocument.uri);
buildTokens(builder, document);
const settings = await getDocumentSettings(document.uri);
buildTokens(builder, document, settings);

return builder.build();
} else {
return { data: [] };
}
});

connection.languages.semanticTokens.onDelta(({ textDocument, previousResultId }) => {
connection.languages.semanticTokens.onDelta(async ({ textDocument, previousResultId }) => {
connection.console.log(`semanticTokens.onDelta: ${textDocument.uri}`);

const document = documents.get(textDocument.uri);
const settings = await getDocumentSettings(document.uri);
if (document === undefined) {
return { edits: [] };
}

const builder = getTokenBuilder(document);
builder.previousResult(previousResultId);
buildTokens(builder, document);
buildTokens(builder, document, settings);

return builder.buildEdits();
});
Expand Down
11 changes: 10 additions & 1 deletion vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@
"onLanguage:json"
],
"contributes": {
"configuration": {
"title": "Configuration for JSON Schema Language Server",
"properties": {
"jsonSchemaLanguageServer.defaultDialect": {
"type": "string",
"description": "The default JSON Schema dialect to use if none is specified in the schema document"
}
}
}
}
}
}