A Regex file extension that makes it way easier to edit large regex.
forcestop boolean makes it very smooth
2 files changed, +14 -9
+5-8src/extension.ts
| @@ -11,17 +11,14 @@ export function activate(context: vscode.ExtensionContext) { | ||
| 11 | 11 | |
| 12 | 12 | vscode.window.onDidChangeActiveTextEditor((editor) => { |
| 13 | 13 | if (editor && editor.document.uri.scheme === "file") { |
| 14 | - if (editor.document.languageId === "regex") { | |
| 15 | - // output.log("Regex file opened"); | |
| 16 | - regexButtons.show(); | |
| 14 | + if (editorsContainRegexFile()) { | |
| 17 | 15 | highlighter.run(); |
| 16 | + regexButtons.show(); | |
| 17 | + } else { | |
| 18 | + highlighter.reset(); | |
| 19 | + regexButtons.hide(); | |
| 18 | 20 | } |
| 19 | 21 | } |
| 20 | - if (!editorsContainRegexFile()) { | |
| 21 | - highlighter.reset(); | |
| 22 | - regexButtons.hide(); | |
| 23 | - output.log("No regex file opened"); | |
| 24 | - } | |
| 25 | 22 | }); |
| 26 | 23 | |
| 27 | 24 | vscode.workspace.onDidChangeTextDocument((event) => { |
+9-1src/highlighter.ts
| @@ -13,6 +13,7 @@ type HighlightGroup = { | ||
| 13 | 13 | class Highlighter { |
| 14 | 14 | context: vscode.ExtensionContext | undefined; |
| 15 | 15 | highlightGroups: { [key: string]: HighlightGroup } = {}; |
| 16 | + forceStop = false; | |
| 16 | 17 | |
| 17 | 18 | constructor() {} |
| 18 | 19 | |
| @@ -65,6 +66,7 @@ class Highlighter { | ||
| 65 | 66 | } |
| 66 | 67 | |
| 67 | 68 | matchOnEditor(regex: string, document: vscode.TextDocument) { |
| 69 | + this.forceStop = false; | |
| 68 | 70 | const flags = regexButtons.getFlagString(); |
| 69 | 71 | const timeout = getConfig().get("timeout") as number; |
| 70 | 72 | const startTime = Date.now(); |
| @@ -82,7 +84,12 @@ class Highlighter { | ||
| 82 | 84 | let i = 0; |
| 83 | 85 | const maxMatches = getConfig().get("maxMatches") as number; |
| 84 | 86 | for (const match of matches) { |
| 85 | - if (i >= maxMatches || Date.now() - startTime > timeout) { | |
| 87 | + if ( | |
| 88 | + i >= maxMatches || | |
| 89 | + Date.now() - startTime > timeout || | |
| 90 | + this.forceStop | |
| 91 | + ) { | |
| 92 | + this.forceStop = false; | |
| 86 | 93 | break; |
| 87 | 94 | } |
| 88 | 95 | if (match.index === undefined) { |
| @@ -148,6 +155,7 @@ class Highlighter { | ||
| 148 | 155 | this.highlightMatches(regexEditor); |
| 149 | 156 | this.clearRanges(); |
| 150 | 157 | const nonRegexEditors = getNonRegexEditors(); |
| 158 | + output.log(`Highlighting ${nonRegexEditors.length} editors`); | |
| 151 | 159 | for (const editor of nonRegexEditors) { |
| 152 | 160 | this.matchOnEditor(regexEditor.document.getText(), editor.document); |
| 153 | 161 | this.highlightMatches(editor); |