A Regex file extension that makes it way easier to edit large regex.
highlights groups with different names hover
1 file changed, +82 -11
+82-11src/extension.ts
| @@ -1,18 +1,30 @@ | ||
| 1 | 1 | import * as vscode from "vscode"; |
| 2 | 2 | |
| 3 | +/* GLOBAL CONSTANTS */ | |
| 4 | +const baseEditorStyle = vscode.window.createTextEditorDecorationType({ | |
| 5 | + backgroundColor: "yellow", | |
| 6 | + color: "black", | |
| 7 | +}); | |
| 8 | + | |
| 3 | 9 | /* GLOBAL VARIABLES */ |
| 4 | 10 | let outputChannel: vscode.OutputChannel; |
| 5 | 11 | let lastRegex = ""; |
| 6 | 12 | let lastText = ""; |
| 7 | 13 | let decorationType: vscode.TextEditorDecorationType | undefined; |
| 14 | +const flagButtons: { [key: string]: InnerMapValue } = {}; | |
| 8 | 15 | |
| 9 | 16 | /* TYPES */ |
| 17 | +type GroupMatches = { | |
| 18 | + style: vscode.TextEditorDecorationType; | |
| 19 | + matches: vscode.Range[]; | |
| 20 | +}; | |
| 21 | +type GroupMatchMap = { | |
| 22 | + [key: string]: GroupMatches; | |
| 23 | +}; | |
| 10 | 24 | interface InnerMapValue { |
| 11 | 25 | state: "on" | "off"; |
| 12 | 26 | button: vscode.StatusBarItem; |
| 13 | 27 | } |
| 14 | - | |
| 15 | -const flagButtons: { [key: string]: InnerMapValue } = {}; | |
| 16 | 28 | const states: Record<"on" | "off", string> = { |
| 17 | 29 | on: "$(check)", |
| 18 | 30 | off: "$(x)", |
| @@ -23,6 +35,16 @@ function getConfigFlags(): string { | ||
| 23 | 35 | return config.get("flags") as string; |
| 24 | 36 | } |
| 25 | 37 | |
| 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 | + | |
| 26 | 48 | function getReEditor(): vscode.TextEditor | undefined { |
| 27 | 49 | const editors = vscode.window.visibleTextEditors; |
| 28 | 50 | |
| @@ -98,6 +120,58 @@ function getRegexFlags(): [string, boolean] { | ||
| 98 | 120 | return [flags, hasX]; |
| 99 | 121 | } |
| 100 | 122 | |
| 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 | + | |
| 101 | 175 | function highlightMatches(): void { |
| 102 | 176 | const reEditor = getReEditor(); |
| 103 | 177 | const textEditor = getTextEditor(); |
| @@ -126,6 +200,7 @@ function highlightMatches(): void { | ||
| 126 | 200 | reText = reText.replace(/\s/g, ""); |
| 127 | 201 | } |
| 128 | 202 | const pattern = new RegExp(reText, flags); |
| 203 | + const groupMatches: GroupMatchMap = {}; | |
| 129 | 204 | let match: RegExpExecArray | null; |
| 130 | 205 | let i = 0; |
| 131 | 206 | while ((match = pattern.exec(searchText))) { |
| @@ -133,6 +208,7 @@ function highlightMatches(): void { | ||
| 133 | 208 | outputChannel.appendLine("The pattern reached max recursion."); |
| 134 | 209 | break; |
| 135 | 210 | } |
| 211 | + updateGroupMatches(textEditor, groupMatches, match); | |
| 136 | 212 | const startPos = textEditor.document.positionAt(match.index); |
| 137 | 213 | const endPos = textEditor.document.positionAt( |
| 138 | 214 | match.index + match[0].length |
| @@ -142,16 +218,11 @@ function highlightMatches(): void { | ||
| 142 | 218 | i++; |
| 143 | 219 | } |
| 144 | 220 | |
| 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); | |
| 152 | 223 | |
| 153 | - // Apply the decorations to the editor | |
| 154 | - textEditor.setDecorations(decorationType, matches); | |
| 224 | + // Apply decorations to the groups | |
| 225 | + applyGroupMatchDecorations(textEditor, groupMatches); | |
| 155 | 226 | } |
| 156 | 227 | |
| 157 | 228 | function toggleFlag(flag: string): void { |