A Regex file extension that makes it way easier to edit large regex.
resets when exit re file
2 files changed, +22 -14
+2-1src/extension.ts
| @@ -17,7 +17,8 @@ export function activate(context: vscode.ExtensionContext) { | ||
| 17 | 17 | } |
| 18 | 18 | } |
| 19 | 19 | if (!editorsContainRegexFile()) { |
| 20 | - console.log("No regex file opened"); | |
| 20 | + highlighter.reset(); | |
| 21 | + output.log("No regex file opened"); | |
| 21 | 22 | } |
| 22 | 23 | }); |
| 23 | 24 |
+20-13src/highlighter.ts
| @@ -6,7 +6,7 @@ import decorationGenerator from "./decorationGenerator"; | ||
| 6 | 6 | import { getConfig } from "./global"; |
| 7 | 7 | |
| 8 | 8 | type HighlightGroup = { |
| 9 | - range: vscode.Range; | |
| 9 | + ranges: vscode.Range[]; | |
| 10 | 10 | decoration: vscode.TextEditorDecorationType; |
| 11 | 11 | }; |
| 12 | 12 | |
| @@ -31,6 +31,12 @@ class Highlighter { | ||
| 31 | 31 | output.clear(); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | + createDecoration(options: vscode.DecorationRenderOptions) { | |
| 35 | + const decoration = decorationGenerator.generate(options); | |
| 36 | + this.decorations.push(decoration); | |
| 37 | + return decoration; | |
| 38 | + } | |
| 39 | + | |
| 34 | 40 | getHighlightGroups( |
| 35 | 41 | regex: string, |
| 36 | 42 | flags: string, |
| @@ -38,23 +44,23 @@ class Highlighter { | ||
| 38 | 44 | ) { |
| 39 | 45 | const re = new RegExp(regex, flags); |
| 40 | 46 | const matches = document.getText().matchAll(re); |
| 41 | - const highlightGroups: HighlightGroup[] = []; | |
| 42 | - const decorationMap: { [key: string]: vscode.TextEditorDecorationType } = | |
| 43 | - {}; | |
| 44 | - const outerDecoration = decorationGenerator.generate({ cursor: "pointer" }); | |
| 45 | - this.decorations.push(outerDecoration); | |
| 47 | + const highlightGroups: { [key: string]: HighlightGroup } = { | |
| 48 | + outer: { | |
| 49 | + ranges: [], | |
| 50 | + decoration: this.createDecoration({}), | |
| 51 | + }, | |
| 52 | + }; | |
| 46 | 53 | let i = 0; |
| 47 | 54 | const maxMatches = getConfig().get("maxMatches") as number; |
| 48 | 55 | for (const match of matches) { |
| 49 | 56 | if (i >= maxMatches) { |
| 50 | - continue; | |
| 57 | + break; | |
| 51 | 58 | } |
| 52 | 59 | const start = document.positionAt(match.index); |
| 53 | 60 | const end = document.positionAt(match.index + match[0].length); |
| 54 | - highlightGroups.push({ | |
| 55 | - range: new vscode.Range(start, end), | |
| 56 | - decoration: outerDecoration, | |
| 57 | - }); | |
| 61 | + if (highlightGroups["outer"].ranges) { | |
| 62 | + highlightGroups["outer"].ranges.push(new vscode.Range(start, end)); | |
| 63 | + } | |
| 58 | 64 | i++; |
| 59 | 65 | } |
| 60 | 66 | output.log(`Found ${i} matches`); |
| @@ -70,8 +76,9 @@ class Highlighter { | ||
| 70 | 76 | flags, |
| 71 | 77 | editor.document |
| 72 | 78 | ); |
| 73 | - for (const group of highlightGroups) { | |
| 74 | - editor.setDecorations(group.decoration, [group.range]); | |
| 79 | + for (const key of Object.keys(highlightGroups)) { | |
| 80 | + const group = highlightGroups[key]; | |
| 81 | + editor.setDecorations(group.decoration, group.ranges); | |
| 75 | 82 | } |
| 76 | 83 | } |
| 77 | 84 |