diff --git a/packages/core/src/browser/preferences/preference-service.ts b/packages/core/src/browser/preferences/preference-service.ts index cbaf9e683d570..381c44dc72a47 100644 --- a/packages/core/src/browser/preferences/preference-service.ts +++ b/packages/core/src/browser/preferences/preference-service.ts @@ -70,7 +70,7 @@ export interface PreferenceService extends Disposable { readonly ready: Promise; get(preferenceName: string): T | undefined; get(preferenceName: string, defaultValue: T): T; - get(preferenceName: string, defaultValue: T, resourceUri: string): T; + get(preferenceName: string, defaultValue: T, resourceUri?: string): T; get(preferenceName: string, defaultValue?: T, resourceUri?: string): T | undefined; set(preferenceName: string, value: any, scope?: PreferenceScope, resourceUri?: string): Promise; onPreferenceChanged: Event; diff --git a/packages/preferences/src/browser/abstract-resource-preference-provider.ts b/packages/preferences/src/browser/abstract-resource-preference-provider.ts index d73c5ea13dc79..85a0a78aac244 100644 --- a/packages/preferences/src/browser/abstract-resource-preference-provider.ts +++ b/packages/preferences/src/browser/abstract-resource-preference-provider.ts @@ -20,7 +20,7 @@ import * as jsoncparser from 'jsonc-parser'; import { JSONExt } from '@phosphor/coreutils/lib/json'; import { inject, injectable, postConstruct } from 'inversify'; import { MessageService, Resource, ResourceProvider, Disposable } from '@theia/core'; -import { PreferenceProvider, PreferenceSchemaProvider, PreferenceScope, PreferenceProviderDataChange } from '@theia/core/lib/browser'; +import { PreferenceProvider, PreferenceSchemaProvider, PreferenceScope, PreferenceProviderDataChange, PreferenceService } from '@theia/core/lib/browser'; import URI from '@theia/core/lib/common/uri'; import { PreferenceConfigurations } from '@theia/core/lib/browser/preferences/preference-configurations'; @@ -30,6 +30,7 @@ export abstract class AbstractResourcePreferenceProvider extends PreferenceProvi protected preferences: { [key: string]: any } = {}; protected resource: Promise; + @inject(PreferenceService) protected readonly preferenceService: PreferenceService; @inject(ResourceProvider) protected readonly resourceProvider: ResourceProvider; @inject(MessageService) protected readonly messageService: MessageService; @inject(PreferenceSchemaProvider) protected readonly schemaProvider: PreferenceSchemaProvider; @@ -104,7 +105,8 @@ export abstract class AbstractResourcePreferenceProvider extends PreferenceProvi try { let newContent = ''; if (path.length || value !== undefined) { - const formattingOptions = { tabSize: 3, insertSpaces: true, eol: '' }; + const [tabSize, insertSpaces] = this.getFormattingOptions(resourceUri); + const formattingOptions = { tabSize, insertSpaces, eol: '' }; const edits = jsoncparser.modify(content, path, value, { formattingOptions }); newContent = jsoncparser.applyEdits(content, edits); } @@ -221,4 +223,21 @@ export abstract class AbstractResourcePreferenceProvider extends PreferenceProvi } } + /** + * Get the formatting options to be used when calling `jsoncparser`. + * The formatting options are based on the corresponding preference values. + * @param uri the preference settings URI. + * + * @returns a tuple representing the tab indentation size, and if it is spaces. + */ + protected getFormattingOptions(uri?: string): [number, boolean] { + console.log(`spaces?: ${this.preferenceService.get('[json].editor.insertSpaces', undefined, uri)}`); + return [ + // Get the preference value for `tabSize`. Defaults to 2 if undefined. + this.preferenceService.get('[json].editor.tabSize', 2, uri), + // Get the preference value for `insertSpaces`. Defaults to `true` if undefined. + this.preferenceService.get('[json].editor.insertSpaces', true, uri) + ]; + } + }