Skip to content

Commit

Permalink
Merge pull request #126577 from microsoft/sandy081/recovery/fix125970
Browse files Browse the repository at this point in the history
Improve migrating old editorAssociations setting format
  • Loading branch information
sandy081 authored Jun 17, 2021
2 parents 08bca48 + 3f4baf4 commit 507ce72
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions src/vs/workbench/services/editor/browser/editorOverrideService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { IHostService } from 'vs/workbench/services/host/browser/host';
import { ILogService } from 'vs/platform/log/common/log';

interface IContributedEditorInput extends IEditorInput {
viewType?: string;
Expand Down Expand Up @@ -56,7 +58,9 @@ export class EditorOverrideService extends Disposable implements IEditorOverride
@INotificationService private readonly notificationService: INotificationService,
@ITelemetryService private readonly telemetryService: ITelemetryService,
@IStorageService private readonly storageService: IStorageService,
@IExtensionService private readonly extensionService: IExtensionService
@IExtensionService private readonly extensionService: IExtensionService,
@IHostService private readonly hostService: IHostService,
@ILogService private readonly logService: ILogService,
) {
super();
// Read in the cache on statup
Expand All @@ -75,8 +79,10 @@ export class EditorOverrideService extends Disposable implements IEditorOverride
});

// When the setting changes we want to ensure that it is properly converted
this._register(this.configurationService.onDidChangeConfiguration(() => {
this.convertOldAssociationFormat();
this._register(this.configurationService.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration(editorsAssociationsSettingId)) {
this.convertOldAssociationFormat();
}
}));
}

Expand Down Expand Up @@ -177,23 +183,42 @@ export class EditorOverrideService extends Disposable implements IEditorOverride
}

private convertOldAssociationFormat(): void {
const rawAssociations = this.configurationService.getValue<EditorAssociations | { [fileNamePattern: string]: string }>(editorsAssociationsSettingId) || [];
// If it's not an array, then it's the new format
if (!Array.isArray(rawAssociations)) {
return;
}
let newSettingObject = Object.create(null);
// Make the correctly formatted object from the array and then set that object
for (const association of rawAssociations) {
if (association.filenamePattern) {
newSettingObject[association.filenamePattern] = association.viewType;
this.hostService.hadLastFocus().then(hadLastFocus => {
if (!hadLastFocus) {
return;
}
}
this.configurationService.updateValue(editorsAssociationsSettingId, newSettingObject);
const rawAssociations = this.configurationService.getValue<EditorAssociations | { [fileNamePattern: string]: string }>(editorsAssociationsSettingId) || {};
// If it's not an array, then it's the new format
if (!Array.isArray(rawAssociations)) {
return;
}
let newSettingObject = Object.create(null);
// Make the correctly formatted object from the array and then set that object
for (const association of rawAssociations) {
if (association.filenamePattern) {
newSettingObject[association.filenamePattern] = association.viewType;
}
}
this.logService.info(`Migrating ${editorsAssociationsSettingId}`);
this.configurationService.updateValue(editorsAssociationsSettingId, newSettingObject);
});
}

private getAllUserAssociations(): EditorAssociations {
const rawAssociations = this.configurationService.getValue<{ [fileNamePattern: string]: string }>(editorsAssociationsSettingId) || [];
let rawAssociations = this.configurationService.getValue<EditorAssociations | { [fileNamePattern: string]: string }>(editorsAssociationsSettingId) || {};

// If it's an array then it is old format
if (Array.isArray(rawAssociations)) {
// Make the correctly formatted object
const newValue = Object.create(null);
for (const association of rawAssociations) {
if (association.filenamePattern) {
newValue[association.filenamePattern] = association.viewType;
}
}
rawAssociations = newValue;
}

let associations = [];
for (const [key, value] of Object.entries(rawAssociations)) {
const association: EditorAssociation = {
Expand Down

1 comment on commit 507ce72

@Habash1990
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.