Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[preferences] fix indentation when updating preferences through the tree #6736

Merged
merged 1 commit into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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;
akosyakov marked this conversation as resolved.
Show resolved Hide resolved
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);
akosyakov marked this conversation as resolved.
Show resolved Hide resolved
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('[jsonc].editor.insertSpaces', jsonInsertSpaces, uri),
eol: ''
};
}

}