Type something to search...
Visual Studio Code - Creating a Custom Text Filter Extension

Visual Studio Code - Creating a Custom Text Filter Extension

In this post I will describe a way to create an extension which allows the user to receive the selected text as a string passed into a Typescript function, run that string through any command line program receive the response as a string in Typescript then return the value of the string that will replace the selection. The user can define as many such functions as they want and bind them to various keyboard shortcuts.

The first thing we will need is to install Jeff Hykin’s Selection Convert Extension. I’m sure this is not the only extension that will achieve this end, but I found this one and it works.

I encourage the reader to spend some time looking for other extensions and attempt to arrive at the same result with another. I followed another such guide that reccomended a different extension myself, and I was unable to make it work. Sometimes things are needlessly complex, and I often find myself frustrated with the instructions given by articles like this one written by other people. It is in these times that I stop. Think about the problem I’m trying to solve, ensure that I understand all the moving parts and then try to ensure the solution I’ve found written is the most streamlined for the problem I’m trying to solved. If it is not, I should have at least gleaned some understanding of the moving parts from what I’ve read. I’ll begin searching the documentation, asking questions to LLMs, and searching stack overflow until I can formulate my own solution. Usually in so doing this, I will learn so many things more things about the technology I’m working with than I had set out to learn.

Anyway back to the details. Once Selection Convert is installed, you need to invoke it by pressing Ctrl+Shift+P and then typing Selection Convert. You will then see a list of options appear. The first option is Selection Convert: Convert Selected Text. Click on it. This will open the file $HOME/.vscode/selection-convert.js where the Typescript functions to define each filter will be stored. In this file, you will create a function for each filter you want to create, and then provide those functions as exports. The Selection Convert Module will create additional items under the Selection Convert: Convert Selected Text menu for each function exported in selection-convert.js.

Let’s have a look at an example selection-convert.js file:

const { execSync } = require('child_process');

//Filter Export Functions

// -- function for Boxes - gradientbox-----------------------------------------

selectionToParchment = function(text) {
  var result = runFilter(text, 'parchment');
  return result;
}

selectionToHeadline = function(text) {
  var result = runFilter(text, 'headline');
  return result; 
}

// --/ END Filter Export Functions /--

runFilter = function(text, design) {
  var command = "echo \""+text+"\" | boxes -d "+design+" -p h1 -s 80 -i none -n UTF-8"
  var result = "";
  result = execSync(command);
  return result.toString();
}

module.exports = { selectionToHeadline, selectionToParchment };

The above file depends on the boxes command line tool. You can install it with npm install -g boxes. The boxes tool is used to convert the selected text into a box. boxes provides a number of filters to place the selected text into various frame designs. There are infinitely many possible uses for the framework provided by selection convert. Spend some time reflecting on the power of this one simple extension. Let’s review this line by line.

const { execSync } = require('child_process');

This line is a common way to use the child_process module in Node.js. It allows us to run command line tools from within our Typescript code. Notice, I’ve imported execSync that’s because the child_process module also provides an asynchronous exec module but that’s not very useful here since we’re expected to provide a function that returns a string. The execSync is a syncronous version of the exec function so it’s possible to return a value from a function that will be called by the IDE. If we needed to call an asynchronous function from within the function called by the menu item, we would run into problems as asyncronous functions are generally written by passing a pointer to a function they should call upon completion. That’s essentially what selection-convert.js is except we’ve passed the functions we define to the menu-items in VSCode. And we’ve no way to specify which function to call with the replacement text, we can only replace this text by returning from the function, so we need a syncronous function to get the result from the command line.e

Next we define the a filter function.

selectionToParchment = function(text) {
  var result = runFilter(text, 'parchment');
  return result;
}

which passes the text though runFilter

runFilter = function(text, design) {
  var command = "echo \""+text+"\" | boxes -d "+design+" -p h1 -s 80 -i none -n UTF-8"
  var result = "";
  result = execSync(command);
  return result.toString();
}

In runFilter the most important line is var command = "echo \""+text+"\" | boxes -d "+design+" -p h1 -s 80 -i none -n UTF-8" where we see an example use of a command line program on the text. Notice in this case, the text must be echoed on the command line then piped through the program. Normally this is done when a program expects a file as input. There are so many text filtering utilities that this extension can be modified to use. Please, I encourage you to share your ideas for possible uses with me, so that I can list them as an addendum to this post.

Related Posts

Horizontal Scaling in Kubernetes

Horizontal Scaling in Kubernetes

Horizontal scaling in Kubernetes refers to dynamically adjusting the number of application instances (pods) based on workload changes to maintain optimal performance. Unlike vertical scaling, which in

read more
How to Use Docker for Development Environments

How to Use Docker for Development Environments

When developing an application running in Docker, you can edit the files on your local machine and have those changes immediately reflected in the running container. This is typically done using Docke

read more
CLI pager commands - more, less, and most

CLI pager commands - more, less, and most

These PAGER commands allow you to navigate through file and data stream content with a variety of useful commands. If you need to manually visually navigate through a lot of text or data, you'll f

read more
Defining New ASCII Designs For Thomas Jensens Boxes Software

Defining New ASCII Designs For Thomas Jensens Boxes Software

The "Boxes" command line tool takes a block of text and wraps it in one of 50 some frames listed with boxed -l and specified by the user with boxes -d the text can either be piped into boxed or a

read more
Javascript ES6 Modules, Introduction

Javascript ES6 Modules, Introduction

With the release of ECMAScript 2015 (ES6), JavaScript introduced a powerful new feature: modules. This addition was a significant shift in how developers structure and manage code, allowing for better

read more
Understanding JavaScript Promises

Understanding JavaScript Promises

In JavaScript, the concept of thenables often arises when working with Promises. Promises inherit from the base class Thenable, meaning that Promises are a type of Thenable, but a Thenable is not

read more
Part 4, Dynamic Imports and Lazy Loading

Part 4, Dynamic Imports and Lazy Loading

Introduction So far, we’ve explored the world of static imports in JavaScript, where dependencies are imported at the start of a script’s execution. However, in modern web development, there are c

read more
Understanding JavaScript Promises and Lazy Loading Callbacks

Understanding JavaScript Promises and Lazy Loading Callbacks

In JavaScript, thenables play a key role in asynchronous programming, particularly with Promises in ES6. One of the advantages of ES6 Promises (which use thenables) over older implementations like

read more
Part 2, Understanding Named and Default Exports

Part 2, Understanding Named and Default Exports

Introduction In the previous part, we introduced the basics of importing and exporting in JavaScript ES6, covering both named and default exports. Now, it’s time to explore these t

read more
Part 1, Getting Started with Modules

Part 1, Getting Started with Modules

Introduction Before ES6, JavaScript did not have a native module system, which made it difficult to split large codebases into manageable pieces. Developers relied on patterns like the Module Patt

read more
Part 3, Re-exports and Module Aggregation

Part 3, Re-exports and Module Aggregation

Introduction As projects grow, the number of modules and dependencies can quickly become overwhelming. In large codebases, managing and organizing these modules is key to maintaining readability a

read more
Managing Multiple Git Identities Per Single User Account

Managing Multiple Git Identities Per Single User Account

If you need to work make changes to code under different identities, there are a few different ways you can approach this. The first solution I saw on many webpages was way too clunky for my taste. It

read more
Secure Authentication & Authorization Exercises

Secure Authentication & Authorization Exercises

Each exercise includes:Scenario Initial Information Problem Statement Tasks for the student Bonus Challenges for deeper thinking**Section 1: OAuth 2.0 + PKCE

read more
Never Been a Huge Fan of IDEs, but I Like Visual Studio Code

Never Been a Huge Fan of IDEs, but I Like Visual Studio Code

To be completely honest, for the past many years, I've debated whether or not to use an IDE. On one had, they provide a number of features like code completion, debugging, and a number of other things

read more
Powerful Text Selection Operations in VSCode

Powerful Text Selection Operations in VSCode

VSCode has become one of the most popular IDEs in recent years. It is also available for free. Here are a few text selection options of which you may not be aware. Multiple Selections It is possi

read more
What is Docker and Where and Why Should You Use it?

What is Docker and Where and Why Should You Use it?

Docker is a platform designed for containerization, allowing developers to package applications and their dependencies into lightweight, portable containers. These containers are isolated environments

read more
What is Kubernetes? Where and Why Should You Use it?

What is Kubernetes? Where and Why Should You Use it?

Key Use Cases and Benefits Kubernetes simplifies the deployment and scaling of applications through automation. It facilitates automated rollouts and rollbacks, ensuring seamless updates without d

read more
Secrets Management DevOps Tools and More

Secrets Management DevOps Tools and More

These tools provide a means of securely storing secrets (encryption keys, passwords, all that good stuff that you want to make available to your production systems, but you must protect from exposure)

read more
Web Application Boilerplate

Web Application Boilerplate

I've been tinkering with a number of projects and along the way I've come up with what I think is a solid starting point for any web application that you might build. Understanding that your applicat

read more
Part 5, Best Practices and Advanced Techniques

Part 5, Best Practices and Advanced Techniques

In the previous parts of this series, we explored the fundamentals of module importing and exporting in ES6, the different ways to define modules, and how to work with default and named exports. In th

read more
Using Makefiles, SOPS, and virtualenv Together for Elegant Python Environments

Using Makefiles, SOPS, and virtualenv Together for Elegant Python Environments

I've been managing my secrets with sops ever since I looked into the subject last month, and I've been using Makefiles to handle bringing up my docker environments as they provide a nice way to not

read more