Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
38 changes: 38 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -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() {}
1 change: 1 addition & 0 deletions src/lib/Escape.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class Escape extends Error { }
18 changes: 18 additions & 0 deletions src/lib/Generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export class Generator
{
public static generate(count: number, from: number | null): string
{
const lines: Array<string> = [];

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');
}
}
35 changes: 35 additions & 0 deletions src/lib/entries.ts
Original file line number Diff line number Diff line change
@@ -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(<string>result);
}