Skip to content

Commit

Permalink
Fix #86077
Browse files Browse the repository at this point in the history
  • Loading branch information
sandy081 committed Nov 10, 2020
1 parent d26f927 commit ab6a005
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/vs/platform/configuration/common/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export interface IConfigurationService {

inspect<T>(key: string, overrides?: IConfigurationOverrides): IConfigurationValue<T>;

reloadConfiguration(folder?: IWorkspaceFolder): Promise<void>;
reloadConfiguration(target?: ConfigurationTarget | IWorkspaceFolder): Promise<void>;

keys(): {
default: string[];
Expand Down
39 changes: 25 additions & 14 deletions src/vs/platform/userDataSync/common/keybindingsSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { URI } from 'vs/base/common/uri';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { VSBuffer } from 'vs/base/common/buffer';
import { Event } from 'vs/base/common/event';

interface ISyncContent {
mac?: string;
Expand All @@ -35,6 +36,10 @@ interface IKeybindingsResourcePreview extends IFileResourcePreview {
previewResult: IMergeResult;
}

interface ILastSyncUserData extends IRemoteUserData {
platformSpecific?: boolean;
}

export function getKeybindingsContentFromSyncContent(syncContent: string, platformSpecific: boolean): string | null {
const parsed = <ISyncContent>JSON.parse(syncContent);
if (!platformSpecific) {
Expand Down Expand Up @@ -72,11 +77,12 @@ export class KeybindingsSynchroniser extends AbstractJsonFileSynchroniser implem
@ITelemetryService telemetryService: ITelemetryService,
) {
super(environmentService.keybindingsResource, SyncResource.Keybindings, fileService, environmentService, storageService, userDataSyncStoreService, userDataSyncBackupStoreService, userDataSyncResourceEnablementService, telemetryService, logService, userDataSyncUtilService, configurationService);
this._register(Event.filter(configurationService.onDidChangeConfiguration, e => e.affectsConfiguration('settingsSync.keybindingsPerPlatform'))(() => this.triggerLocalChange()));
}

protected async generateSyncPreview(remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null, token: CancellationToken): Promise<IKeybindingsResourcePreview[]> {
protected async generateSyncPreview(remoteUserData: IRemoteUserData, lastSyncUserData: ILastSyncUserData | null, token: CancellationToken): Promise<IKeybindingsResourcePreview[]> {
const remoteContent = remoteUserData.syncData ? this.getKeybindingsContentFromSyncContent(remoteUserData.syncData.content) : null;
const lastSyncContent: string | null = lastSyncUserData && lastSyncUserData.syncData ? this.getKeybindingsContentFromSyncContent(lastSyncUserData.syncData.content) : null;
const lastSyncContent: string | null = lastSyncUserData ? this.getKeybindingsContentFromLastSyncUserData(lastSyncUserData) : null;

// Get file content last to get the latest
const fileContent = await this.getLocalFileContent();
Expand Down Expand Up @@ -204,15 +210,15 @@ export class KeybindingsSynchroniser extends AbstractJsonFileSynchroniser implem
if (localChange !== Change.None) {
this.logService.trace(`${this.syncResourceLogLabel}: Updating local keybindings...`);
if (fileContent) {
await this.backupLocal(this.toSyncContent(fileContent.value.toString(), null));
await this.backupLocal(this.toSyncContent(fileContent.value.toString()));
}
await this.updateLocalFileContent(content || '[]', fileContent, force);
this.logService.info(`${this.syncResourceLogLabel}: Updated local keybindings`);
}

if (remoteChange !== Change.None) {
this.logService.trace(`${this.syncResourceLogLabel}: Updating remote keybindings...`);
const remoteContents = this.toSyncContent(content || '[]', remoteUserData.syncData ? remoteUserData.syncData.content : null);
const remoteContents = this.toSyncContent(content || '[]', remoteUserData.syncData?.content);
remoteUserData = await this.updateRemoteUserData(remoteContents, force ? null : remoteUserData.ref);
this.logService.info(`${this.syncResourceLogLabel}: Updated remote keybindings`);
}
Expand All @@ -224,15 +230,7 @@ export class KeybindingsSynchroniser extends AbstractJsonFileSynchroniser implem

if (lastSyncUserData?.ref !== remoteUserData.ref) {
this.logService.trace(`${this.syncResourceLogLabel}: Updating last synchronized keybindings...`);
const lastSyncContent = content !== null ? this.toSyncContent(content, null) : remoteUserData.syncData?.content;
await this.updateLastSyncUserData({
ref: remoteUserData.ref,
syncData: lastSyncContent ? {
version: remoteUserData.syncData ? remoteUserData.syncData.version : this.version,
machineId: remoteUserData.syncData!.machineId,
content: lastSyncContent
} : null
});
await this.updateLastSyncUserData(remoteUserData, { platformSpecific: this.syncKeybindingsPerPlatform() });
this.logService.info(`${this.syncResourceLogLabel}: Updated last synchronized keybindings`);
}

Expand Down Expand Up @@ -281,6 +279,19 @@ export class KeybindingsSynchroniser extends AbstractJsonFileSynchroniser implem
return null;
}

private getKeybindingsContentFromLastSyncUserData(lastSyncUserData: ILastSyncUserData): string | null {
if (!lastSyncUserData.syncData) {
return null;
}

// Return null if there is a change in platform specific property from last time sync.
if (lastSyncUserData.platformSpecific !== undefined && lastSyncUserData.platformSpecific !== this.syncKeybindingsPerPlatform()) {
return null;
}

return this.getKeybindingsContentFromSyncContent(lastSyncUserData.syncData.content);
}

private getKeybindingsContentFromSyncContent(syncContent: string): string | null {
try {
return getKeybindingsContentFromSyncContent(syncContent, this.syncKeybindingsPerPlatform());
Expand All @@ -290,7 +301,7 @@ export class KeybindingsSynchroniser extends AbstractJsonFileSynchroniser implem
}
}

private toSyncContent(keybindingsContent: string, syncContent: string | null): string {
private toSyncContent(keybindingsContent: string, syncContent?: string): string {
let parsed: ISyncContent = {};
try {
parsed = JSON.parse(syncContent || '{}');
Expand Down
3 changes: 2 additions & 1 deletion src/vs/platform/userDataSync/common/settingsSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { VSBuffer } from 'vs/base/common/buffer';
import { localize } from 'vs/nls';
import { Event } from 'vs/base/common/event';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ConfigurationTarget, IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { CancellationToken } from 'vs/base/common/cancellation';
import { updateIgnoredSettings, merge, getIgnoredSettings, isEmpty } from 'vs/platform/userDataSync/common/settingsMerge';
import { edit } from 'vs/platform/userDataSync/common/content';
Expand Down Expand Up @@ -198,6 +198,7 @@ export class SettingsSynchroniser extends AbstractJsonFileSynchroniser implement
await this.backupLocal(JSON.stringify(this.toSettingsSyncContent(fileContent.value.toString())));
}
await this.updateLocalFileContent(content, fileContent, force);
await this.configurationService.reloadConfiguration(ConfigurationTarget.USER_LOCAL);
this.logService.info(`${this.syncResourceLogLabel}: Updated local settings`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,38 @@ export class WorkspaceService extends Disposable implements IConfigurationServic
});
}

reloadConfiguration(folder?: IWorkspaceFolder, key?: string): Promise<void> {
if (folder) {
return this.reloadWorkspaceFolderConfiguration(folder, key);
async reloadConfiguration(target?: ConfigurationTarget | IWorkspaceFolder): Promise<void> {
if (target === undefined) {
const { local, remote } = await this.reloadUserConfiguration();
await this.reloadWorkspaceConfiguration();
await this.loadConfiguration(local, remote);
return;
}

if (IWorkspaceFolder.isIWorkspaceFolder(target)) {
await this.reloadWorkspaceFolderConfiguration(target);
return;
}

switch (target) {
case ConfigurationTarget.USER:
const { local, remote } = await this.reloadUserConfiguration();
await this.loadConfiguration(local, remote);
return;

case ConfigurationTarget.USER_LOCAL:
await this.reloadLocalUserConfiguration();
return;

case ConfigurationTarget.USER_REMOTE:
await this.reloadRemoteUserConfiguration();
return;

case ConfigurationTarget.WORKSPACE:
case ConfigurationTarget.WORKSPACE_FOLDER:
await this.reloadWorkspaceConfiguration();
return;
}
return this.reloadUserConfiguration()
.then(({ local, remote }) => this.reloadWorkspaceConfiguration()
.then(() => this.loadConfiguration(local, remote)));
}

inspect<T>(key: string, overrides?: IConfigurationOverrides): IConfigurationValue<T> {
Expand Down Expand Up @@ -465,19 +490,19 @@ export class WorkspaceService extends Disposable implements IConfigurationServic
return new ConfigurationModel();
}

private reloadWorkspaceConfiguration(key?: string): Promise<void> {
private reloadWorkspaceConfiguration(): Promise<void> {
const workbenchState = this.getWorkbenchState();
if (workbenchState === WorkbenchState.FOLDER) {
return this.onWorkspaceFolderConfigurationChanged(this.workspace.folders[0], key);
return this.onWorkspaceFolderConfigurationChanged(this.workspace.folders[0]);
}
if (workbenchState === WorkbenchState.WORKSPACE) {
return this.workspaceConfiguration.reload().then(() => this.onWorkspaceConfigurationChanged());
}
return Promise.resolve(undefined);
}

private reloadWorkspaceFolderConfiguration(folder: IWorkspaceFolder, key?: string): Promise<void> {
return this.onWorkspaceFolderConfigurationChanged(folder, key);
private reloadWorkspaceFolderConfiguration(folder: IWorkspaceFolder): Promise<void> {
return this.onWorkspaceFolderConfigurationChanged(folder);
}

private loadConfiguration(userConfigurationModel: ConfigurationModel, remoteUserConfigurationModel: ConfigurationModel): Promise<void> {
Expand Down Expand Up @@ -592,7 +617,7 @@ export class WorkspaceService extends Disposable implements IConfigurationServic
}
}

private onWorkspaceFolderConfigurationChanged(folder: IWorkspaceFolder, key?: string): Promise<void> {
private onWorkspaceFolderConfigurationChanged(folder: IWorkspaceFolder): Promise<void> {
return this.loadFolderConfigurations([folder])
.then(([folderConfiguration]) => {
const previous = { data: this._configuration.toData(), workspace: this.workspace };
Expand Down Expand Up @@ -700,7 +725,7 @@ export class WorkspaceService extends Disposable implements IConfigurationServic
case EditableConfigurationTarget.WORKSPACE_FOLDER:
const workspaceFolder = overrides && overrides.resource ? this.workspace.getFolder(overrides.resource) : null;
if (workspaceFolder) {
return this.reloadWorkspaceFolderConfiguration(workspaceFolder, key);
return this.reloadWorkspaceFolderConfiguration(workspaceFolder);
}
}
return Promise.resolve();
Expand Down

0 comments on commit ab6a005

Please sign in to comment.