Make sense of your variables at a glance with semantic highlighting

Let’s say you have a really dense function or method, with lots of arguments passed in and lots of local variables. Syntax highlighting helps some, but can PyCharm do more?

In 2017.1, PyCharm ships with “semantic highlighting” available as a preference. What is it, what problem does it solve, and how do I use it? Let’s take a look.

It’s So Noisy

Sometimes you have really, really big functions. Not in your codebase, of course, because you are tidy. But hypothetically, you encounter this in a library:

2016-noselection

PyCharm helps, of course. Syntax highlighting sorts out the reserved words and different kinds of symbols: bold for keywords, gray for unneeded, yellow means suggestion, green for string literals. But that doesn’t help you focus on the parameter “namespaces”. Clicking on a specific symbol highlights it for the rest of the file:

2016-selection

That kind of works, but not only do you have to perform an action for each symbol you want to focus on, it also moves your cursor. It’s a solution to a different problem.

How can my tool help me scan this Python code without much effort or distraction?

IntelliJ Got It

As you likely know, PyCharm and our other IDEs are built atop the IntelliJ IDE platform. In November, IntelliJ landed an experimental cut of “semantic highlighting”:

“Semantic Highlighting, previously introduced in KDevelop and some other IDEs, is now available in IntelliJ IDEA. It extends the standard syntax highlighting with unique colors for each parameter and local variable.”

It wasn’t available in the IDEs, but you could manually enable it via a developer preference. Here’s a quick IntelliJ video describing the problem and how semantic highlighting helps.

With PyCharm 2017.1, the engine is now available to be turned on in preferences. Let’s see it in action.

Crank Up the Signal

Blah blah blah, what does it look like?

2017Our noisy function now has some help. PyCharm uses semantic highlighting to assign a different color to each parameter and local variable: the “namespaces” parameter is now a certain shade of green. You can then let color help you scan through the function to track the variable, with no distracting action to isolate one of them or switch focus to another.

To turn on semantic highlighting in your project, on a per-font-scheme basis, visit the Editor -> Colors & Fonts -> Language Defaults preference:

prefs

Your Colors Make Me Sad

The default color scheme might not work for you. Some folks have visual issues for red and green, for example. Some might have contrast issues in their theme or workspace. Others might simply hate #114D77 (we’ve all been there.)

If you make IDEs for long enough, you learn self-defense, and that means shipping a flexible means of customization:

colorpicker

The pickers let you assign base colors then gradients to tailor a wide number of local symbols to your needs and taste.

Learn More

PyCharm’s goal is to help you be a badass Python developer, and hopefully our use of semantic highlighting helps you make sense of dense code. We’re still working on the idea itself as well as the implementation, so feel free to follow along in our bug tracker across all our products, since this isn’t a PyCharm-specific feature.

And as usual, if you have any quick questions, drop us a note in the blog comments.

This entry was posted in Cool Feature and tagged . Bookmark the permalink.

14 Responses to Make sense of your variables at a glance with semantic highlighting

  1. bronsen says:

    how about adding a screenshot that shows what the code looks like after enabling “semantic highlighting”?

    • bronsen says:

      Excellent, there it is :)

      /me goes to reboot browser

      • Paul Everitt says:

        I’d be interested in your thoughts on the utility of this feature, after you’ve used it a while. I went through this cycle: “Oh wow, cool! Hmm, all the colors look the same. Oh, wait, now it’s making sense, nice!”

  2. Jason says:

    Your colors do make me sad. Can’t see reds very well. Thanks for including a method to change the defaults. Going to save me a ton of time

  3. Jeremiah says:

    I’m using Pycharm 2016.3.2, but feeling no difference after enabling Sematic in preferences. Colors didn’t change. Is this a bug?

  4. Jungyu Kim says:

    True / False / None color is changed… How do i get reset???????

  5. How do the IDE figure out which color to give each variable?

    I have created a color scheme plugin and I would like to see if I should enable some presets for my users, but I cannot determine how each variable is given its color. It appears to be random in my available code. Once I figure this out, I can try to find a good color combination that works with my existing colors.

    However, If the variables are given colors in a somewhat random manner, then I would rather it not be enabled by default in my color scheme plugin.

    One thing I thought might have been available in this feature was that it could indicate to the user if I method had too many parameters (i.e. set color 1 through 3 as a single color gradient with color 4 through 5 as a red gradient)). However, this doesn’t seem to be the case for this feature.

    • Paul Everitt says:

      Here’s the help page for configuring colors for each variable.

      • Christopher Rucinski says:

        I know how to configure the colors. That wasn’t what I was trying to figure out.

        I am more or less looking for the algorithm, or a way for me to look at some code, and given color #1 – #5, I can reasonable guess what color the IDE will give a specific variable.

        Based on my own code and the code in the attached picture in the “Crank Up the Signal” section, I cannot figure out any type of algorithm. It all seems random at which color a specific variable will get.

      • I look into the RainbowHighlighting code files but wasn’t able to glean too much knowledge into how the colors for a specific variable was decided, but I also checked on other IDE’s have implemented semantic highlighting.

        I have heard that most are decided based on a hash of the variable name. A few have referenced sorting the variables by name and spreading the colors out based on that. Regardless, both of these would appear to randomly select a color for each variable.

        Now, this does have its benefit for developers with specific types of color schemes. This type of implementation should stay as is; however, it seems like a deterministic coloring mechanism could be devised to fill in different types of niches.

        A use case for a deterministic coloring mechanism would be to allow developers the ability to see if their methods have too many parameters (color #1 – 3 be white to gray, color #4 – 5 be red). If a method has 5+ parameters, then the last ones will be red, but the first ones will be white. It is deterministic because I know the 1st parameter will be white because it is the 1st parameter, and if there are too many parameters then I know the last few will be red, but if there are only three parameters, then I know it will not be red but gray.

Leave a Reply

Your email address will not be published. Required fields are marked *