Skip to content

Commit

Permalink
Merge branch 'main' of github.com:halcyon-tech/vscode-db2i
Browse files Browse the repository at this point in the history
  • Loading branch information
worksofliam committed Sep 10, 2024
2 parents cd81fc4 + b2c5afb commit 6b04ac9
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 11 deletions.
34 changes: 24 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@
"title": "Generate JSON SQL parser",
"category": "Db2 for i"
},
{
"command": "vscode-db2i.getStatementUri",
"title": "Copy sharable statement URI",
"category": "Db2 for i"
},
{
"command": "vscode-db2i.refreshSchemaBrowser",
"title": "Refresh Schema Browser",
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -757,6 +754,23 @@
"when": "never"
}
],
"editor/context": [
{
"command": "vscode-db2i.getStatementUri",
"group": "sql@0",
"when": "editorLangId == sql"
},
{
"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",
Expand Down
16 changes: 16 additions & 0 deletions src/contributes.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,26 @@
"command": "vscode-db2i.json.pasteParser",
"title": "Generate JSON SQL parser",
"category": "Db2 for i"
},
{
"command": "vscode-db2i.getStatementUri",
"title": "Copy sharable statement URI",
"category": "Db2 for i"
}
],
"menus": {
"commandPalette": [
{
"command": "vscode-db2i.getStatementUri",
"when": "editorLangId == sql"
}
],
"editor/context": [
{
"command": "vscode-db2i.getStatementUri",
"group": "sql@0",
"when": "editorLangId == sql"
},
{
"command": "vscode-db2i.json.pasteGenerator",
"group": "sql@1",
Expand Down
5 changes: 4 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, getStatementUri } from "./uriHandler";

export interface Db2i {
sqlJobManager: SQLJobManager,
Expand Down Expand Up @@ -66,7 +67,9 @@ export function activate(context: vscode.ExtensionContext): Db2i {
),
vscode.window.registerFileDecorationProvider(
new SelfTreeDecorationProvider()
)
),
vscode.window.registerUriHandler(new Db2iUriHandler()),
getStatementUri
);

JSONServices.initialise(context);
Expand Down
68 changes: 68 additions & 0 deletions src/uriHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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";
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 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.`);
}
}
});

0 comments on commit 6b04ac9

Please sign in to comment.