File tree 2 files changed +31
-2
lines changed
2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change 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 .
2
2
3
3
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
5
11
6
12
### Running out of source
7
13
8
14
* run ` npm install ` in the terminal
9
15
* also run ` npm run compile `
10
16
* select ` F5 ` to debug
17
+
18
+ ### Thanks
19
+
20
+ The rendering uses the https://regexper.com source code which works like a charme 👏
Original file line number Diff line number Diff line change @@ -60,6 +60,25 @@ export function activate(context: vscode.ExtensionContext) {
60
60
const execution = controller . createNotebookCellExecution ( cell ) ;
61
61
execution . start ( ) ;
62
62
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
+
63
82
const regexItem = vscode . NotebookCellOutputItem . text ( cellContent , 'application/x.regexp' ) ;
64
83
const regexOutput = new vscode . NotebookCellOutput ( [ regexItem ] ) ;
65
84
execution . replaceOutput ( regexOutput ) ;
You can’t perform that action at this time.
0 commit comments