Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/RunDebugCodeLens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as vscode from 'vscode';


export class RunDebugCodeLens implements vscode.CodeLensProvider {
async provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): Promise<vscode.CodeLens[]> {
const mainInfo = await vscode.commands.executeCommand("kotlin.resolveMain", document.uri.toString()) as MainInfo

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
}
36 changes: 36 additions & 0 deletions src/languageSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { Status } from "./util/status";
import { JarClassContentProvider } from "./jarClassContentProvider";
import { KotlinApi } from "./lspExtensions";
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): Promise<KotlinApi> {
Expand Down Expand Up @@ -89,6 +91,40 @@ 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(MainClassRequest.type, {
uri: fileUri
})
})

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();

return new KotlinApi(languageClient);
Expand Down
4 changes: 4 additions & 0 deletions src/lspExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export namespace JarClassContentsRequest {
export const type = new RequestType<TextDocumentIdentifier, string, void>("kotlin/jarClassContents");
}

export namespace MainClassRequest {
export const type = new RequestType<TextDocumentIdentifier, any, void>("kotlin/mainClass")
}

export namespace BuildOutputLocationRequest {
export const type = new RequestType0<string, void>("kotlin/buildOutputLocation");
}
Expand Down