irongit

A Regex file extension that makes it way easier to edit large regex.

the pound is able to be ignored with backslash

huncholanehuncholaneauthored
parent 44341e8commit 22cc497f4dc06d8c897cfb97e96e2cbff83a0a2bBrowse files

1 file changed, +7 -3

+7-3src/highlighter.ts
@@ -4,7 +4,6 @@ import { getNonRegexEditors, getRegexEditor } from "./utils";
44 import { regexButtons } from "./regexButtons";
55 import decorationGenerator from "./decorationGenerator";
66 import { getConfig } from "./global";
7-import chalk from "chalk";
87
98 type HighlightGroup = {
109 ranges: vscode.Range[];
@@ -39,7 +38,7 @@ class Highlighter {
3938 }
4039
4140 applyXFlag(regex: string) {
42- return regex.replace(/#.*$/gm, "").replace(/\s+/g, "");
41+ return regex.replace(/(?<!\\)#.*$/gm, "").replace(/\s+/g, "");
4342 }
4443
4544 getHighlightGroups(regex: string, document: vscode.TextDocument) {
@@ -47,6 +46,12 @@ class Highlighter {
4746 if (regexButtons.hasXFlag()) {
4847 regex = this.applyXFlag(regex);
4948 }
49+ try {
50+ new RegExp(regex, flags);
51+ } catch (err) {
52+ output.log(`Invalid regex: ${regex}`);
53+ return {};
54+ }
5055 const re = new RegExp(regex, flags);
5156 const matches = document.getText().matchAll(re);
5257 const highlightGroups: { [key: string]: HighlightGroup } = {
@@ -77,7 +82,6 @@ class Highlighter {
7782 highlightMatches(regexEditor: vscode.TextEditor, editor: vscode.TextEditor) {
7883 // output.log(`Highlighting matches in ${editor.document.fileName}`);
7984 const regex = regexEditor.document.getText();
80- const flags = regexButtons.getFlagString();
8185 const highlightGroups = this.getHighlightGroups(regex, editor.document);
8286 for (const key of Object.keys(highlightGroups)) {
8387 const group = highlightGroups[key];