mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 22:02:55 +01:00
Add config to disable default formatter (#10)
Add config to disable default formatter
This commit is contained in:
commit
58c8efef50
4 changed files with 139 additions and 20 deletions
2
package-lock.json
generated
2
package-lock.json
generated
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "monaco-json",
|
"name": "monaco-json",
|
||||||
"version": "2.5.1",
|
"version": "2.6.0",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
||||||
|
|
@ -13,9 +13,10 @@ import { createTokenizationSupport } from './tokenization';
|
||||||
import Uri = monaco.Uri;
|
import Uri = monaco.Uri;
|
||||||
import IDisposable = monaco.IDisposable;
|
import IDisposable = monaco.IDisposable;
|
||||||
|
|
||||||
export function setupMode(defaults: LanguageServiceDefaultsImpl): void {
|
export function setupMode(defaults: LanguageServiceDefaultsImpl): IDisposable {
|
||||||
|
|
||||||
let disposables: IDisposable[] = [];
|
const disposables: IDisposable[] = [];
|
||||||
|
const providers: IDisposable[] = [];
|
||||||
|
|
||||||
const client = new WorkerManager(defaults);
|
const client = new WorkerManager(defaults);
|
||||||
disposables.push(client);
|
disposables.push(client);
|
||||||
|
|
@ -24,20 +25,67 @@ export function setupMode(defaults: LanguageServiceDefaultsImpl): void {
|
||||||
return client.getLanguageServiceWorker(...uris);
|
return client.getLanguageServiceWorker(...uris);
|
||||||
};
|
};
|
||||||
|
|
||||||
let languageId = defaults.languageId;
|
|
||||||
|
|
||||||
disposables.push(monaco.languages.registerCompletionItemProvider(languageId, new languageFeatures.CompletionAdapter(worker)));
|
function registerProviders(): void {
|
||||||
disposables.push(monaco.languages.registerHoverProvider(languageId, new languageFeatures.HoverAdapter(worker)));
|
const { languageId, modeConfiguration } = defaults;
|
||||||
disposables.push(monaco.languages.registerDocumentSymbolProvider(languageId, new languageFeatures.DocumentSymbolAdapter(worker)));
|
|
||||||
disposables.push(monaco.languages.registerDocumentFormattingEditProvider(languageId, new languageFeatures.DocumentFormattingEditProvider(worker)));
|
disposeAll(providers);
|
||||||
disposables.push(monaco.languages.registerDocumentRangeFormattingEditProvider(languageId, new languageFeatures.DocumentRangeFormattingEditProvider(worker)));
|
|
||||||
disposables.push(new languageFeatures.DiagnosticsAdapter(languageId, worker, defaults));
|
if (modeConfiguration.documentFormattingEdits) {
|
||||||
disposables.push(monaco.languages.setTokensProvider(languageId, createTokenizationSupport(true)));
|
providers.push(monaco.languages.registerDocumentFormattingEditProvider(languageId, new languageFeatures.DocumentFormattingEditProvider(worker)));
|
||||||
disposables.push(monaco.languages.setLanguageConfiguration(languageId, richEditConfiguration));
|
}
|
||||||
disposables.push(monaco.languages.registerColorProvider(languageId, new languageFeatures.DocumentColorAdapter(worker)));
|
if (modeConfiguration.documentRangeFormattingEdits) {
|
||||||
disposables.push(monaco.languages.registerFoldingRangeProvider(languageId, new languageFeatures.FoldingRangeAdapter(worker)));
|
providers.push(monaco.languages.registerDocumentRangeFormattingEditProvider(languageId, new languageFeatures.DocumentRangeFormattingEditProvider(worker)));
|
||||||
|
}
|
||||||
|
if (modeConfiguration.completionItems) {
|
||||||
|
providers.push(monaco.languages.registerCompletionItemProvider(languageId, new languageFeatures.CompletionAdapter(worker)));
|
||||||
|
}
|
||||||
|
if (modeConfiguration.hovers) {
|
||||||
|
providers.push(monaco.languages.registerHoverProvider(languageId, new languageFeatures.HoverAdapter(worker)));
|
||||||
|
}
|
||||||
|
if (modeConfiguration.documentSymbols) {
|
||||||
|
providers.push(monaco.languages.registerDocumentSymbolProvider(languageId, new languageFeatures.DocumentSymbolAdapter(worker)));
|
||||||
|
}
|
||||||
|
if (modeConfiguration.tokens) {
|
||||||
|
providers.push(monaco.languages.setTokensProvider(languageId, createTokenizationSupport(true)));
|
||||||
|
}
|
||||||
|
if (modeConfiguration.colors) {
|
||||||
|
providers.push(monaco.languages.registerColorProvider(languageId, new languageFeatures.DocumentColorAdapter(worker)));
|
||||||
|
}
|
||||||
|
if (modeConfiguration.foldingRanges) {
|
||||||
|
providers.push(monaco.languages.registerFoldingRangeProvider(languageId, new languageFeatures.FoldingRangeAdapter(worker)));
|
||||||
|
}
|
||||||
|
if (modeConfiguration.diagnostics) {
|
||||||
|
providers.push(new languageFeatures.DiagnosticsAdapter(languageId, worker, defaults));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registerProviders();
|
||||||
|
|
||||||
|
disposables.push(monaco.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: monaco.languages.LanguageConfiguration = {
|
const richEditConfiguration: monaco.languages.LanguageConfiguration = {
|
||||||
wordPattern: /(-?\d*\.\d\w*)|([^\[\{\]\}\:\"\,\s]+)/g,
|
wordPattern: /(-?\d*\.\d\w*)|([^\[\{\]\}\:\"\,\s]+)/g,
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import * as mode from './jsonMode';
|
||||||
|
|
||||||
import Emitter = monaco.Emitter;
|
import Emitter = monaco.Emitter;
|
||||||
import IEvent = monaco.IEvent;
|
import IEvent = monaco.IEvent;
|
||||||
import IDisposable = monaco.IDisposable;
|
|
||||||
|
|
||||||
// --- JSON configuration and defaults ---------
|
// --- JSON configuration and defaults ---------
|
||||||
|
|
||||||
|
|
@ -16,11 +15,13 @@ export class LanguageServiceDefaultsImpl implements monaco.languages.json.Langua
|
||||||
|
|
||||||
private _onDidChange = new Emitter<monaco.languages.json.LanguageServiceDefaults>();
|
private _onDidChange = new Emitter<monaco.languages.json.LanguageServiceDefaults>();
|
||||||
private _diagnosticsOptions: monaco.languages.json.DiagnosticsOptions;
|
private _diagnosticsOptions: monaco.languages.json.DiagnosticsOptions;
|
||||||
|
private _modeConfiguration: monaco.languages.json.ModeConfiguration;
|
||||||
private _languageId: string;
|
private _languageId: string;
|
||||||
|
|
||||||
constructor(languageId: string, diagnosticsOptions: monaco.languages.json.DiagnosticsOptions) {
|
constructor(languageId: string, diagnosticsOptions: monaco.languages.json.DiagnosticsOptions, modeConfiguration: monaco.languages.json.ModeConfiguration) {
|
||||||
this._languageId = languageId;
|
this._languageId = languageId;
|
||||||
this.setDiagnosticsOptions(diagnosticsOptions);
|
this.setDiagnosticsOptions(diagnosticsOptions);
|
||||||
|
this.setModeConfiguration(modeConfiguration)
|
||||||
}
|
}
|
||||||
|
|
||||||
get onDidChange(): IEvent<monaco.languages.json.LanguageServiceDefaults> {
|
get onDidChange(): IEvent<monaco.languages.json.LanguageServiceDefaults> {
|
||||||
|
|
@ -31,6 +32,10 @@ export class LanguageServiceDefaultsImpl implements monaco.languages.json.Langua
|
||||||
return this._languageId;
|
return this._languageId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get modeConfiguration(): monaco.languages.json.ModeConfiguration {
|
||||||
|
return this._modeConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
get diagnosticsOptions(): monaco.languages.json.DiagnosticsOptions {
|
get diagnosticsOptions(): monaco.languages.json.DiagnosticsOptions {
|
||||||
return this._diagnosticsOptions;
|
return this._diagnosticsOptions;
|
||||||
}
|
}
|
||||||
|
|
@ -39,6 +44,10 @@ export class LanguageServiceDefaultsImpl implements monaco.languages.json.Langua
|
||||||
this._diagnosticsOptions = options || Object.create(null);
|
this._diagnosticsOptions = options || Object.create(null);
|
||||||
this._onDidChange.fire(this);
|
this._onDidChange.fire(this);
|
||||||
}
|
}
|
||||||
|
setModeConfiguration(modeConfiguration: monaco.languages.json.ModeConfiguration): void {
|
||||||
|
this._modeConfiguration = modeConfiguration || Object.create(null);
|
||||||
|
this._onDidChange.fire(this);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const diagnosticDefault: monaco.languages.json.DiagnosticsOptions = {
|
const diagnosticDefault: monaco.languages.json.DiagnosticsOptions = {
|
||||||
|
|
@ -48,13 +57,24 @@ const diagnosticDefault: monaco.languages.json.DiagnosticsOptions = {
|
||||||
enableSchemaRequest: false
|
enableSchemaRequest: false
|
||||||
};
|
};
|
||||||
|
|
||||||
const jsonDefaults = new LanguageServiceDefaultsImpl('json', diagnosticDefault);
|
const modeConfigurationDefault: monaco.languages.json.ModeConfiguration = {
|
||||||
|
documentFormattingEdits: true,
|
||||||
|
documentRangeFormattingEdits: true,
|
||||||
|
completionItems: true,
|
||||||
|
hovers: true,
|
||||||
|
documentSymbols: true,
|
||||||
|
tokens: true,
|
||||||
|
colors: true,
|
||||||
|
foldingRanges: true,
|
||||||
|
diagnostics: true
|
||||||
|
}
|
||||||
|
|
||||||
|
const jsonDefaults = new LanguageServiceDefaultsImpl('json', diagnosticDefault, modeConfigurationDefault);
|
||||||
|
|
||||||
// Export API
|
// Export API
|
||||||
function createAPI(): typeof monaco.languages.json {
|
function createAPI(): typeof monaco.languages.json {
|
||||||
return {
|
return {
|
||||||
jsonDefaults: jsonDefaults,
|
jsonDefaults: jsonDefaults
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
monaco.languages.json = createAPI();
|
monaco.languages.json = createAPI();
|
||||||
|
|
@ -71,6 +91,7 @@ monaco.languages.register({
|
||||||
aliases: ['JSON', 'json'],
|
aliases: ['JSON', 'json'],
|
||||||
mimetypes: ['application/json'],
|
mimetypes: ['application/json'],
|
||||||
});
|
});
|
||||||
|
|
||||||
monaco.languages.onLanguage('json', () => {
|
monaco.languages.onLanguage('json', () => {
|
||||||
getMode().then(mode => mode.setupMode(jsonDefaults));
|
getMode().then(mode => mode.setupMode(jsonDefaults));
|
||||||
});
|
});
|
||||||
|
|
|
||||||
52
src/monaco.d.ts
vendored
52
src/monaco.d.ts
vendored
|
|
@ -33,13 +33,63 @@ declare module monaco.languages.json {
|
||||||
/**
|
/**
|
||||||
* If set, the schema service would load schema content on-demand with 'fetch' if available
|
* If set, the schema service would load schema content on-demand with 'fetch' if available
|
||||||
*/
|
*/
|
||||||
readonly enableSchemaRequest? : boolean
|
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LanguageServiceDefaults {
|
export interface LanguageServiceDefaults {
|
||||||
readonly onDidChange: IEvent<LanguageServiceDefaults>;
|
readonly onDidChange: IEvent<LanguageServiceDefaults>;
|
||||||
readonly diagnosticsOptions: DiagnosticsOptions;
|
readonly diagnosticsOptions: DiagnosticsOptions;
|
||||||
|
readonly modeConfiguration: ModeConfiguration;
|
||||||
setDiagnosticsOptions(options: DiagnosticsOptions): void;
|
setDiagnosticsOptions(options: DiagnosticsOptions): void;
|
||||||
|
setModeConfiguration(modeConfiguration: ModeConfiguration): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export var jsonDefaults: LanguageServiceDefaults;
|
export var jsonDefaults: LanguageServiceDefaults;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue