irongit

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

fix memory leak by using constant theme types

huncholanehuncholaneauthored
parent 927dd04commit 0104ec9bba6b5900772874db42278fa6df78b53eBrowse files

4 files changed, +32 -29

+23-14src/decorationGenerator.ts → src/decorationSelector.ts
@@ -1,6 +1,7 @@
11 import * as vscode from "vscode";
22
33 const BACKGROUND_COLORS = [
4+ "rgba(255, 255, 255, 0.2)", // White (For outer group)
45 "rgba(255, 0, 0, 0.5)", // Red
56 "rgba(0, 255, 0, 0.5)", // Lime
67 "rgba(0, 0, 255, 0.5)", // Blue
@@ -33,29 +34,37 @@ const BACKGROUND_COLORS = [
3334 "rgba(50, 205, 50, 0.5)", // Lime Green
3435 ];
3536
36-class DecorationGenerator {
37- backgroundColors: string[];
37+class DecorationSelector {
38+ decorations: vscode.TextEditorDecorationType[] = [];
39+ currentIndex = 0;
3840
3941 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+ }
4150 }
4251
4352 reset() {
44- this.backgroundColors = [...BACKGROUND_COLORS];
53+ this.currentIndex = 0;
4554 }
4655
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;
4960 }
5061
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+ }
5766 }
5867 }
5968
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";
33 import { editorsContainRegexFile } from "./utils";
44 import { regexButtons } from "./regexButtons";
55 import { highlighter } from "./highlighter";
6+import decorationSelector from "./decorationSelector";
67
78 export function activate(context: vscode.ExtensionContext) {
89 output.activate();
@@ -40,6 +41,7 @@ export function activate(context: vscode.ExtensionContext) {
4041 }
4142
4243 export function deactivate() {
44+ decorationSelector.dispose();
4345 highlighter.dispose();
4446 // console.log("Disposed highlighter");
4547 regexButtons.dispose();
+6-14src/highlighter.ts
@@ -2,7 +2,7 @@ import * as vscode from "vscode";
22 import { output } from "./output";
33 import { getNonRegexEditors, getRegexEditor, timeit } from "./utils";
44 import { regexButtons } from "./regexButtons";
5-import decorationGenerator from "./decorationGenerator";
5+import decorationSelector from "./decorationSelector";
66 import { getConfig } from "./global";
77
88 type HighlightGroup = {
@@ -31,11 +31,8 @@ class Highlighter {
3131 @timeit
3232 reset() {
3333 output.clear();
34- this.getDecorationTypes().forEach((decoration) => {
35- decoration.dispose();
36- });
3734 this.highlightGroups = {};
38- decorationGenerator.reset();
35+ decorationSelector.reset();
3936 }
4037
4138 clearRanges() {
@@ -44,17 +41,13 @@ class Highlighter {
4441 }
4542 }
4643
47- updateOrCreateHighlightGroup(
48- key: string,
49- range: vscode.Range,
50- options: vscode.DecorationRenderOptions = {}
51- ) {
44+ updateOrCreateHighlightGroup(key: string, range: vscode.Range) {
5245 const decoration = { range, hoverMessage: key };
5346 // output.log(`Highlighting ${key}`);
54- const decorationType = decorationGenerator.generate(options);
5547 if (this.highlightGroups[key]) {
5648 this.highlightGroups[key].decorations.push(decoration);
5749 } else {
50+ const decorationType = decorationSelector.select();
5851 this.highlightGroups[key] = {
5952 decorations: [decoration],
6053 decorationType: decorationType,
@@ -98,9 +91,7 @@ class Highlighter {
9891 }
9992 const start = document.positionAt(match.index);
10093 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));
10495
10596 for (const groupName in match.groups) {
10697 const anyMatch = match as any;
@@ -158,6 +149,7 @@ class Highlighter {
158149 if (!regexEditor) {
159150 return;
160151 }
152+ this.updateOrCreateHighlightGroup("match", new vscode.Range(0, 0, 0, 0));
161153 this.matchRegexGroups(regexEditor.document);
162154 this.highlightMatches(regexEditor);
163155 this.clearRanges();
+1-1src/output.ts
@@ -58,7 +58,7 @@ class Output {
5858 coloredOutput += `${yellow}[warning] ${reset}`;
5959 }
6060 coloredOutput += `${whiteBright}${message}${reset}`;
61- console.log(coloredOutput);
61+ // console.log(coloredOutput);
6262
6363 // Log to VS Code output channel without colors
6464 this.outputChannel?.appendLine(message);