Skip to content

Commit c3eb49f

Browse files
Add support for comment overrides
Signed-off-by: Sanjula Ganepola <[email protected]>
1 parent f697ee2 commit c3eb49f

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@
265265
"type": "string",
266266
"description": "The directory containing SQL example files."
267267
},
268-
"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 at the top of the file with the tags `category` and `description`.",
268+
"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`.",
269269
"default": []
270270
}
271271
}

src/views/examples/contributes.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"type": "string",
6161
"description": "The directory containing SQL example files."
6262
},
63-
"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 at the top of the file with the tags `category` and `description`.",
63+
"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`.",
6464
"default": []
6565
}
6666
}

src/views/examples/index.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EndOfLine, FileType, TextDocument, Uri, workspace } from "vscode";
22
import Configuration from "../../configuration";
33
import * as path from "path";
44
import { getServiceInfo } from "../../database/serviceInfo";
5+
import { getStatementDetail } from "../../notebooks/logic/statement";
56

67
export interface SQLExamplesList {
78
[group: string]: SQLExample[]
@@ -6032,17 +6033,31 @@ export async function getCustomExamples(): Promise<SQLExamplesList> {
60326033
// Organize the SQL files into groups based on their parent directory names
60336034
const examplesList: SQLExamplesList = {};
60346035
for (const textDocument of sqlTextDocuments) {
6036+
// Set group and name based on file path by default
60356037
const parsedPath = path.parse(textDocument.uri.path);
6036-
const group = path.basename(parsedPath.dir);
6037-
const name = parsedPath.name;
6038+
let group = path.basename(parsedPath.dir);
6039+
let name = parsedPath.name;
6040+
6041+
// Override group and name if category and description is specified in the SQL file as comments
6042+
const content = textDocument.getText();
6043+
const eol = textDocument.eol === EndOfLine.LF ? '\n' : '\r\n';
6044+
const detail = getStatementDetail(content, eol);
6045+
const category = detail?.settings.category;
6046+
if (category) {
6047+
group = category;
6048+
}
6049+
const description = detail?.settings.description;
6050+
if (description) {
6051+
name = description;
6052+
}
60386053

60396054
if (!examplesList[group]) {
60406055
examplesList[group] = [];
60416056
}
60426057

60436058
examplesList[group].push({
60446059
name: name,
6045-
content: textDocument.getText().split(textDocument.eol === EndOfLine.LF ? '\n' : '\r\n')
6060+
content: textDocument.getText().split(eol)
60466061
});
60476062
}
60486063

0 commit comments

Comments
 (0)