## Variable highlight according to scope

### Variable highlight according to scope

Hi, how can I set a different color to highlight a global and a local variable?
currently I would like to have different color for different variable scope in Javascript, by using the keys ctrl+alt+shift+p I saw that there is no keyword to set a color scheme for them (key combinations gives as scope a generic 'source.js').
I see also that there is no themes that have different colors for different variable scope and that seems strange to me since I found them into any IDE or texteditor with color highlighting..
azmod

Posts: 11
Joined: Sun Mar 17, 2013 4:58 pm

### Re: Variable highlight according to scope

any hint? can this be achieved with sublime text?
below there is a screenshot of a little sample of how Sublime Text (with a customized theme) highlights the code (left picture) and how another code editor can do it (right picture), the second is what I would like to have also in Sublime since it's really more smart and usefull when coding:

to be honest, sublime text customization features are great but code highlighting capability seems poor (only match-word rules) indipendently from the language (used for java, c++, js, etc.)... or am I missing something?
azmod

Posts: 11
Joined: Sun Mar 17, 2013 4:58 pm

### Re: Variable highlight according to scope

azmod wrote:
to be honest, sublime text customization features are great but code highlighting capability seems poor (only match-word rules) indipendently from the language (used for java, c++, js, etc.)... or am I missing something?

Don't think this is going to be possible with Sublime. Sublime only does syntax coloring based upon string Regular Expression rules on a singular line-by-line basis only. The kind of coloring shown on the right would need a symbol table to be built from a parser specific to the language that understands how each symbol was defined. I agree though the coloring on the right looks really nice and helpful.
robertcollier4

Posts: 154
Joined: Sun Feb 24, 2013 5:37 pm

### Re: Variable highlight according to scope

robertcollier4 wrote:Don't think this is going to be possible with Sublime. Sublime only does syntax coloring based upon string Regular Expression rules on a singular line-by-line basis only. The kind of coloring shown on the right would need a symbol table to be built from a parser specific to the language that understands how each symbol was defined. I agree though the coloring on the right looks really nice and helpful.

thanks, I was hoping there was some 'exotic' way to obtain it in Sublime too.. it's really a step-forward feature that lacks when you're accustomed to use it (like also javadoc/jsdoc/doxygen documentation popup..).

May be it could be implemented in Sublime next releases by adding to language-specific RegEx rules an additional check that 'remembers' the used variables <name, scope> without changing the line-by-line approach.
Or.. does a similar workaroud may be implemented as a plugin? variable type and scope can be obtained by parsing the code for brackets sequence and keywords and then change the color according to what the user defined in plugin settings.

Possible variables type and scope can be:
- function/method declaration;
- function/method call;
- global variable;
- local variable;
- parameter;

Currently I've not great experience in python development.. so if someone else likes and grabs the idea will be great!

(for moderator: it's more appropriate to move this thread to 'Ideas and Feature Request' section)
azmod

Posts: 11
Joined: Sun Mar 17, 2013 4:58 pm

### Re: Variable highlight according to scope

Any progress on this? It is in the roadmap? I think that this is a great feature
d1egoaz

Posts: 1
Joined: Sat Mar 30, 2013 5:56 pm

### Re: Variable highlight according to scope

Varemenos

Posts: 5
Joined: Sat Mar 17, 2012 3:37 pm

### Re: Variable highlight according to scope

I am using PersistentRegexHighlight to define my own regular expressions with colors (background / foreground) or underline, and it can be set to be case sensitive. Modification of the settings file usually requires restarting Sublime Text in order to take effect.

https://github.com/skuroda/PersistentRegexHighlight

Here is my PersistentRegexHighlight.sublime-settings

Code: Select all
{   // Please see the README for more information on settings.      // Array of objects containing a regular expression   // and an optional coloring scheme  "regex": [         {    "pattern": "INSERT",    "color_scope": "insert_text",    "ignore_case": false         },         {    "pattern": "\\\\today",    "color_scope": "automatic_date",    "ignore_case": false         },         {    "pattern": "\\\\\\\\|~|\\\\=|\\\\#|\\\\>|\\\\\\\$|\\\\S",    "underline": true         },         {    "pattern": "\\\\textit|\\\\begin|\\\\end|\\\\newpage|\\\\hspace|\\\\vspace|\\\\tab|\\\\bf|\\\\uline|\\\\uuline",    "underline": true         }      ],   // If highlighting is enabled   "enabled": true,   // If highlighting should occur when a view is loaded   "on_load": true,   // If highlighting should occur as modifications happen   "on_modify": true,   // File pattern to disable on. Should be specified as Unix style patterns   // Note, this looks at the absolute path to match the pattern. So if trying   // ignore a single file (e.g. README.md), you will need to specify "**/README.md"   "disable_pattern": []}

Here is a snippet from my .tmTheme file:

Code: Select all
<!-- BEGIN highlight of the word "INSERT" --><dict><key>name</key><string>Highlight Insert</string><key>scope</key><string>insert_text</string><key>settings</key><dict><key>foreground</key><string>#FFFFFF</string><key>background</key><string>#FF0000</string></dict></dict><!-- END --><!-- BEGIN highlight of the term "\today" --><dict><key>name</key><string>Automatic Date</string><key>scope</key><string>automatic_date</string><key>settings</key><dict><key>foreground</key><string>#FFFFFF</string><key>background</key><string>#0000FF</string></dict></dict><!-- END -->

As an alternative to using PersistentRegexHighlight, it is possible to modify the applicable .tmLanguage file to include whatever expression needs highlighting. Either an entirely new section can be added in the .tmLanguage file, or simply add the additional expression into an existing section using the | symbol as a divider. Then, the .tmTheme file can refer to that specific string (e.g., storage.type.function.latex).

Code: Select all

Code: Select all
<!-- BEGIN global_var|local_var --><dict><key>name</key><string>global_var|local_var</string><key>scope</key><string>storage.type.function.latex</string><key>settings</key><dict><key>foreground</key><string>#FFFFFF</string><key>background</key><string>#FF0000</string></dict></dict><!-- END -->

Here is an example of creating code that goes inside the language file. In this particular example, the term lawlist.one.latex is inserted into the code for the theme file.

Code: Select all
<!-- BEGIN lawlist.one.latex -->      <dict>         <key>match</key>         <string>global_var</string>         <key>name</key>         <string>lawlist.one.latex</string>      </dict><!-- END -->
Attachments
Screen Shot 2013-04-07 at 11.52.41 AM.png (233.61 KiB) Viewed 8968 times
lawlist

Posts: 54
Joined: Thu Feb 21, 2013 6:47 pm

### Re: Variable highlight according to scope

Damn, this feature is absolutely necessary!

Maybe someone can write a plugin for JavaScript?
You can modify this plugin to search for variables - https://github.com/graarh/sublime-AutoPHPDollar
Then just need to highlight them.
Please do not miss this idea.
SARFEX

Posts: 1
Joined: Fri May 31, 2013 12:17 pm

### Re: Variable highlight according to scope

I'm not affiliated with Sublime, but I know enough about this type of tool to tell you that it's going to be difficult.

The highlighting you currently have is based on regular expression which recognize lexical structures.
A highlighting that changes depending on the nature of identifiers/scopes is at least based on grammatical structure and at worst on semantic structures depending on what you wish to highlight. Bottom line: that means that the editor would have to maintain a parse tree of the content of the buffer (based on the grammar of the language) and incrementally reparse as you edit. It would then have to maintain semantic information on that parse tree (also incrementally) so that it produces (on the fly) the highlighting annotations.

It means:
- one grammar for each language supported (C/C++/Java/Ruby/Python/JS/Eifel/R/LaTeX/.....)
- one incremental parser for each
- a semantic analysis tool for each language. Preferably incremental....

This is a lot.

Tools like Xcode, Eclipse, MSDev do it because they have only *1* language to worry about.

Just my 2 cents.
ldmbouge

Posts: 2
Joined: Fri May 31, 2013 8:13 pm

### Re: Variable highlight according to scope

I second this idea. It might be hard for some languages, but for Lisps (Clojure, CL, Scheme, etc.) in theory it would be fairly simple as the code directly represents the AST, unlike Java, C++, and other non-Lisp languages. In principle it would be a lot easier to implement than for JavaScript, which has already been implemented, by the way (https://github.com/mazurov/sublime-levels).
alexandergunnarson

Posts: 4
Joined: Thu Jun 13, 2013 4:09 pm