Break out editor interaction into generic function

This commit is contained in:
nyanotech 2022-03-23 10:49:10 +00:00
parent d3c60af233
commit a6157d0a16
Signed by: nyanotech
GPG Key ID: D2D0A9E8F160472B

View File

@ -1,9 +1,10 @@
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
console.log('Recombobulator is activated!');
function base64(a: string) {
return Buffer.from(a).toString('base64'); // TODO - handle different character encodings
}
context.subscriptions.push(vscode.commands.registerCommand('recombobulator.base64', () => {
function applyEdit(transform: Function) {
const editor = vscode.window.activeTextEditor;
if (editor) {
@ -12,12 +13,18 @@ export function activate(context: vscode.ExtensionContext) {
editor.edit(editBuilder => {
editor.selections.forEach(selection => {
const text = document.getText(selection);
const processed = Buffer.from(text).toString('base64'); // TODO - handle different character encodings
const processed = transform(text);
editBuilder.replace(selection, processed);
});
});
}
}
export function activate(context: vscode.ExtensionContext) {
console.log('Recombobulator is activated!');
context.subscriptions.push(vscode.commands.registerCommand('recombobulator.base64', () => {
applyEdit(base64);
vscode.window.showInformationMessage('base64-encoded the selection!');
}));
}