From d08fb1aee2449709f38e94835fb92d94ae15495c Mon Sep 17 00:00:00 2001 From: Anthony Dresser Date: Wed, 27 Nov 2019 12:48:54 -0800 Subject: [PATCH] fix upgrade path for new serialization model (#8489) --- .../workbench/common/languageAssociation.ts | 30 +++++++++++++++---- src/vs/workbench/common/editor/editorGroup.ts | 3 +- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/sql/workbench/common/languageAssociation.ts b/src/sql/workbench/common/languageAssociation.ts index b9adf3b39f..38fa140942 100644 --- a/src/sql/workbench/common/languageAssociation.ts +++ b/src/sql/workbench/common/languageAssociation.ts @@ -4,10 +4,14 @@ *--------------------------------------------------------------------------------------------*/ import { Registry } from 'vs/platform/registry/common/platform'; -import { IEditorInput } from 'vs/workbench/common/editor'; -import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; +import { IEditorInput, EditorInput } from 'vs/workbench/common/editor'; +import { ServicesAccessor, IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; +import { find } from 'vs/base/common/arrays'; +import { UntitledEditorInput } from 'vs/workbench/common/editor/untitledEditorInput'; +import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput'; +import { getCodeEditor } from 'vs/editor/browser/editorBrowser'; -export type InputCreator = (servicesAccessor: ServicesAccessor, activeEditor: IEditorInput) => IEditorInput | undefined; +export type InputCreator = (servicesAccessor: ServicesAccessor, activeEditor: IEditorInput) => EditorInput | undefined; export type BaseInputCreator = (activeEditor: IEditorInput) => IEditorInput; export interface ILanguageAssociationRegistry { @@ -15,7 +19,7 @@ export interface ILanguageAssociationRegistry { getAssociations(): Array<{ language: string, creator: InputCreator, baseInputCreator: BaseInputCreator, isDefault: boolean }>; } -class LanguageAssociationRegistry implements ILanguageAssociationRegistry { +const languageAssociationRegistery = new class implements ILanguageAssociationRegistry { private associations = new Array<{ language: string, creator: InputCreator, baseInputCreator: BaseInputCreator, isDefault: boolean }>(); registerLanguageAssociation(language: string, creator: InputCreator, baseInputCreator: BaseInputCreator, isDefault: boolean = false): void { @@ -25,10 +29,24 @@ class LanguageAssociationRegistry implements ILanguageAssociationRegistry { getAssociations(): Array<{ language: string, creator: InputCreator, baseInputCreator: BaseInputCreator, isDefault: boolean }> { return this.associations.slice(); } -} +}; export const Extensions = { LanguageAssociations: 'workbench.contributions.editor.languageAssociation' }; -Registry.add(Extensions.LanguageAssociations, new LanguageAssociationRegistry()); \ No newline at end of file +Registry.add(Extensions.LanguageAssociations, languageAssociationRegistery); + +export function doHandleUpgrade(accessor: ServicesAccessor, editor: EditorInput): EditorInput { + if (editor instanceof UntitledEditorInput || editor instanceof FileEditorInput) { + const instantiationService = accessor.get(IInstantiationService); + const activeWidget = getCodeEditor(editor); + const textModel = activeWidget.getModel(); + const oldLanguage = textModel.getLanguageIdentifier().language; + const association = find(languageAssociationRegistery.getAssociations(), l => l.language === oldLanguage); + if (association) { + return instantiationService.invokeFunction(association.creator, editor); + } + } + return editor; +} diff --git a/src/vs/workbench/common/editor/editorGroup.ts b/src/vs/workbench/common/editor/editorGroup.ts index 58199130e6..07815ffaaa 100644 --- a/src/vs/workbench/common/editor/editorGroup.ts +++ b/src/vs/workbench/common/editor/editorGroup.ts @@ -17,6 +17,7 @@ import { coalesce, firstIndex } from 'vs/base/common/arrays'; import { UntitledEditorInput } from 'vs/workbench/common/editor/untitledEditorInput'; import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput'; import { QueryEditorInput } from 'sql/workbench/contrib/query/common/queryEditorInput'; +import { doHandleUpgrade } from 'sql/workbench/common/languageAssociation'; const EditorOpenPositioning = { LEFT: 'left', @@ -688,7 +689,7 @@ export class EditorGroup extends Disposable { this.editors = coalesce(data.editors.map(e => { const factory = registry.getEditorInputFactory(e.id); if (factory) { - const editor = factory.deserialize(this.instantiationService, e.value); + const editor = this.instantiationService.invokeFunction(doHandleUpgrade, factory.deserialize(this.instantiationService, e.value)); // {{SQL CARBON EDIT}} handle upgrade path to new serialization if (editor) { this.registerEditorListeners(editor); this.updateResourceMap(editor, false /* add */);