diff --git a/README.md b/README.md new file mode 100644 index 0000000..3829e9b --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# PHP binary flags + +This is a simple extension to create PHP flags that act like keyword arguments. A flag is a constant defining an integer that can be use for bitwise operations (combined with other flags) and provide multiple configuration entries in one variable. Read more about underlying concept in the [documentation](https://www.php.net/manual/en/language.operators.bitwise.php). + +For now, this extension only provides object-oriented flags. \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 6c40941..c1a8324 100644 --- a/package-lock.json +++ b/package-lock.json @@ -536,9 +536,9 @@ } }, "lodash": { - "version": "4.17.15", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", - "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, "log-symbols": { diff --git a/package.json b/package.json index e189eb5..1009de3 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,12 @@ "name": "phpbinaryflags", "displayName": "PHP binary flags", "description": "Automatically generate sets of binary flags", + "publisher": "bloohirsch", + "repository": { + "type": "git", + "url": "https://github.com/butteredptarmigan/vscode-php-binary-flags" + }, + "license": "GNU GPL 3.0", "version": "0.0.1", "engines": { "vscode": "^1.41.0" diff --git a/src/extension.ts b/src/extension.ts new file mode 100644 index 0000000..60f5c6c --- /dev/null +++ b/src/extension.ts @@ -0,0 +1,38 @@ +import * as vscode from 'vscode'; +import { Generator } from './lib/Generator'; +import { validate, parseResult } from './lib/entries'; +import { Escape } from './lib/Escape'; + +export function activate(context: vscode.ExtensionContext) { + let disposable = vscode.commands.registerCommand('extension.generateFlags', () => { + try { + const args = Object(); + + vscode.window.showInputBox({ + prompt: 'How many flags should be created?', + validateInput: validate + }) + .then((result) => { + args.count = parseResult(result); + + return vscode.window.showInputBox({ + prompt: 'Select a base, i.e. last flag that already exists (leave empty for no base)', + validateInput: validate + }); + }) + .then((result) => { + args.from = parseResult(result); + const text = Generator.generate(args.count, args.from); + const snippet = new vscode.SnippetString(text); + + vscode.window.activeTextEditor?.insertSnippet(snippet); + }); + } catch (err) { + if (err instanceof Escape) return; + } + }); + + context.subscriptions.push(disposable); +} + +export function deactivate() {} \ No newline at end of file diff --git a/src/lib/Escape.ts b/src/lib/Escape.ts new file mode 100644 index 0000000..7f82dda --- /dev/null +++ b/src/lib/Escape.ts @@ -0,0 +1 @@ +export class Escape extends Error { } \ No newline at end of file diff --git a/src/lib/Generator.ts b/src/lib/Generator.ts new file mode 100644 index 0000000..45b5efc --- /dev/null +++ b/src/lib/Generator.ts @@ -0,0 +1,18 @@ +export class Generator +{ + public static generate(count: number, from: number | null): string + { + const lines: Array = []; + + let num = (from === null) + ? 1 + : from * 2; + for (let i = 0; i < count; i++) { + const line = `public const $${i + 1} = ${num};`; + lines.push(line); + num *= 2; + } + + return lines.join('\n'); + } +} \ No newline at end of file diff --git a/src/lib/entries.ts b/src/lib/entries.ts new file mode 100644 index 0000000..3164317 --- /dev/null +++ b/src/lib/entries.ts @@ -0,0 +1,35 @@ +import { Escape } from "./Escape"; + +function parse(input: string): number | null { + if (!input) { + return null; + } + + return Number.parseInt(input.trim()); +} + +export function validate(input: string): string | null { + try { + var actual = parse(input); + + if (actual === null) { + return null; + } + + if (actual < 1) { + return 'Input must be an integer greater that zero.' + } + } catch (error) { + return 'Input must be a valid integer.'; + } + + return null; +} + +export function parseResult(result: string | undefined) { + if (result === undefined) { + throw new Escape(); + } + + return parse(result); +} \ No newline at end of file