Sublime Forum

Variable highlight according to scope

#1

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… :frowning:

0 Likes

#2

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?

0 Likes

#3

[quote=“azmod”]http://i45.tinypic.com/642mbn.jpg
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?[/quote]

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.

0 Likes

#4

[quote=“robertcollier4”]
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.[/quote]

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)

0 Likes

#5

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

0 Likes

#6

JavaScript related:
github.com/daniellmb/JavaScript … t-Coloring
and bug request:
github.com/daniellmb/JavaScript … g/issues/1

Anyone interested should +1 it

0 Likes

#7

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.

github.com/skuroda/PersistentRegexHighlight

Here is my PersistentRegexHighlight.sublime-settings

[code]{
// 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": ]

}[/code]

Here is a snippet from my .tmTheme file:

[code]

name
Highlight Insert
scope
insert_text
settings

foreground
#FFFFFF
background
#FF0000

name Automatic Date scope automatic_date settings foreground #FFFFFF background #0000FF [/code]

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).

<dict> <key>captures</key> <dict> <key>1</key> <dict> <key>name</key> <string>punctuation.definition.function.latex</string> </dict> </dict> <key>match</key> <string>(\\)(newcommand|renewcommand|renewenvironment|global_var|local_var)\b</string> <key>name</key> <string>storage.type.function.latex</string> </dict> <dict>

[code]

name
global_var|local_var
scope
storage.type.function.latex
settings

foreground
#FFFFFF
background
#FF0000

[/code]

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]

match
global_var
name
lawlist.one.latex

[/code]


0 Likes

#8

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.

0 Likes

#9

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.

0 Likes

#10

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).

0 Likes