A Regex file extension that makes it way easier to edit large regex.
fix memory leak by using constant theme types
4 files changed, +32 -29
+23-14src/decorationGenerator.ts → src/decorationSelector.ts
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | import * as vscode from "vscode"; |
| 2 | 2 | |
| 3 | 3 | const BACKGROUND_COLORS = [ |
| 4 | + "rgba(255, 255, 255, 0.2)", // White (For outer group) | |
| 4 | 5 | "rgba(255, 0, 0, 0.5)", // Red |
| 5 | 6 | "rgba(0, 255, 0, 0.5)", // Lime |
| 6 | 7 | "rgba(0, 0, 255, 0.5)", // Blue |
| @@ -33,29 +34,37 @@ const BACKGROUND_COLORS = [ | ||
| 33 | 34 | "rgba(50, 205, 50, 0.5)", // Lime Green |
| 34 | 35 | ]; |
| 35 | 36 | |
| 36 | -class DecorationGenerator { | |
| 37 | - backgroundColors: string[]; | |
| 37 | +class DecorationSelector { | |
| 38 | + decorations: vscode.TextEditorDecorationType[] = []; | |
| 39 | + currentIndex = 0; | |
| 38 | 40 | |
| 39 | 41 | constructor() { |
| 40 | - this.backgroundColors = [...BACKGROUND_COLORS]; | |
| 42 | + for (const color of BACKGROUND_COLORS) { | |
| 43 | + this.decorations.push( | |
| 44 | + vscode.window.createTextEditorDecorationType({ | |
| 45 | + backgroundColor: color, | |
| 46 | + color: "black", | |
| 47 | + }) | |
| 48 | + ); | |
| 49 | + } | |
| 41 | 50 | } |
| 42 | 51 | |
| 43 | 52 | reset() { |
| 44 | - this.backgroundColors = [...BACKGROUND_COLORS]; | |
| 53 | + this.currentIndex = 0; | |
| 45 | 54 | } |
| 46 | 55 | |
| 47 | - getColor() { | |
| 48 | - return this.backgroundColors.pop(); | |
| 56 | + select() { | |
| 57 | + const decoration = this.decorations[this.currentIndex]; | |
| 58 | + this.currentIndex = (this.currentIndex + 1) % this.decorations.length; | |
| 59 | + return decoration; | |
| 49 | 60 | } |
| 50 | 61 | |
| 51 | - generate(options?: vscode.DecorationRenderOptions) { | |
| 52 | - return vscode.window.createTextEditorDecorationType({ | |
| 53 | - backgroundColor: this.getColor(), | |
| 54 | - color: "black", | |
| 55 | - ...options, | |
| 56 | - }); | |
| 62 | + dispose() { | |
| 63 | + for (const decoration of this.decorations) { | |
| 64 | + decoration.dispose(); | |
| 65 | + } | |
| 57 | 66 | } |
| 58 | 67 | } |
| 59 | 68 | |
| 60 | -const decorationGenerator = new DecorationGenerator(); | |
| 61 | -export default decorationGenerator; | |
| 69 | +const decorationSelector = new DecorationSelector(); | |
| 70 | +export default decorationSelector; |
+2-0src/extension.ts
| @@ -3,6 +3,7 @@ import { output } from "./output"; | ||
| 3 | 3 | import { editorsContainRegexFile } from "./utils"; |
| 4 | 4 | import { regexButtons } from "./regexButtons"; |
| 5 | 5 | import { highlighter } from "./highlighter"; |
| 6 | +import decorationSelector from "./decorationSelector"; | |
| 6 | 7 | |
| 7 | 8 | export function activate(context: vscode.ExtensionContext) { |
| 8 | 9 | output.activate(); |
| @@ -40,6 +41,7 @@ export function activate(context: vscode.ExtensionContext) { | ||
| 40 | 41 | } |
| 41 | 42 | |
| 42 | 43 | export function deactivate() { |
| 44 | + decorationSelector.dispose(); | |
| 43 | 45 | highlighter.dispose(); |
| 44 | 46 | // console.log("Disposed highlighter"); |
| 45 | 47 | regexButtons.dispose(); |
+6-14src/highlighter.ts
| @@ -2,7 +2,7 @@ import * as vscode from "vscode"; | ||
| 2 | 2 | import { output } from "./output"; |
| 3 | 3 | import { getNonRegexEditors, getRegexEditor, timeit } from "./utils"; |
| 4 | 4 | import { regexButtons } from "./regexButtons"; |
| 5 | -import decorationGenerator from "./decorationGenerator"; | |
| 5 | +import decorationSelector from "./decorationSelector"; | |
| 6 | 6 | import { getConfig } from "./global"; |
| 7 | 7 | |
| 8 | 8 | type HighlightGroup = { |
| @@ -31,11 +31,8 @@ class Highlighter { | ||
| 31 | 31 | @timeit |
| 32 | 32 | reset() { |
| 33 | 33 | output.clear(); |
| 34 | - this.getDecorationTypes().forEach((decoration) => { | |
| 35 | - decoration.dispose(); | |
| 36 | - }); | |
| 37 | 34 | this.highlightGroups = {}; |
| 38 | - decorationGenerator.reset(); | |
| 35 | + decorationSelector.reset(); | |
| 39 | 36 | } |
| 40 | 37 | |
| 41 | 38 | clearRanges() { |
| @@ -44,17 +41,13 @@ class Highlighter { | ||
| 44 | 41 | } |
| 45 | 42 | } |
| 46 | 43 | |
| 47 | - updateOrCreateHighlightGroup( | |
| 48 | - key: string, | |
| 49 | - range: vscode.Range, | |
| 50 | - options: vscode.DecorationRenderOptions = {} | |
| 51 | - ) { | |
| 44 | + updateOrCreateHighlightGroup(key: string, range: vscode.Range) { | |
| 52 | 45 | const decoration = { range, hoverMessage: key }; |
| 53 | 46 | // output.log(`Highlighting ${key}`); |
| 54 | - const decorationType = decorationGenerator.generate(options); | |
| 55 | 47 | if (this.highlightGroups[key]) { |
| 56 | 48 | this.highlightGroups[key].decorations.push(decoration); |
| 57 | 49 | } else { |
| 50 | + const decorationType = decorationSelector.select(); | |
| 58 | 51 | this.highlightGroups[key] = { |
| 59 | 52 | decorations: [decoration], |
| 60 | 53 | decorationType: decorationType, |
| @@ -98,9 +91,7 @@ class Highlighter { | ||
| 98 | 91 | } |
| 99 | 92 | const start = document.positionAt(match.index); |
| 100 | 93 | const end = document.positionAt(match.index + match[0].length); |
| 101 | - this.updateOrCreateHighlightGroup("match", new vscode.Range(start, end), { | |
| 102 | - backgroundColor: "rgba(255, 255, 255, 0.2)", | |
| 103 | - }); | |
| 94 | + this.updateOrCreateHighlightGroup("match", new vscode.Range(start, end)); | |
| 104 | 95 | |
| 105 | 96 | for (const groupName in match.groups) { |
| 106 | 97 | const anyMatch = match as any; |
| @@ -158,6 +149,7 @@ class Highlighter { | ||
| 158 | 149 | if (!regexEditor) { |
| 159 | 150 | return; |
| 160 | 151 | } |
| 152 | + this.updateOrCreateHighlightGroup("match", new vscode.Range(0, 0, 0, 0)); | |
| 161 | 153 | this.matchRegexGroups(regexEditor.document); |
| 162 | 154 | this.highlightMatches(regexEditor); |
| 163 | 155 | this.clearRanges(); |
+1-1src/output.ts
| @@ -58,7 +58,7 @@ class Output { | ||
| 58 | 58 | coloredOutput += `${yellow}[warning] ${reset}`; |
| 59 | 59 | } |
| 60 | 60 | coloredOutput += `${whiteBright}${message}${reset}`; |
| 61 | - console.log(coloredOutput); | |
| 61 | + // console.log(coloredOutput); | |
| 62 | 62 | |
| 63 | 63 | // Log to VS Code output channel without colors |
| 64 | 64 | this.outputChannel?.appendLine(message); |