forked from microsoft/vscode-java-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.ts
More file actions
158 lines (131 loc) · 5.6 KB
/
utility.ts
File metadata and controls
158 lines (131 loc) · 5.6 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as vscode from "vscode";
import { setUserError } from "vscode-extension-telemetry-wrapper";
import { logger, Type } from "./logger";
const TROUBLESHOOTING_LINK = "https://github.com/Microsoft/vscode-java-debug/blob/master/Troubleshooting.md";
const LEARN_MORE = "Learn More";
const JAVA_EXTENSION_ID = "redhat.java";
export class UserError extends Error {
public context: ITroubleshootingMessage;
constructor(context: ITroubleshootingMessage) {
super(context.message);
this.context = context;
setUserError(this);
}
}
export class JavaExtensionNotActivatedError extends Error {
constructor(message) {
super(message);
setUserError(this);
}
}
interface IProperties {
[key: string]: string;
}
interface ILoggingMessage {
message: string;
type?: Type;
details?: IProperties;
}
interface ITroubleshootingMessage extends ILoggingMessage {
anchor?: string;
}
function logMessage(message: ILoggingMessage): void {
if (!message.type) {
return;
}
if (message.details) {
logger.log(message.type, message.details);
} else {
logger.logMessage(message.type, message.message);
}
}
export async function showInformationMessage(message: ILoggingMessage, ...items: string[]): Promise<string | undefined> {
logMessage(message);
return await vscode.window.showInformationMessage(message.message, ...items);
}
export async function showWarningMessage(message: ILoggingMessage, ...items: string[]): Promise<string | undefined> {
logMessage(message);
return await vscode.window.showWarningMessage(message.message, ...items);
}
export async function showErrorMessage(message: ILoggingMessage, ...items: string[]): Promise<string | undefined> {
logMessage(message);
return await vscode.window.showErrorMessage(message.message, ...items);
}
export async function showInformationMessageWithTroubleshooting(message: ITroubleshootingMessage, ...items: string[]): Promise<string | undefined> {
const choice = await showInformationMessage(message, ...items, LEARN_MORE);
return handleTroubleshooting(choice, message.message, message.anchor);
}
export async function showWarningMessageWithTroubleshooting(message: ITroubleshootingMessage, ...items: string[]): Promise<string | undefined> {
const choice = await showWarningMessage(message, ...items, LEARN_MORE);
return handleTroubleshooting(choice, message.message, message.anchor);
}
export async function showErrorMessageWithTroubleshooting(message: ITroubleshootingMessage, ...items: string[]): Promise<string | undefined> {
const choice = await showErrorMessage(message, ...items, LEARN_MORE);
return handleTroubleshooting(choice, message.message, message.anchor);
}
function handleTroubleshooting(choice: string, message: string, anchor: string): string | undefined {
if (choice === LEARN_MORE) {
vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(anchor ? `${TROUBLESHOOTING_LINK}#${anchor}` : TROUBLESHOOTING_LINK));
logger.log(Type.USAGEDATA, {
troubleshooting: "yes",
troubleshootingMessage: message,
});
return;
}
return choice;
}
export async function guideToInstallJavaExtension() {
const MESSAGE = "Language Support for Java is required. Please install and enable it.";
const INSTALL = "Install";
const choice = await vscode.window.showWarningMessage(MESSAGE, INSTALL);
if (choice === INSTALL) {
await installJavaExtension();
}
}
async function installJavaExtension() {
await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification }, async (p) => {
p.report({ message: "Installing Language Support for Java ..." });
await vscode.commands.executeCommand("workbench.extensions.installExtension", JAVA_EXTENSION_ID);
});
const RELOAD = "Reload Window";
const choice = await vscode.window.showInformationMessage("Please reload window to activate Language Support for Java.", RELOAD);
if (choice === RELOAD) {
await vscode.commands.executeCommand("workbench.action.reloadWindow");
}
}
export function formatErrorProperties(ex: any): IProperties {
const exception = (ex && ex.data && ex.data.cause)
|| { stackTrace: (ex && ex.stack), detailMessage: String((ex && ex.message) || ex || "Unknown exception") };
const properties = {
message: "",
stackTrace: "",
};
if (exception && typeof exception === "object") {
properties.message = exception.detailMessage;
properties.stackTrace = (Array.isArray(exception.stackTrace) && JSON.stringify(exception.stackTrace))
|| String(exception.stackTrace);
} else {
properties.message = String(exception);
}
return properties;
}
export async function getJavaHome(): Promise<string> {
const extension = vscode.extensions.getExtension(JAVA_EXTENSION_ID);
if (!extension) {
throw new JavaExtensionNotActivatedError("VS Code Java Extension is not enabled.");
}
try {
const extensionApi = await extension.activate();
if (extensionApi && extensionApi.javaRequirement) {
return extensionApi.javaRequirement.java_home;
}
} catch (ex) {
}
return "";
}
export function isJavaExtEnabled() {
const javaExt = vscode.extensions.getExtension(JAVA_EXTENSION_ID);
return !!javaExt;
}