Skip to content

Commit 06c7868

Browse files
Add support for saving new examples
Signed-off-by: Sanjula Ganepola <[email protected]>
1 parent 09dba79 commit 06c7868

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,12 @@
793793
"category": "Db2 for i (Examples)",
794794
"icon": "$(refresh)"
795795
},
796+
{
797+
"command": "vscode-db2i.examples.save",
798+
"title": "Save As New Example",
799+
"category": "Db2 for i (Examples)",
800+
"icon": "$(save)"
801+
},
796802
{
797803
"command": "vscode-db2i.examples.add",
798804
"title": "Add...",
@@ -1115,14 +1121,19 @@
11151121
"when": "view == exampleBrowser"
11161122
},
11171123
{
1118-
"submenu": "vscode-db2i.customExampleDirectories",
1124+
"command": "vscode-db2i.examples.save",
11191125
"group": "navigation@2",
11201126
"when": "view == exampleBrowser"
11211127
},
11221128
{
1123-
"command": "vscode-db2i.examples.reload",
1129+
"submenu": "vscode-db2i.customExampleDirectories",
11241130
"group": "navigation@3",
11251131
"when": "view == exampleBrowser"
1132+
},
1133+
{
1134+
"command": "vscode-db2i.examples.reload",
1135+
"group": "navigation@4",
1136+
"when": "view == exampleBrowser"
11261137
}
11271138
],
11281139
"view/item/context": [

src/views/examples/contributes.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
"category": "Db2 for i (Examples)",
3030
"icon": "$(refresh)"
3131
},
32+
{
33+
"command": "vscode-db2i.examples.save",
34+
"title": "Save As New Example",
35+
"category": "Db2 for i (Examples)",
36+
"icon": "$(save)"
37+
},
3238
{
3339
"command": "vscode-db2i.examples.add",
3440
"title": "Add...",
@@ -77,19 +83,24 @@
7783
"when": "view == exampleBrowser"
7884
},
7985
{
80-
"submenu": "vscode-db2i.customExampleDirectories",
86+
"command": "vscode-db2i.examples.save",
8187
"group": "navigation@2",
8288
"when": "view == exampleBrowser"
8389
},
8490
{
85-
"command": "vscode-db2i.examples.reload",
91+
"submenu": "vscode-db2i.customExampleDirectories",
8692
"group": "navigation@3",
8793
"when": "view == exampleBrowser"
94+
},
95+
{
96+
"command": "vscode-db2i.examples.reload",
97+
"group": "navigation@4",
98+
"when": "view == exampleBrowser"
8899
}
89100
],
90101
"view/item/context": [
91102
{
92-
"command": "vscode-db2i.examples.edit",
103+
"command": "vscode-db2i.examples.edit",
93104
"when": "view == exampleBrowser && viewItem == example.custom",
94105
"group": "0_open"
95106
}

src/views/examples/exampleBrowser.ts

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Event, EventEmitter, ExtensionContext, MarkdownString, ThemeIcon, TreeDataProvider, TreeItem, TreeItemCollapsibleState, Uri, commands, window, workspace } from "vscode";
1+
import { Event, EventEmitter, ExtensionContext, MarkdownString, TextDocument, ThemeIcon, TreeDataProvider, TreeItem, TreeItemCollapsibleState, Uri, commands, window, workspace } from "vscode";
22
import { Examples, SQLExample, ServiceInfoLabel, getMergedExamples } from ".";
33
import { notebookFromStatements } from "../../notebooks/logic/openAsNotebook";
44
import { osDetail } from "../../config";
55
import Configuration from "../../configuration";
6+
import * as path from 'path';
67

78
export const openExampleCommand = `vscode-db2i.examples.open`;
89

@@ -49,6 +50,61 @@ export class ExampleBrowser implements TreeDataProvider<any> {
4950
this.refresh();
5051
}),
5152

53+
commands.registerCommand("vscode-db2i.examples.save", async () => {
54+
const editor = window.activeTextEditor;
55+
if (editor) {
56+
const document = editor.document;
57+
if (document.languageId === `sql`) {
58+
const existingDirectories = Configuration.get<string[]>(`examples.customExampleDirectories`) || [];
59+
if (existingDirectories.length === 0) {
60+
window.showErrorMessage(`You must first add a custom examples directory before saving new examples.`, { modal: true }, `Add Custom Examples Directory`).then(async selection => {
61+
if (selection === `Add Custom Examples Directory`) {
62+
const isAdded = await commands.executeCommand(`vscode-db2i.examples.add`);
63+
if (isAdded) {
64+
await commands.executeCommand(`vscode-db2i.examples.save`);
65+
}
66+
67+
return;
68+
};
69+
});
70+
} else {
71+
const quickPickItems = existingDirectories.map(dir => ({ label: dir }));
72+
const selectedDirectory = await window.showQuickPick(quickPickItems, {
73+
title: `Select a custom example directories to save this example to`
74+
});
75+
if (selectedDirectory) {
76+
const suggestedFileName = path.basename(document.fileName);
77+
let exampleFileName = await window.showInputBox({
78+
title: `Example file name`,
79+
prompt: `Enter example file name`,
80+
value: suggestedFileName,
81+
});
82+
83+
if (exampleFileName) {
84+
if (!exampleFileName.includes(`.`)) {
85+
exampleFileName = `${exampleFileName}.sql`;
86+
}
87+
88+
try {
89+
const filePath = Uri.joinPath(Uri.file(selectedDirectory.label), exampleFileName);
90+
const fileContent = Buffer.from(document.getText(), 'utf8')
91+
await workspace.fs.writeFile(filePath, fileContent);
92+
window.showInformationMessage(`Example saved to ${filePath.fsPath}`);
93+
this.refresh();
94+
} catch (error) {
95+
window.showErrorMessage(`Failed to save example: ${error}`);
96+
}
97+
}
98+
}
99+
}
100+
} else {
101+
window.showErrorMessage(`The active document is not a SQL file.`);
102+
}
103+
} else {
104+
window.showErrorMessage(`No SQL file open.`);
105+
}
106+
}),
107+
52108
commands.registerCommand("vscode-db2i.examples.add", async () => {
53109
const dirsToAdd = await window.showOpenDialog({
54110
title: "Add Custom Example Directory",
@@ -62,6 +118,9 @@ export class ExampleBrowser implements TreeDataProvider<any> {
62118
const newDirectoryPaths = dirsToAdd.map(dir => dir.fsPath);
63119
const updatedDirectories = Array.from(new Set([...existingDirectories, ...newDirectoryPaths]));
64120
await Configuration.set(`examples.customExampleDirectories`, updatedDirectories);
121+
return true;
122+
} else {
123+
return false;
65124
}
66125
}),
67126

@@ -74,7 +133,7 @@ export class ExampleBrowser implements TreeDataProvider<any> {
74133

75134
const quickPickItems = existingDirectories.map(dir => ({ label: dir }));
76135
const selectedDirectories = await window.showQuickPick(quickPickItems, {
77-
title: `Select custom example directories to remove`,
136+
title: `Select a custom example directories to remove`,
78137
canPickMany: true
79138
});
80139

@@ -96,7 +155,7 @@ export class ExampleBrowser implements TreeDataProvider<any> {
96155
if (e.affectsConfiguration('vscode-db2i.examples.customExampleDirectories')) {
97156
this.refresh();
98157
}
99-
}),
158+
})
100159
);
101160
}
102161

0 commit comments

Comments
 (0)