volar-service-json
Advanced tools
Comparing version 0.0.30 to 0.0.31
@@ -1,2 +0,2 @@ | ||
import type { ServicePlugin } from '@volar/language-service'; | ||
import type { ServicePlugin, DocumentSelector, ServiceContext, Disposable, Result } from '@volar/language-service'; | ||
import * as json from 'vscode-json-languageservice'; | ||
@@ -8,3 +8,17 @@ import type { TextDocument } from 'vscode-languageserver-textdocument'; | ||
} | ||
export declare function create(settings?: json.LanguageSettings): ServicePlugin; | ||
export interface JSONSchemaSettings { | ||
fileMatch?: string[]; | ||
url?: string; | ||
schema?: json.JSONSchema; | ||
folderUri?: string; | ||
} | ||
export declare function create({ documentSelector, getWorkspaceContextService, isFormattingEnabled, getFormattingOptions, getLanguageSettings, getDocumentLanguageSettings, onDidChangeLanguageSettings, }?: { | ||
documentSelector?: DocumentSelector; | ||
getWorkspaceContextService?(context: ServiceContext): json.WorkspaceContextService; | ||
isFormattingEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>; | ||
getFormattingOptions?(document: TextDocument, context: ServiceContext): Result<json.FormattingOptions | undefined>; | ||
getLanguageSettings?(context: ServiceContext): Result<json.LanguageSettings>; | ||
getDocumentLanguageSettings?(document: TextDocument, context: ServiceContext): Result<json.DocumentLanguageSettings | undefined>; | ||
onDidChangeLanguageSettings?(listener: () => void, context: ServiceContext): Disposable; | ||
}): ServicePlugin; | ||
//# sourceMappingURL=index.d.ts.map |
96
index.js
@@ -6,4 +6,42 @@ "use strict"; | ||
const vscode_uri_1 = require("vscode-uri"); | ||
function create(settings) { | ||
function create({ documentSelector = ['json', 'jsonc'], getWorkspaceContextService = () => { | ||
return { | ||
resolveRelativePath(relativePath, resource) { | ||
const base = resource.substring(0, resource.lastIndexOf('/') + 1); | ||
return vscode_uri_1.Utils.resolvePath(vscode_uri_1.URI.parse(base), relativePath).toString(); | ||
}, | ||
}; | ||
}, isFormattingEnabled = async (_document, context) => { | ||
return await context.env.getConfiguration?.('json.format.enable') ?? true; | ||
}, getFormattingOptions = async (_document, context) => { | ||
return await context.env.getConfiguration?.('json.format'); | ||
}, getLanguageSettings = async (context) => { | ||
const languageSettings = {}; | ||
languageSettings.validate = await context.env.getConfiguration?.('json.validate') ?? true; | ||
languageSettings.schemas ??= []; | ||
const schemas = await context.env.getConfiguration?.('json.schemas') ?? []; | ||
for (let i = 0; i < schemas.length; i++) { | ||
const schema = schemas[i]; | ||
let uri = schema.url; | ||
if (!uri && schema.schema) { | ||
uri = schema.schema.id || `vscode://schemas/custom/${i}`; | ||
} | ||
if (uri) { | ||
languageSettings.schemas.push({ uri, fileMatch: schema.fileMatch, schema: schema.schema, folderUri: schema.folderUri }); | ||
} | ||
} | ||
return languageSettings; | ||
}, getDocumentLanguageSettings = document => { | ||
return document.languageId === 'jsonc' | ||
? { comments: 'ignore', trailingCommas: 'warning' } | ||
: { comments: 'error', trailingCommas: 'error' }; | ||
}, onDidChangeLanguageSettings = (listener, context) => { | ||
const disposable = context.env.onDidChangeConfiguration?.(listener); | ||
return { | ||
dispose() { | ||
disposable?.dispose(); | ||
}, | ||
}; | ||
}, } = {}) { | ||
return { | ||
name: 'json', | ||
@@ -14,25 +52,13 @@ // https://github.com/microsoft/vscode/blob/09850876e652688fb142e2e19fd00fd38c0bc4ba/extensions/json-language-features/server/src/jsonServer.ts#L150 | ||
const jsonDocuments = new WeakMap(); | ||
const workspaceContext = { | ||
resolveRelativePath: (ref, base) => { | ||
if (ref.match(/^\w[\w\d+.-]*:/)) { | ||
// starts with a schema | ||
return ref; | ||
} | ||
if (ref[0] === '/') { // resolve absolute path against the current workspace folder | ||
return base + ref; | ||
} | ||
const baseUri = vscode_uri_1.URI.parse(base); | ||
const baseUriDir = baseUri.path.endsWith('/') ? baseUri : vscode_uri_1.Utils.dirname(baseUri); | ||
return vscode_uri_1.Utils.resolvePath(baseUriDir, ref).toString(true); | ||
}, | ||
}; | ||
const jsonLs = json.getLanguageService({ | ||
schemaRequestService: async (uri) => await context.env.fs?.readFile(uri) ?? '', | ||
workspaceContext, | ||
workspaceContext: getWorkspaceContextService(context), | ||
clientCapabilities: context.env.clientCapabilities, | ||
}); | ||
if (settings) { | ||
jsonLs.configure(settings); | ||
} | ||
const disposable = onDidChangeLanguageSettings(() => initializing = undefined, context); | ||
let initializing; | ||
return { | ||
dispose() { | ||
disposable.dispose(); | ||
}, | ||
provide: { | ||
@@ -57,4 +83,4 @@ 'json/jsonDocument': getJsonDocument, | ||
return worker(document, async (jsonDocument) => { | ||
const documentLanguageSettings = undefined; // await getSettings(); // TODO | ||
return await jsonLs.doValidation(document, jsonDocument, documentLanguageSettings, undefined); | ||
const settings = await getDocumentLanguageSettings(document, context); | ||
return await jsonLs.doValidation(document, jsonDocument, settings); | ||
}); | ||
@@ -89,3 +115,3 @@ }, | ||
return worker(document, async () => { | ||
return await jsonLs.getFoldingRanges(document); | ||
return await jsonLs.getFoldingRanges(document, context.env.clientCapabilities?.textDocument?.foldingRange); | ||
}); | ||
@@ -100,9 +126,9 @@ }, | ||
return worker(document, async () => { | ||
const options_2 = await context.env.getConfiguration?.('json.format'); | ||
if (!(options_2?.enable ?? true)) { | ||
if (!await isFormattingEnabled(document, context)) { | ||
return; | ||
} | ||
const formatOptions = await getFormattingOptions(document, context); | ||
return jsonLs.format(document, range, { | ||
...options_2, | ||
...options, | ||
...formatOptions, | ||
}); | ||
@@ -112,11 +138,17 @@ }); | ||
}; | ||
function worker(document, callback) { | ||
async function worker(document, callback) { | ||
const jsonDocument = getJsonDocument(document); | ||
if (!jsonDocument) | ||
return; | ||
return callback(jsonDocument); | ||
await (initializing ??= initialize()); | ||
return await callback(jsonDocument); | ||
} | ||
async function initialize() { | ||
const settings = await getLanguageSettings(context); | ||
jsonLs.configure(settings); | ||
} | ||
function getJsonDocument(textDocument) { | ||
if (textDocument.languageId !== 'json' && textDocument.languageId !== 'jsonc') | ||
if (!matchDocument(documentSelector, textDocument)) { | ||
return; | ||
} | ||
const cache = jsonDocuments.get(textDocument); | ||
@@ -137,2 +169,10 @@ if (cache) { | ||
exports.create = create; | ||
function matchDocument(selector, document) { | ||
for (const sel of selector) { | ||
if (sel === document.languageId || (typeof sel === 'object' && sel.language === document.languageId)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "volar-service-json", | ||
"version": "0.0.30", | ||
"version": "0.0.31", | ||
"description": "Integrate vscode-json-languageservice into Volar", | ||
@@ -34,3 +34,3 @@ "homepage": "https://github.com/volarjs/services/tree/master/packages/json", | ||
"peerDependencies": { | ||
"@volar/language-service": "~2.0.1" | ||
"@volar/language-service": "~2.1.0" | ||
}, | ||
@@ -42,3 +42,3 @@ "peerDependenciesMeta": { | ||
}, | ||
"gitHead": "30c3cc3c76e90f75f14fe0c2fa4fd33b7ff06507" | ||
"gitHead": "f7005aef724767786ee9fe943fa976231cc79bf1" | ||
} |
11799
192