irongit

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

highlights groups with different names hover

FIRST_NAME LAST_NAMEauthored
parent 08817cfcommit c4380d256fc264ff102492c99c31fa8c21aa297dBrowse files

1 file changed, +82 -11

+82-11src/extension.ts
@@ -1,18 +1,30 @@
11 import * as vscode from "vscode";
22
3+/* GLOBAL CONSTANTS */
4+const baseEditorStyle = vscode.window.createTextEditorDecorationType({
5+ backgroundColor: "yellow",
6+ color: "black",
7+});
8+
39 /* GLOBAL VARIABLES */
410 let outputChannel: vscode.OutputChannel;
511 let lastRegex = "";
612 let lastText = "";
713 let decorationType: vscode.TextEditorDecorationType | undefined;
14+const flagButtons: { [key: string]: InnerMapValue } = {};
815
916 /* TYPES */
17+type GroupMatches = {
18+ style: vscode.TextEditorDecorationType;
19+ matches: vscode.Range[];
20+};
21+type GroupMatchMap = {
22+ [key: string]: GroupMatches;
23+};
1024 interface InnerMapValue {
1125 state: "on" | "off";
1226 button: vscode.StatusBarItem;
1327 }
14-
15-const flagButtons: { [key: string]: InnerMapValue } = {};
1628 const states: Record<"on" | "off", string> = {
1729 on: "$(check)",
1830 off: "$(x)",
@@ -23,6 +35,16 @@ function getConfigFlags(): string {
2335 return config.get("flags") as string;
2436 }
2537
38+function getRandomColor() {
39+ // Generate a random hex color
40+ const letters = "0123456789ABCDEF";
41+ let color = "#";
42+ for (let i = 0; i < 6; i++) {
43+ color += letters[Math.floor(Math.random() * 16)];
44+ }
45+ return color;
46+}
47+
2648 function getReEditor(): vscode.TextEditor | undefined {
2749 const editors = vscode.window.visibleTextEditors;
2850
@@ -98,6 +120,58 @@ function getRegexFlags(): [string, boolean] {
98120 return [flags, hasX];
99121 }
100122
123+function applyGroupMatchDecorations(
124+ editor: vscode.TextEditor,
125+ groupMatches: GroupMatchMap
126+) {
127+ for (const groupName of Object.keys(groupMatches)) {
128+ const { style, matches } = groupMatches[groupName];
129+
130+ const decorations: vscode.DecorationOptions[] = matches.map((range) => ({
131+ range,
132+ hoverMessage: `Group: ${groupName}`,
133+ }));
134+
135+ editor.setDecorations(style, decorations);
136+ }
137+}
138+
139+function updateGroupMatches(
140+ textEditor: vscode.TextEditor,
141+ groupMatches: GroupMatchMap,
142+ match: RegExpExecArray
143+) {
144+ for (const groupName in match.groups) {
145+ const groupMatch = match.groups[groupName];
146+
147+ if (groupMatch) {
148+ const startPos = textEditor.document.positionAt(
149+ match.index + match[0].indexOf(groupMatch)
150+ );
151+ const endPos = textEditor.document.positionAt(
152+ match.index + match[0].indexOf(groupMatch) + groupMatch.length
153+ );
154+ const matchRange = new vscode.Range(startPos, endPos);
155+
156+ if (!groupMatches[groupName]) {
157+ // Generate a random color for the group
158+ const randomColor = getRandomColor();
159+
160+ // Define a new TextEditorDecorationType for the group with the random color
161+ groupMatches[groupName] = {
162+ style: vscode.window.createTextEditorDecorationType({
163+ backgroundColor: randomColor,
164+ color: "black", // You can customize the text color
165+ }),
166+ matches: [matchRange],
167+ };
168+ } else {
169+ groupMatches[groupName].matches.push(matchRange);
170+ }
171+ }
172+ }
173+}
174+
101175 function highlightMatches(): void {
102176 const reEditor = getReEditor();
103177 const textEditor = getTextEditor();
@@ -126,6 +200,7 @@ function highlightMatches(): void {
126200 reText = reText.replace(/\s/g, "");
127201 }
128202 const pattern = new RegExp(reText, flags);
203+ const groupMatches: GroupMatchMap = {};
129204 let match: RegExpExecArray | null;
130205 let i = 0;
131206 while ((match = pattern.exec(searchText))) {
@@ -133,6 +208,7 @@ function highlightMatches(): void {
133208 outputChannel.appendLine("The pattern reached max recursion.");
134209 break;
135210 }
211+ updateGroupMatches(textEditor, groupMatches, match);
136212 const startPos = textEditor.document.positionAt(match.index);
137213 const endPos = textEditor.document.positionAt(
138214 match.index + match[0].length
@@ -142,16 +218,11 @@ function highlightMatches(): void {
142218 i++;
143219 }
144220
145- // Create a decoration type if not already created
146- if (!decorationType) {
147- decorationType = vscode.window.createTextEditorDecorationType({
148- backgroundColor: "yellow",
149- color: "black",
150- });
151- }
221+ // Apply base decorations
222+ textEditor.setDecorations(baseEditorStyle, matches);
152223
153- // Apply the decorations to the editor
154- textEditor.setDecorations(decorationType, matches);
224+ // Apply decorations to the groups
225+ applyGroupMatchDecorations(textEditor, groupMatches);
155226 }
156227
157228 function toggleFlag(flag: string): void {