Skip to content

Commit e80e72c

Browse files
committed
simple regex validation, readme 💄
1 parent cc88a69 commit e80e72c

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

Diff for: README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
Sample notebook for VS Code that renders regular expressions using https://regexper.com.
1+
Native notebook for VS Code that renders regular expressions using https://regexper.com.
22

33

4-
![Sample showing rendered regular expression](img.png)
4+
![Sample showing rendered regular expression](https://github.com/jrieken/vscode-regex-notebook/raw/master/img.png)
5+
6+
This samples makes use of the following APIs/concepts
7+
8+
* `NotebookSerializer` to load and save notebook documents from disk
9+
* `NotebookController` to execute regular expressions cells
10+
* `NotebookRenderer` to present regular expressions in a nice way
511

612
### Running out of source
713

814
* run `npm install` in the terminal
915
* also run `npm run compile`
1016
* select `F5` to debug
17+
18+
### Thanks
19+
20+
The rendering uses the https://regexper.com source code which works like a charme 👏

Diff for: src/extension/extension.ts

+19
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ export function activate(context: vscode.ExtensionContext) {
6060
const execution = controller.createNotebookCellExecution(cell);
6161
execution.start();
6262
const cellContent = execution.cell.document.getText();
63+
64+
try {
65+
// "validation" by parsing
66+
const match = /\/(.*)\/(.*)/.exec(cellContent);
67+
if (match) {
68+
new RegExp(match[1], match[2]);
69+
} else {
70+
new RegExp(cellContent);
71+
}
72+
} catch (err) {
73+
// show validation error and continue with next cell
74+
const errorItem = vscode.NotebookCellOutputItem.error(err as Error);
75+
const regexOutput = new vscode.NotebookCellOutput([errorItem]);
76+
execution.replaceOutput(regexOutput);
77+
execution.end(false);
78+
79+
continue;
80+
}
81+
6382
const regexItem = vscode.NotebookCellOutputItem.text(cellContent, 'application/x.regexp');
6483
const regexOutput = new vscode.NotebookCellOutput([regexItem]);
6584
execution.replaceOutput(regexOutput);

0 commit comments

Comments
 (0)