Re-register contributed book commands when new extension loaded (#13403)

This commit is contained in:
Charles Gagnon
2020-11-16 10:47:27 -08:00
committed by GitHub
parent 690937443c
commit c699179e15

View File

@@ -86,22 +86,25 @@ export interface BookContributionProvider {
class AzdataExtensionBookContributionProvider extends Disposable implements BookContributionProvider { class AzdataExtensionBookContributionProvider extends Disposable implements BookContributionProvider {
private _contributions?: BookContribution[]; private _contributions?: BookContribution[];
private _contributionCommands: vscode.Disposable[] = [];
public constructor( public constructor(
public readonly extensionPath: string, public readonly extensionPath: string,
) { ) {
super(); super();
vscode.extensions.onDidChange(() => { vscode.extensions.onDidChange(async () => {
const currentContributions = this.getCurrentContributions(); const currentContributions = this.getCurrentContributions();
const existingContributions = this._contributions || undefined; const existingContributions = this._contributions || undefined;
if (!arrays.equals(existingContributions, currentContributions, BookContributions.equal)) { if (!arrays.equals(existingContributions, currentContributions, BookContributions.equal)) {
await this.unregisterCommands();
this._contributions = currentContributions; this._contributions = currentContributions;
await this.registerCommands();
this._onContributionsChanged.fire(this); this._onContributionsChanged.fire(this);
} }
}, undefined, this._disposables); }, undefined, this._disposables);
this.registerCommands(); this.registerCommands().catch(err => console.log(`Error registering contributed book commands : ${err}`));
} }
private readonly _onContributionsChanged = this._register(new vscode.EventEmitter<this>()); private readonly _onContributionsChanged = this._register(new vscode.EventEmitter<this>());
@@ -120,14 +123,23 @@ class AzdataExtensionBookContributionProvider extends Disposable implements Book
.reduce(BookContributions.merge, []); .reduce(BookContributions.merge, []);
} }
private registerCommands(): void { private async registerCommands(): Promise<void> {
this.contributions.map(book => { await Promise.all(this.contributions.map(async book => {
let bookName: string = path.basename(book.path); let bookName: string = path.basename(book.path);
vscode.commands.executeCommand('setContext', bookName, true); await vscode.commands.executeCommand('setContext', bookName, true);
vscode.commands.registerCommand('books.' + bookName, async (urlToOpen?: string) => { this._contributionCommands.push(vscode.commands.registerCommand('books.' + bookName, async (urlToOpen?: string) => {
vscode.commands.executeCommand('bookTreeView.openBook', book.path, true, urlToOpen); await vscode.commands.executeCommand('bookTreeView.openBook', book.path, true, urlToOpen);
}); }));
}); }));
}
private async unregisterCommands(): Promise<void> {
this._contributionCommands.forEach(command => command.dispose());
this._contributionCommands = [];
await Promise.all(this.contributions.map(async book => {
let bookName: string = path.basename(book.path);
await vscode.commands.executeCommand('setContext', bookName, false);
}));
} }
} }