irongit

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

resets when exit re file

huncholanehuncholaneauthored
parent 9ea3c04commit 41349cd15dc54c6b5700097f9242621218f36f5dBrowse files

2 files changed, +22 -14

+2-1src/extension.ts
@@ -17,7 +17,8 @@ export function activate(context: vscode.ExtensionContext) {
1717 }
1818 }
1919 if (!editorsContainRegexFile()) {
20- console.log("No regex file opened");
20+ highlighter.reset();
21+ output.log("No regex file opened");
2122 }
2223 });
2324
+20-13src/highlighter.ts
@@ -6,7 +6,7 @@ import decorationGenerator from "./decorationGenerator";
66 import { getConfig } from "./global";
77
88 type HighlightGroup = {
9- range: vscode.Range;
9+ ranges: vscode.Range[];
1010 decoration: vscode.TextEditorDecorationType;
1111 };
1212
@@ -31,6 +31,12 @@ class Highlighter {
3131 output.clear();
3232 }
3333
34+ createDecoration(options: vscode.DecorationRenderOptions) {
35+ const decoration = decorationGenerator.generate(options);
36+ this.decorations.push(decoration);
37+ return decoration;
38+ }
39+
3440 getHighlightGroups(
3541 regex: string,
3642 flags: string,
@@ -38,23 +44,23 @@ class Highlighter {
3844 ) {
3945 const re = new RegExp(regex, flags);
4046 const matches = document.getText().matchAll(re);
41- const highlightGroups: HighlightGroup[] = [];
42- const decorationMap: { [key: string]: vscode.TextEditorDecorationType } =
43- {};
44- const outerDecoration = decorationGenerator.generate({ cursor: "pointer" });
45- this.decorations.push(outerDecoration);
47+ const highlightGroups: { [key: string]: HighlightGroup } = {
48+ outer: {
49+ ranges: [],
50+ decoration: this.createDecoration({}),
51+ },
52+ };
4653 let i = 0;
4754 const maxMatches = getConfig().get("maxMatches") as number;
4855 for (const match of matches) {
4956 if (i >= maxMatches) {
50- continue;
57+ break;
5158 }
5259 const start = document.positionAt(match.index);
5360 const end = document.positionAt(match.index + match[0].length);
54- highlightGroups.push({
55- range: new vscode.Range(start, end),
56- decoration: outerDecoration,
57- });
61+ if (highlightGroups["outer"].ranges) {
62+ highlightGroups["outer"].ranges.push(new vscode.Range(start, end));
63+ }
5864 i++;
5965 }
6066 output.log(`Found ${i} matches`);
@@ -70,8 +76,9 @@ class Highlighter {
7076 flags,
7177 editor.document
7278 );
73- for (const group of highlightGroups) {
74- editor.setDecorations(group.decoration, [group.range]);
79+ for (const key of Object.keys(highlightGroups)) {
80+ const group = highlightGroups[key];
81+ editor.setDecorations(group.decoration, group.ranges);
7582 }
7683 }
7784