A Regex file extension that makes it way easier to edit large regex.
the pound is able to be ignored with backslash
1 file changed, +7 -3
+7-3src/highlighter.ts
| @@ -4,7 +4,6 @@ import { getNonRegexEditors, getRegexEditor } from "./utils"; | ||
| 4 | 4 | import { regexButtons } from "./regexButtons"; |
| 5 | 5 | import decorationGenerator from "./decorationGenerator"; |
| 6 | 6 | import { getConfig } from "./global"; |
| 7 | -import chalk from "chalk"; | |
| 8 | 7 | |
| 9 | 8 | type HighlightGroup = { |
| 10 | 9 | ranges: vscode.Range[]; |
| @@ -39,7 +38,7 @@ class Highlighter { | ||
| 39 | 38 | } |
| 40 | 39 | |
| 41 | 40 | applyXFlag(regex: string) { |
| 42 | - return regex.replace(/#.*$/gm, "").replace(/\s+/g, ""); | |
| 41 | + return regex.replace(/(?<!\\)#.*$/gm, "").replace(/\s+/g, ""); | |
| 43 | 42 | } |
| 44 | 43 | |
| 45 | 44 | getHighlightGroups(regex: string, document: vscode.TextDocument) { |
| @@ -47,6 +46,12 @@ class Highlighter { | ||
| 47 | 46 | if (regexButtons.hasXFlag()) { |
| 48 | 47 | regex = this.applyXFlag(regex); |
| 49 | 48 | } |
| 49 | + try { | |
| 50 | + new RegExp(regex, flags); | |
| 51 | + } catch (err) { | |
| 52 | + output.log(`Invalid regex: ${regex}`); | |
| 53 | + return {}; | |
| 54 | + } | |
| 50 | 55 | const re = new RegExp(regex, flags); |
| 51 | 56 | const matches = document.getText().matchAll(re); |
| 52 | 57 | const highlightGroups: { [key: string]: HighlightGroup } = { |
| @@ -77,7 +82,6 @@ class Highlighter { | ||
| 77 | 82 | highlightMatches(regexEditor: vscode.TextEditor, editor: vscode.TextEditor) { |
| 78 | 83 | // output.log(`Highlighting matches in ${editor.document.fileName}`); |
| 79 | 84 | const regex = regexEditor.document.getText(); |
| 80 | - const flags = regexButtons.getFlagString(); | |
| 81 | 85 | const highlightGroups = this.getHighlightGroups(regex, editor.document); |
| 82 | 86 | for (const key of Object.keys(highlightGroups)) { |
| 83 | 87 | const group = highlightGroups[key]; |