From a3f3115996b62608dd7780b04511d542dcba6853 Mon Sep 17 00:00:00 2001 From: vince-fugnitto Date: Wed, 11 Dec 2019 18:28:07 -0500 Subject: [PATCH] Fix indentation when using preferences tree Fixes #5052 - fixes the indentation when updating/setting a preference using the `preferences-tree` widget. - the current implementation had a hardcoded value of `tabSize`=3 and `insertSpaces`=true which meant that if the `settings.json` file had different formatting, it would insert incorrectly. - added a new method `getFormattingOptions` which gets the preference options for tabSize and indentation type. - fixed typings from the `preference-service`. If a default value is provided, the preference should return that value if the preference initially returns `undefined`. Signed-off-by: vince-fugnitto --- .../browser/preferences/preference-service.ts | 2 +- .../abstract-resource-preference-provider.ts | 23 +++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) 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) + ]; + } + }