Skip to content

Commit

Permalink
JSONEditingService: update multiple value
Browse files Browse the repository at this point in the history
  • Loading branch information
sandy081 committed Sep 16, 2019
1 parent 8fbdeb7 commit 5bca175
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 31 deletions.
14 changes: 7 additions & 7 deletions src/vs/workbench/contrib/extensions/browser/extensionsActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2049,13 +2049,13 @@ export abstract class AbstractConfigureRecommendedExtensionsAction extends Actio
removeFrom = removeFrom.filter(x => x.toLowerCase() !== extensionIdLowerCase);

return this.jsonEditingService.write(workspaceConfigurationFile,
{
[{
key: 'extensions',
value: {
recommendations: shouldRecommend ? insertInto : removeFrom,
unwantedRecommendations: shouldRecommend ? removeFrom : insertInto
}
},
}],
true);
});
}
Expand All @@ -2078,19 +2078,19 @@ export abstract class AbstractConfigureRecommendedExtensionsAction extends Actio
if (removeFrom.some(e => e.toLowerCase() === extensionIdLowerCase)) {
removeFrom = removeFrom.filter(x => x.toLowerCase() !== extensionIdLowerCase);
removeFromPromise = this.jsonEditingService.write(extensionsFileResource,
{
[{
key: shouldRecommend ? 'unwantedRecommendations' : 'recommendations',
value: removeFrom
},
}],
true);
}

return removeFromPromise.then(() =>
this.jsonEditingService.write(extensionsFileResource,
{
[{
key: shouldRecommend ? 'recommendations' : 'unwantedRecommendations',
value: insertInto
},
}],
true)
);
});
Expand All @@ -2115,7 +2115,7 @@ export abstract class AbstractConfigureRecommendedExtensionsAction extends Actio
.then(content => {
const workspaceRecommendations = <IExtensionsConfigContent>json.parse(content.value.toString())['extensions'];
if (!workspaceRecommendations || !workspaceRecommendations.recommendations) {
return this.jsonEditingService.write(workspaceConfigurationFile, { key: 'extensions', value: { recommendations: [] } }, true)
return this.jsonEditingService.write(workspaceConfigurationFile, [{ key: 'extensions', value: { recommendations: [] } }], true)
.then(() => this.fileService.readFile(workspaceConfigurationFile));
}
return content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class LocalizationWorkbenchContribution extends Disposable implements IWo
[{
label: updateAndRestart ? localize('yes', "Yes") : localize('restart now', "Restart Now"),
run: () => {
const updatePromise = updateAndRestart ? this.jsonEditingService.write(this.environmentService.localeResource, { key: 'locale', value: locale }, true) : Promise.resolve(undefined);
const updatePromise = updateAndRestart ? this.jsonEditingService.write(this.environmentService.localeResource, [{ key: 'locale', value: locale }], true) : Promise.resolve(undefined);
updatePromise.then(() => this.windowsService.relaunch({}), e => this.notificationService.error(e));
}
}],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class ConfigureLocaleAction extends Action {
}

if (selectedLanguage) {
await this.jsonEditingService.write(this.environmentService.localeResource, { key: 'locale', value: selectedLanguage.label }, true);
await this.jsonEditingService.write(this.environmentService.localeResource, [{ key: 'locale', value: selectedLanguage.label }], true);
const restart = await this.dialogService.confirm({
type: 'info',
message: localize('relaunchDisplayLanguageMessage', "A restart is required for the change in display language to take effect."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export class WorkspaceConfiguration extends Disposable {

setFolders(folders: IStoredWorkspaceFolder[], jsonEditingService: JSONEditingService): Promise<void> {
if (this._workspaceIdentifier) {
return jsonEditingService.write(this._workspaceIdentifier.configPath, { key: 'folders', value: folders }, true)
return jsonEditingService.write(this._workspaceIdentifier.configPath, [{ key: 'folders', value: folders }], true)
.then(() => this.reload());
}
return Promise.resolve();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ export interface IJSONEditingService {

_serviceBrand: undefined;

write(resource: URI, value: IJSONValue, save: boolean): Promise<void>;
write(resource: URI, values: IJSONValue[], save: boolean): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,24 @@ export class JSONEditingService implements IJSONEditingService {
this.queue = new Queue<void>();
}

write(resource: URI, value: IJSONValue, save: boolean): Promise<void> {
return Promise.resolve(this.queue.queue(() => this.doWriteConfiguration(resource, value, save))); // queue up writes to prevent race conditions
write(resource: URI, values: IJSONValue[], save: boolean): Promise<void> {
return Promise.resolve(this.queue.queue(() => this.doWriteConfiguration(resource, values, save))); // queue up writes to prevent race conditions
}

private async doWriteConfiguration(resource: URI, value: IJSONValue, save: boolean): Promise<void> {
private async doWriteConfiguration(resource: URI, values: IJSONValue[], save: boolean): Promise<void> {
const reference = await this.resolveAndValidate(resource, save);
await this.writeToBuffer(reference.object.textEditorModel, value);
await this.writeToBuffer(reference.object.textEditorModel, values);

reference.dispose();
}

private async writeToBuffer(model: ITextModel, value: IJSONValue): Promise<any> {
const edit = this.getEdits(model, value)[0];
if (this.applyEditsToBuffer(edit, model)) {
private async writeToBuffer(model: ITextModel, values: IJSONValue[]): Promise<any> {
let hasEdits: boolean = false;
for (const value of values) {
const edit = this.getEdits(model, value)[0];
hasEdits = this.applyEditsToBuffer(edit, model);
}
if (hasEdits) {
return this.textFileService.save(model.uri);
}
}
Expand Down Expand Up @@ -133,4 +137,4 @@ export class JSONEditingService implements IJSONEditingService {
}
}

registerSingleton(IJSONEditingService, JSONEditingService, true);
registerSingleton(IJSONEditingService, JSONEditingService, true);
Original file line number Diff line number Diff line change
Expand Up @@ -1161,21 +1161,21 @@ suite('WorkspaceConfigurationService-Multiroot', () => {

test('application settings are not read from workspace', () => {
fs.writeFileSync(globalSettingsFile, '{ "configurationService.workspace.applicationSetting": "userValue" }');
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'settings', value: { 'configurationService.workspace.applicationSetting': 'workspaceValue' } }, true)
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, [{ key: 'settings', value: { 'configurationService.workspace.applicationSetting': 'workspaceValue' } }], true)
.then(() => testObject.reloadConfiguration())
.then(() => assert.equal(testObject.getValue('configurationService.workspace.applicationSetting'), 'userValue'));
});

test('machine settings are not read from workspace', () => {
fs.writeFileSync(globalSettingsFile, '{ "configurationService.workspace.machineSetting": "userValue" }');
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'settings', value: { 'configurationService.workspace.machineSetting': 'workspaceValue' } }, true)
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, [{ key: 'settings', value: { 'configurationService.workspace.machineSetting': 'workspaceValue' } }], true)
.then(() => testObject.reloadConfiguration())
.then(() => assert.equal(testObject.getValue('configurationService.workspace.machineSetting'), 'userValue'));
});

test('workspace settings override user settings after defaults are registered ', () => {
fs.writeFileSync(globalSettingsFile, '{ "configurationService.workspace.newSetting": "userValue" }');
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'settings', value: { 'configurationService.workspace.newSetting': 'workspaceValue' } }, true)
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, [{ key: 'settings', value: { 'configurationService.workspace.newSetting': 'workspaceValue' } }], true)
.then(() => testObject.reloadConfiguration())
.then(() => {
configurationRegistry.registerConfiguration({
Expand All @@ -1194,7 +1194,7 @@ suite('WorkspaceConfigurationService-Multiroot', () => {

test('workspace settings override user settings after defaults are registered for machine overridable settings ', () => {
fs.writeFileSync(globalSettingsFile, '{ "configurationService.workspace.newMachineOverridableSetting": "userValue" }');
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'settings', value: { 'configurationService.workspace.newMachineOverridableSetting': 'workspaceValue' } }, true)
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, [{ key: 'settings', value: { 'configurationService.workspace.newMachineOverridableSetting': 'workspaceValue' } }], true)
.then(() => testObject.reloadConfiguration())
.then(() => {
configurationRegistry.registerConfiguration({
Expand Down Expand Up @@ -1268,7 +1268,7 @@ suite('WorkspaceConfigurationService-Multiroot', () => {

test('resource setting in folder is read after it is registered later', () => {
fs.writeFileSync(workspaceContextService.getWorkspace().folders[0].toResource('.vscode/settings.json').fsPath, '{ "configurationService.workspace.testNewResourceSetting2": "workspaceFolderValue" }');
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'settings', value: { 'configurationService.workspace.testNewResourceSetting2': 'workspaceValue' } }, true)
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, [{ key: 'settings', value: { 'configurationService.workspace.testNewResourceSetting2': 'workspaceValue' } }], true)
.then(() => testObject.reloadConfiguration())
.then(() => {
configurationRegistry.registerConfiguration({
Expand All @@ -1288,7 +1288,7 @@ suite('WorkspaceConfigurationService-Multiroot', () => {

test('machine overridable setting in folder is read after it is registered later', () => {
fs.writeFileSync(workspaceContextService.getWorkspace().folders[0].toResource('.vscode/settings.json').fsPath, '{ "configurationService.workspace.testNewMachineOverridableSetting2": "workspaceFolderValue" }');
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'settings', value: { 'configurationService.workspace.testNewMachineOverridableSetting2': 'workspaceValue' } }, true)
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, [{ key: 'settings', value: { 'configurationService.workspace.testNewMachineOverridableSetting2': 'workspaceValue' } }], true)
.then(() => testObject.reloadConfiguration())
.then(() => {
configurationRegistry.registerConfiguration({
Expand Down Expand Up @@ -1331,7 +1331,7 @@ suite('WorkspaceConfigurationService-Multiroot', () => {
assert.equal(actual.workspaceFolder, undefined);
assert.equal(actual.value, 'userValue');

return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'settings', value: { 'configurationService.workspace.testResourceSetting': 'workspaceValue' } }, true)
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, [{ key: 'settings', value: { 'configurationService.workspace.testResourceSetting': 'workspaceValue' } }], true)
.then(() => testObject.reloadConfiguration())
.then(() => {
actual = testObject.inspect('configurationService.workspace.testResourceSetting');
Expand Down Expand Up @@ -1373,7 +1373,7 @@ suite('WorkspaceConfigurationService-Multiroot', () => {
}
]
};
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'launch', value: expectedLaunchConfiguration }, true)
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, [{ key: 'launch', value: expectedLaunchConfiguration }], true)
.then(() => testObject.reloadConfiguration())
.then(() => {
const actual = testObject.getValue('launch');
Expand All @@ -1398,7 +1398,7 @@ suite('WorkspaceConfigurationService-Multiroot', () => {
}
]
};
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'launch', value: expectedLaunchConfiguration }, true)
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, [{ key: 'launch', value: expectedLaunchConfiguration }], true)
.then(() => testObject.reloadConfiguration())
.then(() => {
const actual = testObject.inspect('launch').workspace;
Expand Down Expand Up @@ -1496,7 +1496,7 @@ suite('WorkspaceConfigurationService-Multiroot', () => {
});

test('task configurations are not read from workspace', () => {
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'tasks', value: { 'version': '1.0' } }, true)
return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, [{ key: 'tasks', value: { 'version': '1.0' } }], true)
.then(() => testObject.reloadConfiguration())
.then(() => {
const actual = testObject.inspect('tasks.version');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ export class PreferencesService extends Disposable implements IPreferencesServic
return this.textFileService.read(workspaceConfig)
.then(content => {
if (Object.keys(parse(content.value)).indexOf('settings') === -1) {
return this.jsonEditingService.write(resource, { key: 'settings', value: {} }, true).then(undefined, () => { });
return this.jsonEditingService.write(resource, [{ key: 'settings', value: {} }], true).then(undefined, () => { });
}
return undefined;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export class WorkspaceEditingService implements IWorkspaceEditingService {
}
}

return this.jsonEditingService.write(toWorkspace.configPath, { key: 'settings', value: targetWorkspaceConfiguration }, true);
return this.jsonEditingService.write(toWorkspace.configPath, [{ key: 'settings', value: targetWorkspaceConfiguration }], true);
}

private getCurrentWorkspaceIdentifier(): IWorkspaceIdentifier | undefined {
Expand Down

0 comments on commit 5bca175

Please sign in to comment.