Skip to content

Commit fc93a06

Browse files
Merge pull request #535 from KxSystems/KXI-60351-new
[KXI-60351] Reset scratchpad
2 parents e2f54dd + dea13b9 commit fc93a06

File tree

9 files changed

+205
-29
lines changed

9 files changed

+205
-29
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
All notable changes to the **kdb VS Code extension** are documented in this file.
44

5+
# v1.11.0
6+
7+
### Enhancements
8+
9+
- Add from UDAs tab to the Datasource Files
10+
- Add reset scratchpad for insights connections version 1.13 or higher
11+
12+
### Fixes
13+
514
# v1.10.2
615

716
### Fixes

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,16 @@
287287
"command": "kdb.runScratchpad",
288288
"title": "Run workbook"
289289
},
290+
{
291+
"category": "KX",
292+
"command": "kdb.scratchpad.reset",
293+
"title": "Reset scratchpad"
294+
},
295+
{
296+
"category": "KX",
297+
"command": "kdb.scratchpad.resetEditorConnection",
298+
"title": "Reset editor connection"
299+
},
290300
{
291301
"category": "KX",
292302
"command": "kdb.startLocalProcess",
@@ -491,6 +501,12 @@
491501
"mac": "cmd+shift+d",
492502
"when": "editorLangId == python && (resourceFilename =~ /.kdb.py/ || kdb.pythonEnabled)"
493503
},
504+
{
505+
"command": "kdb.scratchpad.resetEditorConnection",
506+
"key": "ctrl+shift+delete",
507+
"mac": "cmd+shift+delete",
508+
"when": "(editorLangId == python && (resourceFilename =~ /.kdb.py/ || kdb.pythonEnabled)) || editorLangId == q"
509+
},
494510
{
495511
"command": "kdb.terminal.run",
496512
"key": "ctrl+shift+r",
@@ -660,6 +676,10 @@
660676
"command": "kdb.open.meta",
661677
"when": "false"
662678
},
679+
{
680+
"command": "kdb.scratchpad.resetEditorConnection",
681+
"when": "false"
682+
},
663683
{
664684
"command": "kdb.connect.via.dialog",
665685
"when": "false"
@@ -815,6 +835,11 @@
815835
"when": "view == kdb-servers && (viewItem in kdb.rootNodes || viewItem in kdb.insightsNodes)",
816836
"group": "connection@6"
817837
},
838+
{
839+
"command": "kdb.scratchpad.reset",
840+
"when": "view == kdb-servers && viewItem in kdb.connected && viewItem in kdb.insightsNodes",
841+
"group": "connection@7"
842+
},
818843
{
819844
"command": "kdb.startLocalProcess",
820845
"when": "view == kdb-servers && viewItem in kdb.local && viewItem not in kdb.running && viewItem in kdb.rootNodes",

src/classes/insightsConnection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export class InsightsConnection {
271271
importSql: "servicebroker/scratchpad/import/sql",
272272
importQsql: "servicebroker/scratchpad/import/qsql",
273273
importUDA: "servicebroker/scratchpad/import/uda",
274-
reset: "servicebroker/scratchpad/reset",
274+
reset: "scratchpadmanager/reset",
275275
},
276276
serviceGateway: {
277277
meta: "servicegateway/meta",

src/commands/serverCommand.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,9 +759,9 @@ export function activeConnection(viewItem: KdbNode | InsightsNode): void {
759759
ext.serverProvider.reload();
760760
}
761761

762-
export async function resetScratchPad(): Promise<void> {
762+
export async function resetScratchpad(connName?: string): Promise<void> {
763763
const connMngService = new ConnectionManagementService();
764-
await connMngService.resetScratchpad();
764+
await connMngService.resetScratchpad(connName);
765765
}
766766

767767
export async function refreshGetMeta(connLabel?: string): Promise<void> {

src/commands/workspaceCommand.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
import { ext } from "../extensionVariables";
3030
import { ConnectionManagementService } from "../services/connectionManagerService";
3131
import { InsightsNode, KdbNode, LabelNode } from "../services/kdbTreeProvider";
32-
import { runQuery } from "./serverCommand";
32+
import { resetScratchpad, runQuery } from "./serverCommand";
3333
import { ExecutionTypes } from "../models/execution";
3434
import { importOldDsFiles, oldFilesExists } from "../utils/dataSource";
3535
import { kdbOutputLog, offerConnectAction } from "../utils/core";
@@ -291,6 +291,23 @@ export async function runActiveEditor(type?: ExecutionTypes) {
291291
}
292292
}
293293

294+
export async function resetScratchpadFromEditor(): Promise<void> {
295+
if (ext.activeTextEditor) {
296+
const uri = ext.activeTextEditor.document.uri;
297+
const isWorkbook = uri.path.endsWith(".kdb.q");
298+
let server = getServerForUri(uri);
299+
if (!server && isWorkbook) {
300+
server = await pickConnection(uri);
301+
}
302+
if (!server) {
303+
server = "";
304+
}
305+
const connection = await getConnectionForServer(server);
306+
server = connection?.label || "";
307+
resetScratchpad(server);
308+
}
309+
}
310+
294311
function update(uri: Uri) {
295312
if (isDataSource(uri)) {
296313
ext.dataSourceTreeProvider.reload();

src/extension.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import {
5656
refreshGetMeta,
5757
removeConnection,
5858
rerunQuery,
59-
resetScratchPad,
59+
resetScratchpad,
6060
} from "./commands/serverCommand";
6161
import { showInstallationDetails } from "./commands/walkthroughCommand";
6262
import { ext } from "./extensionVariables";
@@ -98,6 +98,7 @@ import {
9898
connectWorkspaceCommands,
9999
importOldDSFiles,
100100
pickConnection,
101+
resetScratchpadFromEditor,
101102
runActiveEditor,
102103
setServerForUri,
103104
} from "./commands/workspaceCommand";
@@ -469,10 +470,19 @@ export async function activate(context: ExtensionContext) {
469470
commands.registerCommand("kdb.execute.pythonScratchpadQuery", async () => {
470471
await runActiveEditor(ExecutionTypes.PythonQuerySelection);
471472
}),
472-
// TODO renable it when the scratchpad reset API is fixed
473-
// commands.registerCommand("kdb.scratchpad.reset", async () => {
474-
// await resetScratchPad();
475-
// }),
473+
commands.registerCommand(
474+
"kdb.scratchpad.reset",
475+
async (viewItem?: InsightsNode) => {
476+
const connLabel = viewItem ? viewItem.label : undefined;
477+
await resetScratchpad(connLabel);
478+
},
479+
),
480+
commands.registerCommand(
481+
"kdb.scratchpad.resetEditorConnection",
482+
async () => {
483+
await resetScratchpadFromEditor();
484+
},
485+
),
476486
commands.registerCommand(
477487
"kdb.execute.pythonFileScratchpadQuery",
478488
async (item) => {

src/services/connectionManagerService.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -353,35 +353,54 @@ export class ConnectionManagementService {
353353
}
354354
}
355355

356-
public async resetScratchpad(): Promise<void> {
357-
if (
358-
!ext.activeConnection ||
359-
!(ext.activeConnection instanceof InsightsConnection)
360-
) {
361-
kdbOutputLog(
362-
"[RESET SCRATCHPAD] Please activate an Insights connection to use this feature.",
363-
"ERROR",
364-
);
365-
return;
356+
public async resetScratchpad(connLabel?: string): Promise<void> {
357+
let conn: InsightsConnection | undefined;
358+
if (connLabel) {
359+
const retrievedConn = this.retrieveConnectedConnection(connLabel);
360+
if (retrievedConn instanceof InsightsConnection) {
361+
conn = retrievedConn;
362+
} else {
363+
kdbOutputLog(
364+
"[RESET SCRATCHPAD] Please connect to an Insights connection to use this feature.",
365+
"ERROR",
366+
);
367+
return;
368+
}
369+
}
370+
if (!conn) {
371+
if (
372+
!ext.activeConnection ||
373+
!(ext.activeConnection instanceof InsightsConnection)
374+
) {
375+
kdbOutputLog(
376+
"[RESET SCRATCHPAD] Please activate an Insights connection to use this feature.",
377+
"ERROR",
378+
);
379+
return;
380+
}
381+
conn = ext.activeConnection;
366382
}
367383

368-
if (
369-
ext.activeConnection.insightsVersion &&
370-
compareVersions(ext.activeConnection.insightsVersion, 1.12)
371-
) {
372-
const confirmationPrompt = `Are you sure you want to reset the scratchpad from the connection ${ext.activeConnection.connLabel}?`;
384+
if (conn.insightsVersion && compareVersions(conn.insightsVersion, 1.13)) {
385+
const confirmationPrompt = `Are you sure you want to reset the scratchpad from the connection ${conn.connLabel}?`;
373386
const selection = await window.showInformationMessage(
374387
confirmationPrompt,
375388
"Yes",
376389
"No",
377390
);
378391

379392
if (selection === "Yes") {
380-
await ext.activeConnection.resetScratchpad();
393+
await conn.resetScratchpad();
394+
} else {
395+
kdbOutputLog(
396+
"[RESET SCRATCHPAD] The user canceled the scratchpad reset.",
397+
"INFO",
398+
);
399+
return;
381400
}
382401
} else {
383402
kdbOutputLog(
384-
"[RESET SCRATCHPAD] Please connect to an Insights connection with version superior to 1.11",
403+
"[RESET SCRATCHPAD] Please connect to an Insights connection with version 1.13 or higher.",
385404
"ERROR",
386405
);
387406
}

test/suite/commands.test.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ describe("serverCommand", () => {
17971797
ConnectionManagementService.prototype,
17981798
"resetScratchpad",
17991799
);
1800-
await serverCommand.resetScratchPad();
1800+
await serverCommand.resetScratchpad();
18011801
sinon.assert.calledOnce(resetScratchpadStub);
18021802
sinon.restore();
18031803
});
@@ -2625,6 +2625,67 @@ describe("workspaceCommand", () => {
26252625
);
26262626
});
26272627
});
2628+
2629+
describe("resetScratchpadFromEditor", () => {
2630+
let getServerForUriStub: sinon.SinonStub;
2631+
let pickConnectionStub: sinon.SinonStub;
2632+
let getConnectionForServerStub: sinon.SinonStub;
2633+
let resetScratchpadStub: sinon.SinonStub;
2634+
2635+
const insightsNode = new InsightsNode(
2636+
[],
2637+
"insightsnode1",
2638+
{
2639+
server: "https://insightsservername.com/",
2640+
alias: "insightsserveralias",
2641+
auth: true,
2642+
},
2643+
vscode.TreeItemCollapsibleState.None,
2644+
);
2645+
2646+
beforeEach(() => {
2647+
ext.activeTextEditor = <vscode.TextEditor>{
2648+
options: { insertSpaces: true, indentSize: 4 },
2649+
selection: { active: new vscode.Position(0, 0) },
2650+
document: {
2651+
uri: vscode.Uri.file("/tmp/some.q"),
2652+
getText: () => "",
2653+
},
2654+
};
2655+
2656+
getServerForUriStub = sinon.stub(workspaceCommand, "getServerForUri");
2657+
pickConnectionStub = sinon.stub(workspaceCommand, "pickConnection");
2658+
getConnectionForServerStub = sinon
2659+
.stub(workspaceCommand, "getConnectionForServer")
2660+
.resolves(insightsNode);
2661+
resetScratchpadStub = sinon
2662+
.stub(serverCommand, "resetScratchpad")
2663+
.resolves();
2664+
});
2665+
2666+
afterEach(() => {
2667+
sinon.restore();
2668+
});
2669+
2670+
it("should call resetScratchpad with empty server label", async () => {
2671+
getServerForUriStub.returns("");
2672+
await workspaceCommand.resetScratchpadFromEditor();
2673+
assert.strictEqual(resetScratchpadStub.calledWith(""), true);
2674+
});
2675+
2676+
it("should set server to an empty string if no server is found", async () => {
2677+
getServerForUriStub.returns(undefined);
2678+
pickConnectionStub.resolves(undefined);
2679+
await workspaceCommand.resetScratchpadFromEditor();
2680+
assert.strictEqual(resetScratchpadStub.calledWith(""), true);
2681+
});
2682+
2683+
it("should not call resetScratchpad if activeTextEditor is not set", async () => {
2684+
ext.activeTextEditor = undefined;
2685+
await workspaceCommand.resetScratchpadFromEditor();
2686+
assert.strictEqual(resetScratchpadStub.called, false);
2687+
});
2688+
});
26282689
});
26292690

26302691
describe("clientCommands", () => {

test/suite/services.test.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,7 @@ describe("connectionManagerService", () => {
13531353

13541354
describe("resetScratchpad", () => {
13551355
let connMngService: ConnectionManagementService;
1356+
let retrieveConnectedConnectionStub: sinon.SinonStub;
13561357
let resetScratchpadStub: sinon.SinonStub;
13571358
let kdbOutputLogStub: sinon.SinonStub;
13581359
let showInformationMessageStub: sinon.SinonStub;
@@ -1361,7 +1362,11 @@ describe("connectionManagerService", () => {
13611362
beforeEach(() => {
13621363
connMngService = new ConnectionManagementService();
13631364
ext.activeConnection = insightsConn;
1364-
resetScratchpadStub = sinon.stub(ext.activeConnection, "resetScratchpad");
1365+
resetScratchpadStub = sinon.stub(insightsConn, "resetScratchpad");
1366+
retrieveConnectedConnectionStub = sinon.stub(
1367+
connMngService,
1368+
"retrieveConnectedConnection",
1369+
);
13651370
kdbOutputLogStub = sinon.stub(coreUtils, "kdbOutputLog");
13661371
showInformationMessageStub = sinon.stub(window, "showInformationMessage");
13671372
showErrorMessageStub = sinon.stub(window, "showErrorMessage");
@@ -1393,12 +1398,42 @@ describe("connectionManagerService", () => {
13931398

13941399
it("should reset the scratchpad if the active connection is an InsightsConnection", async () => {
13951400
ext.activeConnection = insightsConn;
1396-
ext.activeConnection.insightsVersion = 1.12;
1401+
ext.activeConnection.insightsVersion = 1.13;
13971402
showInformationMessageStub.resolves("Yes");
13981403
await connMngService.resetScratchpad();
13991404
sinon.assert.calledOnce(resetScratchpadStub);
14001405
});
14011406

1407+
it("should retrieve insights connection and procced with resetScratchpad", async () => {
1408+
insightsConn.insightsVersion = 1.13;
1409+
retrieveConnectedConnectionStub.returns(insightsConn);
1410+
showInformationMessageStub.resolves("Yes");
1411+
await connMngService.resetScratchpad("test");
1412+
sinon.assert.calledOnce(retrieveConnectedConnectionStub);
1413+
});
1414+
1415+
it("should retrieve insights connection and procced with resetScratchpad", async () => {
1416+
insightsConn.insightsVersion = 1.13;
1417+
retrieveConnectedConnectionStub.returns(insightsConn);
1418+
showInformationMessageStub.resolves("No");
1419+
await connMngService.resetScratchpad("test");
1420+
sinon.assert.calledWith(
1421+
kdbOutputLogStub,
1422+
"[RESET SCRATCHPAD] The user canceled the scratchpad reset.",
1423+
"INFO",
1424+
);
1425+
});
1426+
1427+
it("should retrieve kdb connection not proceed", async () => {
1428+
retrieveConnectedConnectionStub.returns(localConn);
1429+
await connMngService.resetScratchpad("test");
1430+
sinon.assert.calledWith(
1431+
kdbOutputLogStub,
1432+
"[RESET SCRATCHPAD] Please connect to an Insights connection to use this feature.",
1433+
"ERROR",
1434+
);
1435+
});
1436+
14021437
it("should log an error if insightsVersion is less than or equal to 1.11", async () => {
14031438
ext.activeConnection = insightsConn;
14041439
ext.activeConnection.insightsVersion = 1.11;

0 commit comments

Comments
 (0)