From 418cc785d202e7991701ef376632c7cac4fea6ec Mon Sep 17 00:00:00 2001 From: worksofliam Date: Tue, 10 Sep 2024 15:53:50 -0400 Subject: [PATCH 1/4] Ability to run a statement via URI Signed-off-by: worksofliam --- package.json | 29 ++++++++++++------- src/contributes.json | 11 ++++++++ src/extension.ts | 4 ++- src/uriHandler.ts | 66 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 src/uriHandler.ts diff --git a/package.json b/package.json index 1ac96712..2af4adb0 100644 --- a/package.json +++ b/package.json @@ -317,6 +317,11 @@ "title": "Generate JSON SQL parser", "category": "Db2 for i" }, + { + "command": "vscode-db2i.getStatementUri", + "title": "Get shared statement URI", + "category": "Db2 for i" + }, { "command": "vscode-db2i.refreshSchemaBrowser", "title": "Refresh Schema Browser", @@ -667,19 +672,11 @@ } ], "menus": { - "editor/context": [ + "commandPalette": [ { - "command": "vscode-db2i.json.pasteGenerator", - "group": "sql@1", + "command": "vscode-db2i.getStatementUri", "when": "editorLangId == sql" }, - { - "command": "vscode-db2i.json.pasteParser", - "group": "sql@2", - "when": "editorLangId == sql" - } - ], - "commandPalette": [ { "command": "vscode-db2i.setSchemaFilter", "when": "never" @@ -757,6 +754,18 @@ "when": "never" } ], + "editor/context": [ + { + "command": "vscode-db2i.json.pasteGenerator", + "group": "sql@1", + "when": "editorLangId == sql" + }, + { + "command": "vscode-db2i.json.pasteParser", + "group": "sql@2", + "when": "editorLangId == sql" + } + ], "view/title": [ { "command": "vscode-db2i.refreshSchemaBrowser", diff --git a/src/contributes.json b/src/contributes.json index 85f6d56b..6ded2246 100644 --- a/src/contributes.json +++ b/src/contributes.json @@ -94,9 +94,20 @@ "command": "vscode-db2i.json.pasteParser", "title": "Generate JSON SQL parser", "category": "Db2 for i" + }, + { + "command": "vscode-db2i.getStatementUri", + "title": "Get shared statement URI", + "category": "Db2 for i" } ], "menus": { + "commandPalette": [ + { + "command": "vscode-db2i.getStatementUri", + "when": "editorLangId == sql" + } + ], "editor/context": [ { "command": "vscode-db2i.json.pasteGenerator", diff --git a/src/extension.ts b/src/extension.ts index 17f69f8f..818a749c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -20,6 +20,7 @@ import { notebookInit } from "./notebooks/IBMiSerializer"; import { SelfTreeDecorationProvider, selfCodesResultsView } from "./views/jobManager/selfCodes/selfCodesResultsView"; import Configuration from "./configuration"; import { JDBCOptions } from "@ibm/mapepire-js/dist/src/types"; +import { Db2iUriHandler } from "./uriHandler"; export interface Db2i { sqlJobManager: SQLJobManager, @@ -66,7 +67,8 @@ export function activate(context: vscode.ExtensionContext): Db2i { ), vscode.window.registerFileDecorationProvider( new SelfTreeDecorationProvider() - ) + ), + vscode.window.registerUriHandler(new Db2iUriHandler()) ); JSONServices.initialise(context); diff --git a/src/uriHandler.ts b/src/uriHandler.ts new file mode 100644 index 00000000..a0adc57c --- /dev/null +++ b/src/uriHandler.ts @@ -0,0 +1,66 @@ +import { commands, env, Uri, UriHandler, window, workspace } from "vscode"; +import querystring from "querystring"; +import Document from "./language/sql/document"; +import { ServerComponent } from "./connection/serverComponent"; +import { remoteAssistIsEnabled } from "./language/providers/available"; + +export class Db2iUriHandler implements UriHandler { + handleUri(uri: Uri) { + const path = uri.path; + + switch (path) { + case '/sql': + const queryData = querystring.parse(uri.query); + const content = String(queryData.content).trim(); + + if (content) { + const asLower = content.toLowerCase(); + const isValid = asLower.startsWith(`select `) || asLower.startsWith(`with `); + + if (isValid) { + const run = queryData.run === `true`; + + if (run) { + if (remoteAssistIsEnabled()) { + commands.executeCommand(`vscode-db2i.runEditorStatement`, { + content, + qualifier: `statement`, + open: true, + }); + } else { + window.showErrorMessage(`You must be connected to a system to run a statement.`); + } + } else { + workspace.openTextDocument({ language: `sql`, content }).then(textDoc => { + window.showTextDocument(textDoc); + }); + } + } else { + window.showErrorMessage(`Only SELECT or WITH statements are supported.`); + } + } + + break; + } + } +} + +export const getStatementUri = commands.registerCommand(`vscode-db2i.getStatementUri`, async () => { + const editor = window.activeTextEditor; + + if (editor) { + const content = editor.document.getText(); + + const sqlDocument = new Document(content); + const cursor = editor.document.offsetAt(editor.selection.active); + + const currentStmt = sqlDocument.getGroupByOffset(cursor); + + if (currentStmt) { + const uri = `vscode://halcyontechltd.vscode-db2i/sql?content=${encodeURIComponent(sqlDocument.content)}`; + + env.clipboard.writeText(uri); + window.showInformationMessage(`Statement URI copied to clipboard.`); + } + } +}); \ No newline at end of file From 780be18bc9da12e5703db6bc6b80ff0cff0027db Mon Sep 17 00:00:00 2001 From: worksofliam Date: Tue, 10 Sep 2024 15:59:09 -0400 Subject: [PATCH 2/4] Add missing register for new command Signed-off-by: worksofliam --- src/extension.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 818a749c..fcd14bed 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -20,7 +20,7 @@ import { notebookInit } from "./notebooks/IBMiSerializer"; import { SelfTreeDecorationProvider, selfCodesResultsView } from "./views/jobManager/selfCodes/selfCodesResultsView"; import Configuration from "./configuration"; import { JDBCOptions } from "@ibm/mapepire-js/dist/src/types"; -import { Db2iUriHandler } from "./uriHandler"; +import { Db2iUriHandler, getStatementUri } from "./uriHandler"; export interface Db2i { sqlJobManager: SQLJobManager, @@ -68,7 +68,8 @@ export function activate(context: vscode.ExtensionContext): Db2i { vscode.window.registerFileDecorationProvider( new SelfTreeDecorationProvider() ), - vscode.window.registerUriHandler(new Db2iUriHandler()) + vscode.window.registerUriHandler(new Db2iUriHandler()), + getStatementUri ); JSONServices.initialise(context); From 147752b68814a3612135c91eb405de18684bbfe1 Mon Sep 17 00:00:00 2001 From: worksofliam Date: Tue, 10 Sep 2024 16:17:03 -0400 Subject: [PATCH 3/4] Fix broken copy Signed-off-by: worksofliam --- src/uriHandler.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/uriHandler.ts b/src/uriHandler.ts index a0adc57c..002433fd 100644 --- a/src/uriHandler.ts +++ b/src/uriHandler.ts @@ -1,4 +1,4 @@ -import { commands, env, Uri, UriHandler, window, workspace } from "vscode"; +import { commands, env, Selection, Uri, UriHandler, window, workspace } from "vscode"; import querystring from "querystring"; import Document from "./language/sql/document"; import { ServerComponent } from "./connection/serverComponent"; @@ -57,7 +57,9 @@ export const getStatementUri = commands.registerCommand(`vscode-db2i.getStatemen const currentStmt = sqlDocument.getGroupByOffset(cursor); if (currentStmt) { - const uri = `vscode://halcyontechltd.vscode-db2i/sql?content=${encodeURIComponent(sqlDocument.content)}`; + const stmtContent = content.substring(currentStmt.range.start, currentStmt.range.end); + editor.selection = new Selection(editor.document.positionAt(currentStmt.range.start), editor.document.positionAt(currentStmt.range.end)); + const uri = `vscode://halcyontechltd.vscode-db2i/sql?content=${encodeURIComponent(stmtContent)}`; env.clipboard.writeText(uri); window.showInformationMessage(`Statement URI copied to clipboard.`); From 84475dc5032e1311879a32d49213ec791f00f44e Mon Sep 17 00:00:00 2001 From: worksofliam Date: Tue, 10 Sep 2024 16:17:09 -0400 Subject: [PATCH 4/4] Add right click option Signed-off-by: worksofliam --- package.json | 7 ++++++- src/contributes.json | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 2af4adb0..24668f88 100644 --- a/package.json +++ b/package.json @@ -319,7 +319,7 @@ }, { "command": "vscode-db2i.getStatementUri", - "title": "Get shared statement URI", + "title": "Copy sharable statement URI", "category": "Db2 for i" }, { @@ -755,6 +755,11 @@ } ], "editor/context": [ + { + "command": "vscode-db2i.getStatementUri", + "group": "sql@0", + "when": "editorLangId == sql" + }, { "command": "vscode-db2i.json.pasteGenerator", "group": "sql@1", diff --git a/src/contributes.json b/src/contributes.json index 6ded2246..623eb652 100644 --- a/src/contributes.json +++ b/src/contributes.json @@ -97,7 +97,7 @@ }, { "command": "vscode-db2i.getStatementUri", - "title": "Get shared statement URI", + "title": "Copy sharable statement URI", "category": "Db2 for i" } ], @@ -109,6 +109,11 @@ } ], "editor/context": [ + { + "command": "vscode-db2i.getStatementUri", + "group": "sql@0", + "when": "editorLangId == sql" + }, { "command": "vscode-db2i.json.pasteGenerator", "group": "sql@1",