Run prettier
parent
06e3393f83
commit
0b344d31d0
@ -1,21 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Microsoft Corporation
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Microsoft Corporation
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
@ -1,37 +1,39 @@
|
||||
# Monaco JSON
|
||||
|
||||
JSON language plugin for the Monaco Editor. It provides the following features when editing JSON files:
|
||||
* Code completion, based on JSON schemas or by looking at similar objects in the same file
|
||||
* Hovers, based on JSON schemas
|
||||
* Validation: Syntax errors and schema validation
|
||||
* Formatting
|
||||
* Document Symbols
|
||||
* Syntax highlighting
|
||||
* Color decorators for all properties matching a schema containing `format: "color-hex"'` (non-standard schema extension)
|
||||
|
||||
Schemas can be provided by configuration. See [here](https://github.com/Microsoft/monaco-json/blob/master/monaco.d.ts)
|
||||
for the API that the JSON plugin offers to configure the JSON language support.
|
||||
|
||||
Internally the JSON plugin uses the [vscode-json-languageservice](https://github.com/Microsoft/vscode-json-languageservice)
|
||||
node module, providing the implementation of the features listed above. The same module is also used
|
||||
in [Visual Studio Code](https://github.com/Microsoft/vscode) to power the JSON editing experience.
|
||||
|
||||
## Issues
|
||||
|
||||
Please file issues concerning `monaco-json` in the [`monaco-editor` repository](https://github.com/Microsoft/monaco-editor/issues).
|
||||
|
||||
## Installing
|
||||
|
||||
This npm module is bundled and distributed in the [monaco-editor](https://www.npmjs.com/package/monaco-editor) npm module.
|
||||
|
||||
## Development
|
||||
|
||||
* `git clone https://github.com/Microsoft/monaco-json`
|
||||
* `npm install .`
|
||||
* compile with `npm run compile`
|
||||
* watch with `npm run watch`
|
||||
* `npm run prepublishOnly`
|
||||
* open `$/monaco-json/test/index.html` in your favorite browser.
|
||||
|
||||
## License
|
||||
[MIT](https://github.com/Microsoft/monaco-json/blob/master/LICENSE.md)
|
||||
# Monaco JSON
|
||||
|
||||
JSON language plugin for the Monaco Editor. It provides the following features when editing JSON files:
|
||||
|
||||
- Code completion, based on JSON schemas or by looking at similar objects in the same file
|
||||
- Hovers, based on JSON schemas
|
||||
- Validation: Syntax errors and schema validation
|
||||
- Formatting
|
||||
- Document Symbols
|
||||
- Syntax highlighting
|
||||
- Color decorators for all properties matching a schema containing `format: "color-hex"'` (non-standard schema extension)
|
||||
|
||||
Schemas can be provided by configuration. See [here](https://github.com/Microsoft/monaco-json/blob/master/monaco.d.ts)
|
||||
for the API that the JSON plugin offers to configure the JSON language support.
|
||||
|
||||
Internally the JSON plugin uses the [vscode-json-languageservice](https://github.com/Microsoft/vscode-json-languageservice)
|
||||
node module, providing the implementation of the features listed above. The same module is also used
|
||||
in [Visual Studio Code](https://github.com/Microsoft/vscode) to power the JSON editing experience.
|
||||
|
||||
## Issues
|
||||
|
||||
Please file issues concerning `monaco-json` in the [`monaco-editor` repository](https://github.com/Microsoft/monaco-editor/issues).
|
||||
|
||||
## Installing
|
||||
|
||||
This npm module is bundled and distributed in the [monaco-editor](https://www.npmjs.com/package/monaco-editor) npm module.
|
||||
|
||||
## Development
|
||||
|
||||
- `git clone https://github.com/Microsoft/monaco-json`
|
||||
- `npm install .`
|
||||
- compile with `npm run compile`
|
||||
- watch with `npm run watch`
|
||||
- `npm run prepublishOnly`
|
||||
- open `$/monaco-json/test/index.html` in your favorite browser.
|
||||
|
||||
## License
|
||||
|
||||
[MIT](https://github.com/Microsoft/monaco-json/blob/master/LICENSE.md)
|
||||
|
@ -1,109 +1,157 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { WorkerManager } from './workerManager';
|
||||
import type { JSONWorker } from './jsonWorker';
|
||||
import { LanguageServiceDefaults } from './monaco.contribution';
|
||||
import * as languageFeatures from './languageFeatures';
|
||||
import { createTokenizationSupport } from './tokenization';
|
||||
import { Uri, IDisposable, languages } from './fillers/monaco-editor-core'
|
||||
|
||||
export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
|
||||
|
||||
const disposables: IDisposable[] = [];
|
||||
const providers: IDisposable[] = [];
|
||||
|
||||
const client = new WorkerManager(defaults);
|
||||
disposables.push(client);
|
||||
|
||||
const worker: languageFeatures.WorkerAccessor = (...uris: Uri[]): Promise<JSONWorker> => {
|
||||
return client.getLanguageServiceWorker(...uris);
|
||||
};
|
||||
|
||||
|
||||
function registerProviders(): void {
|
||||
const { languageId, modeConfiguration } = defaults;
|
||||
|
||||
disposeAll(providers);
|
||||
|
||||
if (modeConfiguration.documentFormattingEdits) {
|
||||
providers.push(languages.registerDocumentFormattingEditProvider(languageId, new languageFeatures.DocumentFormattingEditProvider(worker)));
|
||||
}
|
||||
if (modeConfiguration.documentRangeFormattingEdits) {
|
||||
providers.push(languages.registerDocumentRangeFormattingEditProvider(languageId, new languageFeatures.DocumentRangeFormattingEditProvider(worker)));
|
||||
}
|
||||
if (modeConfiguration.completionItems) {
|
||||
providers.push(languages.registerCompletionItemProvider(languageId, new languageFeatures.CompletionAdapter(worker)));
|
||||
}
|
||||
if (modeConfiguration.hovers) {
|
||||
providers.push(languages.registerHoverProvider(languageId, new languageFeatures.HoverAdapter(worker)));
|
||||
}
|
||||
if (modeConfiguration.documentSymbols) {
|
||||
providers.push(languages.registerDocumentSymbolProvider(languageId, new languageFeatures.DocumentSymbolAdapter(worker)));
|
||||
}
|
||||
if (modeConfiguration.tokens) {
|
||||
providers.push(languages.setTokensProvider(languageId, createTokenizationSupport(true)));
|
||||
}
|
||||
if (modeConfiguration.colors) {
|
||||
providers.push(languages.registerColorProvider(languageId, new languageFeatures.DocumentColorAdapter(worker)));
|
||||
}
|
||||
if (modeConfiguration.foldingRanges) {
|
||||
providers.push(languages.registerFoldingRangeProvider(languageId, new languageFeatures.FoldingRangeAdapter(worker)));
|
||||
}
|
||||
if (modeConfiguration.diagnostics) {
|
||||
providers.push(new languageFeatures.DiagnosticsAdapter(languageId, worker, defaults));
|
||||
}
|
||||
if (modeConfiguration.selectionRanges) {
|
||||
providers.push(languages.registerSelectionRangeProvider(languageId, new languageFeatures.SelectionRangeAdapter(worker)));
|
||||
}
|
||||
}
|
||||
|
||||
registerProviders();
|
||||
|
||||
disposables.push(languages.setLanguageConfiguration(defaults.languageId, richEditConfiguration));
|
||||
|
||||
let modeConfiguration = defaults.modeConfiguration;
|
||||
defaults.onDidChange((newDefaults) => {
|
||||
if (newDefaults.modeConfiguration !== modeConfiguration) {
|
||||
modeConfiguration = newDefaults.modeConfiguration;
|
||||
registerProviders();
|
||||
}
|
||||
});
|
||||
|
||||
disposables.push(asDisposable(providers));
|
||||
|
||||
return asDisposable(disposables);
|
||||
}
|
||||
|
||||
function asDisposable(disposables: IDisposable[]): IDisposable {
|
||||
return { dispose: () => disposeAll(disposables) };
|
||||
}
|
||||
|
||||
function disposeAll(disposables: IDisposable[]) {
|
||||
while (disposables.length) {
|
||||
disposables.pop().dispose();
|
||||
}
|
||||
}
|
||||
|
||||
const richEditConfiguration: languages.LanguageConfiguration = {
|
||||
wordPattern: /(-?\d*\.\d\w*)|([^\[\{\]\}\:\"\,\s]+)/g,
|
||||
|
||||
comments: {
|
||||
lineComment: '//',
|
||||
blockComment: ['/*', '*/']
|
||||
},
|
||||
|
||||
brackets: [
|
||||
['{', '}'],
|
||||
['[', ']']
|
||||
],
|
||||
|
||||
autoClosingPairs: [
|
||||
{ open: '{', close: '}', notIn: ['string'] },
|
||||
{ open: '[', close: ']', notIn: ['string'] },
|
||||
{ open: '"', close: '"', notIn: ['string'] }
|
||||
]
|
||||
};
|
||||
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { WorkerManager } from './workerManager';
|
||||
import type { JSONWorker } from './jsonWorker';
|
||||
import { LanguageServiceDefaults } from './monaco.contribution';
|
||||
import * as languageFeatures from './languageFeatures';
|
||||
import { createTokenizationSupport } from './tokenization';
|
||||
import { Uri, IDisposable, languages } from './fillers/monaco-editor-core';
|
||||
|
||||
export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
|
||||
const disposables: IDisposable[] = [];
|
||||
const providers: IDisposable[] = [];
|
||||
|
||||
const client = new WorkerManager(defaults);
|
||||
disposables.push(client);
|
||||
|
||||
const worker: languageFeatures.WorkerAccessor = (
|
||||
...uris: Uri[]
|
||||
): Promise<JSONWorker> => {
|
||||
return client.getLanguageServiceWorker(...uris);
|
||||
};
|
||||
|
||||
function registerProviders(): void {
|
||||
const { languageId, modeConfiguration } = defaults;
|
||||
|
||||
disposeAll(providers);
|
||||
|
||||
if (modeConfiguration.documentFormattingEdits) {
|
||||
providers.push(
|
||||
languages.registerDocumentFormattingEditProvider(
|
||||
languageId,
|
||||
new languageFeatures.DocumentFormattingEditProvider(worker)
|
||||
)
|
||||
);
|
||||
}
|
||||
if (modeConfiguration.documentRangeFormattingEdits) {
|
||||
providers.push(
|
||||
languages.registerDocumentRangeFormattingEditProvider(
|
||||
languageId,
|
||||
new languageFeatures.DocumentRangeFormattingEditProvider(worker)
|
||||
)
|
||||
);
|
||||
}
|
||||
if (modeConfiguration.completionItems) {
|
||||
providers.push(
|
||||
languages.registerCompletionItemProvider(
|
||||
languageId,
|
||||
new languageFeatures.CompletionAdapter(worker)
|
||||
)
|
||||
);
|
||||
}
|
||||
if (modeConfiguration.hovers) {
|
||||
providers.push(
|
||||
languages.registerHoverProvider(
|
||||
languageId,
|
||||
new languageFeatures.HoverAdapter(worker)
|
||||
)
|
||||
);
|
||||
}
|
||||
if (modeConfiguration.documentSymbols) {
|
||||
providers.push(
|
||||
languages.registerDocumentSymbolProvider(
|
||||
languageId,
|
||||
new languageFeatures.DocumentSymbolAdapter(worker)
|
||||
)
|
||||
);
|
||||
}
|
||||
if (modeConfiguration.tokens) {
|
||||
providers.push(
|
||||
languages.setTokensProvider(languageId, createTokenizationSupport(true))
|
||||
);
|
||||
}
|
||||
if (modeConfiguration.colors) {
|
||||
providers.push(
|
||||
languages.registerColorProvider(
|
||||
languageId,
|
||||
new languageFeatures.DocumentColorAdapter(worker)
|
||||
)
|
||||
);
|
||||
}
|
||||
if (modeConfiguration.foldingRanges) {
|
||||
providers.push(
|
||||
languages.registerFoldingRangeProvider(
|
||||
languageId,
|
||||
new languageFeatures.FoldingRangeAdapter(worker)
|
||||
)
|
||||
);
|
||||
}
|
||||
if (modeConfiguration.diagnostics) {
|
||||
providers.push(
|
||||
new languageFeatures.DiagnosticsAdapter(languageId, worker, defaults)
|
||||
);
|
||||
}
|
||||
if (modeConfiguration.selectionRanges) {
|
||||
providers.push(
|
||||
languages.registerSelectionRangeProvider(
|
||||
languageId,
|
||||
new languageFeatures.SelectionRangeAdapter(worker)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
registerProviders();
|
||||
|
||||
disposables.push(
|
||||
languages.setLanguageConfiguration(
|
||||
defaults.languageId,
|
||||
richEditConfiguration
|
||||
)
|
||||
);
|
||||
|
||||
let modeConfiguration = defaults.modeConfiguration;
|
||||
defaults.onDidChange((newDefaults) => {
|
||||
if (newDefaults.modeConfiguration !== modeConfiguration) {
|
||||
modeConfiguration = newDefaults.modeConfiguration;
|
||||
registerProviders();
|
||||
}
|
||||
});
|
||||
|
||||
disposables.push(asDisposable(providers));
|
||||
|
||||
return asDisposable(disposables);
|
||||
}
|
||||
|
||||
function asDisposable(disposables: IDisposable[]): IDisposable {
|
||||
return { dispose: () => disposeAll(disposables) };
|
||||
}
|
||||
|
||||
function disposeAll(disposables: IDisposable[]) {
|
||||
while (disposables.length) {
|
||||
disposables.pop().dispose();
|
||||
}
|
||||
}
|
||||
|
||||
const richEditConfiguration: languages.LanguageConfiguration = {
|
||||
wordPattern: /(-?\d*\.\d\w*)|([^\[\{\]\}\:\"\,\s]+)/g,
|
||||
|
||||
comments: {
|
||||
lineComment: '//',
|
||||
blockComment: ['/*', '*/']
|
||||
},
|
||||
|
||||
brackets: [
|
||||
['{', '}'],
|
||||
['[', ']']
|
||||
],
|
||||
|
||||
autoClosingPairs: [
|
||||
{ open: '{', close: '}', notIn: ['string'] },
|
||||
{ open: '[', close: ']', notIn: ['string'] },
|
||||
{ open: '"', close: '"', notIn: ['string'] }
|
||||
]
|
||||
};
|
||||
|
@ -1,108 +1,159 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as jsonService from 'vscode-json-languageservice';
|
||||
import type { worker } from './fillers/monaco-editor-core'
|
||||
|
||||
let defaultSchemaRequestService;
|
||||
if (typeof fetch !== 'undefined') {
|
||||
defaultSchemaRequestService = function (url) { return fetch(url).then(response => response.text()) };
|
||||
}
|
||||
|
||||
export class JSONWorker {
|
||||
|
||||
private _ctx: worker.IWorkerContext;
|
||||
private _languageService: jsonService.LanguageService;
|
||||
private _languageSettings: jsonService.LanguageSettings;
|
||||
private _languageId: string;
|
||||
|
||||
constructor(ctx: worker.IWorkerContext, createData: ICreateData) {
|
||||
this._ctx = ctx;
|
||||
this._languageSettings = createData.languageSettings;
|
||||
this._languageId = createData.languageId;
|
||||
this._languageService = jsonService.getLanguageService({
|
||||
schemaRequestService: createData.enableSchemaRequest && defaultSchemaRequestService
|
||||
});
|
||||
this._languageService.configure(this._languageSettings);
|
||||
}
|
||||
|
||||
async doValidation(uri: string): Promise<jsonService.Diagnostic[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
if (document) {
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
return this._languageService.doValidation(document, jsonDocument);
|
||||
}
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
async doComplete(uri: string, position: jsonService.Position): Promise<jsonService.CompletionList> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
return this._languageService.doComplete(document, position, jsonDocument);
|
||||
}
|
||||
async doResolve(item: jsonService.CompletionItem): Promise<jsonService.CompletionItem> {
|
||||
return this._languageService.doResolve(item);
|
||||
}
|
||||
async doHover(uri: string, position: jsonService.Position): Promise<jsonService.Hover> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
return this._languageService.doHover(document, position, jsonDocument);
|
||||
}
|
||||
async format(uri: string, range: jsonService.Range, options: jsonService.FormattingOptions): Promise<jsonService.TextEdit[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let textEdits = this._languageService.format(document, range, options);
|
||||
return Promise.resolve(textEdits);
|
||||
}
|
||||
async resetSchema(uri: string): Promise<boolean> {
|
||||
return Promise.resolve(this._languageService.resetSchema(uri));
|
||||
}
|
||||
async findDocumentSymbols(uri: string): Promise<jsonService.SymbolInformation[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
let symbols = this._languageService.findDocumentSymbols(document, jsonDocument);
|
||||
return Promise.resolve(symbols);
|
||||
}
|
||||
async findDocumentColors(uri: string): Promise<jsonService.ColorInformation[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
let colorSymbols = this._languageService.findDocumentColors(document, jsonDocument);
|
||||
return Promise.resolve(colorSymbols);
|
||||
}
|
||||
async getColorPresentations(uri: string, color: jsonService.Color, range: jsonService.Range): Promise<jsonService.ColorPresentation[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
let colorPresentations = this._languageService.getColorPresentations(document, jsonDocument, color, range);
|
||||
return Promise.resolve(colorPresentations);
|
||||
}
|
||||
async getFoldingRanges(uri: string, context?: { rangeLimit?: number; }): Promise<jsonService.FoldingRange[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let ranges = this._languageService.getFoldingRanges(document, context);
|
||||
return Promise.resolve(ranges);
|
||||
}
|
||||
async getSelectionRanges(uri: string, positions: jsonService.Position[]): Promise<jsonService.SelectionRange[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
let ranges = this._languageService.getSelectionRanges(document, positions, jsonDocument);
|
||||
return Promise.resolve(ranges);
|
||||
}
|
||||
private _getTextDocument(uri: string): jsonService.TextDocument {
|
||||
let models = this._ctx.getMirrorModels();
|
||||
for (let model of models) {
|
||||
if (model.uri.toString() === uri) {
|
||||
return jsonService.TextDocument.create(uri, this._languageId, model.version, model.getValue());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export interface ICreateData {
|
||||
languageId: string;
|
||||
languageSettings: jsonService.LanguageSettings;
|
||||
enableSchemaRequest: boolean;
|
||||
}
|
||||
|
||||
export function create(ctx: worker.IWorkerContext, createData: ICreateData): JSONWorker {
|
||||
return new JSONWorker(ctx, createData);
|
||||
}
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as jsonService from 'vscode-json-languageservice';
|
||||
import type { worker } from './fillers/monaco-editor-core';
|
||||
|
||||
let defaultSchemaRequestService;
|
||||
if (typeof fetch !== 'undefined') {
|
||||
defaultSchemaRequestService = function (url) {
|
||||
return fetch(url).then((response) => response.text());
|
||||
};
|
||||
}
|
||||
|
||||
export class JSONWorker {
|
||||
private _ctx: worker.IWorkerContext;
|
||||
private _languageService: jsonService.LanguageService;
|
||||
private _languageSettings: jsonService.LanguageSettings;
|
||||
private _languageId: string;
|
||||
|
||||
constructor(ctx: worker.IWorkerContext, createData: ICreateData) {
|
||||
this._ctx = ctx;
|
||||
this._languageSettings = createData.languageSettings;
|
||||
this._languageId = createData.languageId;
|
||||
this._languageService = jsonService.getLanguageService({
|
||||
schemaRequestService:
|
||||
createData.enableSchemaRequest && defaultSchemaRequestService
|
||||
});
|
||||
this._languageService.configure(this._languageSettings);
|
||||
}
|
||||
|
||||
async doValidation(uri: string): Promise<jsonService.Diagnostic[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
if (document) {
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
return this._languageService.doValidation(document, jsonDocument);
|
||||
}
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
async doComplete(
|
||||
uri: string,
|
||||
position: jsonService.Position
|
||||
): Promise<jsonService.CompletionList> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
return this._languageService.doComplete(document, position, jsonDocument);
|
||||
}
|
||||
async doResolve(
|
||||
item: jsonService.CompletionItem
|
||||
): Promise<jsonService.CompletionItem> {
|
||||
return this._languageService.doResolve(item);
|
||||
}
|
||||
async doHover(
|
||||
uri: string,
|
||||
position: jsonService.Position
|
||||
): Promise<jsonService.Hover> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
return this._languageService.doHover(document, position, jsonDocument);
|
||||
}
|
||||
async format(
|
||||
uri: string,
|
||||
range: jsonService.Range,
|
||||
options: jsonService.FormattingOptions
|
||||
): Promise<jsonService.TextEdit[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let textEdits = this._languageService.format(document, range, options);
|
||||
return Promise.resolve(textEdits);
|
||||
}
|
||||
async resetSchema(uri: string): Promise<boolean> {
|
||||
return Promise.resolve(this._languageService.resetSchema(uri));
|
||||
}
|
||||
async findDocumentSymbols(
|
||||
uri: string
|
||||
): Promise<jsonService.SymbolInformation[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
let symbols = this._languageService.findDocumentSymbols(
|
||||
document,
|
||||
jsonDocument
|
||||
);
|
||||
return Promise.resolve(symbols);
|
||||
}
|
||||
async findDocumentColors(
|
||||
uri: string
|
||||
): Promise<jsonService.ColorInformation[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
let colorSymbols = this._languageService.findDocumentColors(
|
||||
document,
|
||||
jsonDocument
|
||||
);
|
||||
return Promise.resolve(colorSymbols);
|
||||
}
|
||||
async getColorPresentations(
|
||||
uri: string,
|
||||
color: jsonService.Color,
|
||||
range: jsonService.Range
|
||||
): Promise<jsonService.ColorPresentation[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
let colorPresentations = this._languageService.getColorPresentations(
|
||||
document,
|
||||
jsonDocument,
|
||||
color,
|
||||
range
|
||||
);
|
||||
return Promise.resolve(colorPresentations);
|
||||
}
|
||||
async getFoldingRanges(
|
||||
uri: string,
|
||||
context?: { rangeLimit?: number }
|
||||
): Promise<jsonService.FoldingRange[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let ranges = this._languageService.getFoldingRanges(document, context);
|
||||
return Promise.resolve(ranges);
|
||||
}
|
||||
async getSelectionRanges(
|
||||
uri: string,
|
||||
positions: jsonService.Position[]
|
||||
): Promise<jsonService.SelectionRange[]> {
|
||||
let document = this._getTextDocument(uri);
|
||||
let jsonDocument = this._languageService.parseJSONDocument(document);
|
||||
let ranges = this._languageService.getSelectionRanges(
|
||||
document,
|
||||
positions,
|
||||
jsonDocument
|
||||
);
|
||||
return Promise.resolve(ranges);
|
||||
}
|
||||
private _getTextDocument(uri: string): jsonService.TextDocument {
|
||||
let models = this._ctx.getMirrorModels();
|
||||
for (let model of models) {
|
||||
if (model.uri.toString() === uri) {
|
||||
return jsonService.TextDocument.create(
|
||||
uri,
|
||||
this._languageId,
|
||||
model.version,
|
||||
model.getValue()
|
||||
);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export interface ICreateData {
|
||||
languageId: string;
|
||||
languageSettings: jsonService.LanguageSettings;
|
||||
enableSchemaRequest: boolean;
|
||||
}
|
||||
|
||||
export function create(
|
||||
ctx: worker.IWorkerContext,
|
||||
createData: ICreateData
|
||||
): JSONWorker {
|
||||
return new JSONWorker(ctx, createData);
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,185 +1,199 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as mode from './jsonMode';
|
||||
import { Emitter, IEvent, languages } from './fillers/monaco-editor-core'
|
||||
|
||||
// --- JSON configuration and defaults ---------
|
||||
|
||||
export interface DiagnosticsOptions {
|
||||
/**
|
||||
* If set, the validator will be enabled and perform syntax validation as well as schema based validation.
|
||||
*/
|
||||
readonly validate?: boolean;
|
||||
/**
|
||||
* If set, comments are tolerated. If set to false, syntax errors will be emitted for comments.
|
||||
*/
|
||||
readonly allowComments?: boolean;
|
||||
/**
|
||||
* A list of known schemas and/or associations of schemas to file names.
|
||||
*/
|
||||
readonly schemas?: {
|
||||
/**
|
||||
* The URI of the schema, which is also the identifier of the schema.
|
||||
*/
|
||||
readonly uri: string;
|
||||
/**
|
||||
* A list of file names that are associated to the schema. The '*' wildcard can be used. For example '*.schema.json', 'package.json'
|
||||
*/
|
||||
readonly fileMatch?: string[];
|
||||
/**
|
||||
* The schema for the given URI.
|
||||
*/
|
||||
readonly schema?: any;
|
||||
}[];
|
||||
/**
|
||||
* If set, the schema service would load schema content on-demand with 'fetch' if available
|
||||
*/
|
||||
readonly enableSchemaRequest?: boolean;
|
||||
}
|
||||
|
||||
export interface ModeConfiguration {
|
||||
/**
|
||||
* Defines whether the built-in documentFormattingEdit provider is enabled.
|
||||
*/
|
||||
readonly documentFormattingEdits?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in documentRangeFormattingEdit provider is enabled.
|
||||
*/
|
||||
readonly documentRangeFormattingEdits?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in completionItemProvider is enabled.
|
||||
*/
|
||||
readonly completionItems?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in hoverProvider is enabled.
|
||||
*/
|
||||
readonly hovers?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in documentSymbolProvider is enabled.
|
||||
*/
|
||||
readonly documentSymbols?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in tokens provider is enabled.
|
||||
*/
|
||||
readonly tokens?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in color provider is enabled.
|
||||
*/
|
||||
readonly colors?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in foldingRange provider is enabled.
|
||||
*/
|
||||
readonly foldingRanges?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in diagnostic provider is enabled.
|
||||
*/
|
||||
readonly diagnostics?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in selection range provider is enabled.
|
||||
*/
|
||||
readonly selectionRanges?: boolean;
|
||||
|
||||
}
|
||||
|
||||
export interface LanguageServiceDefaults {
|
||||
readonly languageId: string;
|
||||
readonly onDidChange: IEvent<LanguageServiceDefaults>;
|
||||
readonly diagnosticsOptions: DiagnosticsOptions;
|
||||
readonly modeConfiguration: ModeConfiguration;
|
||||
setDiagnosticsOptions(options: DiagnosticsOptions): void;
|
||||
setModeConfiguration(modeConfiguration: ModeConfiguration): void;
|
||||
}
|
||||
|
||||
class LanguageServiceDefaultsImpl implements LanguageServiceDefaults {
|
||||
|
||||
private _onDidChange = new Emitter<LanguageServiceDefaults>();
|
||||
private _diagnosticsOptions: DiagnosticsOptions;
|
||||
private _modeConfiguration: ModeConfiguration;
|
||||
private _languageId: string;
|
||||
|
||||
constructor(languageId: string, diagnosticsOptions: DiagnosticsOptions, modeConfiguration: ModeConfiguration) {
|
||||
this._languageId = languageId;
|
||||
this.setDiagnosticsOptions(diagnosticsOptions);
|
||||
this.setModeConfiguration(modeConfiguration);
|
||||
}
|
||||
|
||||
get onDidChange(): IEvent<LanguageServiceDefaults> {
|
||||
return this._onDidChange.event;
|
||||
}
|
||||
|
||||
get languageId(): string {
|
||||
return this._languageId;
|
||||
}
|
||||
|
||||
get modeConfiguration(): ModeConfiguration {
|
||||
return this._modeConfiguration;
|
||||
}
|
||||
|
||||
get diagnosticsOptions(): DiagnosticsOptions {
|
||||
return this._diagnosticsOptions;
|
||||
}
|
||||
|
||||
setDiagnosticsOptions(options: DiagnosticsOptions): void {
|
||||
this._diagnosticsOptions = options || Object.create(null);
|
||||
this._onDidChange.fire(this);
|
||||
}
|
||||
|
||||
setModeConfiguration(modeConfiguration: ModeConfiguration): void {
|
||||
this._modeConfiguration = modeConfiguration || Object.create(null);
|
||||
this._onDidChange.fire(this);
|
||||
};
|
||||
}
|
||||
|
||||
const diagnosticDefault: Required<DiagnosticsOptions> = {
|
||||
validate: true,
|
||||
allowComments: true,
|
||||
schemas: [],
|
||||
enableSchemaRequest: false
|
||||
};
|
||||
|
||||
const modeConfigurationDefault: Required<ModeConfiguration> = {
|
||||
documentFormattingEdits: true,
|
||||
documentRangeFormattingEdits: true,
|
||||
completionItems: true,
|
||||
hovers: true,
|
||||
documentSymbols: true,
|
||||
tokens: true,
|
||||
colors: true,
|
||||
foldingRanges: true,
|
||||
diagnostics: true,
|
||||
selectionRanges: true
|
||||
}
|
||||
|
||||
export const jsonDefaults: LanguageServiceDefaults = new LanguageServiceDefaultsImpl('json', diagnosticDefault, modeConfigurationDefault);
|
||||
|
||||
// export to the global based API
|
||||
(<any>languages).json = { jsonDefaults };
|
||||
|
||||
// --- Registration to monaco editor ---
|
||||
|
||||
function getMode(): Promise<typeof mode> {
|
||||
return import('./jsonMode');
|
||||
}
|
||||
|
||||
languages.register({
|
||||
id: 'json',
|
||||
extensions: ['.json', '.bowerrc', '.jshintrc', '.jscsrc', '.eslintrc', '.babelrc', '.har'],
|
||||
aliases: ['JSON', 'json'],
|
||||
mimetypes: ['application/json'],
|
||||
});
|
||||
|
||||
languages.onLanguage('json', () => {
|
||||
getMode().then(mode => mode.setupMode(jsonDefaults));
|
||||
});
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as mode from './jsonMode';
|
||||
import { Emitter, IEvent, languages } from './fillers/monaco-editor-core';
|
||||
|
||||
// --- JSON configuration and defaults ---------
|
||||
|
||||
export interface DiagnosticsOptions {
|
||||
/**
|
||||
* If set, the validator will be enabled and perform syntax validation as well as schema based validation.
|
||||
*/
|
||||
readonly validate?: boolean;
|
||||
/**
|
||||
* If set, comments are tolerated. If set to false, syntax errors will be emitted for comments.
|
||||
*/
|
||||
readonly allowComments?: boolean;
|
||||
/**
|
||||
* A list of known schemas and/or associations of schemas to file names.
|
||||
*/
|
||||
readonly schemas?: {
|
||||
/**
|
||||
* The URI of the schema, which is also the identifier of the schema.
|
||||
*/
|
||||
readonly uri: string;
|
||||
/**
|
||||
* A list of file names that are associated to the schema. The '*' wildcard can be used. For example '*.schema.json', 'package.json'
|
||||
*/
|
||||
readonly fileMatch?: string[];
|
||||
/**
|
||||
* The schema for the given URI.
|
||||
*/
|
||||
readonly schema?: any;
|
||||
}[];
|
||||
/**
|
||||
* If set, the schema service would load schema content on-demand with 'fetch' if available
|
||||
*/
|
||||
readonly enableSchemaRequest?: boolean;
|
||||
}
|
||||
|
||||
export interface ModeConfiguration {
|
||||
/**
|
||||
* Defines whether the built-in documentFormattingEdit provider is enabled.
|
||||
*/
|
||||
readonly documentFormattingEdits?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in documentRangeFormattingEdit provider is enabled.
|
||||
*/
|
||||
readonly documentRangeFormattingEdits?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in completionItemProvider is enabled.
|
||||
*/
|
||||
readonly completionItems?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in hoverProvider is enabled.
|
||||
*/
|
||||
readonly hovers?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in documentSymbolProvider is enabled.
|
||||
*/
|
||||
readonly documentSymbols?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in tokens provider is enabled.
|
||||
*/
|
||||
readonly tokens?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in color provider is enabled.
|
||||
*/
|
||||
readonly colors?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in foldingRange provider is enabled.
|
||||
*/
|
||||
readonly foldingRanges?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in diagnostic provider is enabled.
|
||||
*/
|
||||
readonly diagnostics?: boolean;
|
||||
|
||||
/**
|
||||
* Defines whether the built-in selection range provider is enabled.
|
||||
*/
|
||||
readonly selectionRanges?: boolean;
|
||||
}
|
||||
|
||||
export interface LanguageServiceDefaults {
|
||||
readonly languageId: string;
|
||||
readonly onDidChange: IEvent<LanguageServiceDefaults>;
|
||||
readonly diagnosticsOptions: DiagnosticsOptions;
|
||||
readonly modeConfiguration: ModeConfiguration;
|
||||
setDiagnosticsOptions(options: DiagnosticsOptions): void;
|
||||
setModeConfiguration(modeConfiguration: ModeConfiguration): void;
|
||||
}
|
||||
|
||||
class LanguageServiceDefaultsImpl implements LanguageServiceDefaults {
|
||||
private _onDidChange = new Emitter<LanguageServiceDefaults>();
|
||||
private _diagnosticsOptions: DiagnosticsOptions;
|
||||
private _modeConfiguration: ModeConfiguration;
|
||||
private _languageId: string;
|
||||
|
||||
constructor(
|
||||
languageId: string,
|
||||
diagnosticsOptions: DiagnosticsOptions,
|
||||
modeConfiguration: ModeConfiguration
|
||||
) {
|
||||
this._languageId = languageId;
|
||||
this.setDiagnosticsOptions(diagnosticsOptions);
|
||||
this.setModeConfiguration(modeConfiguration);
|
||||
}
|
||||
|
||||
get onDidChange(): IEvent<LanguageServiceDefaults> {
|
||||
return this._onDidChange.event;
|
||||
}
|
||||
|
||||
get languageId(): string {
|
||||
return this._languageId;
|
||||
}
|
||||
|
||||
get modeConfiguration(): ModeConfiguration {
|
||||
return this._modeConfiguration;
|
||||
}
|
||||
|
||||
get diagnosticsOptions(): DiagnosticsOptions {
|
||||
return this._diagnosticsOptions;
|
||||
}
|
||||
|
||||
setDiagnosticsOptions(options: DiagnosticsOptions): void {
|
||||
this._diagnosticsOptions = options || Object.create(null);
|
||||
this._onDidChange.fire(this);
|
||||
}
|
||||
|
||||
setModeConfiguration(modeConfiguration: ModeConfiguration): void {
|
||||
this._modeConfiguration = modeConfiguration || Object.create(null);
|
||||
this._onDidChange.fire(this);
|
||||
}
|
||||
}
|
||||
|
||||
const diagnosticDefault: Required<DiagnosticsOptions> = {
|
||||
validate: true,
|
||||
allowComments: true,
|
||||
schemas: [],
|
||||
enableSchemaRequest: false
|
||||
};
|
||||
|
||||
const modeConfigurationDefault: Required<ModeConfiguration> = {
|
||||
documentFormattingEdits: true,
|
||||
documentRangeFormattingEdits: true,
|
||||
completionItems: true,
|
||||
hovers: true,
|
||||
documentSymbols: true,
|
||||
tokens: true,
|
||||
colors: true,
|
||||
foldingRanges: true,
|
||||
diagnostics: true,
|
||||
selectionRanges: true
|
||||
};
|
||||
|
||||
export const jsonDefaults: LanguageServiceDefaults = new LanguageServiceDefaultsImpl(
|
||||
'json',
|
||||
diagnosticDefault,
|
||||
modeConfigurationDefault
|
||||
);
|
||||
|
||||
// export to the global based API
|
||||
(<any>languages).json = { jsonDefaults };
|
||||
|
||||
// --- Registration to monaco editor ---
|
||||
|
||||
function getMode(): Promise<typeof mode> {
|
||||
return import('./jsonMode');
|
||||
}
|
||||
|
||||
languages.register({
|
||||
id: 'json',
|
||||
extensions: [
|
||||
'.json',
|
||||
'.bowerrc',
|
||||
'.jshintrc',
|
||||
'.jscsrc',
|
||||
'.eslintrc',
|
||||
'.babelrc',
|
||||
'.har'
|
||||
],
|
||||
aliases: ['JSON', 'json'],
|
||||
mimetypes: ['application/json']
|
||||
});
|
||||
|
||||
languages.onLanguage('json', () => {
|
||||
getMode().then((mode) => mode.setupMode(jsonDefaults));
|
||||
});
|
||||
|
@ -1,16 +1,16 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"declaration": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "node",
|
||||
"outDir": "../out/esm",
|
||||
"target": "es5",
|
||||
"lib": [
|
||||
"dom",
|
||||
"es5",
|
||||
"es2015.collection",
|
||||
"es2015.promise",
|
||||
"es2015.iterable"
|
||||
]
|
||||
}
|
||||
"compilerOptions": {
|
||||
"declaration": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "node",
|
||||
"outDir": "../out/esm",
|
||||
"target": "es5",
|
||||
"lib": [
|
||||
"dom",
|
||||
"es5",
|
||||
"es2015.collection",
|
||||
"es2015.promise",
|
||||
"es2015.iterable"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "amd",
|
||||
"moduleResolution": "node",
|
||||
"outDir": "../out/amd",
|
||||
"declaration": true,
|
||||
"target": "es5",
|
||||
"lib": [
|
||||
"dom",
|
||||
"es5",
|
||||
"es2015.collection",
|
||||
"es2015.promise",
|
||||
"es2015.iterable"
|
||||
]
|
||||
}
|
||||
"compilerOptions": {
|
||||
"module": "amd",
|
||||
"moduleResolution": "node",
|
||||
"outDir": "../out/amd",
|
||||
"declaration": true,
|
||||
"target": "es5",
|
||||
"lib": [
|
||||
"dom",
|
||||
"es5",
|
||||
"es2015.collection",
|
||||
"es2015.promise",
|
||||
"es2015.iterable"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -1,87 +1,91 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { LanguageServiceDefaults } from './monaco.contribution';
|
||||
import type { JSONWorker } from './jsonWorker';
|
||||
import { IDisposable, Uri, editor } from './fillers/monaco-editor-core'
|
||||
|
||||
const STOP_WHEN_IDLE_FOR = 2 * 60 * 1000; // 2min
|
||||
|
||||
export class WorkerManager {
|
||||
|
||||
private _defaults: LanguageServiceDefaults;
|
||||
private _idleCheckInterval: number;
|
||||
private _lastUsedTime: number;
|
||||
private _configChangeListener: IDisposable;
|
||||
|
||||
private _worker: editor.MonacoWebWorker<JSONWorker>;
|
||||
private _client: Promise<JSONWorker>;
|
||||
|
||||
constructor(defaults: LanguageServiceDefaults) {
|
||||
this._defaults = defaults;
|
||||
this._worker = null;
|
||||
this._idleCheckInterval = setInterval(() => this._checkIfIdle(), 30 * 1000);
|
||||
this._lastUsedTime = 0;
|
||||
this._configChangeListener = this._defaults.onDidChange(() => this._stopWorker());
|
||||
}
|
||||
|
||||
private _stopWorker(): void {
|
||||
if (this._worker) {
|
||||
this._worker.dispose();
|
||||
this._worker = null;
|
||||
}
|
||||
this._client = null;
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
clearInterval(this._idleCheckInterval);
|
||||
this._configChangeListener.dispose();
|
||||
this._stopWorker();
|
||||
}
|
||||
|
||||
private _checkIfIdle(): void {
|
||||
if (!this._worker) {
|
||||
return;
|
||||
}
|
||||
let timePassedSinceLastUsed = Date.now() - this._lastUsedTime;
|
||||
if (timePassedSinceLastUsed > STOP_WHEN_IDLE_FOR) {
|
||||
this._stopWorker();
|
||||
}
|
||||
}
|
||||
|
||||
private _getClient(): Promise<JSONWorker> {
|
||||
this._lastUsedTime = Date.now();
|
||||
|
||||
if (!this._client) {
|
||||
this._worker = editor.createWebWorker<JSONWorker>({
|
||||
|
||||
// module that exports the create() method and returns a `JSONWorker` instance
|
||||
moduleId: 'vs/language/json/jsonWorker',
|
||||
|
||||
label: this._defaults.languageId,
|
||||
|
||||
// passed in to the create() method
|
||||
createData: {
|
||||
languageSettings: this._defaults.diagnosticsOptions,
|
||||
languageId: this._defaults.languageId,
|
||||
enableSchemaRequest: this._defaults.diagnosticsOptions.enableSchemaRequest
|
||||
}
|
||||
});
|
||||
|
||||
this._client = <Promise<JSONWorker>><any>this._worker.getProxy();
|
||||
}
|
||||
|
||||
return this._client;
|
||||
}
|
||||
|
||||
getLanguageServiceWorker(...resources: Uri[]): Promise<JSONWorker> {
|
||||
let _client: JSONWorker;
|
||||
return this._getClient().then((client) => {
|
||||
_client = client
|
||||
}).then(_ => {
|
||||
return this._worker.withSyncedResources(resources)
|
||||
}).then(_ => _client);
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { LanguageServiceDefaults } from './monaco.contribution';
|
||||
import type { JSONWorker } from './jsonWorker';
|
||||
import { IDisposable, Uri, editor } from './fillers/monaco-editor-core';
|
||||
|
||||
const STOP_WHEN_IDLE_FOR = 2 * 60 * 1000; // 2min
|
||||
|
||||
export class WorkerManager {
|
||||
private _defaults: LanguageServiceDefaults;
|
||||
private _idleCheckInterval: number;
|
||||
private _lastUsedTime: number;
|
||||
private _configChangeListener: IDisposable;
|
||||
|
||||
private _worker: editor.MonacoWebWorker<JSONWorker>;
|
||||
private _client: Promise<JSONWorker>;
|
||||
|
||||
constructor(defaults: LanguageServiceDefaults) {
|
||||
this._defaults = defaults;
|
||||
this._worker = null;
|
||||
this._idleCheckInterval = setInterval(() => this._checkIfIdle(), 30 * 1000);
|
||||
this._lastUsedTime = 0;
|
||||
this._configChangeListener = this._defaults.onDidChange(() =>
|
||||
this._stopWorker()
|
||||
);
|
||||
}
|
||||
|
||||
private _stopWorker(): void {
|
||||
if (this._worker) {
|
||||
this._worker.dispose();
|
||||
this._worker = null;
|
||||
}
|
||||
this._client = null;
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
clearInterval(this._idleCheckInterval);
|
||||
this._configChangeListener.dispose();
|
||||
this._stopWorker();
|
||||
}
|
||||
|
||||
private _checkIfIdle(): void {
|
||||
if (!this._worker) {
|
||||
return;
|
||||
}
|
||||
let timePassedSinceLastUsed = Date.now() - this._lastUsedTime;
|
||||
if (timePassedSinceLastUsed > STOP_WHEN_IDLE_FOR) {
|
||||
this._stopWorker();
|
||||
}
|
||||
}
|
||||
|
||||
private _getClient(): Promise<JSONWorker> {
|
||||
this._lastUsedTime = Date.now();
|
||||
|
||||
if (!this._client) {
|
||||
this._worker = editor.createWebWorker<JSONWorker>({
|
||||
// module that exports the create() method and returns a `JSONWorker` instance
|
||||
moduleId: 'vs/language/json/jsonWorker',
|
||||
|
||||
label: this._defaults.languageId,
|
||||
|
||||
// passed in to the create() method
|
||||
createData: {
|
||||
languageSettings: this._defaults.diagnosticsOptions,
|
||||
languageId: this._defaults.languageId,
|
||||
enableSchemaRequest: this._defaults.diagnosticsOptions
|
||||
.enableSchemaRequest
|
||||
}
|
||||
});
|
||||
|
||||
this._client = <Promise<JSONWorker>>(<any>this._worker.getProxy());
|
||||
}
|
||||
|
||||
return this._client;
|
||||
}
|
||||
|
||||
getLanguageServiceWorker(...resources: Uri[]): Promise<JSONWorker> {
|
||||
let _client: JSONWorker;
|
||||
return this._getClient()
|
||||
.then((client) => {
|
||||
_client = client;
|
||||
})
|
||||
.then((_) => {
|
||||
return this._worker.withSyncedResources(resources);
|
||||
})
|
||||
.then((_) => _client);
|
||||
}
|
||||
}
|
||||
|
@ -1,118 +1,126 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
|
||||
<link rel="stylesheet" data-name="vs/editor/editor.main" href="../node_modules/monaco-editor-core/dev/vs/editor/editor.main.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>Monaco Editor JSON test page</h2>
|
||||
<div id="container" style="width:800px;height:600px;border:1px solid grey"></div>
|
||||
|
||||
<script>
|
||||
// Loading basic-languages to get the json language definition
|
||||
var paths = {
|
||||
'vs/basic-languages': '../node_modules/monaco-languages/release/dev',
|
||||
'vs/language/json': '../release/dev',
|
||||
'vs': '../node_modules/monaco-editor-core/dev/vs'
|
||||
};
|
||||
if (document.location.protocol === 'http:') {
|
||||
// Add support for running local http server
|
||||
let testIndex = document.location.pathname.indexOf('/test/');
|
||||
if (testIndex !== -1) {
|
||||
let prefix = document.location.pathname.substr(0, testIndex);
|
||||
paths['vs/language/json'] = prefix + '/release/dev';
|
||||
}
|
||||
}
|
||||
var require = {
|
||||
paths: paths
|
||||
};
|
||||
</script>
|
||||
<script src="../node_modules/monaco-editor-core/dev/vs/loader.js"></script>
|
||||
<script src="../node_modules/monaco-editor-core/dev/vs/editor/editor.main.nls.js"></script>
|
||||
<script src="../node_modules/monaco-editor-core/dev/vs/editor/editor.main.js"></script>
|
||||
|
||||
<script>
|
||||
require([
|
||||
'vs/basic-languages/monaco.contribution',
|
||||
'vs/language/json/monaco.contribution'
|
||||
], function() {
|
||||
var editor = monaco.editor.create(document.getElementById('container'), {
|
||||
value: [
|
||||
'{',
|
||||
' "type": "team",',
|
||||
' "test": {',
|
||||
' "testPage": "tools/testing/run-tests.htm",',
|
||||
' "enabled": true',
|
||||
' },',
|
||||
' "search": {',
|
||||
' "excludeFolders": [',
|
||||
' ".git",',
|
||||
' "node_modules",',
|
||||
' "tools/bin",',
|
||||
' "tools/counts",',
|
||||
' "tools/policheck",',
|
||||
' "tools/tfs_build_extensions",',
|
||||
' "tools/testing/jscoverage",',
|
||||
' "tools/testing/qunit",',
|
||||
' "tools/testing/chutzpah",',
|
||||
' "server.net"',
|
||||
' ]',
|
||||
' },',
|
||||
' "languages": {',
|
||||
' "vs.languages.typescript": {',
|
||||
' "validationSettings": [{',
|
||||
' "scope":"/",',
|
||||
' "noImplicitAny":true,',
|
||||
' "noLib":false,',
|
||||
' "extraLibs":[],',
|
||||
' "semanticValidation":true,',
|
||||
' "syntaxValidation":true,',
|
||||
' "codeGenTarget":"ES5",',
|
||||
' "moduleGenTarget":"",',
|
||||
' "lint": {',
|
||||
' "emptyBlocksWithoutComment": "warning",',
|
||||
' "curlyBracketsMustNotBeOmitted": "warning",',
|
||||
' "comparisonOperatorsNotStrict": "warning",',
|
||||
' "missingSemicolon": "warning",',
|
||||
' "unknownTypeOfResults": "warning",',
|
||||
' "semicolonsInsteadOfBlocks": "warning",',
|
||||
' "functionsInsideLoops": "warning",',
|
||||
' "functionsWithoutReturnType": "warning",',
|
||||
' "tripleSlashReferenceAlike": "warning",',
|
||||
' "unusedImports": "warning",',
|
||||
' "unusedVariables": "warning",',
|
||||
' "unusedFunctions": "warning",',
|
||||
' "unusedMembers": "warning"',
|
||||
' }',
|
||||
' },',
|
||||
' {',
|
||||
' "scope":"/client",',
|
||||
' "baseUrl":"/client",',
|
||||
' "moduleGenTarget":"amd"',
|
||||
' },',
|
||||
' {',
|
||||
' "scope":"/server",',
|
||||
' "moduleGenTarget":"commonjs"',
|
||||
' },',
|
||||
' {',
|
||||
' "scope":"/build",',
|
||||
' "moduleGenTarget":"commonjs"',
|
||||
' },',
|
||||
' {',
|
||||
' "scope":"/node_modules/nake",',
|
||||
' "moduleGenTarget":"commonjs"',
|
||||
' }],',
|
||||
' "allowMultipleWorkers": true',
|
||||
' }',
|
||||
' }',
|
||||
'}',
|
||||
].join('\n'),
|
||||
language: 'json'
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
|
||||
<link
|
||||
rel="stylesheet"
|
||||
data-name="vs/editor/editor.main"
|
||||
href="../node_modules/monaco-editor-core/dev/vs/editor/editor.main.css"
|
||||
/>
|
||||
</head>
|
||||
<body>
|
||||
<h2>Monaco Editor JSON test page</h2>
|
||||
<div
|
||||
id="container"
|
||||
style="width: 800px; height: 600px; border: 1px solid grey"
|
||||
></div>
|
||||
|
||||
<script>
|
||||
// Loading basic-languages to get the json language definition
|
||||
var paths = {
|
||||
'vs/basic-languages': '../node_modules/monaco-languages/release/dev',
|
||||
'vs/language/json': '../release/dev',
|
||||
vs: '../node_modules/monaco-editor-core/dev/vs'
|
||||
};
|
||||
if (document.location.protocol === 'http:') {
|
||||
// Add support for running local http server
|
||||
let testIndex = document.location.pathname.indexOf('/test/');
|
||||
if (testIndex !== -1) {
|
||||
let prefix = document.location.pathname.substr(0, testIndex);
|
||||
paths['vs/language/json'] = prefix + '/release/dev';
|
||||
}
|
||||
}
|
||||
var require = {
|
||||
paths: paths
|
||||
};
|
||||
</script>
|
||||
<script src="../node_modules/monaco-editor-core/dev/vs/loader.js"></script>
|
||||
<script src="../node_modules/monaco-editor-core/dev/vs/editor/editor.main.nls.js"></script>
|
||||
<script src="../node_modules/monaco-editor-core/dev/vs/editor/editor.main.js"></script>
|
||||
|
||||
<script>
|
||||
require([
|
||||
'vs/basic-languages/monaco.contribution',
|
||||
'vs/language/json/monaco.contribution'
|
||||
], function () {
|
||||
var editor = monaco.editor.create(
|
||||
document.getElementById('container'),
|
||||
{
|
||||
value: [
|
||||
'{',
|
||||
' "type": "team",',
|
||||
' "test": {',
|
||||
' "testPage": "tools/testing/run-tests.htm",',
|
||||
' "enabled": true',
|
||||
' },',
|
||||
' "search": {',
|
||||
' "excludeFolders": [',
|
||||
' ".git",',
|
||||
' "node_modules",',
|
||||
' "tools/bin",',
|
||||
' "tools/counts",',
|
||||
' "tools/policheck",',
|
||||
' "tools/tfs_build_extensions",',
|
||||
' "tools/testing/jscoverage",',
|
||||
' "tools/testing/qunit",',
|
||||
' "tools/testing/chutzpah",',
|
||||
' "server.net"',
|
||||
' ]',
|
||||
' },',
|
||||
' "languages": {',
|
||||
' "vs.languages.typescript": {',
|
||||
' "validationSettings": [{',
|
||||
' "scope":"/",',
|
||||
' "noImplicitAny":true,',
|
||||
' "noLib":false,',
|
||||
' "extraLibs":[],',
|
||||
' "semanticValidation":true,',
|
||||
' "syntaxValidation":true,',
|
||||
' "codeGenTarget":"ES5",',
|
||||
' "moduleGenTarget":"",',
|
||||
' "lint": {',
|
||||
' "emptyBlocksWithoutComment": "warning",',
|
||||
' "curlyBracketsMustNotBeOmitted": "warning",',
|
||||
' "comparisonOperatorsNotStrict": "warning",',
|
||||
' "missingSemicolon": "warning",',
|
||||
' "unknownTypeOfResults": "warning",',
|
||||
' "semicolonsInsteadOfBlocks": "warning",',
|
||||
' "functionsInsideLoops": "warning",',
|
||||
' "functionsWithoutReturnType": "warning",',
|
||||
' "tripleSlashReferenceAlike": "warning",',
|
||||
' "unusedImports": "warning",',
|
||||
' "unusedVariables": "warning",',
|
||||
' "unusedFunctions": "warning",',
|
||||
' "unusedMembers": "warning"',
|
||||
' }',
|
||||
' },',
|
||||
' {',
|
||||
' "scope":"/client",',
|
||||
' "baseUrl":"/client",',
|
||||
' "moduleGenTarget":"amd"',
|
||||
' },',
|
||||
' {',
|
||||
' "scope":"/server",',
|
||||
' "moduleGenTarget":"commonjs"',
|
||||
' },',
|
||||
' {',
|
||||
' "scope":"/build",',
|
||||
' "moduleGenTarget":"commonjs"',
|
||||
' },',
|
||||
' {',
|
||||
' "scope":"/node_modules/nake",',
|
||||
' "moduleGenTarget":"commonjs"',
|
||||
' }],',
|
||||
' "allowMultipleWorkers": true',
|
||||
' }',
|
||||
' }',
|
||||
'}'
|
||||
].join('\n'),
|
||||
language: 'json'
|
||||
}
|
||||
);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue