mirror of
https://github.com/nyanotech/recombobulator
synced 2026-02-08 13:47:28 -08:00
reinitialize the project as a web extension
This commit is contained in:
50
src/web/extension.ts
Normal file
50
src/web/extension.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
function applyEdit(transform: Function) {
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
|
||||
if (editor) {
|
||||
const document = editor.document;
|
||||
|
||||
editor.edit(editBuilder => {
|
||||
editor.selections.forEach(selection => {
|
||||
const text = document.getText(selection);
|
||||
const processed = transform(text);
|
||||
editBuilder.replace(selection, processed);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function base64(a: string) {
|
||||
return btoa(a); // TODO - figure out how to handle different character encodings
|
||||
}
|
||||
|
||||
function unbase64(a: string) {
|
||||
return atob(a); // TODO - figure out how to handle different character encodings
|
||||
}
|
||||
|
||||
function generateUUID(a: string) {
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
|
||||
function rot13(a: string) {
|
||||
const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const rotatedAlphabet = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM";
|
||||
return a.replace(/[a-zA-Z]/g, letter => rotatedAlphabet[alphabet.indexOf(letter)]);
|
||||
}
|
||||
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
context.subscriptions.push(vscode.commands.registerCommand('recombobulator.base64', () => {
|
||||
applyEdit(base64);
|
||||
}));
|
||||
context.subscriptions.push(vscode.commands.registerCommand('recombobulator.unbase64', () => {
|
||||
applyEdit(unbase64);
|
||||
}));
|
||||
context.subscriptions.push(vscode.commands.registerCommand('recombobulator.generate-uuid', () => {
|
||||
applyEdit(generateUUID);
|
||||
}));
|
||||
context.subscriptions.push(vscode.commands.registerCommand('recombobulator.rot13', () => {
|
||||
applyEdit(rot13);
|
||||
}));
|
||||
}
|
||||
15
src/web/test/suite/extension.test.ts
Normal file
15
src/web/test/suite/extension.test.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import * as assert from 'assert';
|
||||
|
||||
// You can import and use all API from the 'vscode' module
|
||||
// as well as import your extension to test it
|
||||
import * as vscode from 'vscode';
|
||||
// import * as myExtension from '../../extension';
|
||||
|
||||
suite('Web Extension Test Suite', () => {
|
||||
vscode.window.showInformationMessage('Start all tests.');
|
||||
|
||||
test('Sample test', () => {
|
||||
assert.strictEqual(-1, [1, 2, 3].indexOf(5));
|
||||
assert.strictEqual(-1, [1, 2, 3].indexOf(0));
|
||||
});
|
||||
});
|
||||
30
src/web/test/suite/index.ts
Normal file
30
src/web/test/suite/index.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
// Imports mocha for the browser, defining the `mocha` global.
|
||||
require('mocha/mocha');
|
||||
|
||||
export function run(): Promise<void> {
|
||||
|
||||
return new Promise((c, e) => {
|
||||
mocha.setup({
|
||||
ui: 'tdd',
|
||||
reporter: undefined
|
||||
});
|
||||
|
||||
// Bundles all files in the current directory matching `*.test`
|
||||
const importAll = (r: __WebpackModuleApi.RequireContext) => r.keys().forEach(r);
|
||||
importAll(require.context('.', true, /\.test$/));
|
||||
|
||||
try {
|
||||
// Run the mocha test
|
||||
mocha.run(failures => {
|
||||
if (failures > 0) {
|
||||
e(new Error(`${failures} tests failed.`));
|
||||
} else {
|
||||
c();
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
e(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user