From 20e5eb745697daf51e1522140a63bde81ce19437 Mon Sep 17 00:00:00 2001 From: nyanotech Date: Wed, 23 Mar 2022 10:24:16 +0000 Subject: [PATCH] MVP of a base64-encode command --- package.json | 6 +++--- src/extension.ts | 37 +++++++++++++++++-------------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 327927d..855f82f 100644 --- a/package.json +++ b/package.json @@ -10,14 +10,14 @@ "Other" ], "activationEvents": [ - "onCommand:recombobulator.helloWorld" + "onCommand:recombobulator.base64" ], "main": "./dist/extension.js", "contributes": { "commands": [ { - "command": "recombobulator.helloWorld", - "title": "Hello World" + "command": "recombobulator.base64", + "title": "base64 encode" } ] }, diff --git a/src/extension.ts b/src/extension.ts index 8ca370a..a3185a5 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,26 +1,23 @@ -// The module 'vscode' contains the VS Code extensibility API -// Import the module and reference it with the alias vscode in your code below import * as vscode from 'vscode'; -// this method is called when your extension is activated -// your extension is activated the very first time the command is executed export function activate(context: vscode.ExtensionContext) { - - // Use the console to output diagnostic information (console.log) and errors (console.error) - // This line of code will only be executed once when your extension is activated - console.log('Congratulations, your extension "recombobulator" is now active!'); + console.log('Recombobulator is activated!'); - // The command has been defined in the package.json file - // Now provide the implementation of the command with registerCommand - // The commandId parameter must match the command field in package.json - let disposable = vscode.commands.registerCommand('recombobulator.helloWorld', () => { - // The code you place here will be executed every time your command is executed - // Display a message box to the user - vscode.window.showInformationMessage('Hello World from recombobulator!'); - }); + context.subscriptions.push(vscode.commands.registerCommand('recombobulator.base64', () => { + const editor = vscode.window.activeTextEditor; - context.subscriptions.push(disposable); + if (editor) { + const document = editor.document; + const selection = editor.selection; + const text = document.getText(selection); + + const processed = Buffer.from(text).toString('base64'); // TODO - handle different character encodings + + editor.edit(editBuilder => { + editBuilder.replace(selection, processed); + }); + } + + vscode.window.showInformationMessage('base64-encoded the selection!'); + })); } - -// this method is called when your extension is deactivated -export function deactivate() {}