Skip to content

Commit 09dba79

Browse files
New actions to add, remove, edit
Signed-off-by: Sanjula Ganepola <[email protected]>
1 parent c3eb49f commit 09dba79

File tree

4 files changed

+140
-5
lines changed

4 files changed

+140
-5
lines changed

package.json

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,21 @@
793793
"category": "Db2 for i (Examples)",
794794
"icon": "$(refresh)"
795795
},
796+
{
797+
"command": "vscode-db2i.examples.add",
798+
"title": "Add...",
799+
"category": "Db2 for i (Examples)"
800+
},
801+
{
802+
"command": "vscode-db2i.examples.remove",
803+
"title": "Remove...",
804+
"category": "Db2 for i (Examples)"
805+
},
806+
{
807+
"command": "vscode-db2i.examples.edit",
808+
"title": "Edit Example",
809+
"category": "Db2 for i (Examples)"
810+
},
796811
{
797812
"command": "vscode-db2i.notebook.fromSqlUri",
798813
"title": "Open as Notebook",
@@ -1100,9 +1115,14 @@
11001115
"when": "view == exampleBrowser"
11011116
},
11021117
{
1103-
"command": "vscode-db2i.examples.reload",
1118+
"submenu": "vscode-db2i.customExampleDirectories",
11041119
"group": "navigation@2",
11051120
"when": "view == exampleBrowser"
1121+
},
1122+
{
1123+
"command": "vscode-db2i.examples.reload",
1124+
"group": "navigation@3",
1125+
"when": "view == exampleBrowser"
11061126
}
11071127
],
11081128
"view/item/context": [
@@ -1274,6 +1294,11 @@
12741294
"command": "vscode-db2i.self.explainSelf",
12751295
"when": "view == vscode-db2i.self.nodes && viewItem == selfCodeNode && vscode-db2i:continueExtensionActive",
12761296
"group": "navigation"
1297+
},
1298+
{
1299+
"command": "vscode-db2i.examples.edit",
1300+
"when": "view == exampleBrowser && viewItem == example.custom",
1301+
"group": "0_open"
12771302
}
12781303
],
12791304
"editor/title": [
@@ -1348,6 +1373,11 @@
13481373
"icon": "$(notebook-execute)",
13491374
"id": "sql/editor/context",
13501375
"label": "Run SQL statement"
1376+
},
1377+
{
1378+
"id": "vscode-db2i.customExampleDirectories",
1379+
"label": "Custom Example Directories",
1380+
"icon": "$(sparkle)"
13511381
}
13521382
],
13531383
"keybindings": [
@@ -1470,6 +1500,18 @@
14701500
"contents": "🛠️ SELF Codes will appear here. You can set the SELF log level on specific jobs, or you can set the default for new jobs in the User Settings.\n\n[Set Default for New Jobs](command:vscode-db2i.jobManager.defaultSettings)\n\n[Learn about SELF](command:vscode-db2i.self.help)"
14711501
}
14721502
],
1503+
"vscode-db2i.customExampleDirectories": [
1504+
{
1505+
"command": "vscode-db2i.examples.add",
1506+
"group": "navigation@0",
1507+
"when": "view == exampleBrowser"
1508+
},
1509+
{
1510+
"command": "vscode-db2i.examples.remove",
1511+
"group": "navigation@1",
1512+
"when": "view == exampleBrowser"
1513+
}
1514+
],
14731515
"notebooks": [
14741516
{
14751517
"id": "db2i-notebook",

src/views/examples/contributes.json

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,40 @@
2828
"title": "Refresh Examples",
2929
"category": "Db2 for i (Examples)",
3030
"icon": "$(refresh)"
31+
},
32+
{
33+
"command": "vscode-db2i.examples.add",
34+
"title": "Add...",
35+
"category": "Db2 for i (Examples)"
36+
},
37+
{
38+
"command": "vscode-db2i.examples.remove",
39+
"title": "Remove...",
40+
"category": "Db2 for i (Examples)"
41+
},
42+
{
43+
"command": "vscode-db2i.examples.edit",
44+
"title": "Edit Example",
45+
"category": "Db2 for i (Examples)"
46+
}
47+
],
48+
"submenus": [
49+
{
50+
"id": "vscode-db2i.customExampleDirectories",
51+
"label": "Custom Example Directories",
52+
"icon": "$(sparkle)"
53+
}
54+
],
55+
"vscode-db2i.customExampleDirectories": [
56+
{
57+
"command": "vscode-db2i.examples.add",
58+
"group": "navigation@0",
59+
"when": "view == exampleBrowser"
60+
},
61+
{
62+
"command": "vscode-db2i.examples.remove",
63+
"group": "navigation@1",
64+
"when": "view == exampleBrowser"
3165
}
3266
],
3367
"menus": {
@@ -43,9 +77,21 @@
4377
"when": "view == exampleBrowser"
4478
},
4579
{
46-
"command": "vscode-db2i.examples.reload",
80+
"submenu": "vscode-db2i.customExampleDirectories",
4781
"group": "navigation@2",
4882
"when": "view == exampleBrowser"
83+
},
84+
{
85+
"command": "vscode-db2i.examples.reload",
86+
"group": "navigation@3",
87+
"when": "view == exampleBrowser"
88+
}
89+
],
90+
"view/item/context": [
91+
{
92+
"command": "vscode-db2i.examples.edit",
93+
"when": "view == exampleBrowser && viewItem == example.custom",
94+
"group": "0_open"
4995
}
5096
]
5197
},

src/views/examples/exampleBrowser.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Event, EventEmitter, ExtensionContext, MarkdownString, ThemeIcon, TreeD
22
import { Examples, SQLExample, ServiceInfoLabel, getMergedExamples } from ".";
33
import { notebookFromStatements } from "../../notebooks/logic/openAsNotebook";
44
import { osDetail } from "../../config";
5+
import Configuration from "../../configuration";
56

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

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

52+
commands.registerCommand("vscode-db2i.examples.add", async () => {
53+
const dirsToAdd = await window.showOpenDialog({
54+
title: "Add Custom Example Directory",
55+
canSelectFolders: true,
56+
canSelectFiles: false,
57+
canSelectMany: true
58+
});
59+
60+
if (dirsToAdd && dirsToAdd.length > 0) {
61+
const existingDirectories = Configuration.get<string[]>(`examples.customExampleDirectories`) || [];
62+
const newDirectoryPaths = dirsToAdd.map(dir => dir.fsPath);
63+
const updatedDirectories = Array.from(new Set([...existingDirectories, ...newDirectoryPaths]));
64+
await Configuration.set(`examples.customExampleDirectories`, updatedDirectories);
65+
}
66+
}),
67+
68+
commands.registerCommand("vscode-db2i.examples.remove", async () => {
69+
const existingDirectories = Configuration.get<string[]>(`examples.customExampleDirectories`) || [];
70+
if (existingDirectories.length === 0) {
71+
window.showErrorMessage(`No custom example directories to remove.`);
72+
return;
73+
}
74+
75+
const quickPickItems = existingDirectories.map(dir => ({ label: dir }));
76+
const selectedDirectories = await window.showQuickPick(quickPickItems, {
77+
title: `Select custom example directories to remove`,
78+
canPickMany: true
79+
});
80+
81+
if (selectedDirectories && selectedDirectories.length > 0) {
82+
const dirsToRemove = selectedDirectories.map(item => item.label);
83+
const updatedDirectories = existingDirectories.filter(dir => !dirsToRemove.includes(dir));
84+
await Configuration.set(`examples.customExampleDirectories`, updatedDirectories);
85+
}
86+
}),
87+
88+
commands.registerCommand("vscode-db2i.examples.edit", async (item: SQLExampleItem) => {
89+
if (item.example.customFileUri) {
90+
const document = await workspace.openTextDocument(item.example.customFileUri);
91+
await window.showTextDocument(document);
92+
}
93+
}),
94+
5195
workspace.onDidChangeConfiguration(e => {
5296
if (e.affectsConfiguration('vscode-db2i.examples.customExampleDirectories')) {
5397
this.refresh();
@@ -64,7 +108,7 @@ export class ExampleBrowser implements TreeDataProvider<any> {
64108
return element;
65109
}
66110

67-
async getChildren(element?: ExampleGroupItem): Promise<SQLExampleItem[]> {
111+
async getChildren(element?: ExampleGroupItem): Promise<TreeItem[]> {
68112
if (element) {
69113
return element.getChildren();
70114
}
@@ -104,11 +148,12 @@ class ExampleGroupItem extends TreeItem {
104148
}
105149

106150
class SQLExampleItem extends TreeItem {
107-
constructor(example: SQLExample) {
151+
constructor(public example: SQLExample) {
108152
super(example.name, TreeItemCollapsibleState.None);
109153
this.iconPath = ThemeIcon.File;
110154
this.resourceUri = Uri.parse('_.sql');
111155
this.tooltip = new MarkdownString(['```sql', example.content.join(`\n`), '```'].join(`\n`));
156+
this.contextValue = example.customFileUri ? `example.custom` : `example`;
112157

113158
this.command = {
114159
command: openExampleCommand,

src/views/examples/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface SQLExample {
1818
content: string[];
1919
requirements?: ExampleSystemRequirements;
2020
isNotebook?: boolean;
21+
customFileUri?: Uri;
2122
};
2223

2324
// Unlike the bulk of the examples defined below, the services examples are retrieved dynamically
@@ -6057,7 +6058,8 @@ export async function getCustomExamples(): Promise<SQLExamplesList> {
60576058

60586059
examplesList[group].push({
60596060
name: name,
6060-
content: textDocument.getText().split(eol)
6061+
content: textDocument.getText().split(eol),
6062+
customFileUri: textDocument.uri
60616063
});
60626064
}
60636065

0 commit comments

Comments
 (0)