Skip to content

Commit

Permalink
Fix indentation when using preferences tree
Browse files Browse the repository at this point in the history
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 <vincent.fugnitto@ericsson.com>
  • Loading branch information
vince-fugnitto committed Dec 16, 2019
1 parent 928e1e0 commit a3f3115
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export interface PreferenceService extends Disposable {
readonly ready: Promise<void>;
get<T>(preferenceName: string): T | undefined;
get<T>(preferenceName: string, defaultValue: T): T;
get<T>(preferenceName: string, defaultValue: T, resourceUri: string): T;
get<T>(preferenceName: string, defaultValue: T, resourceUri?: string): T;
get<T>(preferenceName: string, defaultValue?: T, resourceUri?: string): T | undefined;
set(preferenceName: string, value: any, scope?: PreferenceScope, resourceUri?: string): Promise<void>;
onPreferenceChanged: Event<PreferenceChange>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -30,6 +30,7 @@ export abstract class AbstractResourcePreferenceProvider extends PreferenceProvi
protected preferences: { [key: string]: any } = {};
protected resource: Promise<Resource>;

@inject(PreferenceService) protected readonly preferenceService: PreferenceService;
@inject(ResourceProvider) protected readonly resourceProvider: ResourceProvider;
@inject(MessageService) protected readonly messageService: MessageService;
@inject(PreferenceSchemaProvider) protected readonly schemaProvider: PreferenceSchemaProvider;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
];
}

}

0 comments on commit a3f3115

Please sign in to comment.