Skip to content

Commit 9f07534

Browse files
authored
Merge pull request #436 from codefori/feature/custom-examples
Add support for adding custom examples to `Examples` view
2 parents 68c7254 + 970deb5 commit 9f07534

File tree

6 files changed

+670
-245
lines changed

6 files changed

+670
-245
lines changed

package.json

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,21 @@
277277
}
278278
}
279279
},
280+
{
281+
"id": "vscode-db2i.examples",
282+
"title": "Examples",
283+
"properties": {
284+
"vscode-db2i.examples.customExampleDirectories": {
285+
"type": "array",
286+
"items": {
287+
"type": "string",
288+
"description": "The directory containing SQL example files."
289+
},
290+
"markdownDescription": "Set of custom directories containing SQL example files to be shown in the `Examples` view. All SQL files in the specified directories and at most one subdirectory level deeper will be picked up.\n\nBy default, the folder name will be the category and the file name will be the name of the example. This can be customized by optionally including a comment in the file with the tags `category` and `description`.",
291+
"default": []
292+
}
293+
}
294+
},
280295
{
281296
"id": "vscode-db2i.syntax",
282297
"title": "SQL Syntax Options",
@@ -804,6 +819,33 @@
804819
"category": "Db2 for i (Examples)",
805820
"icon": "$(clear-all)"
806821
},
822+
{
823+
"command": "vscode-db2i.examples.reload",
824+
"title": "Refresh Examples",
825+
"category": "Db2 for i (Examples)",
826+
"icon": "$(refresh)"
827+
},
828+
{
829+
"command": "vscode-db2i.examples.save",
830+
"title": "Save As New Example",
831+
"category": "Db2 for i (Examples)",
832+
"icon": "$(save)"
833+
},
834+
{
835+
"command": "vscode-db2i.examples.add",
836+
"title": "Add...",
837+
"category": "Db2 for i (Examples)"
838+
},
839+
{
840+
"command": "vscode-db2i.examples.remove",
841+
"title": "Remove...",
842+
"category": "Db2 for i (Examples)"
843+
},
844+
{
845+
"command": "vscode-db2i.examples.edit",
846+
"title": "Edit Example",
847+
"category": "Db2 for i (Examples)"
848+
},
807849
{
808850
"command": "vscode-db2i.notebook.fromSqlUri",
809851
"title": "Open as Notebook",
@@ -966,6 +1008,10 @@
9661008
"command": "vscode-db2i.jobManager.deleteConfig",
9671009
"when": "never"
9681010
},
1011+
{
1012+
"command": "vscode-db2i.examples.edit",
1013+
"when": "never"
1014+
},
9691015
{
9701016
"command": "vscode-db2i.notebook.fromSqlUri",
9711017
"when": "never"
@@ -1039,13 +1085,13 @@
10391085
"when": "view == vscode-db2i.dove.node"
10401086
},
10411087
{
1042-
"command": "vscode-db2i.queryHistory.clear",
1043-
"group": "navigation",
1088+
"command": "vscode-db2i.queryHistory.find",
1089+
"group": "navigation@0",
10441090
"when": "view == queryHistory"
10451091
},
10461092
{
1047-
"command": "vscode-db2i.queryHistory.find",
1048-
"group": "navigation",
1093+
"command": "vscode-db2i.queryHistory.clear",
1094+
"group": "navigation@1",
10491095
"when": "view == queryHistory"
10501096
},
10511097
{
@@ -1110,12 +1156,27 @@
11101156
},
11111157
{
11121158
"command": "vscode-db2i.examples.setFilter",
1113-
"group": "navigation",
1159+
"group": "navigation@0",
11141160
"when": "view == exampleBrowser"
11151161
},
11161162
{
11171163
"command": "vscode-db2i.examples.clearFilter",
1118-
"group": "navigation",
1164+
"group": "navigation@1",
1165+
"when": "view == exampleBrowser"
1166+
},
1167+
{
1168+
"command": "vscode-db2i.examples.save",
1169+
"group": "navigation@2",
1170+
"when": "view == exampleBrowser"
1171+
},
1172+
{
1173+
"submenu": "vscode-db2i.customExampleDirectories",
1174+
"group": "navigation@3",
1175+
"when": "view == exampleBrowser"
1176+
},
1177+
{
1178+
"command": "vscode-db2i.examples.reload",
1179+
"group": "navigation@4",
11191180
"when": "view == exampleBrowser"
11201181
}
11211182
],
@@ -1288,6 +1349,11 @@
12881349
"command": "vscode-db2i.self.explainSelf",
12891350
"when": "view == vscode-db2i.self.nodes && viewItem == selfCodeNode && vscode-db2i:continueExtensionActive",
12901351
"group": "navigation"
1352+
},
1353+
{
1354+
"command": "vscode-db2i.examples.edit",
1355+
"when": "view == exampleBrowser && viewItem == example.custom",
1356+
"group": "0_open"
12911357
}
12921358
],
12931359
"editor/title": [
@@ -1349,6 +1415,16 @@
13491415
"group": "navigation_notebook@1"
13501416
}
13511417
],
1418+
"vscode-db2i.customExampleDirectories": [
1419+
{
1420+
"command": "vscode-db2i.examples.add",
1421+
"group": "navigation@0"
1422+
},
1423+
{
1424+
"command": "vscode-db2i.examples.remove",
1425+
"group": "navigation@1"
1426+
}
1427+
],
13521428
"notebook/toolbar": [
13531429
{
13541430
"command": "vscode-db2i.notebook.exportAsHtml",
@@ -1362,6 +1438,11 @@
13621438
"icon": "$(notebook-execute)",
13631439
"id": "sql/editor/context",
13641440
"label": "Run SQL statement"
1441+
},
1442+
{
1443+
"id": "vscode-db2i.customExampleDirectories",
1444+
"label": "Custom Example Directories",
1445+
"icon": "$(folder-library)"
13651446
}
13661447
],
13671448
"keybindings": [

src/notebooks/logic/statement.ts

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,58 @@
1-
import { ChartDetail, chartTypes } from "./chart";
2-
import { ChartJsType, chartJsTypes } from "./chartJs";
3-
4-
export interface StatementSettings {
5-
chart?: ChartJsType;
6-
title?: string;
7-
y?: string;
8-
hideStatement?: string;
9-
[key: string]: string
10-
};
11-
12-
export function getStatementDetail(content: string, eol: string) {
13-
let chartDetail: ChartDetail = {};
14-
let settings: StatementSettings = {};
15-
16-
// Strip out starting comments
17-
if (content.startsWith(`--`)) {
18-
const lines = content.split(eol);
19-
const firstNonCommentLine = lines.findIndex(line => !line.startsWith(`--`));
20-
21-
const startingComments = lines.slice(0, firstNonCommentLine).map(line => line.substring(2).trim());
22-
content = lines.slice(firstNonCommentLine).join(eol);
23-
24-
for (let comment of startingComments) {
25-
const sep = comment.indexOf(`:`);
26-
const key = comment.substring(0, sep).trim();
27-
const value = comment.substring(sep + 1).trim();
28-
settings[key] = value;
29-
}
30-
31-
// Chart settings defined by comments
32-
if (settings[`chart`] && chartJsTypes.includes(settings[`chart`])) {
33-
chartDetail.type = settings[`chart`];
34-
}
35-
36-
if (settings[`title`]) {
37-
chartDetail.title = settings[`title`];
38-
}
39-
40-
if (settings[`y`]) {
41-
chartDetail.y = settings[`y`];
42-
}
43-
}
44-
45-
// Remove trailing semicolon. The Service Component doesn't like it.
46-
if (content.endsWith(`;`)) {
47-
content = content.substring(0, content.length - 1);
48-
}
49-
50-
// Perhaps the chart type is defined by the statement prefix
51-
const chartType: ChartJsType | undefined = chartTypes.find(type => content.startsWith(`${type}:`));
52-
if (chartType) {
53-
chartDetail.type = chartType;
54-
content = content.substring(chartType.length + 1);
55-
}
56-
return { chartDetail, content, settings };
1+
import { ChartDetail, chartTypes } from "./chart";
2+
import { ChartJsType, chartJsTypes } from "./chartJs";
3+
4+
export interface StatementSettings {
5+
chart?: ChartJsType;
6+
title?: string;
7+
y?: string;
8+
hideStatement?: string;
9+
[key: string]: string
10+
};
11+
12+
export function getStatementDetail(content: string, eol: string) {
13+
let chartDetail: ChartDetail = {};
14+
let settings: StatementSettings = {};
15+
16+
// Strip out starting comments
17+
if (content.startsWith(`--`)) {
18+
const lines = content.split(eol);
19+
const firstNonCommentLine = lines.findIndex(line => !line.startsWith(`--`));
20+
21+
const startingCommentLines = firstNonCommentLine === -1 ? lines : lines.slice(0, firstNonCommentLine);
22+
const startingComments = startingCommentLines.map(line => line.substring(2).trim());
23+
content = lines.slice(firstNonCommentLine).join(eol);
24+
25+
for (let comment of startingComments) {
26+
const sep = comment.indexOf(`:`);
27+
const key = comment.substring(0, sep).trim();
28+
const value = comment.substring(sep + 1).trim();
29+
settings[key] = value;
30+
}
31+
32+
// Chart settings defined by comments
33+
if (settings[`chart`] && chartJsTypes.includes(settings[`chart`])) {
34+
chartDetail.type = settings[`chart`];
35+
}
36+
37+
if (settings[`title`]) {
38+
chartDetail.title = settings[`title`];
39+
}
40+
41+
if (settings[`y`]) {
42+
chartDetail.y = settings[`y`];
43+
}
44+
}
45+
46+
// Remove trailing semicolon. The Service Component doesn't like it.
47+
if (content.endsWith(`;`)) {
48+
content = content.substring(0, content.length - 1);
49+
}
50+
51+
// Perhaps the chart type is defined by the statement prefix
52+
const chartType: ChartJsType | undefined = chartTypes.find(type => content.startsWith(`${type}:`));
53+
if (chartType) {
54+
chartDetail.type = chartType;
55+
content = content.substring(chartType.length + 1);
56+
}
57+
return { chartDetail, content, settings };
5758
}

0 commit comments

Comments
 (0)