Fix SQL/Notebook editors opening as plaintext (#16442) (#16456)

* Register overrides at startup

* Always wait for extensions

* Fix compile errors

(cherry picked from commit 2d8e0d648a)
This commit is contained in:
Charles Gagnon
2021-07-28 09:07:43 -07:00
committed by GitHub
parent ce93729d5c
commit 65fb22cc7c
3 changed files with 85 additions and 80 deletions

View File

@@ -677,47 +677,48 @@ export class NotebookEditorOverrideContribution extends Disposable implements IW
@IModeService private _modeService: IModeService @IModeService private _modeService: IModeService
) { ) {
super(); super();
this.registerEditorOverride(); this.registerEditorOverrides();
}
private registerEditorOverride(): void {
// Refresh the editor overrides whenever the languages change so we ensure we always have // Refresh the editor overrides whenever the languages change so we ensure we always have
// the latest up to date list of extensions for each language // the latest up to date list of extensions for each language
this._modeService.onLanguagesMaybeChanged(() => { this._modeService.onLanguagesMaybeChanged(() => {
this._registeredOverrides.clear(); this.registerEditorOverrides();
// List of language IDs to associate the query editor for. These are case sensitive. });
NotebookEditorInputAssociation.languages.map(lang => { }
const langExtensions = this._modeService.getExtensions(lang);
if (langExtensions.length === 0) { private registerEditorOverrides(): void {
return; this._registeredOverrides.clear();
// List of language IDs to associate the query editor for. These are case sensitive.
NotebookEditorInputAssociation.languages.map(lang => {
const langExtensions = this._modeService.getExtensions(lang);
if (langExtensions.length === 0) {
return;
}
// Create the selector from the list of all the language extensions we want to associate with the
// notebook editor (filtering out any languages which didn't have any extensions registered yet)
const selector = `*{${langExtensions.join(',')}}`;
this._registeredOverrides.add(this._editorOverrideService.registerContributionPoint(
selector,
{
id: NotebookEditor.ID,
label: NotebookEditor.LABEL,
describes: (currentEditor) => currentEditor instanceof FileNotebookInput,
priority: ContributedEditorPriority.builtin
},
{},
(resource, options, group) => {
const fileInput = this._editorService.createEditorInput({
resource: resource
}) as FileEditorInput;
// Try to convert the input, falling back to just a plain file input if we're unable to
const newInput = this.tryConvertInput(fileInput, lang) ?? fileInput;
return { editor: newInput, options: options, group: group };
},
(diffEditorInput, options, group) => {
// Try to convert the input, falling back to the original input if we're unable to
const newInput = this.tryConvertInput(diffEditorInput, lang) ?? diffEditorInput;
return { editor: newInput, options: options, group: group };
} }
// Create the selector from the list of all the language extensions we want to associate with the ));
// notebook editor (filtering out any languages which didn't have any extensions registered yet)
const selector = `*{${langExtensions.join(',')}}`;
this._registeredOverrides.add(this._editorOverrideService.registerContributionPoint(
selector,
{
id: NotebookEditor.ID,
label: NotebookEditor.LABEL,
describes: (currentEditor) => currentEditor instanceof FileNotebookInput,
priority: ContributedEditorPriority.builtin
},
{},
(resource, options, group) => {
const fileInput = this._editorService.createEditorInput({
resource: resource
}) as FileEditorInput;
// Try to convert the input, falling back to just a plain file input if we're unable to
const newInput = this.tryConvertInput(fileInput, lang) ?? fileInput;
return { editor: newInput, options: options, group: group };
},
(diffEditorInput, options, group) => {
// Try to convert the input, falling back to the original input if we're unable to
const newInput = this.tryConvertInput(diffEditorInput, lang) ?? diffEditorInput;
return { editor: newInput, options: options, group: group };
}
));
});
}); });
} }
@@ -733,4 +734,4 @@ export class NotebookEditorOverrideContribution extends Disposable implements IW
} }
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench) Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench)
.registerWorkbenchContribution(NotebookEditorOverrideContribution, LifecyclePhase.Restored); .registerWorkbenchContribution(NotebookEditorOverrideContribution, LifecyclePhase.Starting);

View File

@@ -510,47 +510,47 @@ export class QueryEditorOverrideContribution extends Disposable implements IWork
) { ) {
super(); super();
this.registerEditorOverrides(); this.registerEditorOverrides();
}
private registerEditorOverrides(): void {
// Refresh the editor overrides whenever the languages change so we ensure we always have // Refresh the editor overrides whenever the languages change so we ensure we always have
// the latest up to date list of extensions for each language // the latest up to date list of extensions for each language
this._modeService.onLanguagesMaybeChanged(() => { this._modeService.onLanguagesMaybeChanged(() => {
this._registeredOverrides.clear(); this.registerEditorOverrides();
// List of language IDs to associate the query editor for. These are case sensitive. });
QueryEditorLanguageAssociation.languages.map(lang => { }
const langExtensions = this._modeService.getExtensions(lang);
if (langExtensions.length === 0) { private registerEditorOverrides(): void {
return; this._registeredOverrides.clear();
} // List of language IDs to associate the query editor for. These are case sensitive.
// Create the selector from the list of all the language extensions we want to associate with the QueryEditorLanguageAssociation.languages.map(lang => {
// query editor (filtering out any languages which didn't have any extensions registered yet) const langExtensions = this._modeService.getExtensions(lang);
const selector = `*{${langExtensions.join(',')}}`; if (langExtensions.length === 0) {
this._registeredOverrides.add(this._editorOverrideService.registerContributionPoint( return;
selector, }
{ // Create the selector from the list of all the language extensions we want to associate with the
id: QueryEditor.ID, // query editor (filtering out any languages which didn't have any extensions registered yet)
label: QueryEditor.LABEL, const selector = `*{${langExtensions.join(',')}}`;
describes: (currentEditor) => currentEditor instanceof FileQueryEditorInput, this._registeredOverrides.add(this._editorOverrideService.registerContributionPoint(
priority: ContributedEditorPriority.builtin selector,
}, {
{}, id: QueryEditor.ID,
(resource, options, group) => { label: QueryEditor.LABEL,
const fileInput = this._editorService.createEditorInput({ describes: (currentEditor) => currentEditor instanceof FileQueryEditorInput,
resource: resource priority: ContributedEditorPriority.builtin
}) as FileEditorInput; },
const langAssociation = languageAssociationRegistry.getAssociationForLanguage(lang); {},
const queryEditorInput = langAssociation?.syncConvertinput?.(fileInput); (resource, options, group) => {
if (!queryEditorInput) { const fileInput = this._editorService.createEditorInput({
this._logService.warn('Unable to create input for overriding editor ', resource); resource: resource
return undefined; }) as FileEditorInput;
} const langAssociation = languageAssociationRegistry.getAssociationForLanguage(lang);
return { editor: queryEditorInput, options: options, group: group }; const queryEditorInput = langAssociation?.syncConvertinput?.(fileInput);
if (!queryEditorInput) {
this._logService.warn('Unable to create input for overriding editor ', resource);
return undefined;
} }
)); return { editor: queryEditorInput, options: options, group: group };
}); }
));
}); });
} }
} }
workbenchRegistry.registerWorkbenchContribution(QueryEditorOverrideContribution, LifecyclePhase.Starting);
workbenchRegistry.registerWorkbenchContribution(QueryEditorOverrideContribution, LifecyclePhase.Restored);

View File

@@ -43,7 +43,7 @@ export class EditorOverrideService extends Disposable implements IEditorOverride
private _contributionPoints: Map<string | glob.IRelativePattern, ContributionPoints> = new Map<string | glob.IRelativePattern, ContributionPoints>(); private _contributionPoints: Map<string | glob.IRelativePattern, ContributionPoints> = new Map<string | glob.IRelativePattern, ContributionPoints>();
private static readonly overrideCacheStorageID = 'editorOverrideService.cache'; private static readonly overrideCacheStorageID = 'editorOverrideService.cache';
private cache: Set<string> | undefined; // private cache: Set<string> | undefined; {{SQL CARBON EDIT}} Remove unused
constructor( constructor(
@IEditorGroupsService private readonly editorGroupService: IEditorGroupsService, @IEditorGroupsService private readonly editorGroupService: IEditorGroupsService,
@@ -56,7 +56,7 @@ export class EditorOverrideService extends Disposable implements IEditorOverride
) { ) {
super(); super();
// Read in the cache on statup // Read in the cache on statup
this.cache = new Set<string>(JSON.parse(this.storageService.get(EditorOverrideService.overrideCacheStorageID, StorageScope.GLOBAL, JSON.stringify([])))); // this.cache = new Set<string>(JSON.parse(this.storageService.get(EditorOverrideService.overrideCacheStorageID, StorageScope.GLOBAL, JSON.stringify([])))); {{SQL CARBON EDIT}} Remove unused
this.storageService.remove(EditorOverrideService.overrideCacheStorageID, StorageScope.GLOBAL); this.storageService.remove(EditorOverrideService.overrideCacheStorageID, StorageScope.GLOBAL);
this._register(this.storageService.onWillSaveState(() => { this._register(this.storageService.onWillSaveState(() => {
@@ -65,16 +65,18 @@ export class EditorOverrideService extends Disposable implements IEditorOverride
})); }));
// When extensions have registered we no longer need the cache // When extensions have registered we no longer need the cache
/* {{SQL CARBON EDIT}} Remove unused
this.extensionService.onDidRegisterExtensions(() => { this.extensionService.onDidRegisterExtensions(() => {
this.cache = undefined; this.cache = undefined;
}); });
*/
} }
async resolveEditorOverride(editor: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup): Promise<IEditorInputWithOptionsAndGroup | undefined> { async resolveEditorOverride(editor: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup): Promise<IEditorInputWithOptionsAndGroup | undefined> {
// If it was an override before we await for the extensions to activate and then proceed with overriding or else they won't be registered // If it was an override before we await for the extensions to activate and then proceed with overriding or else they won't be registered
if (this.cache && editor.resource && this.resourceMatchesCache(editor.resource)) { //if (this.cache && editor.resource && this.resourceMatchesCache(editor.resource)) { // {{SQL CARBON EDIT}} Always wait for extensions so that our language-based overrides (SQL/Notebooks) will always have those registered
await this.extensionService.whenInstalledExtensionsRegistered(); await this.extensionService.whenInstalledExtensionsRegistered();
} //}
if (options?.override === EditorOverride.DISABLED) { if (options?.override === EditorOverride.DISABLED) {
throw new Error(`Calling resolve editor override when override is explicitly disabled!`); throw new Error(`Calling resolve editor override when override is explicitly disabled!`);
@@ -547,6 +549,7 @@ export class EditorOverrideService extends Disposable implements IEditorOverride
this.storageService.store(EditorOverrideService.overrideCacheStorageID, JSON.stringify(Array.from(cacheStorage)), StorageScope.GLOBAL, StorageTarget.MACHINE); this.storageService.store(EditorOverrideService.overrideCacheStorageID, JSON.stringify(Array.from(cacheStorage)), StorageScope.GLOBAL, StorageTarget.MACHINE);
} }
/* {{SQL CARBON EDIT}} Remove unused
private resourceMatchesCache(resource: URI): boolean { private resourceMatchesCache(resource: URI): boolean {
if (!this.cache) { if (!this.cache) {
return false; return false;
@@ -559,6 +562,7 @@ export class EditorOverrideService extends Disposable implements IEditorOverride
} }
return false; return false;
} }
*/
} }
registerSingleton(IEditorOverrideService, EditorOverrideService); registerSingleton(IEditorOverrideService, EditorOverrideService);