Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight each identifier with a different color #132689

Closed
Sawtaytoes opened this issue Sep 8, 2021 · 3 comments
Closed

Highlight each identifier with a different color #132689

Sawtaytoes opened this issue Sep 8, 2021 · 3 comments
Assignees
Labels
*extension-candidate Issue identified as good extension implementation

Comments

@Sawtaytoes
Copy link

I searched and looked through all issues relating to Semantic Highlighting and found none about this particular topic.

I want to add word-based Semantic Highlighting to VS Code. This is not the same as token-based Semantic Highlighting which is already part of VS Code.

  • Token-based: "a class is highlighted differently from an object".
  • Word-based: "a variable is colored differently from another variable based on its name".

NOTE: I've been using the Colorcoder plugin for Sublime Text, a Semantic Highlighting plugin, since 2013.

What is Semantic Highlighting?

Semantic Highlighting is a revolutionary way of highlighting code not by syntax selectors or token names, but by variable names. In this way, a variable fun would be colored the same no matter where it appeared in the code. Even .fun would have the same coloring.

It's a fundamentally different way of highlighting; although, it's different from, and works side-by-side with syntax highlighting.

word-based semantic highlighting

Prior Art

These are own sources showing Colorcoder in action in Sublime Text:

There were a bunch of articles written on Semantic Highlighting from 2013-2014:

Implementations:

Colorcoder for Sublime Text

This is the plugin I still use today.

It has 2 modes:

  1. Modify a TextMate theme with a bunch of CRC32 hash scopes for every possible scope and color based on a lightness and saturation calculation.
  2. Read from the theme and process word colors based on the hashes in the theme file.

Thinking about it, the theme file modifications most-likely don't even need to be there and could be generated on-the-fly or statically added to the Colorcoder plugin's Python file.

There are a few reasons this solution no longer works for me:

  1. All the latest JavaScript development is happening in VS Code, and I'm a JavaScript developer.
  2. The JavaScript world is moving to TypeScript and that plays best with VS Code.
  3. VS Code has no equivalent Semantic Highlighter.
  4. Colorcoder for Sublime Text is no longer maintained.
  5. The Babel-Sublime plugin for Sublime Text (which adds JSX syntax highlighting) got some major updates, but that version is no longer compatible with Colorcolor. At this time, I'm using an older version of the plugin.
  6. The TextMate theme modification code no longer works in Sublime Text 4. I'm stuck with the color schemes I've already modified.

VS Code Implementation Ideas

I've looked at possible methods of implementing Semantic Highlighting in VS Code, but I'm not sure where to start:

  1. Create a plugin that runs a callback anytime the text is inserted and goes through the available "variable name" scopes, hashes the name, and changes the color. As far as I understand it, this is absolutely not possible in VS Code. You can't even listen to inputs from the document view.
  2. Use the webview API and make my own text renderer. This could work, but would be me making my own VS Code at that point.
  3. Download VS Code's source and add this into core. I would love to do this, but I have no clue where to start. Everything in VS Code is class-based, which is hard enough to follow, but it's also in TypeScript, which I don't know. The VS Code API docs page crawls to a stop because it's literally every possible piece of code thrown in the DOM. This doesn't help me when trying to figure out how it works. On top of that, nothing's documented. Sure, function names and members exist, but I have no clue what anything does. I'm missing the context of "here's a thing, here's why it's important, here's how you use it".

If anyone knows where to point me on how I can get this into VS Code, that would be incredible. To me, this is an accessibility feature that assists with my ability to read, understand, and skim code.

If I can get this integrated into VS Code core, then it's only a setting away to turn it on. At that point, it'll no longer be this niche idea that died in 2015.

@aeschli
Copy link
Contributor

aeschli commented Sep 9, 2021

I think there's already an extension that goes in the direction you want:
https://marketplace.visualstudio.com/items?itemName=MatthewNespor.vscode-color-identifiers-mode?

Maybe connect with the author from the extension to help and join forces.

@aeschli aeschli added the *extension-candidate Issue identified as good extension implementation label Sep 9, 2021
@aeschli aeschli changed the title I want to add word-based Semantic Highlighting to VS Code Highlight each identifier with a different color Sep 9, 2021
@Sawtaytoes
Copy link
Author

I think there's already an extension that goes in the direction you want:
https://marketplace.visualstudio.com/items?itemName=MatthewNespor.vscode-color-identifiers-mode?

Maybe connect with the author from the extension to help and join forces.

That's a good plan. I'll take a look.

The important piece for me is the existence of these docs: https://code.visualstudio.com/api/language-extensions/language-server-extension-guide.

I had no clue a language server even existed.

I've been scouring the API docs trying to find an event listener to register that listens for keystrokes and gives access to the document.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
*extension-candidate Issue identified as good extension implementation
Projects
None yet
Development

No branches or pull requests

3 participants
@Sawtaytoes @aeschli and others