From c1dd3e107f9ae6371c15ddd7b262a2848023b487 Mon Sep 17 00:00:00 2001 From: DavidMulder0 Date: Thu, 11 May 2023 08:37:40 +0000 Subject: [PATCH] Expose JSON worker Fix #727 --- src/language/json/jsonMode.ts | 16 +++++++++++++--- src/language/json/jsonWorker.ts | 16 ++++++++++++++++ src/language/json/monaco.contribution.ts | 9 +++++++-- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/language/json/jsonMode.ts b/src/language/json/jsonMode.ts index 2133e84803..d1345b2dd1 100644 --- a/src/language/json/jsonMode.ts +++ b/src/language/json/jsonMode.ts @@ -10,6 +10,18 @@ import * as languageFeatures from '../common/lspLanguageFeatures'; import { createTokenizationSupport } from './tokenization'; import { Uri, IDisposable, languages, editor } from '../../fillers/monaco-editor-core'; +let worker: languageFeatures.WorkerAccessor; + +export function getWorker(): Promise<(...uris: Uri[]) => Promise> { + return new Promise((resolve, reject) => { + if (!worker) { + return reject('JSON not registered!'); + } + + resolve(worker); + }); +} + class JSONDiagnosticsAdapter extends languageFeatures.DiagnosticsAdapter { constructor( languageId: string, @@ -44,9 +56,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable { const client = new WorkerManager(defaults); disposables.push(client); - const worker: languageFeatures.WorkerAccessor = ( - ...uris: Uri[] - ): Promise => { + worker = (...uris: Uri[]): Promise => { return client.getLanguageServiceWorker(...uris); }; diff --git a/src/language/json/jsonWorker.ts b/src/language/json/jsonWorker.ts index d18a83de10..bdd34825f1 100644 --- a/src/language/json/jsonWorker.ts +++ b/src/language/json/jsonWorker.ts @@ -141,6 +141,22 @@ export class JSONWorker { let ranges = this._languageService.getSelectionRanges(document, positions, jsonDocument); return Promise.resolve(ranges); } + async parseJSONDocument(uri: string): Promise { + let document = this._getTextDocument(uri); + if (!document) { + return null; + } + let jsonDocument = this._languageService.parseJSONDocument(document); + return Promise.resolve(jsonDocument); + } + async getMatchingSchemas(uri: string): Promise { + let document = this._getTextDocument(uri); + if (!document) { + return []; + } + let jsonDocument = this._languageService.parseJSONDocument(document); + return Promise.resolve(this._languageService.getMatchingSchemas(document, jsonDocument)); + } private _getTextDocument(uri: string): jsonService.TextDocument | null { let models = this._ctx.getMirrorModels(); for (let model of models) { diff --git a/src/language/json/monaco.contribution.ts b/src/language/json/monaco.contribution.ts index 3f9544c614..663aa1d1a1 100644 --- a/src/language/json/monaco.contribution.ts +++ b/src/language/json/monaco.contribution.ts @@ -4,7 +4,8 @@ *--------------------------------------------------------------------------------------------*/ import * as mode from './jsonMode'; -import { Emitter, IEvent, languages } from '../../fillers/monaco-editor-core'; +import { Emitter, IEvent, languages, Uri } from '../../fillers/monaco-editor-core'; +import type { JSONWorker } from './jsonWorker'; // --- JSON configuration and defaults --------- @@ -197,8 +198,12 @@ export const jsonDefaults: LanguageServiceDefaults = new LanguageServiceDefaults modeConfigurationDefault ); +export const getWorker = (): Promise<(...uris: Uri[]) => Promise> => { + return getMode().then((mode) => mode.getWorker()); +}; + // export to the global based API -(languages).json = { jsonDefaults }; +(languages).json = { jsonDefaults, getWorker }; // --- Registration to monaco editor ---