A Regex file extension that makes it way easier to edit large regex.
buttons show
5 files changed, +129 -20
+18-2src/extension.ts
| @@ -1,9 +1,25 @@ | ||
| 1 | 1 | import * as vscode from "vscode"; |
| 2 | 2 | import { output } from "./output"; |
| 3 | +import { editorsContainRegexFile } from "./utils"; | |
| 4 | +import { regexButtons } from "./regexButtons"; | |
| 3 | 5 | |
| 4 | 6 | export function activate(context: vscode.ExtensionContext) { |
| 5 | 7 | output.activate(); |
| 6 | - output.log("Starting File Regex extension"); | |
| 8 | + regexButtons.activate(context); | |
| 9 | + | |
| 10 | + vscode.window.onDidChangeActiveTextEditor((editor) => { | |
| 11 | + if (editor && editor.document.uri.scheme === "file") { | |
| 12 | + if (editor.document.languageId === "regex") { | |
| 13 | + output.log("Regex file opened"); | |
| 14 | + } | |
| 15 | + } | |
| 16 | + if (!editorsContainRegexFile()) { | |
| 17 | + console.log("No regex file opened"); | |
| 18 | + } | |
| 19 | + }); | |
| 7 | 20 | } |
| 8 | 21 | |
| 9 | -export function deactivate() {} | |
| 22 | +export function deactivate() { | |
| 23 | + output.log("Stopping File Regex extension"); | |
| 24 | + output.deactivate(); | |
| 25 | +} |
+5-0src/global.ts
| @@ -3,3 +3,8 @@ import * as vscode from "vscode"; | ||
| 3 | 3 | export function getConfig(): vscode.WorkspaceConfiguration { |
| 4 | 4 | return vscode.workspace.getConfiguration("regex-file"); |
| 5 | 5 | } |
| 6 | + | |
| 7 | +export function setConfig(key: string, value: any) { | |
| 8 | + const config = getConfig(); | |
| 9 | + return config.update(key, value, true); | |
| 10 | +} |
+9-2src/output.ts
| @@ -4,8 +4,10 @@ class Output { | ||
| 4 | 4 | outputChannel: vscode.OutputChannel | undefined; |
| 5 | 5 | |
| 6 | 6 | activate() { |
| 7 | - this.outputChannel = vscode.window.createOutputChannel("Regex File"); | |
| 8 | - this.show(); | |
| 7 | + if (!this.outputChannel) { | |
| 8 | + this.outputChannel = vscode.window.createOutputChannel("Regex File"); | |
| 9 | + this.show(); | |
| 10 | + } | |
| 9 | 11 | } |
| 10 | 12 | |
| 11 | 13 | show() { |
| @@ -28,6 +30,11 @@ class Output { | ||
| 28 | 30 | this.outputChannel?.appendLine(message); |
| 29 | 31 | console.log(message); |
| 30 | 32 | } |
| 33 | + | |
| 34 | + deactivate() { | |
| 35 | + this.hide(); | |
| 36 | + this.dispose(); | |
| 37 | + } | |
| 31 | 38 | } |
| 32 | 39 | |
| 33 | 40 | export const output = new Output(); |
+87-16src/regexButtons.ts
| @@ -2,37 +2,108 @@ import * as vscode from "vscode"; | ||
| 2 | 2 | import { getConfig } from "./global"; |
| 3 | 3 | import { output } from "./output"; |
| 4 | 4 | |
| 5 | -const FLAG_CHARS = ["g", "i", "m", "s", "u", "y", "x"]; | |
| 5 | +type FlagChar = "g" | "i" | "m" | "s" | "u" | "y" | "x"; | |
| 6 | +type FlagState = "on" | "off"; | |
| 7 | +type FlagDict = { [key in FlagChar]: Flag }; | |
| 8 | +type Flag = { | |
| 9 | + char: FlagChar; | |
| 10 | + tooltip: string; | |
| 11 | + button?: vscode.StatusBarItem; | |
| 12 | + state?: FlagState; | |
| 13 | +}; | |
| 14 | + | |
| 15 | +const FLAG_ICONS = { | |
| 16 | + on: "$(check)", | |
| 17 | + off: "$(x)", | |
| 18 | +}; | |
| 19 | + | |
| 20 | +const FLAG_COLORS = { | |
| 21 | + on: "#00FF00", | |
| 22 | + off: "#FF0000", | |
| 23 | +}; | |
| 24 | + | |
| 25 | +const FLAG_CHARS: Flag[] = [ | |
| 26 | + { char: "g", tooltip: "Global search: find all matches in the string" }, | |
| 27 | + { | |
| 28 | + char: "i", | |
| 29 | + tooltip: | |
| 30 | + "Case-insensitive search: match both uppercase and lowercase characters", | |
| 31 | + }, | |
| 32 | + { | |
| 33 | + char: "m", | |
| 34 | + tooltip: "Multi-line search: ^ and $ match the start and end of each line", | |
| 35 | + }, | |
| 36 | + { char: "s", tooltip: "Dot matches newline: . matches newline characters" }, | |
| 37 | + { char: "u", tooltip: "Unicode: enable full Unicode matching" }, | |
| 38 | + { | |
| 39 | + char: "y", | |
| 40 | + tooltip: | |
| 41 | + "Sticky search: start at the last index where the previous match ended", | |
| 42 | + }, | |
| 43 | + { | |
| 44 | + char: "x", | |
| 45 | + tooltip: | |
| 46 | + "Extended: allow whitespace and comments within the pattern (non-standard in JavaScript)", | |
| 47 | + }, | |
| 48 | +]; | |
| 6 | 49 | |
| 7 | 50 | class RegexButtons { |
| 8 | - flagButtons: { | |
| 9 | - [key: string]: { | |
| 10 | - state: "on" | "off"; | |
| 11 | - button: vscode.StatusBarItem; | |
| 12 | - }; | |
| 13 | - } = {}; | |
| 51 | + context: vscode.ExtensionContext | undefined; | |
| 52 | + flags: FlagDict; | |
| 14 | 53 | |
| 15 | - constructor() {} | |
| 54 | + constructor() { | |
| 55 | + this.flags = FLAG_CHARS.reduce((acc, flag) => { | |
| 56 | + flag.state = "off"; | |
| 57 | + acc[flag.char] = flag; | |
| 58 | + return acc; | |
| 59 | + }, {} as { [key in FlagChar]: Flag }); | |
| 60 | + } | |
| 16 | 61 | |
| 17 | 62 | activate(context: vscode.ExtensionContext) { |
| 18 | - this.createButtons(context); | |
| 19 | - this.updateFlagsFromConfig(); | |
| 63 | + this.context = context; | |
| 64 | + this.createButtons(); | |
| 65 | + } | |
| 66 | + | |
| 67 | + getFlags() { | |
| 68 | + return getConfig().get("flags") as string; | |
| 20 | 69 | } |
| 21 | 70 | |
| 22 | - updateFlagsFromConfig() { | |
| 23 | - output.log("Updating flags"); | |
| 71 | + toggleFlag(flag: Flag) { | |
| 72 | + output.log(`Toggling flag ${flag.char}`); | |
| 73 | + flag.state = flag.state === "on" ? "off" : "on"; | |
| 74 | + this.decorateFlag(flag); | |
| 24 | 75 | } |
| 25 | 76 | |
| 26 | - updateFlagsFromUI() { | |
| 27 | - output.log("Updating flags from UI"); | |
| 77 | + decorateFlag(flag: Flag) { | |
| 78 | + if (flag.button && flag.state) { | |
| 79 | + flag.button.text = `${FLAG_ICONS[flag.state]} ${flag.char.toUpperCase()}`; | |
| 80 | + flag.button.color = FLAG_COLORS[flag.state]; | |
| 81 | + } | |
| 28 | 82 | } |
| 29 | 83 | |
| 30 | - createButton(flag: string) { | |
| 84 | + createButton(flag: Flag) { | |
| 31 | 85 | output.log(`Creating button for ${flag}`); |
| 86 | + flag.button = vscode.window.createStatusBarItem( | |
| 87 | + vscode.StatusBarAlignment.Left | |
| 88 | + ); | |
| 89 | + this.decorateFlag(flag); | |
| 90 | + flag.button.command = `regex.toggleFlag${flag.char.toUpperCase()}`; | |
| 91 | + let disposable = vscode.commands.registerCommand( | |
| 92 | + `regex.toggleFlag${flag.char.toUpperCase()}`, | |
| 93 | + () => { | |
| 94 | + this.toggleFlag(flag); | |
| 95 | + } | |
| 96 | + ); | |
| 97 | + flag.button.show(); | |
| 98 | + this.context?.subscriptions.push(flag.button); | |
| 99 | + this.context?.subscriptions.push(disposable); | |
| 32 | 100 | } |
| 33 | 101 | |
| 34 | - createButtons(context: vscode.ExtensionContext) { | |
| 102 | + createButtons() { | |
| 35 | 103 | output.log("Creating buttons"); |
| 104 | + for (const flag of FLAG_CHARS) { | |
| 105 | + this.createButton(flag); | |
| 106 | + } | |
| 36 | 107 | } |
| 37 | 108 | |
| 38 | 109 | disposeButtons() { |
+10-0src/utils.ts
| @@ -0,0 +1,10 @@ | ||
| 1 | +import * as vscode from "vscode"; | |
| 2 | + | |
| 3 | +export function editorsContainRegexFile() { | |
| 4 | + const editors = vscode.window.visibleTextEditors; | |
| 5 | + return editors.some( | |
| 6 | + (editor) => | |
| 7 | + editor.document.languageId === "regex" && | |
| 8 | + editor.document.uri.scheme === "file" | |
| 9 | + ); | |
| 10 | +} |