diff --git a/README.md b/README.md index 921c1ee5..9d25a365 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ Please also check the documentation of [Language Support for Java by Red Hat](ht - `java.debug.settings.showQualifiedNames`: show fully qualified class names in "Variables" viewlet, defaults to `false`. - `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. - `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. +- `java.debug.settings.enableRunDebugCodeLens`: enable the code lens provider for the run and debug buttons over main entry points, defaults to `true`. ## Troubleshooting Reference the [troubleshooting guide](https://github.com/Microsoft/vscode-java-debug/blob/master/Troubleshooting.md) for common errors. diff --git a/package.json b/package.json index 2fdba654..7591e72b 100644 --- a/package.json +++ b/package.json @@ -364,6 +364,11 @@ "type": "boolean", "description": "Enable hot code replace for Java code", "default": true + }, + "java.debug.settings.enableRunDebugCodeLens": { + "type": "boolean", + "description": "Enable the run and debug code lens providers over main methods", + "default": true } } } diff --git a/src/debugCodeLensProvider.ts b/src/debugCodeLensProvider.ts index 5bcc2185..2934a9a2 100644 --- a/src/debugCodeLensProvider.ts +++ b/src/debugCodeLensProvider.ts @@ -11,11 +11,53 @@ import * as utility from "./utility"; const JAVA_RUN_COMMAND = "vscode.java.run"; const JAVA_DEBUG_COMMAND = "vscode.java.debug"; +const JAVA_DEBUG_CONFIGURATION = "java.debug.settings"; +const ENABLE_CODE_LENS_VARIABLE = "enableRunDebugCodeLens"; export function initializeCodeLensProvider(context: vscode.ExtensionContext): void { - context.subscriptions.push(vscode.languages.registerCodeLensProvider(JAVA_LANGID, new DebugCodeLensProvider())); - context.subscriptions.push(vscode.commands.registerCommand(JAVA_RUN_COMMAND, runJavaProgram)); - context.subscriptions.push(vscode.commands.registerCommand(JAVA_DEBUG_COMMAND, debugJavaProgram)); + context.subscriptions.push(new DebugCodeLensContainer()); +} + +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 = vscode.commands.registerCommand(JAVA_RUN_COMMAND, runJavaProgram); + this.debugCommand = vscode.commands.registerCommand(JAVA_DEBUG_COMMAND, debugJavaProgram); + + const configuration = vscode.workspace.getConfiguration(JAVA_DEBUG_CONFIGURATION) + const isCodeLensEnabled = configuration.get(ENABLE_CODE_LENS_VARIABLE); + + if (isCodeLensEnabled) { + this.lensProvider = vscode.languages.registerCodeLensProvider(JAVA_LANGID, new DebugCodeLensProvider()); + } + + this.configurationEvent = vscode.workspace.onDidChangeConfiguration((event) => { + if (event.affectsConfiguration(JAVA_DEBUG_CONFIGURATION)) { + const newConfiguration = vscode.workspace.getConfiguration(JAVA_DEBUG_CONFIGURATION); + const newEnabled = newConfiguration.get(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 {