mirror of
https://github.com/nyanotech/recombobulator
synced 2025-12-16 11:30:21 -08:00
Break out editor interaction into generic function
This commit is contained in:
parent
d3c60af233
commit
a6157d0a16
@ -1,23 +1,30 @@
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
function base64(a: string) {
|
||||||
|
return Buffer.from(a).toString('base64'); // TODO - handle different character encodings
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function activate(context: vscode.ExtensionContext) {
|
export function activate(context: vscode.ExtensionContext) {
|
||||||
console.log('Recombobulator is activated!');
|
console.log('Recombobulator is activated!');
|
||||||
|
|
||||||
context.subscriptions.push(vscode.commands.registerCommand('recombobulator.base64', () => {
|
context.subscriptions.push(vscode.commands.registerCommand('recombobulator.base64', () => {
|
||||||
const editor = vscode.window.activeTextEditor;
|
applyEdit(base64);
|
||||||
|
|
||||||
if (editor) {
|
|
||||||
const document = editor.document;
|
|
||||||
|
|
||||||
editor.edit(editBuilder => {
|
|
||||||
editor.selections.forEach(selection => {
|
|
||||||
const text = document.getText(selection);
|
|
||||||
const processed = Buffer.from(text).toString('base64'); // TODO - handle different character encodings
|
|
||||||
editBuilder.replace(selection, processed);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
vscode.window.showInformationMessage('base64-encoded the selection!');
|
vscode.window.showInformationMessage('base64-encoded the selection!');
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user