24
votes

I think standard highlighting is useful -- to some extent. When programming with callbacks and nested structures, this does not help. Keywords and strings, they appear everywhere, and it helps when they are shown in a distinct color, but these colors gives no clue about in which scope I am, where I am in a lexical standpoint.

I have heard of context highlighting, in a talk of Douglas Crockford, which I can't remember a url to. The idea is, to highlight lexical levels of scope. Toplevel definitions are colored in color0, inner level block statements are colored in color1, and this repeats recursively every time a new level of scope is introduced. Below is an example for this, using some imaginary node libraries. (Now added a (ish (or scheme lisp)) example)

Example.Pseudo Scheme Multi-Paren Example

This is not necessarily for node or javascript. I wonder if there is an editor/vim plugin implementing this kind of feature. I don't know if context highlighting is the word for this, but I can't just find one. Googling for context highlighting brings up results for generic token based highlighting and ConTeXt (which I don't have a clue about).

Does this exists? Is there an editor implements this? And more importantly, can I have this in vim?

Another question which is identical to mine, with no real answer: Is Crockford style Context Coloring implemented in any code editor?

3
I like this. Wouldn't if & else be the same level and their respective {}'s be another level?gooddadmike
The problem with colors in general and your colors in particular is that they don't really come with a natural order that could be used to indicate context. The rainbow colors may be an approximation of such an order but it doesn't work with the ideas of nesting or graduality, IMO. Luminance could be used, maybe: lighter around the cursor and progressively darker as we go away. That's an interesting idea, anyway.romainl
Here's a link to a Douglas Cockroft talk on YouTube. He talks about "context coloring" at about 17:30 into it. An important difference is that his variables are colored according to the scope they were declared in, not the scope they appear in.Matthew Strawbridge
@lazfish romainl I just tried to illustrate idea. Colors were just randomly picked for the same reason, I concur romainl's idea, though IDK what Luminance is. lazfish IMO coloring the actual keyword which block's are bound to makes it easier to reckognize levels. MatthewStrawbridge, Thanks a lot for the link, I've been looking it up for a while. Thanks for comments, everybody!user1621465

3 Answers

2
votes

I also couldn't find anything similar, so I wrote one:

rainbow_levels.vim: A different approach to code highlighting.

Of course it is a very simplistic implementation, only considering indentation levels instead of real context, but it gets the job done ;D

1
votes

Not exactly what you are requesting, but code folding is a powerful feature to let you focus on specific levels of your code. Vim supports folding http://vim.wikia.com/wiki/Folding

Indention guides are very handy also, for focusing in on specific parts of code. The sublime text editor has this (http://sublimetext.userecho.com/topic/98136-indent-guide-highlighting-changed/)

I saw this plugin for vim (https://github.com/nathanaelkane/vim-indent-guides) that appears to do the same thing.

You could possibly create a syntax highlighter that uses the indentation logic of this plugin to change the color scheme. What you describe is more involved where it needs to work not just off the indention, but like a language parser.

0
votes

There is now a plugin for this in VIM called vim-js-context-coloring. I've only just played with it a little and am not super impressed yet, but it looks promising!

Note: you need to install npm and then run npm install in the directory where the plugin is installed. I regretfully, forgot to read the instructions when I installed it.