JatinSanghvi / color-my-text-vscode

Create aesthetically pleasing and shareable color and style schemes quickly in Visual Studio Code for your files with custom formats.
MIT License
1 stars 8 forks source link
vscode-extension

Color My Text

color-my-text

Introduction

The Color My Text extension enables you to add custom text-decoration rules to files that do not conform to a standard syntax, such as log files or personal to-do lists. These decoration rules can be stored as workspace settings, so they can be shared with others who work on the same files. This means that when you share files in non-standard formats with other users, you can now also supplement those files with information on how they should be decorated. Isn't that amazing?

Still confused? Clone this repository and open the example folder inside Visual Studio Code. Make sure that you install the 'Color My Text' extension which should be recommended to you after the folder is opened.

Check out the extension on Visual Studio Marketplace.

Features

Apply Text Decorations as You Type

live-color

live-style

The text decorations get applied as you type in the files and also as you update the decoration rules in the settings file. There is no need to reopen the files to reapply the text decorations. It does not matter if the document is split over multiple windows. It just works.

When you are creating text decoration rules, I would suggest having the file to be decorated opened on one side of the editor and the settings file opened on the other side. This way, as you change the configuration, you can see them being applied live on the file. This can help you quickly find issues with the regex patterns and experiment with different styles so as to choose one that offers you the best appearance.

quick-configuration

Fully Configurable

Anatomy of Workspace Setting

workspace-setting

Specify files, file types, folders, or any combination of them

You can specify multiple paths with paths property to apply a common set of rules on all of them. In the example setting above, the lines ending with - done will be struck out in all the Markdown files and all files named todo.txt inside todos folder or its subfolders.

Specify list of rules for each set of files

You can specify multiple rules with rules property for each set of paths. In the example setting above, the lines ending with - doing will be set to bold and underlined, and the lines ending with - todo will be italicized for the log files inside C:\Log Files folder.

Specify list of regular expression patterns to match the text

You can specify multiple regular expression patterns with patterns property for each rule. Text matching any of the regular expressions will have that rule applied. These patterns can also contain Unicode regular expressions tokens. In the example setting above, both words Critical and Error will be colored with bright red color. Note that these are not wildcard patterns, so a pattern such as "Create*Async" will not work as expected. Instead, you may want to set the pattern as "Create\\w*Async".

Specify whether to perform case-sensitive matches

You can specify whether to perform case-sensitive matches of text with the regular expressions with the matchCase property. If the property is not specified, the extension will perform case-insensitive matches. In the example setting above, all rules for paths *.md and todos/**/todo.txt will be applied case-insensitively while the rules for path C:\Log Files\*.log will be applied case-sensitively.

Numerous Color and Style Combinations

theme-support

You can set any of the color and style combinations for each of the rules.

color

You can choose one of the sixteen colors as listed in the Integrated Terminal colors section with the terminal.ansi prefix removed. These colors are Black, Blue, BrightBlack, BrightBlue, BrightCyan, BrightGreen, BrightMagenta, BrightRed, BrightWhite, BrightYellow, Cyan, Green, Magenta, Red, White, and Yellow.

By providing a limited set of color options, we can ensure the colored text will mix well with the rest of the text irrespective of the color theme selected by the user. Most of the standard Visual Studio Code color themes will choose the colors for the text that will contrast well with the editor background. And it prevents you from fixing all colors in the settings if you ever want to switch between Light and Dark colored themes. It also removes the requirement for two users sharing the configuration to have the same color theme set in their respective editors. In short, the text colors will update themselves automatically to match with the color themes.

bold

You can set the value of this property to true to bold the text, to false to un-bold the text (if the color theme had bolded it), or do not set it to retain the original font weight.

italic

You can set the value of this property to true to italicize the text, to false to un-italicize the text (if the color theme had italicized it), or do not set it to retain the original font style.

underline

You can set the value of this property to true to underline the text, to false to un-underline the text (if the color theme had underlined it), or do not set it to retain the original text decoration.

strikeThrough

You can set the value of this property to true to strike out the text, to false to un-strike the text (if the color theme had struck it), or do not set it to retain the original text decoration.

Useful Information

Share and Collaborate Over Text Decorations

share-decorations

As I mentioned earlier, the text decoration rules can be shared through the workspace settings file (.vscode/settings.json) along with the files on which those rules will need to be applied. Hence, when you share your files with other users via a version control system, then just like you can accept contributions for the regular files, you can also accept fixes and enhancements to the decoration rules from other users.

Known Issues

All the below are minor issues and in most cases, should not affect your experience. These are all results of various platform limitations.

Difficult Choices

Following are some of the choices that I could not make up my mind on. I may reconsider them in future based on user feedback.

Supporting Decoration of Capture Groups

It would be interesting to support decorations for capture groups within a regular expression. This allows more flexibility in terms of what text gets decorated. For example, if you have some text that looks like Seattle - Washington - USA and you want to color individual items differently, then a way to do it would be to specify regex pattern as (\\w+) - (\\w+) - (\\w+) with three capture groups and provide separate coloring rules for text matching the individual capture groups. In its current state, if we need to achieve the same results, we need to specify three rules with positive lookahead and lookbehind assertions in the pattern such as \\w+(?= - \\w+ - \\w+) to match Seattle, (?<=\\w+ - )\\w+(?= - \\w+) to match Washington and (?<=\\w+ - \\w+ - )\\w+ to match USA. This is not only cumbersome but also not very intuitive.

However, after more contemplation, I decided not to implement this feature, the reasons being:

Decorating Visible Text Only

The existing implementation decorates the entire file content. This process consumes significant processor time and becomes visibly slow when user is editing a large text file. I need to check if it is possible to find just the lines that were edited since the last time and decorate only those ones. Another possible optimization would be to only decorate the text that is visible in the document window. However, in this case, when user would scroll through the document, the text would first appear undecorated and then get decorated after a short delay, which might degrade the user experience. Yet another potential solution would be to decorate the visible text first and the invisible text later (with a larger interval between successive decorations). Again, I can compare these options and choose to work on them based on user feedback.

Meta Section

Background

This extension was developed as part of Microsoft Hackathon 2022 project. A week before the Hackathon week, I was looking to color lines where shared variables are accessed inside a C# file to visibly ensure there are no concurrency issues present inside the code. I could not find a great extension that fulfilled my requirements, hence I decided to build an extension by myself. I hope more similar use cases will be uncovered with this extension and it will serve many people and teams.

Non-Goals

Some of these non-goals were set considering my little experience working on JavaScript and TypeScript, and the short time I had to work on the extension. But now the project is nearing completion, I think all of these were the right choices. I do not intend to convert any of these non-goals into goals at the time of this writing.