forked from microsoft/vscode-java-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugCodeLensProvider.ts
More file actions
157 lines (131 loc) · 6.56 KB
/
debugCodeLensProvider.ts
File metadata and controls
157 lines (131 loc) · 6.56 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as _ from "lodash";
import * as vscode from "vscode";
import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper";
import { JAVA_LANGID } from "./constants";
import { IMainMethod, resolveMainMethod } from "./languageServerPlugin";
import { getJavaExtensionAPI, isJavaExtEnabled } from "./utility";
const JAVA_RUN_CODELENS_COMMAND = "java.debug.runCodeLens";
const JAVA_DEBUG_CODELENS_COMMAND = "java.debug.debugCodeLens";
const JAVA_DEBUG_CONFIGURATION = "java.debug.settings";
const ENABLE_CODE_LENS_VARIABLE = "enableRunDebugCodeLens";
export function initializeCodeLensProvider(context: vscode.ExtensionContext): void {
// delay registering codelens provider until the Java extension is activated.
if (isActivatedByJavaFile() && isJavaExtEnabled()) {
getJavaExtensionAPI().then(() => {
context.subscriptions.push(new DebugCodeLensContainer());
});
} else {
context.subscriptions.push(new DebugCodeLensContainer());
}
}
function isActivatedByJavaFile(): boolean {
if (vscode.window.activeTextEditor) {
return vscode.window.activeTextEditor.document && vscode.window.activeTextEditor.document.languageId === "java";
}
return false;
}
class DebugCodeLensContainer implements vscode.Disposable {
private runCommand: vscode.Disposable;
private debugCommand: vscode.Disposable;
private lensProvider: vscode.Disposable | undefined;
private configurationEvent: vscode.Disposable;
constructor() {
this.runCommand = instrumentOperationAsVsCodeCommand(JAVA_RUN_CODELENS_COMMAND, runJavaProgram);
this.debugCommand = instrumentOperationAsVsCodeCommand(JAVA_DEBUG_CODELENS_COMMAND, debugJavaProgram);
const configuration = vscode.workspace.getConfiguration(JAVA_DEBUG_CONFIGURATION);
const isCodeLensEnabled = configuration.get<boolean>(ENABLE_CODE_LENS_VARIABLE);
if (isCodeLensEnabled) {
this.lensProvider = vscode.languages.registerCodeLensProvider(JAVA_LANGID, new DebugCodeLensProvider());
}
this.configurationEvent = vscode.workspace.onDidChangeConfiguration((event: vscode.ConfigurationChangeEvent) => {
if (event.affectsConfiguration(JAVA_DEBUG_CONFIGURATION)) {
const newConfiguration = vscode.workspace.getConfiguration(JAVA_DEBUG_CONFIGURATION);
const newEnabled = newConfiguration.get<boolean>(ENABLE_CODE_LENS_VARIABLE);
if (newEnabled && this.lensProvider === undefined) {
this.lensProvider = vscode.languages.registerCodeLensProvider(JAVA_LANGID, new DebugCodeLensProvider());
} else if (!newEnabled && this.lensProvider !== undefined) {
this.lensProvider.dispose();
this.lensProvider = undefined;
}
}
}, this);
}
public dispose() {
if (this.lensProvider !== undefined) {
this.lensProvider.dispose();
}
this.runCommand.dispose();
this.debugCommand.dispose();
this.configurationEvent.dispose();
}
}
class DebugCodeLensProvider implements vscode.CodeLensProvider {
public async provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): Promise<vscode.CodeLens[]> {
try {
const mainMethods: IMainMethod[] = await resolveMainMethod(document.uri);
return _.flatten(mainMethods.map((method) => {
return [
new vscode.CodeLens(method.range, {
title: "Run",
command: JAVA_RUN_CODELENS_COMMAND,
tooltip: "Run Java Program",
arguments: [ method.mainClass, method.projectName, document.uri ],
}),
new vscode.CodeLens(method.range, {
title: "Debug",
command: JAVA_DEBUG_CODELENS_COMMAND,
tooltip: "Debug Java Program",
arguments: [ method.mainClass, method.projectName, document.uri ],
}),
];
}));
} catch (ex) {
// do nothing.
return [];
}
}
}
function runJavaProgram(mainClass: string, projectName: string, uri: vscode.Uri): Promise<boolean> {
return startDebugging(mainClass, projectName, uri, true);
}
function debugJavaProgram(mainClass: string, projectName: string, uri: vscode.Uri): Promise<boolean> {
return startDebugging(mainClass, projectName, uri, false);
}
async function constructDebugConfig(mainClass: string, projectName: string, workspace: vscode.Uri): Promise<vscode.DebugConfiguration> {
const launchConfigurations: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("launch", workspace);
const rawConfigs: vscode.DebugConfiguration[] = launchConfigurations.configurations;
let debugConfig: vscode.DebugConfiguration = _.find(rawConfigs, (config) => {
return config.mainClass === mainClass && _.toString(config.projectName) === _.toString(projectName);
});
if (!debugConfig) {
debugConfig = _.find(rawConfigs, (config) => {
return config.mainClass === mainClass && !config.projectName;
});
}
if (!debugConfig) {
debugConfig = {
type: "java",
name: `CodeLens (Launch) - ${mainClass.substr(mainClass.lastIndexOf(".") + 1)}`,
request: "launch",
mainClass,
projectName,
};
// Persist the default debug configuration only if the workspace exists.
if (workspace) {
// Insert the default debug configuration to the beginning of launch.json.
rawConfigs.splice(0, 0, debugConfig);
await launchConfigurations.update("configurations", rawConfigs, vscode.ConfigurationTarget.WorkspaceFolder);
}
}
return _.cloneDeep(debugConfig);
}
export async function startDebugging(mainClass: string, projectName: string, uri: vscode.Uri, noDebug: boolean): Promise<boolean> {
const workspaceFolder: vscode.WorkspaceFolder = vscode.workspace.getWorkspaceFolder(uri);
const workspaceUri: vscode.Uri = workspaceFolder ? workspaceFolder.uri : undefined;
const debugConfig: vscode.DebugConfiguration = await constructDebugConfig(mainClass, projectName, workspaceUri);
debugConfig.projectName = projectName;
debugConfig.noDebug = noDebug;
return vscode.debug.startDebugging(workspaceFolder, debugConfig);
}