From daf7da01a7bdcfb74f6d3355cf54a4cdb2f08cfe Mon Sep 17 00:00:00 2001 From: Marie Katrine Ekeberg Date: Sun, 10 Apr 2022 16:08:54 +0200 Subject: [PATCH 1/4] Run/debug code lens using the new resolve main functionality in the language server. --- src/RunDebugCodeLens.ts | 38 ++++++++++++++++++++++++++++++++++++++ src/languageSetup.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/RunDebugCodeLens.ts diff --git a/src/RunDebugCodeLens.ts b/src/RunDebugCodeLens.ts new file mode 100644 index 0000000..927e9cc --- /dev/null +++ b/src/RunDebugCodeLens.ts @@ -0,0 +1,38 @@ +import * as vscode from 'vscode'; + + +export class RunDebugCodeLens implements vscode.CodeLensProvider { + async provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): Promise { + const mainInfo = await vscode.commands.executeCommand("kotlin.resolveMain", document.uri.toString()) as MainInfo + + // TODO: any way we can disable these buttons while it is running? + if(mainInfo && mainInfo.mainClass) { + return [ + new vscode.CodeLens( + mainInfo.range, + { + title: "Run", + command: "kotlin.runMain", + arguments: [mainInfo.mainClass, mainInfo.projectRoot] + } + ), + new vscode.CodeLens( + mainInfo.range, + { + title: "Debug", + command: "kotlin.debugMain", + arguments: [mainInfo.mainClass, mainInfo.projectRoot] + } + ) + ] + } + + return [] + } +} + +interface MainInfo { + mainClass: String, + projectRoot: String, + range: vscode.Range +} diff --git a/src/languageSetup.ts b/src/languageSetup.ts index 881fe00..1292b7b 100644 --- a/src/languageSetup.ts +++ b/src/languageSetup.ts @@ -10,6 +10,7 @@ import { ServerDownloader } from './serverDownloader'; import { Status } from "./util/status"; import { JarClassContentProvider } from "./jarClassContentProvider"; import { fsExists } from "./util/fsUtils"; +import { RunDebugCodeLens } from "./RunDebugCodeLens"; /** Downloads and starts the language server. */ export async function activateLanguageServer(context: vscode.ExtensionContext, status: Status, config: vscode.WorkspaceConfiguration) { @@ -88,6 +89,42 @@ export async function activateLanguageServer(context: vscode.ExtensionContext, s context.subscriptions.push(languageClientDisposable); })); + // Activating run/debug code lens if the debug adapter is enabled + const kotlinConfig = vscode.workspace.getConfiguration("kotlin"); + const debugAdapterEnabled = kotlinConfig.get("debugAdapter.enabled"); + if(debugAdapterEnabled) { + vscode.languages.registerCodeLensProvider("kotlin", new RunDebugCodeLens()) + } + + vscode.commands.registerCommand("kotlin.resolveMain", async(fileUri) => { + return await languageClient.sendRequest("workspace/executeCommand", { + command: "resolveMain", + arguments: [fileUri] + }) + }) + + // TODO: maybe the settings can be persisted in a launch.json? + vscode.commands.registerCommand("kotlin.runMain", async(mainClass, projectRoot) => { + vscode.debug.startDebugging(vscode.workspace.getWorkspaceFolder(vscode.Uri.file(projectRoot)), { + type: "kotlin", + name: "Run Kotlin main", + request: "launch", + noDebug: true, + mainClass, + projectRoot, + }) + }); + + vscode.commands.registerCommand("kotlin.debugMain", async(mainClass, projectRoot) => { + vscode.debug.startDebugging(vscode.workspace.getWorkspaceFolder(vscode.Uri.file(projectRoot)), { + type: "kotlin", + name: "Debug Kotlin main", + request: "launch", + mainClass, + projectRoot, + }) + }); + await languageClient.onReady(); } From c9caad7eafeb6dc852cedfeeaca1542216b12b8b Mon Sep 17 00:00:00 2001 From: Marie Katrine Ekeberg Date: Sun, 10 Apr 2022 21:56:17 +0200 Subject: [PATCH 2/4] Put all debugging related functionality into the debug adapter enabled if-check --- src/languageSetup.ts | 54 ++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/languageSetup.ts b/src/languageSetup.ts index 1292b7b..c0bfd62 100644 --- a/src/languageSetup.ts +++ b/src/languageSetup.ts @@ -94,36 +94,36 @@ export async function activateLanguageServer(context: vscode.ExtensionContext, s const debugAdapterEnabled = kotlinConfig.get("debugAdapter.enabled"); if(debugAdapterEnabled) { vscode.languages.registerCodeLensProvider("kotlin", new RunDebugCodeLens()) - } - vscode.commands.registerCommand("kotlin.resolveMain", async(fileUri) => { - return await languageClient.sendRequest("workspace/executeCommand", { - command: "resolveMain", - arguments: [fileUri] + vscode.commands.registerCommand("kotlin.resolveMain", async(fileUri) => { + return await languageClient.sendRequest("workspace/executeCommand", { + command: "resolveMain", + arguments: [fileUri] + }) }) - }) - // TODO: maybe the settings can be persisted in a launch.json? - vscode.commands.registerCommand("kotlin.runMain", async(mainClass, projectRoot) => { - vscode.debug.startDebugging(vscode.workspace.getWorkspaceFolder(vscode.Uri.file(projectRoot)), { - type: "kotlin", - name: "Run Kotlin main", - request: "launch", - noDebug: true, - mainClass, - projectRoot, - }) - }); - - vscode.commands.registerCommand("kotlin.debugMain", async(mainClass, projectRoot) => { - vscode.debug.startDebugging(vscode.workspace.getWorkspaceFolder(vscode.Uri.file(projectRoot)), { - type: "kotlin", - name: "Debug Kotlin main", - request: "launch", - mainClass, - projectRoot, - }) - }); + // TODO: maybe the settings can be persisted in a launch.json? + vscode.commands.registerCommand("kotlin.runMain", async(mainClass, projectRoot) => { + vscode.debug.startDebugging(vscode.workspace.getWorkspaceFolder(vscode.Uri.file(projectRoot)), { + type: "kotlin", + name: "Run Kotlin main", + request: "launch", + noDebug: true, + mainClass, + projectRoot, + }) + }); + + vscode.commands.registerCommand("kotlin.debugMain", async(mainClass, projectRoot) => { + vscode.debug.startDebugging(vscode.workspace.getWorkspaceFolder(vscode.Uri.file(projectRoot)), { + type: "kotlin", + name: "Debug Kotlin main", + request: "launch", + mainClass, + projectRoot, + }) + }); + } await languageClient.onReady(); } From 3e32f2dc1cdbeef44920694516f6039f2be2cfcf Mon Sep 17 00:00:00 2001 From: Marie Katrine Ekeberg Date: Mon, 11 Apr 2022 15:49:21 +0200 Subject: [PATCH 3/4] Minor cleanup --- src/RunDebugCodeLens.ts | 1 - src/languageSetup.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/src/RunDebugCodeLens.ts b/src/RunDebugCodeLens.ts index 927e9cc..c7da3f9 100644 --- a/src/RunDebugCodeLens.ts +++ b/src/RunDebugCodeLens.ts @@ -5,7 +5,6 @@ export class RunDebugCodeLens implements vscode.CodeLensProvider { async provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): Promise { const mainInfo = await vscode.commands.executeCommand("kotlin.resolveMain", document.uri.toString()) as MainInfo - // TODO: any way we can disable these buttons while it is running? if(mainInfo && mainInfo.mainClass) { return [ new vscode.CodeLens( diff --git a/src/languageSetup.ts b/src/languageSetup.ts index c0bfd62..767236e 100644 --- a/src/languageSetup.ts +++ b/src/languageSetup.ts @@ -102,7 +102,6 @@ export async function activateLanguageServer(context: vscode.ExtensionContext, s }) }) - // TODO: maybe the settings can be persisted in a launch.json? vscode.commands.registerCommand("kotlin.runMain", async(mainClass, projectRoot) => { vscode.debug.startDebugging(vscode.workspace.getWorkspaceFolder(vscode.Uri.file(projectRoot)), { type: "kotlin", From 49c01780d340eb6d78afd6cae6321eb4a6b3b2d2 Mon Sep 17 00:00:00 2001 From: Marie Katrine Ekeberg Date: Mon, 25 Apr 2022 17:22:09 +0200 Subject: [PATCH 4/4] Changed main command according to changes in language server --- src/languageSetup.ts | 6 +++--- src/lspExtensions.ts | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/languageSetup.ts b/src/languageSetup.ts index 767236e..eed54f8 100644 --- a/src/languageSetup.ts +++ b/src/languageSetup.ts @@ -11,6 +11,7 @@ import { Status } from "./util/status"; import { JarClassContentProvider } from "./jarClassContentProvider"; import { fsExists } from "./util/fsUtils"; import { RunDebugCodeLens } from "./RunDebugCodeLens"; +import { MainClassRequest } from "./lspExtensions"; /** Downloads and starts the language server. */ export async function activateLanguageServer(context: vscode.ExtensionContext, status: Status, config: vscode.WorkspaceConfiguration) { @@ -96,9 +97,8 @@ export async function activateLanguageServer(context: vscode.ExtensionContext, s vscode.languages.registerCodeLensProvider("kotlin", new RunDebugCodeLens()) vscode.commands.registerCommand("kotlin.resolveMain", async(fileUri) => { - return await languageClient.sendRequest("workspace/executeCommand", { - command: "resolveMain", - arguments: [fileUri] + return await languageClient.sendRequest(MainClassRequest.type, { + uri: fileUri }) }) diff --git a/src/lspExtensions.ts b/src/lspExtensions.ts index ebbdf57..fb8583e 100644 --- a/src/lspExtensions.ts +++ b/src/lspExtensions.ts @@ -4,3 +4,7 @@ import { TextDocumentIdentifier } from "vscode-languageclient"; export namespace JarClassContentsRequest { export const type = new RequestType("kotlin/jarClassContents"); } + +export namespace MainClassRequest { + export const type = new RequestType("kotlin/mainClass") +}