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