Skip to content

Commit ddb9e10

Browse files
ThadHousetestforstephen
authored andcommitted
Add way to disable run and debug code lens provider (#465)
* Add way to disable run and debug code lens provider There are cases where it will never work properly, and it is confusing a chunk of my users that attempt to use it, and wonder why it fails Closes #464 * Add translation support * Add type to lambda * Add period to description
1 parent 8bd8705 commit ddb9e10

5 files changed

Lines changed: 58 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Please also check the documentation of [Language Support for Java by Red Hat](ht
9090
- `java.debug.settings.showQualifiedNames`: show fully qualified class names in "Variables" viewlet, defaults to `false`.
9191
- `java.debug.settings.maxStringLength`: the maximum length of string displayed in "Variables" or "Debug Console" viewlet, the string longer than this length will be trimmed, defaults to `0` which means no trim is performed.
9292
- `java.debug.settings.enableHotCodeReplace`: enable hot code replace for Java code. Make sure the auto build is not disabled for [VSCode Java](https://github.com/redhat-developer/vscode-java). See the [wiki page](https://github.com/Microsoft/vscode-java-debug/wiki/Hot-Code-Replace) for more information about usages and limitations.
93+
- `java.debug.settings.enableRunDebugCodeLens`: enable the code lens provider for the run and debug buttons over main entry points, defaults to `true`.
9394

9495
## Troubleshooting
9596
Reference the [troubleshooting guide](https://github.com/Microsoft/vscode-java-debug/blob/master/Troubleshooting.md) for common errors.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@
364364
"type": "boolean",
365365
"description": "%java.debugger.configuration.enableHotCodeReplace.description%",
366366
"default": true
367+
},
368+
"java.debug.settings.enableRunDebugCodeLens": {
369+
"type": "boolean",
370+
"description": "%java.debugger.configuration.enableRunDebugCodeLens.description%",
371+
"default": true
367372
}
368373
}
369374
}

package.nls.it.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"java.debugger.configuration.showHex.description": "Mostra numeri in formato esadecimale nella scheda \"variabili\".",
55
"java.debugger.configuration.showStaticVariables.description": "Mostra variabili statiche nella scheda \"variabili\".",
66
"java.debugger.configuration.showQualifiedNames.description": "Mostra nome completo delle classi nella scheda \"variabili\".",
7-
"java.debugger.configuration.maxStringLength.description": "Lunghezza massima delle stringhe visualizzate nella scheda \"Variabili\" o \"Console di Debug\", stringhe più lunghe di questo numero verranno tagliate, se 0 nessun taglio viene eseguito.",
8-
"java.debugger.configuration.enableHotCodeReplace.description": "Attiva sostituzione hotcode per codice Java."
9-
10-
}
7+
"java.debugger.configuration.maxStringLength.description": "Lunghezza massima delle stringhe visualizzate nella scheda \"Variabili\" o \"Console di Debug\", stringhe più lunghe di questo numero verranno tagliate, se 0 nessun taglio viene eseguito.",
8+
"java.debugger.configuration.enableHotCodeReplace.description": "Attiva sostituzione hotcode per codice Java.",
9+
"java.debugger.configuration.enableRunDebugCodeLens.description": "Abilitare i provider di lenti di codice run e debug sui metodi principali."
10+
}

package.nls.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"java.debugger.configuration.showStaticVariables.description": "Show static variables in \"Variables\" viewlet.",
66
"java.debugger.configuration.showQualifiedNames.description": "Show fully qualified class names in \"Variables\" viewlet.",
77
"java.debugger.configuration.maxStringLength.description": "The maximum length of strings displayed in \"Variables\" or \"Debug Console\" viewlet, strings longer than this length will be trimmed, if 0 no trim is performed.",
8-
"java.debugger.configuration.enableHotCodeReplace.description": "Enable hot code replace for Java code."
9-
}
8+
"java.debugger.configuration.enableHotCodeReplace.description": "Enable hot code replace for Java code.",
9+
"java.debugger.configuration.enableRunDebugCodeLens.description": "Enable the run and debug code lens providers over main methods."
10+
}

src/debugCodeLensProvider.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,53 @@ import * as utility from "./utility";
1111

1212
const JAVA_RUN_COMMAND = "vscode.java.run";
1313
const JAVA_DEBUG_COMMAND = "vscode.java.debug";
14+
const JAVA_DEBUG_CONFIGURATION = "java.debug.settings";
15+
const ENABLE_CODE_LENS_VARIABLE = "enableRunDebugCodeLens";
1416

1517
export function initializeCodeLensProvider(context: vscode.ExtensionContext): void {
16-
context.subscriptions.push(vscode.languages.registerCodeLensProvider(JAVA_LANGID, new DebugCodeLensProvider()));
17-
context.subscriptions.push(vscode.commands.registerCommand(JAVA_RUN_COMMAND, runJavaProgram));
18-
context.subscriptions.push(vscode.commands.registerCommand(JAVA_DEBUG_COMMAND, debugJavaProgram));
18+
context.subscriptions.push(new DebugCodeLensContainer());
19+
}
20+
21+
class DebugCodeLensContainer implements vscode.Disposable {
22+
private runCommand: vscode.Disposable;
23+
private debugCommand: vscode.Disposable;
24+
private lensProvider: vscode.Disposable | undefined;
25+
private configurationEvent: vscode.Disposable;
26+
27+
constructor() {
28+
this.runCommand = vscode.commands.registerCommand(JAVA_RUN_COMMAND, runJavaProgram);
29+
this.debugCommand = vscode.commands.registerCommand(JAVA_DEBUG_COMMAND, debugJavaProgram);
30+
31+
const configuration = vscode.workspace.getConfiguration(JAVA_DEBUG_CONFIGURATION)
32+
const isCodeLensEnabled = configuration.get<boolean>(ENABLE_CODE_LENS_VARIABLE);
33+
34+
if (isCodeLensEnabled) {
35+
this.lensProvider = vscode.languages.registerCodeLensProvider(JAVA_LANGID, new DebugCodeLensProvider());
36+
}
37+
38+
this.configurationEvent = vscode.workspace.onDidChangeConfiguration((event: vscode.ConfigurationChangeEvent) => {
39+
if (event.affectsConfiguration(JAVA_DEBUG_CONFIGURATION)) {
40+
const newConfiguration = vscode.workspace.getConfiguration(JAVA_DEBUG_CONFIGURATION);
41+
const newEnabled = newConfiguration.get<boolean>(ENABLE_CODE_LENS_VARIABLE);
42+
if (newEnabled && this.lensProvider === undefined) {
43+
this.lensProvider = vscode.languages.registerCodeLensProvider(JAVA_LANGID, new DebugCodeLensProvider());
44+
} else if (!newEnabled && this.lensProvider !== undefined) {
45+
this.lensProvider.dispose();
46+
this.lensProvider = undefined;
47+
}
48+
}
49+
}, this);
50+
}
51+
52+
public dispose() {
53+
if (this.lensProvider !== undefined) {
54+
this.lensProvider.dispose();
55+
}
56+
this.runCommand.dispose();
57+
this.debugCommand.dispose();
58+
this.configurationEvent.dispose();
59+
}
60+
1961
}
2062

2163
class DebugCodeLensProvider implements vscode.CodeLensProvider {

0 commit comments

Comments
 (0)