Highlighting program variables instead of keywords?

Chris Angelico rosuav at gmail.com
Sun Jan 26 20:51:30 EST 2014


On Mon, Jan 27, 2014 at 12:29 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Hmmm, I'm not convinced, but then I wasn't convinced by syntax
> highlighting either until I had used it for a while. (I still think it's
> a nice-to-have rather than a must-have.)

It's definitely just a nice-to-have. I've built miniature text editors
into various programs (including two MUD clients, for the purpose of
editing files on the server), and haven't bothered with the level of
sophistication required for smart-indent, much less syntax
highlighting. (I'll occasionally put in an auto-indent - hit enter and
it indents to the same level as the previous line - but not even
always that, and definitely not "hey, that line has more ( than ) so
I'll indent an extra level", which is a *hugely* helpful feature when
I'm doing main coding (it catches errors, as well as saving time on
indentation), but one I can do without if it means I can knock
together an editor in a tenth the time.)

> Seems to me that beyond a dozen or so variables, the colours won't be
> distinctive enough to get much benefit. Especially if similar names get
> similar colours, e.g. the name "handle_process" and the typo
> "handle_prosess" will be barely distinguishable. In a well-designed
> program, most variables will appear in a fairly limited number of places,
> possibly in only a single function or method, so in even a fairly small
> project you might have a few dozen distinct variables, each of which
> might appear only three or five times.

Hmm. Would similar names get similar colours? I would guess that a
naive approach would just go sequentially through the file, but if
it's smart enough to recognize similarity, it'd be nice if it could
then be smart enough to make them contrast. The more similar the
words, the less similar the colours, and vice versa. Or at very least,
sort all the words alphabetically, and then distribute them in some
binary flip-flop method that will tend to put the ones with the same
start further apart. (That would catch typos anywhere other than the
beginning of the name.)

Oh. Yes, they will get similar colours. Just re-read the page:
"""
Variable names with similar prefixes will be assigned similar colors.
We collect all the custom names and order them alphabetically, then
distributing them evenly along a spectrum. This way we make sure we
use as distinct colors as possible.
"""
IMO this is a flawed design decision. The purpose of the color is to
add information, not to replicate what's already there. (And it's
using the simplistic method I described, of simply sorting
alphabetically to determine similarity. There are other ways of
distinguishing, but they're a lot more complicated.)

If it's smart enough to recognize the difference between local and
global names, that would be useful. Might not be so useful in JS, but
definitely in Python. It would take a bit of smartness, but most
editors these days have to be able to fully parse the language anyway.
Hmm. Actually, this might be a plausible idea just on its own: never
mind about distinguishing specific names, just create a highlight
class for each of "local name" (including function parameters),
"global name, read-only", and "global name, assigned to in this
function", and possibly non-local (read-only and assigned) as well. If
you suddenly see that the name 'len' has changed from being "global
name, read-only" to "local name", you'll know instantly that you just
shadowed a built-in (probably unintentionally and in a way likely to
cause bugs). Do any editors currently offer this?

ChrisA



More information about the Python-list mailing list