|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +import * as vscode from "vscode"; |
| 6 | +import { IHandler } from "./handler"; |
| 7 | + |
| 8 | +const KEY_RECOMMENDATION_USER_CHOICE_MAP = "recommendationUserChoice"; |
| 9 | + |
| 10 | +async function installExtensionCmdHandler(extensionName: string, displayName: string) { |
| 11 | + return vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, title: `Installing ${displayName||extensionName}...`}, progress => { |
| 12 | + return vscode.commands.executeCommand("workbench.extensions.installExtension", extensionName); |
| 13 | + }).then(() => { |
| 14 | + vscode.window.showInformationMessage(`Successfully installed ${displayName||extensionName}.`); |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +enum UserChoice { |
| 19 | + Install = "Install", |
| 20 | + Never = "Never", |
| 21 | + Later = "Later", |
| 22 | +} |
| 23 | + |
| 24 | +export class HandlerImpl implements IHandler { |
| 25 | + userChoice: any; |
| 26 | + storeUserChoice: any; |
| 27 | + constructor(context: vscode.ExtensionContext) { |
| 28 | + this.userChoice = () => { |
| 29 | + return context.globalState.get(KEY_RECOMMENDATION_USER_CHOICE_MAP, {}); |
| 30 | + }; |
| 31 | + |
| 32 | + this.storeUserChoice = (choice: object) => { |
| 33 | + context.globalState.update(KEY_RECOMMENDATION_USER_CHOICE_MAP, choice); |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + isExtensionInstalled(extName: string): boolean { |
| 38 | + return !!vscode.extensions.getExtension(extName); |
| 39 | + } |
| 40 | + |
| 41 | + canRecommendExtension(extName: string): boolean { |
| 42 | + return this.userChoice()[extName] !== UserChoice.Never && !this.isExtensionInstalled(extName); |
| 43 | + } |
| 44 | + |
| 45 | + async handle(extName: string, message: string): Promise<void> { |
| 46 | + if (this.isExtensionInstalled(extName)) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + const choice = this.userChoice(); |
| 51 | + if (choice[extName] === UserChoice.Never) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const actions: Array<string> = Object.keys(UserChoice); |
| 56 | + const answer = await vscode.window.showInformationMessage(message, ...actions); |
| 57 | + if (answer === UserChoice.Install) { |
| 58 | + await installExtensionCmdHandler(extName, extName); |
| 59 | + } |
| 60 | + |
| 61 | + choice[extName] = answer; |
| 62 | + this.storeUserChoice(choice); |
| 63 | + } |
| 64 | +} |
0 commit comments