Skip to content

Commit 6b04ac9

Browse files
committed
Merge branch 'main' of github.com:halcyon-tech/vscode-db2i
2 parents cd81fc4 + b2c5afb commit 6b04ac9

File tree

4 files changed

+112
-11
lines changed

4 files changed

+112
-11
lines changed

package.json

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@
317317
"title": "Generate JSON SQL parser",
318318
"category": "Db2 for i"
319319
},
320+
{
321+
"command": "vscode-db2i.getStatementUri",
322+
"title": "Copy sharable statement URI",
323+
"category": "Db2 for i"
324+
},
320325
{
321326
"command": "vscode-db2i.refreshSchemaBrowser",
322327
"title": "Refresh Schema Browser",
@@ -667,19 +672,11 @@
667672
}
668673
],
669674
"menus": {
670-
"editor/context": [
675+
"commandPalette": [
671676
{
672-
"command": "vscode-db2i.json.pasteGenerator",
673-
"group": "sql@1",
677+
"command": "vscode-db2i.getStatementUri",
674678
"when": "editorLangId == sql"
675679
},
676-
{
677-
"command": "vscode-db2i.json.pasteParser",
678-
"group": "sql@2",
679-
"when": "editorLangId == sql"
680-
}
681-
],
682-
"commandPalette": [
683680
{
684681
"command": "vscode-db2i.setSchemaFilter",
685682
"when": "never"
@@ -757,6 +754,23 @@
757754
"when": "never"
758755
}
759756
],
757+
"editor/context": [
758+
{
759+
"command": "vscode-db2i.getStatementUri",
760+
"group": "sql@0",
761+
"when": "editorLangId == sql"
762+
},
763+
{
764+
"command": "vscode-db2i.json.pasteGenerator",
765+
"group": "sql@1",
766+
"when": "editorLangId == sql"
767+
},
768+
{
769+
"command": "vscode-db2i.json.pasteParser",
770+
"group": "sql@2",
771+
"when": "editorLangId == sql"
772+
}
773+
],
760774
"view/title": [
761775
{
762776
"command": "vscode-db2i.refreshSchemaBrowser",

src/contributes.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,26 @@
9494
"command": "vscode-db2i.json.pasteParser",
9595
"title": "Generate JSON SQL parser",
9696
"category": "Db2 for i"
97+
},
98+
{
99+
"command": "vscode-db2i.getStatementUri",
100+
"title": "Copy sharable statement URI",
101+
"category": "Db2 for i"
97102
}
98103
],
99104
"menus": {
105+
"commandPalette": [
106+
{
107+
"command": "vscode-db2i.getStatementUri",
108+
"when": "editorLangId == sql"
109+
}
110+
],
100111
"editor/context": [
112+
{
113+
"command": "vscode-db2i.getStatementUri",
114+
"group": "sql@0",
115+
"when": "editorLangId == sql"
116+
},
101117
{
102118
"command": "vscode-db2i.json.pasteGenerator",
103119
"group": "sql@1",

src/extension.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { notebookInit } from "./notebooks/IBMiSerializer";
2020
import { SelfTreeDecorationProvider, selfCodesResultsView } from "./views/jobManager/selfCodes/selfCodesResultsView";
2121
import Configuration from "./configuration";
2222
import { JDBCOptions } from "@ibm/mapepire-js/dist/src/types";
23+
import { Db2iUriHandler, getStatementUri } from "./uriHandler";
2324

2425
export interface Db2i {
2526
sqlJobManager: SQLJobManager,
@@ -66,7 +67,9 @@ export function activate(context: vscode.ExtensionContext): Db2i {
6667
),
6768
vscode.window.registerFileDecorationProvider(
6869
new SelfTreeDecorationProvider()
69-
)
70+
),
71+
vscode.window.registerUriHandler(new Db2iUriHandler()),
72+
getStatementUri
7073
);
7174

7275
JSONServices.initialise(context);

src/uriHandler.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { commands, env, Selection, Uri, UriHandler, window, workspace } from "vscode";
2+
import querystring from "querystring";
3+
import Document from "./language/sql/document";
4+
import { ServerComponent } from "./connection/serverComponent";
5+
import { remoteAssistIsEnabled } from "./language/providers/available";
6+
7+
export class Db2iUriHandler implements UriHandler {
8+
handleUri(uri: Uri) {
9+
const path = uri.path;
10+
11+
switch (path) {
12+
case '/sql':
13+
const queryData = querystring.parse(uri.query);
14+
const content = String(queryData.content).trim();
15+
16+
if (content) {
17+
const asLower = content.toLowerCase();
18+
const isValid = asLower.startsWith(`select `) || asLower.startsWith(`with `);
19+
20+
if (isValid) {
21+
const run = queryData.run === `true`;
22+
23+
if (run) {
24+
if (remoteAssistIsEnabled()) {
25+
commands.executeCommand(`vscode-db2i.runEditorStatement`, {
26+
content,
27+
qualifier: `statement`,
28+
open: true,
29+
});
30+
} else {
31+
window.showErrorMessage(`You must be connected to a system to run a statement.`);
32+
}
33+
} else {
34+
workspace.openTextDocument({ language: `sql`, content }).then(textDoc => {
35+
window.showTextDocument(textDoc);
36+
});
37+
}
38+
} else {
39+
window.showErrorMessage(`Only SELECT or WITH statements are supported.`);
40+
}
41+
}
42+
43+
break;
44+
}
45+
}
46+
}
47+
48+
export const getStatementUri = commands.registerCommand(`vscode-db2i.getStatementUri`, async () => {
49+
const editor = window.activeTextEditor;
50+
51+
if (editor) {
52+
const content = editor.document.getText();
53+
54+
const sqlDocument = new Document(content);
55+
const cursor = editor.document.offsetAt(editor.selection.active);
56+
57+
const currentStmt = sqlDocument.getGroupByOffset(cursor);
58+
59+
if (currentStmt) {
60+
const stmtContent = content.substring(currentStmt.range.start, currentStmt.range.end);
61+
editor.selection = new Selection(editor.document.positionAt(currentStmt.range.start), editor.document.positionAt(currentStmt.range.end));
62+
const uri = `vscode://halcyontechltd.vscode-db2i/sql?content=${encodeURIComponent(stmtContent)}`;
63+
64+
env.clipboard.writeText(uri);
65+
window.showInformationMessage(`Statement URI copied to clipboard.`);
66+
}
67+
}
68+
});

0 commit comments

Comments
 (0)