Sublime Forum

For ruby syntax highlighting, how do I distinguish `end` keyword between `class`, `def`, `do`, and `if`?

#1

Hello,

Is there a way I can syntax highlight the keyword end differently depending on its corresponding opening keyword (class, def, do, if, etc.)

For example, I’ve targeted the class (yellow) and def (red), but the end keyword is orange throughout.

Is there a way to make the class end yellow and the def end red?

Thanks,

Sean

0 Likes

#2

Assigning different colors seems not possible with the current version of the builtin ruby syntax definition. To distinguish the different end keywords the structure of the syntax definition would need to be changed to be able to identify the matching ends for the different opening keywords. But this starts to make things more complex.

0 Likes

#3

If the Ruby syntax had proper meta scopes, this could be done (except for #2152).

0 Likes

#4

What does count as ‘proper meta dcopes’? That is something I hear from time to time in the frorums, but find little actual explanation for.

0 Likes

#5

For instance, the entirety of a function (from def to end) might be enclosed in a meta.function scope, and likewise for scopes like meta.class, meta.if, and so on. As long as every construct that uses end were scoped that way, you could target an end that terminates a function with the selector meta.function keyword.control.end.

The only problem is #2152. Due to a bug in the selector scoring function, the wrong selector will win if the meta scopes are nested in certain ways. This bug was actually discovered because @borela was trying to do something similar with a color scheme. If this bug is fixed, the above technique would work right away in JavaScript and other languages that provide detailed meta scopes, and other languages like Ruby could offer them as well.

1 Like