Skip to content

Commit a222bff

Browse files
authored
Merge pull request fwcd#104 from themkat/override_members
General override member(s) functionality - VSCode version
2 parents dc413a2 + 763fd9b commit a222bff

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

Diff for: package.json

+14
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,22 @@
6464
"command": "kotlin.languageServer.restart",
6565
"title": "Restart the Language Server",
6666
"category": "Kotlin"
67+
},
68+
{
69+
"command": "kotlin.overrideMember",
70+
"title": "Override member(s)",
71+
"category": "Kotlin"
6772
}
6873
],
74+
"menus": {
75+
"editor/context": [
76+
{
77+
"command": "kotlin.overrideMember",
78+
"group": "Kotlin",
79+
"when": "editorTextFocus && editorLangId == kotlin"
80+
}
81+
]
82+
},
6983
"breakpoints": [
7084
{
7185
"language": "kotlin"

Diff for: src/languageSetup.ts

+38-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { KotlinApi } from "./lspExtensions";
1212
import { fsExists } from "./util/fsUtils";
1313
import { ServerSetupParams } from "./setupParams";
1414
import { RunDebugCodeLens } from "./runDebugCodeLens";
15-
import { MainClassRequest } from "./lspExtensions";
15+
import { MainClassRequest, OverrideMemberRequest } from "./lspExtensions";
1616

1717
/** Downloads and starts the language server. */
1818
export async function activateLanguageServer({ context, status, config, javaInstallation }: ServerSetupParams): Promise<KotlinApi> {
@@ -81,6 +81,42 @@ export async function activateLanguageServer({ context, status, config, javaInst
8181
const contentProvider = new JarClassContentProvider(languageClient);
8282
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider("kls", contentProvider));
8383

84+
// register override members command
85+
vscode.commands.registerCommand("kotlin.overrideMember", async() => {
86+
const activeEditor = vscode.window.activeTextEditor;
87+
const currentDocument = activeEditor?.document;
88+
// TODO: seems like we cant interact with the inner edit-fields as if it were a WorkspaceEdit object?? See if there is a way to solve this
89+
const overrideOptions = await languageClient.sendRequest(OverrideMemberRequest.type, {
90+
textDocument: {
91+
uri: currentDocument.uri.toString()
92+
},
93+
position: activeEditor?.selection.start
94+
});
95+
96+
// show an error message if nothing is found
97+
if(0 == overrideOptions.length) {
98+
vscode.window.showWarningMessage("No overrides found for class");
99+
return;
100+
}
101+
102+
const selected = await vscode.window.showQuickPick(overrideOptions.map(elem => ({
103+
label: elem.title,
104+
data: elem.edit.changes[currentDocument.uri.toString()]
105+
})), {
106+
canPickMany: true,
107+
placeHolder: 'Select overrides'
108+
});
109+
110+
// TODO: find out why we can't use vscode.workspace.applyEdit directly with the results. Probably related to the issue mentioned above
111+
// we know all the edits are in the current document, and that each one only contain one edit, so this hack works
112+
activeEditor.edit(editBuilder => {
113+
selected.forEach(elem => {
114+
const textEdit = elem.data[0];
115+
editBuilder.insert(textEdit.range.start, textEdit.newText);
116+
});
117+
});
118+
});
119+
84120
// Activating run/debug code lens if the debug adapter is enabled
85121
// and we are using 'kotlin-language-server' (other language servers
86122
// might not support the non-standard 'kotlin/mainClass' request)
@@ -93,7 +129,7 @@ export async function activateLanguageServer({ context, status, config, javaInst
93129
return await languageClient.sendRequest(MainClassRequest.type, {
94130
uri: fileUri
95131
})
96-
})
132+
});
97133

98134
vscode.commands.registerCommand("kotlin.runMain", async(mainClass, projectRoot) => {
99135
vscode.debug.startDebugging(vscode.workspace.getWorkspaceFolder(vscode.Uri.file(projectRoot)), {

Diff for: src/lspExtensions.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { RequestType0, RequestType } from "vscode-jsonrpc";
2-
import { TextDocumentIdentifier } from "vscode-languageclient";
2+
import { TextDocumentIdentifier, TextDocumentPositionParams } from "vscode-languageclient";
33
import { LanguageClient } from "vscode-languageclient/node";
44

55
export namespace JarClassContentsRequest {
66
export const type = new RequestType<TextDocumentIdentifier, string, void>("kotlin/jarClassContents");
77
}
88

99
export namespace MainClassRequest {
10-
export const type = new RequestType<TextDocumentIdentifier, any, void>("kotlin/mainClass")
10+
export const type = new RequestType<TextDocumentIdentifier, any, void>("kotlin/mainClass");
11+
}
12+
13+
export namespace OverrideMemberRequest {
14+
export const type = new RequestType<TextDocumentPositionParams, any[], void>("kotlin/overrideMember");
1115
}
1216

1317
export namespace BuildOutputLocationRequest {

0 commit comments

Comments
 (0)