Skip to content

Commit 5053d05

Browse files
authored
feat(tolk): add code lenses for building and generating Tolk and TypeScript wrappers (#306)
1 parent 625f460 commit 5053d05

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

editors/code/src/acton/ActonCommand.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ export class BuildCommand extends ActonCommand {
2929
}
3030
}
3131

32+
export class WrapperCommand extends ActonCommand {
33+
public constructor(
34+
public contractId: string,
35+
public typescript: boolean = false,
36+
) {
37+
super("wrapper")
38+
}
39+
40+
public override getArguments(): string[] {
41+
const args: string[] = []
42+
if (this.typescript) args.push("--ts")
43+
if (this.contractId.trim() !== "") {
44+
args.push(this.contractId)
45+
}
46+
return args
47+
}
48+
}
49+
3250
export enum TestMode {
3351
FUNCTION = "FUNCTION",
3452
FILE = "FILE",

editors/code/src/acton/tolk/ActonTolkCodeLensProvider.ts

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as path from "node:path"
66
import * as vscode from "vscode"
77

88
import {Acton} from "../Acton"
9-
import {ScriptCommand} from "../ActonCommand"
9+
import {BuildCommand, ScriptCommand, WrapperCommand} from "../ActonCommand"
1010

1111
import {startActonScriptDebugging} from "./ActonScriptDebug"
1212

@@ -28,6 +28,8 @@ const SCRIPT_BROADCAST_NETWORKS = [
2828
},
2929
] as const
3030

31+
const CONTRACT_DECLARATION_PATTERN = /^\s*contract\s+([$A-Z_a-z][\w$]*)\b/
32+
3133
export class ActonTolkCodeLensProvider implements vscode.CodeLensProvider {
3234
public provideCodeLenses(
3335
document: vscode.TextDocument,
@@ -42,8 +44,32 @@ export class ActonTolkCodeLensProvider implements vscode.CodeLensProvider {
4244
for (let i = 0; i < document.lineCount; i++) {
4345
const line = document.lineAt(i)
4446
const text = line.text
47+
const code = text.split("//", 1)[0]
4548

46-
if (/fun\s+main\s*\(/i.test(text)) {
49+
const contractMatch = CONTRACT_DECLARATION_PATTERN.exec(code)
50+
if (contractMatch) {
51+
const contractId = contractMatch[1]
52+
const range = new vscode.Range(i, 0, i, text.length)
53+
lenses.push(
54+
new vscode.CodeLens(range, {
55+
title: "Build contract",
56+
command: "ton.acton.buildContractFromTolk",
57+
arguments: [document.uri, contractId],
58+
}),
59+
new vscode.CodeLens(range, {
60+
title: "Generate Tolk wrapper",
61+
command: "ton.acton.generateTolkWrapper",
62+
arguments: [document.uri, contractId],
63+
}),
64+
new vscode.CodeLens(range, {
65+
title: "Generate TypeScript wrapper",
66+
command: "ton.acton.generateTypescriptWrapper",
67+
arguments: [document.uri, contractId],
68+
}),
69+
)
70+
}
71+
72+
if (/fun\s+main\s*\(/i.test(code)) {
4773
const range = new vscode.Range(i, 0, i, text.length)
4874
lenses.push(
4975
new vscode.CodeLens(range, {
@@ -73,6 +99,33 @@ export class ActonTolkCodeLensProvider implements vscode.CodeLensProvider {
7399
vscode.commands.registerCommand("ton.acton.run", async (fileUri: vscode.Uri) => {
74100
await ActonTolkCodeLensProvider.runScript(fileUri, "")
75101
}),
102+
vscode.commands.registerCommand(
103+
"ton.acton.buildContractFromTolk",
104+
async (fileUri: vscode.Uri, contractId: string) => {
105+
await ActonTolkCodeLensProvider.runContractCommand(
106+
fileUri,
107+
new BuildCommand(contractId),
108+
)
109+
},
110+
),
111+
vscode.commands.registerCommand(
112+
"ton.acton.generateTolkWrapper",
113+
async (fileUri: vscode.Uri, contractId: string) => {
114+
await ActonTolkCodeLensProvider.runContractCommand(
115+
fileUri,
116+
new WrapperCommand(contractId),
117+
)
118+
},
119+
),
120+
vscode.commands.registerCommand(
121+
"ton.acton.generateTypescriptWrapper",
122+
async (fileUri: vscode.Uri, contractId: string) => {
123+
await ActonTolkCodeLensProvider.runContractCommand(
124+
fileUri,
125+
new WrapperCommand(contractId, true),
126+
)
127+
},
128+
),
76129
vscode.commands.registerCommand(
77130
"ton.acton.debugScript",
78131
async (fileUri: vscode.Uri) => {
@@ -114,4 +167,14 @@ export class ActonTolkCodeLensProvider implements vscode.CodeLensProvider {
114167

115168
await Acton.getInstance().execute(command, workingDir)
116169
}
170+
171+
private static async runContractCommand(
172+
fileUri: vscode.Uri,
173+
command: BuildCommand | WrapperCommand,
174+
): Promise<void> {
175+
const tomlUri = await Acton.getInstance().findActonToml(fileUri)
176+
const workingDir = tomlUri ? path.dirname(tomlUri.fsPath) : path.dirname(fileUri.fsPath)
177+
178+
await Acton.getInstance().execute(command, workingDir)
179+
}
117180
}

0 commit comments

Comments
 (0)