Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2 Fixes/12231.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Infinite loop of asking to reload the extension when enabling custom editor.
32 changes: 25 additions & 7 deletions src/client/common/application/customEditorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ export class CustomEditorService implements ICustomEditorService {
@inject(IApplicationEnvironment) private readonly appEnvironment: IApplicationEnvironment,
@inject(IFileSystem) private readonly fileSystem: IFileSystem
) {
// Double check the package json has the necessary entries for contributing a custom editor
if (this.useCustomEditorApi && !appEnvironment.packageJson.contributes?.customEditors) {
this.rewritePackageJson().catch((e) => traceError(`Error rewriting package json: `, e));
}
this.verifyPackageJson().catch((e) => traceError(`Error rewriting package json: `, e));
}

public registerCustomEditorProvider(
Expand All @@ -48,12 +45,23 @@ export class CustomEditorService implements ICustomEditorService {
}
}

private async rewritePackageJson() {
private async verifyPackageJson(): Promise<void> {
// Double check the package json has the necessary entries for contributing a custom editor. Note
// we have to actually read it because appEnvironment.packageJson is the webpacked version
const packageJson = JSON.parse(await this.fileSystem.readFile(path.join(EXTENSION_ROOT_DIR, 'package.json')));
if (this.useCustomEditorApi && !packageJson.contributes?.customEditors) {
return this.addCustomEditors(packageJson);
} else if (!this.useCustomEditorApi && packageJson.contributes.customEditors) {
return this.removeCustomEditors();
}
}

// tslint:disable-next-line: no-any
private async addCustomEditors(currentPackageJson: any) {
// tslint:disable-next-line:no-require-imports no-var-requires
const _mergeWith = require('lodash/mergeWith') as typeof import('lodash/mergeWith');
const current = this.appEnvironment.packageJson;
const improvedContents = await this.fileSystem.readFile(path.join(EXTENSION_ROOT_DIR, 'customEditor.json'));
const improved = _mergeWith({ ...current }, JSON.parse(improvedContents), (l, r) => {
const improved = _mergeWith({ ...currentPackageJson }, JSON.parse(improvedContents), (l, r) => {
if (Array.isArray(l) && Array.isArray(r)) {
return [...l, ...r];
}
Expand All @@ -64,4 +72,14 @@ export class CustomEditorService implements ICustomEditorService {
);
this.commandManager.executeCommand('python.reloadVSCode', DataScience.reloadCustomEditor());
}
private async removeCustomEditors() {
// Note, to put it back, use the shipped version. This packageJson is required into the product
// so it's packed by webpack into the source.
const original = { ...this.appEnvironment.packageJson };
await this.fileSystem.writeFile(
path.join(EXTENSION_ROOT_DIR, 'package.json'),
JSON.stringify(original, null, 4)
);
this.commandManager.executeCommand('python.reloadVSCode', DataScience.reloadCustomEditor());
}
}