Skip to content

Commit 8acd56b

Browse files
committed
Allowed function to run in right click menu
1 parent d56ef99 commit 8acd56b

File tree

2 files changed

+59
-30
lines changed

2 files changed

+59
-30
lines changed

src/contributes.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,23 @@
114114
{
115115
"command": "vscode-db2i.importData",
116116
"title": "Generate Insert from File",
117-
"category": "Db2 for IBM i"
117+
"category": "Db2 for i"
118+
},
119+
{
120+
"command": "vscode-db2i.importDataContextMenu",
121+
"title": "Generate Insert from File",
122+
"category": "Db2 for i"
118123
}
119124
],
120125
"menus": {
121126
"commandPalette": [
122127
{
123128
"command": "vscode-db2i.getStatementUri",
124129
"when": "editorLangId == sql"
130+
},
131+
{
132+
"command": "vscode-db2i.importDataContextMenu",
133+
"when": "false"
125134
}
126135
],
127136
"editor/context": [
@@ -139,6 +148,11 @@
139148
"command": "vscode-db2i.json.pasteParser",
140149
"group": "sql@2",
141150
"when": "editorLangId == sql"
151+
},
152+
{
153+
"command": "vscode-db2i.importDataContextMenu",
154+
"group": "sql@3",
155+
"when": "editorLangId == csv || editorLangId == json "
142156
}
143157
]
144158
}

src/views/schemaBrowser/index.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,55 @@ export default class schemaBrowser {
397397
}
398398
}),
399399

400-
vscode.commands.registerCommand(`vscode-db2i.importData`, async () => {
401-
vscode.window.withProgress({ location: vscode.ProgressLocation.Window, title: `Generating SQL` }, async () => {
400+
vscode.commands.registerCommand(`vscode-db2i.importDataContextMenu`, async () => {
401+
vscode.window.withProgress({ location: vscode.ProgressLocation.Window, title: `Generating SQL` }, async (arg?: any) => {
402402
try {
403+
const data = vscode.window.activeTextEditor.document.getText();
404+
const uri = vscode.window.activeTextEditor.document.uri;
405+
await this.generateInsert(uri, data);
406+
} catch (e) {
407+
vscode.window.showErrorMessage(e.message);
408+
}
409+
});
410+
}),
411+
412+
vscode.commands.registerCommand(`vscode-db2i.importData`, async () => {
413+
vscode.window.withProgress({ location: vscode.ProgressLocation.Window, title: `Generating SQL` }, async (arg?: any) => {
414+
try {
403415
const uri = await this.pickFile();
404416
if (!uri) { return; }
405417
const data = await this.readFile(uri);
406-
const tableName = `SYSIBM.SYSDUMMY1`;
407-
let ext: string = (uri.fsPath.split('.').pop() || '').toLowerCase();
408418

419+
await this.generateInsert(uri, data);
420+
} catch (e) {
421+
vscode.window.showErrorMessage(e.message);
422+
}
423+
});
424+
})
425+
)
426+
427+
getInstance().subscribe(context, `connected`, `db2i-clearCacheAndRefresh`, () => this.clearCacheAndRefresh());
428+
}
429+
430+
async pickFile(): Promise<vscode.Uri | undefined> {
431+
const res = await vscode.window.showOpenDialog({
432+
canSelectMany: false,
433+
openLabel: 'Import',
434+
filters: {
435+
'Data files': ['csv', 'json'],
436+
'All files': ['*']
437+
}
438+
});
439+
return res?.[0];
440+
}
441+
442+
async readFile(uri: vscode.Uri): Promise<string> {
443+
const ab = await vscode.workspace.fs.readFile(uri);
444+
return new TextDecoder('utf-8').decode(ab);
445+
}
446+
447+
async generateInsert(uri: vscode.Uri, data: string) {
448+
let ext: string = (uri.fsPath.split('.').pop() || '').toLowerCase();
409449
if (ext != `csv` && ext != `json`) {
410450
ext = await vscode.window.showQuickPick(['csv','json'], { placeHolder: 'What format is this file?' });
411451
if (!ext) { return; }
@@ -467,31 +507,6 @@ export default class schemaBrowser {
467507
// Open the generated SQL in a new file
468508
const textDoc = await vscode.workspace.openTextDocument({ language: `sql`, content });
469509
await vscode.window.showTextDocument(textDoc);
470-
} catch (e) {
471-
vscode.window.showErrorMessage(e.message);
472-
}
473-
});
474-
})
475-
)
476-
477-
getInstance().subscribe(context, `connected`, `db2i-clearCacheAndRefresh`, () => this.clearCacheAndRefresh());
478-
}
479-
480-
async pickFile(): Promise<vscode.Uri | undefined> {
481-
const res = await vscode.window.showOpenDialog({
482-
canSelectMany: false,
483-
openLabel: 'Import',
484-
filters: {
485-
'Data files': ['csv', 'json'],
486-
'All files': ['*']
487-
}
488-
});
489-
return res?.[0];
490-
}
491-
492-
async readFile(uri: vscode.Uri): Promise<string> {
493-
const ab = await vscode.workspace.fs.readFile(uri);
494-
return new TextDecoder('utf-8').decode(ab);
495510
}
496511

497512
clearCacheAndRefresh() {

0 commit comments

Comments
 (0)