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 17, 2019
1 parent 928e1e0 commit 597af1b
Show file tree
Hide file tree
Showing 2 changed files with 30 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,7 @@ export abstract class AbstractResourcePreferenceProvider extends PreferenceProvi
try {
let newContent = '';
if (path.length || value !== undefined) {
const formattingOptions = { tabSize: 3, insertSpaces: true, eol: '' };
const formattingOptions = this.getFormattingOptions(resourceUri);
const edits = jsoncparser.modify(content, path, value, { formattingOptions });
newContent = jsoncparser.applyEdits(content, edits);
}
Expand Down Expand Up @@ -221,4 +222,30 @@ 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.
*
* The formatting options should attempt to obtain the preference values from JSONC,
* and if necessary fallback to JSON and the global values.
* @param uri the preference settings URI.
*
* @returns a tuple representing the tab indentation size, and if it is spaces.
*/
protected getFormattingOptions(uri?: string): jsoncparser.FormattingOptions {
// Get the global formatting options for both `tabSize` and `insertSpaces`.
const globalTabSize = this.preferenceService.get('editor.tabSize', 2, uri);
const globalInsertSpaces = this.preferenceService.get('editor.insertSpaces', true, uri);

// Get the superset JSON formatting options for both `tabSize` and `insertSpaces`.
const jsonTabSize = this.preferenceService.get('[json].editor.tabSize', globalTabSize, uri);
const jsonInsertSpaces = this.preferenceService.get('[json].editor.insertSpaces', globalInsertSpaces, uri);

return {
tabSize: this.preferenceService.get('[jsonc].editor.tabSize', jsonTabSize, uri),
insertSpaces: this.preferenceService.get('[json].editor.insertSpaces', jsonInsertSpaces, uri),
eol: ''
};
}

}

0 comments on commit 597af1b

Please sign in to comment.