Skip to content

Commit 507ce72

Browse files
authored
Merge pull request microsoft#126577 from microsoft/sandy081/recovery/fix125970
Improve migrating old editorAssociations setting format
2 parents 08bca48 + 3f4baf4 commit 507ce72

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

src/vs/workbench/services/editor/browser/editorOverrideService.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
2222
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
2323
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
2424
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
25+
import { IHostService } from 'vs/workbench/services/host/browser/host';
26+
import { ILogService } from 'vs/platform/log/common/log';
2527

2628
interface IContributedEditorInput extends IEditorInput {
2729
viewType?: string;
@@ -56,7 +58,9 @@ export class EditorOverrideService extends Disposable implements IEditorOverride
5658
@INotificationService private readonly notificationService: INotificationService,
5759
@ITelemetryService private readonly telemetryService: ITelemetryService,
5860
@IStorageService private readonly storageService: IStorageService,
59-
@IExtensionService private readonly extensionService: IExtensionService
61+
@IExtensionService private readonly extensionService: IExtensionService,
62+
@IHostService private readonly hostService: IHostService,
63+
@ILogService private readonly logService: ILogService,
6064
) {
6165
super();
6266
// Read in the cache on statup
@@ -75,8 +79,10 @@ export class EditorOverrideService extends Disposable implements IEditorOverride
7579
});
7680

7781
// When the setting changes we want to ensure that it is properly converted
78-
this._register(this.configurationService.onDidChangeConfiguration(() => {
79-
this.convertOldAssociationFormat();
82+
this._register(this.configurationService.onDidChangeConfiguration((e) => {
83+
if (e.affectsConfiguration(editorsAssociationsSettingId)) {
84+
this.convertOldAssociationFormat();
85+
}
8086
}));
8187
}
8288

@@ -177,23 +183,42 @@ export class EditorOverrideService extends Disposable implements IEditorOverride
177183
}
178184

179185
private convertOldAssociationFormat(): void {
180-
const rawAssociations = this.configurationService.getValue<EditorAssociations | { [fileNamePattern: string]: string }>(editorsAssociationsSettingId) || [];
181-
// If it's not an array, then it's the new format
182-
if (!Array.isArray(rawAssociations)) {
183-
return;
184-
}
185-
let newSettingObject = Object.create(null);
186-
// Make the correctly formatted object from the array and then set that object
187-
for (const association of rawAssociations) {
188-
if (association.filenamePattern) {
189-
newSettingObject[association.filenamePattern] = association.viewType;
186+
this.hostService.hadLastFocus().then(hadLastFocus => {
187+
if (!hadLastFocus) {
188+
return;
190189
}
191-
}
192-
this.configurationService.updateValue(editorsAssociationsSettingId, newSettingObject);
190+
const rawAssociations = this.configurationService.getValue<EditorAssociations | { [fileNamePattern: string]: string }>(editorsAssociationsSettingId) || {};
191+
// If it's not an array, then it's the new format
192+
if (!Array.isArray(rawAssociations)) {
193+
return;
194+
}
195+
let newSettingObject = Object.create(null);
196+
// Make the correctly formatted object from the array and then set that object
197+
for (const association of rawAssociations) {
198+
if (association.filenamePattern) {
199+
newSettingObject[association.filenamePattern] = association.viewType;
200+
}
201+
}
202+
this.logService.info(`Migrating ${editorsAssociationsSettingId}`);
203+
this.configurationService.updateValue(editorsAssociationsSettingId, newSettingObject);
204+
});
193205
}
194206

195207
private getAllUserAssociations(): EditorAssociations {
196-
const rawAssociations = this.configurationService.getValue<{ [fileNamePattern: string]: string }>(editorsAssociationsSettingId) || [];
208+
let rawAssociations = this.configurationService.getValue<EditorAssociations | { [fileNamePattern: string]: string }>(editorsAssociationsSettingId) || {};
209+
210+
// If it's an array then it is old format
211+
if (Array.isArray(rawAssociations)) {
212+
// Make the correctly formatted object
213+
const newValue = Object.create(null);
214+
for (const association of rawAssociations) {
215+
if (association.filenamePattern) {
216+
newValue[association.filenamePattern] = association.viewType;
217+
}
218+
}
219+
rawAssociations = newValue;
220+
}
221+
197222
let associations = [];
198223
for (const [key, value] of Object.entries(rawAssociations)) {
199224
const association: EditorAssociation = {

0 commit comments

Comments
 (0)