-
Notifications
You must be signed in to change notification settings - Fork 588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JavaScript] Syntax highlight - how to get different colors for object literal key and a function value? #154
Comments
can you paste the code in too? I'll play around with it and see if I can get what you want. fwiw, honestly the first one ( |
I took a quick look and it is currently impossible to specifically target functions defined as object keys without also matching "normal" function definitions. The syntax definition needs to be changed. |
@jonschlinkert i've updated post, to include code. It does not look bad in case of example above, but it starts to look messy in something like this: var obj = {
one: 1,
two: () => 2,
three: "3",
four: function four () {
return 4;
}
} But that's just a visual preference, and one could argue that such object literals should not be defined anyway. Thing is that the key is not highlighted if a function is passed through a variable, like this: var one = () => 1;
var obj = {
one: one
}
@FichteFoll i never edited syntax files before, but maybe we could add a marker while keeping backwards compatibility? In JavaScript.sublime-syntax file there is Here's modified part:
Of course I tested changes and they work great, once i've added If you think that such change to syntax file is ok, i can create pull request. |
One more thing: allowing for separate color of |
The thought behind your proposed patch is correct, but it should be executed differently. Instead of adding another capture group you can just adjust the existing captures' scope names to include the additional identifier after entity.name.function. You should also probably add one for all function matches. |
Unfortunately it seems that some color schemes (like monokai extemded) define specific colors to the entity.name.function.js scope, which would not match anymore afaik. I can't test if it would still work, but I have to question why such a specific highlight is necessary and why source.js entity.name.function wouldn't suffice. |
@FichteFoll thanks. So captures support comma separated identifiers? For example:
But this is only about function names in case of
Yes, that is why i wanted to just add another, more specific identifier, without removing previous one - to keep backward compatibility (and because this case does work as both regular
Because currently entity.name.function is used for both sides of expression, i.e.: var entityNameFunction = function entityNameFunction () {}; And they're not exactly the same. I understand that historically they often were colored the same, but it would allow themes/highlighters for a lot more flexibility if they could be treated differently. It started from what i described above as issue, but i can imagine a theme where |
IMHO that's a bit like asking "why isn't red good enough?" lol. Also I retract my earlier comment. I'm finding it difficult to apply syntax highlighting with the same amount of specificity as before the patch.
they're not at all the same. One is a variable and one is a function name. e.g. I agree. |
No. Just make the capture define two scopes by separating with a space.
I mean that a special scope should be added for the other type of functions as well (anonymous function assigned to an object key).
Well, the thing is that tech-depth imposed by third-party package authors is seemingly holding us back here. That's pretty frustrating, if you ask me. |
@FichteFoll thanks for help. I've prepared pull request for function label entities. As for anonymous functions, that will require a lot more changes to syntax file, because right now function "label" is optional everywhere, so to differentiate between anonymous and named functions, we would have to create separate matches for them. Unless there is a way to declare conditional meta scopes in syntax file? |
Thanks. |
As mentioned in #141, keys of object literals will get different color if value is a function.
I managed to modify Monokai Extended theme to prevent that, but i do not know how to keep key white, but function name colored.
Here's theme rule i added:
Here's test code:
Here's result:
Is there a way to make function name colored, but keep object literal key color unchanged? It would be great if i could get something like this mockup:
The text was updated successfully, but these errors were encountered: