Skip to content

Commit

Permalink
fix #4651: apply default configuration overrides properly
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed Mar 22, 2019
1 parent 8c9ecd8 commit 56e8b23
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
26 changes: 21 additions & 5 deletions packages/core/src/browser/preferences/preference-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,30 @@ export class PreferenceSchemaProvider extends PreferenceProvider {
if (schemaProps.overridable) {
this.overridePatternProperties.properties[preferenceName] = schemaProps;
}
const newValue = schemaProps.default = this.getDefaultValue(schemaProps);
this.combinedSchema.properties[preferenceName] = schemaProps;
this.preferences[preferenceName] = newValue;
changes.push({ preferenceName, newValue, scope, domain });

const value = schemaProps.default = this.getDefaultValue(schemaProps);
if (this.testOverrideValue(preferenceName, value)) {
for (const overridenPreferenceName in value) {
const overrideValue = value[overridenPreferenceName];
const overridePreferenceName = `${preferenceName}.${overridenPreferenceName}`;
changes.push(this.doSetPreferenceValue(overridePreferenceName, overrideValue, { scope, domain }));
}
} else {
changes.push(this.doSetPreferenceValue(preferenceName, value, { scope, domain }));
}
}
}
return changes;
}
protected doSetPreferenceValue(preferenceName: string, newValue: any, { scope, domain }: {
scope: PreferenceScope,
domain: string[]
}): PreferenceProviderDataChange {
const oldValue = this.preferences[preferenceName];
this.preferences[preferenceName] = newValue;
return { preferenceName, oldValue, newValue, scope, domain };
}

protected getDefaultValue(property: PreferenceItem): any {
if (property.default) {
Expand Down Expand Up @@ -243,7 +259,7 @@ export class PreferenceSchemaProvider extends PreferenceProvider {
return { preferenceName, overrideIdentifier };
}

testOverrideValue(name: string, value: any): boolean {
return typeof value === 'object' && OVERRIDE_PROPERTY_PATTERN.test(name);
testOverrideValue(name: string, value: any): value is PreferenceSchemaProperties {
return PreferenceSchemaProperties.is(value) && OVERRIDE_PROPERTY_PATTERN.test(name);
}
}
51 changes: 48 additions & 3 deletions packages/preferences/src/browser/preference-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import * as temp from 'temp';
import { Emitter } from '@theia/core/lib/common';
import {
PreferenceService, PreferenceScope, PreferenceProviderDataChanges,
PreferenceSchemaProvider, PreferenceProviderProvider, PreferenceServiceImpl, bindPreferenceSchemaProvider, PreferenceChange
PreferenceSchemaProvider, PreferenceProviderProvider, PreferenceServiceImpl, bindPreferenceSchemaProvider, PreferenceChange, PreferenceSchema
} from '@theia/core/lib/browser/preferences';
import { FileSystem, FileShouldOverwrite, FileStat } from '@theia/filesystem/lib/common/';
import { FileSystemWatcher } from '@theia/filesystem/lib/browser/filesystem-watcher';
Expand Down Expand Up @@ -623,14 +623,59 @@ describe('Preference Service', () => {
})));
});

function prepareServices() {
it('defaultOverrides [go].editor.formatOnSave', () => {
const { preferences, schema } = prepareServices({
schema: {
properties: {
'editor.insertSpaces': {
type: 'boolean',
default: true,
overridable: true
},
'editor.formatOnSave': {
type: 'boolean',
default: false,
overridable: true
}
}
}
});

assert.equal(true, preferences.get('editor.insertSpaces'));
assert.equal(undefined, preferences.get('[go].editor.insertSpaces'));
assert.equal(false, preferences.get('editor.formatOnSave'));
assert.equal(undefined, preferences.get('[go].editor.formatOnSave'));

schema.registerOverrideIdentifier('go');
schema.setSchema({
id: 'defaultOverrides',
title: 'Default Configuration Overrides',
properties: {
'[go]': {
type: 'object',
default: {
'editor.insertSpaces': false,
'editor.formatOnSave': true
},
description: 'Configure editor settings to be overridden for go language.'
}
}
});

assert.equal(true, preferences.get('editor.insertSpaces'));
assert.equal(false, preferences.get('[go].editor.insertSpaces'));
assert.equal(false, preferences.get('editor.formatOnSave'));
assert.equal(true, preferences.get('[go].editor.formatOnSave'));
});

function prepareServices(options?: { schema: PreferenceSchema }) {
const container = new Container();
bindPreferenceSchemaProvider(container.bind.bind(container));
container.bind(PreferenceProviderProvider).toFactory(() => () => new MockPreferenceProvider());
container.bind(PreferenceServiceImpl).toSelf().inSingletonScope();

const schema = container.get(PreferenceSchemaProvider);
schema.setSchema({
schema.setSchema(options && options.schema || {
properties: {
'editor.tabSize': {
type: 'number',
Expand Down

0 comments on commit 56e8b23

Please sign in to comment.