Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Add config to disable default formatter #10

Merged
merged 7 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 55 additions & 13 deletions src/jsonMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import IDisposable = monaco.IDisposable;

export function setupMode(defaults: LanguageServiceDefaultsImpl): void {

let disposables: IDisposable[] = [];
const disposables: IDisposable[] = [];
const providers: IDisposable[] = [];

const client = new WorkerManager(defaults);
disposables.push(client);
Expand All @@ -24,20 +25,61 @@ export function setupMode(defaults: LanguageServiceDefaultsImpl): void {
return client.getLanguageServiceWorker(...uris);
};

let languageId = defaults.languageId;

disposables.push(monaco.languages.registerCompletionItemProvider(languageId, new languageFeatures.CompletionAdapter(worker)));
disposables.push(monaco.languages.registerHoverProvider(languageId, new languageFeatures.HoverAdapter(worker)));
disposables.push(monaco.languages.registerDocumentSymbolProvider(languageId, new languageFeatures.DocumentSymbolAdapter(worker)));
disposables.push(monaco.languages.registerDocumentFormattingEditProvider(languageId, new languageFeatures.DocumentFormattingEditProvider(worker)));
disposables.push(monaco.languages.registerDocumentRangeFormattingEditProvider(languageId, new languageFeatures.DocumentRangeFormattingEditProvider(worker)));
disposables.push(new languageFeatures.DiagnosticsAdapter(languageId, worker, defaults));
disposables.push(monaco.languages.setTokensProvider(languageId, createTokenizationSupport(true)));
disposables.push(monaco.languages.setLanguageConfiguration(languageId, richEditConfiguration));
disposables.push(monaco.languages.registerColorProvider(languageId, new languageFeatures.DocumentColorAdapter(worker)));
disposables.push(monaco.languages.registerFoldingRangeProvider(languageId, new languageFeatures.FoldingRangeAdapter(worker)));

function registerProviders(): void {
const { languageId, modeConfiguration } = defaults;

disposeAll(providers);

if (modeConfiguration.documentFormattingEdits) {
providers.push(monaco.languages.registerDocumentFormattingEditProvider(languageId, new languageFeatures.DocumentFormattingEditProvider(worker)));
}
if (modeConfiguration.documentRangeFormattingEdits) {
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({ dispose: () => disposeAll(providers) });
}

function disposeAll(disposables: IDisposable[]) {
while (disposables.length) {
disposables.pop().dispose();
}
}

const richEditConfiguration: monaco.languages.LanguageConfiguration = {
wordPattern: /(-?\d*\.\d\w*)|([^\[\{\]\}\:\"\,\s]+)/g,
Expand Down
30 changes: 26 additions & 4 deletions src/monaco.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export class LanguageServiceDefaultsImpl implements monaco.languages.json.Langua

private _onDidChange = new Emitter<monaco.languages.json.LanguageServiceDefaults>();
private _diagnosticsOptions: monaco.languages.json.DiagnosticsOptions;
private _modeConfiguration: monaco.languages.json.ModeConfiguration;
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.setDiagnosticsOptions(diagnosticsOptions);
this.setModeConfiguration(modeConfiguration)
}

get onDidChange(): IEvent<monaco.languages.json.LanguageServiceDefaults> {
Expand All @@ -31,6 +33,10 @@ export class LanguageServiceDefaultsImpl implements monaco.languages.json.Langua
return this._languageId;
}

get modeConfiguration(): monaco.languages.json.ModeConfiguration {
return this._modeConfiguration;
}

get diagnosticsOptions(): monaco.languages.json.DiagnosticsOptions {
return this._diagnosticsOptions;
}
Expand All @@ -39,22 +45,37 @@ export class LanguageServiceDefaultsImpl implements monaco.languages.json.Langua
this._diagnosticsOptions = options || Object.create(null);
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 = {
validate: true,
allowComments: true,
schemas: [],
enableSchemaRequest: false
enableSchemaRequest: false
};

const jsonDefaults = new LanguageServiceDefaultsImpl('json', diagnosticDefault);
const providersDefault: 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, providersDefault);

// Export API
function createAPI(): typeof monaco.languages.json {
return {
jsonDefaults: jsonDefaults,
jsonDefaults: jsonDefaults
}
}
monaco.languages.json = createAPI();
Expand All @@ -71,6 +92,7 @@ monaco.languages.register({
aliases: ['JSON', 'json'],
mimetypes: ['application/json'],
});

monaco.languages.onLanguage('json', () => {
getMode().then(mode => mode.setupMode(jsonDefaults));
});
52 changes: 51 additions & 1 deletion src/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,63 @@ declare module monaco.languages.json {
/**
* 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 wheter the built-in documentFormattingEdit provider is enabled.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo. Should be "whether".

*/
readonly documentFormattingEdits?: boolean;

/**
* Defines wheter the built-in documentRangeFormattingEdit provider is enabled.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same typo as above (also note the same typo below)

*/
readonly documentRangeFormattingEdits?: boolean;

/**
* Defines wheter the built-in completionItemProvider is enabled.
*/
readonly completionItems?: boolean;

/**
* Defines wheter the built-in hoverProvider is enabled.
*/
readonly hovers?: boolean;

/**
* Defines wheter the built-in documentSymbolProvider is enabled.
*/
readonly documentSymbols?: boolean;

/**
* Defines wheter the built-in tokens provider is enabled.
*/
readonly tokens?: boolean;

/**
* Defines wheter the built-in color provider is enabled.
*/
readonly colors?: boolean;

/**
* Defines wheter the built-in foldingRange provider is enabled.
*/
readonly foldingRanges?: boolean;

/**
* Defines wheter the built-in diagnostic provider is enabled.
*/
readonly diagnostics?: boolean;

}

export interface LanguageServiceDefaults {
readonly onDidChange: IEvent<LanguageServiceDefaults>;
readonly diagnosticsOptions: DiagnosticsOptions;
readonly modeConfiguration: ModeConfiguration;
setDiagnosticsOptions(options: DiagnosticsOptions): void;
setModeConfiguration(modeConfiguration: ModeConfiguration): void;
}

export var jsonDefaults: LanguageServiceDefaults;
Expand Down