Skip to content

Commit 2af7617

Browse files
Ricardo Alves dos Santos JuniorLeoAnders
authored andcommitted
feat: add resolveContextExpression command Ctrl+Alt+Space to call API and insert returned code
1 parent 2edfa33 commit 2af7617

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,11 @@
839839
"command": "vscode-objectscript.export",
840840
"title": "Export Code from Server"
841841
},
842+
{
843+
"category": "ObjectScript",
844+
"command": "vscode-objectscript.resolveContextExpression",
845+
"title": "Resolve Context Expression"
846+
},
842847
{
843848
"category": "ObjectScript",
844849
"command": "vscode-objectscript.compile",
@@ -1219,6 +1224,12 @@
12191224
"mac": "Cmd+Shift+F7",
12201225
"when": "editorLangId =~ /^objectscript/"
12211226
},
1227+
{
1228+
"command": "vscode-objectscript.resolveContextExpression",
1229+
"key": "Ctrl+Alt+Space",
1230+
"mac": "Cmd+Alt+Space",
1231+
"when": "editorTextFocus && editorLangId =~ /^objectscript/"
1232+
},
12221233
{
12231234
"command": "vscode-objectscript.viewOthers",
12241235
"key": "Ctrl+Shift+V",

src/commands/contextHelp.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import axios from "axios";
2+
import * as https from "https";
3+
import * as path from "path";
4+
import * as vscode from "vscode";
5+
6+
import { AtelierAPI } from "../api";
7+
import { handleError } from "../utils";
8+
9+
interface ResolveContextExpressionResponse {
10+
status?: string;
11+
textExpression?: string;
12+
message?: string;
13+
}
14+
15+
export async function resolveContextExpression(): Promise<void> {
16+
const editor = vscode.window.activeTextEditor;
17+
if (!editor) {
18+
return;
19+
}
20+
21+
const { document, selection } = editor;
22+
const contextExpression = selection.isEmpty
23+
? document.lineAt(selection.active.line).text.trim()
24+
: document.getText(selection).trim();
25+
26+
if (!contextExpression) {
27+
void vscode.window.showErrorMessage("Context expression is empty.");
28+
return;
29+
}
30+
31+
const routine = path.basename(document.fileName);
32+
const api = new AtelierAPI(document.uri);
33+
const { host, port, username, password, https: useHttps, pathPrefix } = api.config;
34+
35+
if (!host || !port) {
36+
void vscode.window.showErrorMessage("No active InterSystems server connection for this file.");
37+
return;
38+
}
39+
40+
const normalizedPrefix = pathPrefix ? (pathPrefix.startsWith("/") ? pathPrefix : `/${pathPrefix}`) : "";
41+
42+
const baseUrl = `${useHttps ? "https" : "http"}://${host}:${port}${encodeURI(normalizedPrefix)}`;
43+
const url = `${baseUrl}/api/sourcecontrol/vscode/resolveContextExpression`;
44+
45+
const httpsAgent = new https.Agent({
46+
rejectUnauthorized: vscode.workspace.getConfiguration("http").get("proxyStrictSSL"),
47+
});
48+
49+
try {
50+
const response = await axios.post<ResolveContextExpressionResponse>(
51+
url,
52+
{
53+
routine,
54+
contextExpression,
55+
},
56+
{
57+
headers: {
58+
"Content-Type": "application/json",
59+
},
60+
auth:
61+
typeof username === "string" && typeof password === "string"
62+
? {
63+
username,
64+
password,
65+
}
66+
: undefined,
67+
httpsAgent,
68+
}
69+
);
70+
71+
const data = response.data ?? {};
72+
if (typeof data.status === "string" && data.status.toLowerCase() === "success" && data.textExpression) {
73+
const eol = document.eol === vscode.EndOfLine.CRLF ? "\r\n" : "\n";
74+
const textExpression = data.textExpression.replace(/\r?\n/g, eol);
75+
const formattedTextExpression = textExpression.replace(/^/, "\t");
76+
const lineRange = document.lineAt(selection.active.line).range;
77+
await editor.edit((editBuilder) => {
78+
editBuilder.replace(lineRange, formattedTextExpression);
79+
});
80+
} else {
81+
const errorMessage = data.message || "Failed to resolve context expression.";
82+
void vscode.window.showErrorMessage(errorMessage);
83+
}
84+
} catch (error) {
85+
handleError(error, "Failed to resolve context expression.");
86+
}
87+
}

src/extension.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ import { WorkspaceNode, NodeBase } from "./explorer/nodes";
163163
import { showPlanWebview } from "./commands/showPlanPanel";
164164
import { isfsConfig } from "./utils/FileProviderUtil";
165165
import { showAllClassMembers } from "./commands/showAllClassMembers";
166+
import { resolveContextExpression } from "./commands/contextHelp";
166167

167168
const packageJson = vscode.extensions.getExtension(extensionId).packageJSON;
168169
const extensionVersion = packageJson.version;
@@ -1261,6 +1262,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
12611262
sendCommandTelemetryEvent("copyToClipboard");
12621263
vscode.env.clipboard.writeText(command);
12631264
}),
1265+
vscode.commands.registerCommand("vscode-objectscript.resolveContextExpression", () => {
1266+
sendCommandTelemetryEvent("resolveContextExpression");
1267+
void resolveContextExpression();
1268+
}),
12641269
vscode.commands.registerCommand("vscode-objectscript.debug", (program: string, askArgs: boolean) => {
12651270
sendCommandTelemetryEvent("debug");
12661271
const startDebugging = (args) => {

0 commit comments

Comments
 (0)