irongit

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

can highlight in both files

huncholanehuncholaneauthored
parent 22cc497commit 11370f66623e7791b6d8320ce13b39da7ded4984Browse files

1 file changed, +42 -23

+42-23src/highlighter.ts
@@ -13,6 +13,7 @@ type HighlightGroup = {
1313 class Highlighter {
1414 context: vscode.ExtensionContext | undefined;
1515 decorations: vscode.TextEditorDecorationType[] = [];
16+ highlightGroups: { [key: string]: HighlightGroup } = {};
1617
1718 constructor() {}
1819
@@ -26,22 +27,34 @@ class Highlighter {
2627 this.decorations.forEach((decoration) => {
2728 decoration.dispose();
2829 });
30+ this.highlightGroups = {};
2931 this.decorations = [];
3032 decorationGenerator.reset();
3133 output.clear();
3234 }
3335
34- createDecoration(options: vscode.DecorationRenderOptions) {
35- const decoration = decorationGenerator.generate(options);
36- this.decorations.push(decoration);
37- return decoration;
36+ clearRanges() {
37+ for (const key of Object.keys(this.highlightGroups)) {
38+ this.highlightGroups[key].ranges = [];
39+ }
40+ }
41+
42+ updateOrCreateHighlightGroup(key: string, range: vscode.Range) {
43+ if (this.highlightGroups[key]) {
44+ this.highlightGroups[key].ranges.push(range);
45+ } else {
46+ this.highlightGroups[key] = {
47+ ranges: [range],
48+ decoration: decorationGenerator.generate(),
49+ };
50+ }
3851 }
3952
4053 applyXFlag(regex: string) {
4154 return regex.replace(/(?<!\\)#.*$/gm, "").replace(/\s+/g, "");
4255 }
4356
44- getHighlightGroups(regex: string, document: vscode.TextDocument) {
57+ matchOnEditor(regex: string, document: vscode.TextDocument) {
4558 const flags = regexButtons.getFlagString();
4659 if (regexButtons.hasXFlag()) {
4760 regex = this.applyXFlag(regex);
@@ -54,14 +67,6 @@ class Highlighter {
5467 }
5568 const re = new RegExp(regex, flags);
5669 const matches = document.getText().matchAll(re);
57- const highlightGroups: { [key: string]: HighlightGroup } = {
58- outer: {
59- ranges: [],
60- decoration: this.createDecoration({
61- fontStyle: "italic",
62- }),
63- },
64- };
6570 let i = 0;
6671 const maxMatches = getConfig().get("maxMatches") as number;
6772 for (const match of matches) {
@@ -70,21 +75,32 @@ class Highlighter {
7075 }
7176 const start = document.positionAt(match.index);
7277 const end = document.positionAt(match.index + match[0].length);
73- if (highlightGroups["outer"].ranges) {
74- highlightGroups["outer"].ranges.push(new vscode.Range(start, end));
75- }
78+ this.updateOrCreateHighlightGroup("outer", new vscode.Range(start, end));
7679 i++;
7780 }
7881 output.log(`Found ${i} matches`);
79- return highlightGroups;
8082 }
8183
82- highlightMatches(regexEditor: vscode.TextEditor, editor: vscode.TextEditor) {
84+ matchRegexGroups(document: vscode.TextDocument) {
85+ const re = /(?<=\().+?(?=\))/g;
86+ const matches = document.getText().matchAll(re);
87+ let i = 0;
88+ const maxMatches = getConfig().get("maxMatches") as number;
89+ for (const match of matches) {
90+ if (i >= maxMatches) {
91+ break;
92+ }
93+ const start = document.positionAt(match.index);
94+ const end = document.positionAt(match.index + match[0].length);
95+ this.updateOrCreateHighlightGroup("inner", new vscode.Range(start, end));
96+ i++;
97+ }
98+ }
99+
100+ highlightMatches(editor: vscode.TextEditor) {
83101 // output.log(`Highlighting matches in ${editor.document.fileName}`);
84- const regex = regexEditor.document.getText();
85- const highlightGroups = this.getHighlightGroups(regex, editor.document);
86- for (const key of Object.keys(highlightGroups)) {
87- const group = highlightGroups[key];
102+ for (const key of Object.keys(this.highlightGroups)) {
103+ const group = this.highlightGroups[key];
88104 editor.setDecorations(group.decoration, group.ranges);
89105 }
90106 }
@@ -95,9 +111,12 @@ class Highlighter {
95111 if (!regexEditor) {
96112 return;
97113 }
114+ this.matchRegexGroups(regexEditor.document);
115+ this.highlightMatches(regexEditor);
98116 const nonRegexEditors = getNonRegexEditors();
99117 for (const editor of nonRegexEditors) {
100- this.highlightMatches(regexEditor, editor);
118+ this.matchOnEditor(regexEditor.document.getText(), editor.document);
119+ this.highlightMatches(editor);
101120 }
102121 }
103122 }