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

refactor(vue-json): provide schema with Ls configure #17

Merged
merged 1 commit into from Oct 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 15 additions & 34 deletions packages/vue-json/src/index.ts
Expand Up @@ -6,7 +6,7 @@ import { TextDocument } from 'vscode-languageserver-textdocument';
// modify of https://github.com/johnsoncodehk/volar/blob/master/plugins/json/src/index.ts
export = function (schemaUrls: Record<string, string>): LanguageServicePlugin {

const jsonDocuments = new WeakMap<TextDocument, [number, TextDocument, json.JSONDocument]>();
const jsonDocuments = new WeakMap<TextDocument, [number, json.JSONDocument]>();

let jsonLs: json.LanguageService;

Expand All @@ -19,6 +19,10 @@ export = function (schemaUrls: Record<string, string>): LanguageServicePlugin {
resolveRelativePath: (ref, base) => _context.env.documentContext!.resolveReference(ref, base) ?? ref
} : undefined,
});
const schemas = Object.entries(schemaUrls).map(entry =>
({ fileMatch: [`*.customBlock_${entry[0]}_*.json*`], uri: new URL(entry[1], _context.env.rootUri.toString() + '/').toString() })
)
jsonLs.configure({ schemas })
},

complete: {
Expand All @@ -27,7 +31,7 @@ export = function (schemaUrls: Record<string, string>): LanguageServicePlugin {
triggerCharacters: ['"', ':'],

on(document, position, context) {
return worker(document, async (document, jsonDocument) => {
return worker(document, async (jsonDocument) => {
return await jsonLs.doComplete(document, position, jsonDocument);
});
},
Expand All @@ -39,70 +43,47 @@ export = function (schemaUrls: Record<string, string>): LanguageServicePlugin {

validation: {
onSyntactic(document) {
return worker(document, async (document, jsonDocument) => {

const documentLanguageSettings = undefined; // await getSettings(); // TODO

return worker(document, async (jsonDocument) => {
return await jsonLs.doValidation(
document,
jsonDocument,
documentLanguageSettings,
undefined, // TODO
) as vscode.Diagnostic[];
});
},
},

doHover(document, position) {
return worker(document, async (document, jsonDocument) => {
return worker(document, async (jsonDocument) => {
return await jsonLs.doHover(document, position, jsonDocument);
});
},
};

function worker<T>(document: TextDocument, callback: (doc: TextDocument, jsonDocument: json.JSONDocument) => T) {
function worker<T>(document: TextDocument, callback: (jsonDocument: json.JSONDocument) => T) {

const jsonDocument = getJsonDocument(document);
if (!jsonDocument)
return;

return callback(jsonDocument[0], jsonDocument[1]);
return callback(jsonDocument);
}

function getJsonDocument(textDocument: TextDocument) {

if (textDocument.languageId !== 'json' && textDocument.languageId !== 'jsonc')
return;

const match = textDocument.uri.match(/^(.*)\.customBlock_([^_]+)_(\d+)\.([^.]+)$/);
if (!match)
return;

const blockType = match[2];
const schemaUrl = schemaUrls[blockType];
if (!schemaUrl)
return;

const cache = jsonDocuments.get(textDocument);
if (cache) {
const [cacheVersion, cacheDoc, cacheJsonDoc] = cache;
const [cacheVersion, cacheDoc] = cache;
if (cacheVersion === textDocument.version) {
return [cacheDoc, cacheJsonDoc] as const;
return cacheDoc;
}
}

const insertIndex = textDocument.getText().lastIndexOf('}');
const needComma = textDocument.getText().indexOf('"') >= 0;
const modifyDoc = insertIndex >= 0 ? TextDocument.create(
textDocument.uri,
textDocument.languageId,
textDocument.version,
textDocument.getText().substring(0, insertIndex) + (needComma ? ',' : '') + `"$schema":"${schemaUrl}"` + textDocument.getText().substring(insertIndex),
) : textDocument;
const jsonDoc = jsonLs.parseJSONDocument(modifyDoc);

jsonDocuments.set(textDocument, [textDocument.version, modifyDoc, jsonDoc]);
const doc = jsonLs.parseJSONDocument(textDocument);
jsonDocuments.set(textDocument, [textDocument.version, doc]);

return [modifyDoc, jsonDoc] as const;
return doc;
}
}