Sublime Forum

Issue with Javascript highlighting

#1

In the following code snippet, the bar and baz variables are not highlighted as function parameters. Their context is given plainly as “source.js”

load("foo", function(bar, baz) { });

However, it appears to work for this:

load("foo", function (bar, baz) { });

0 Likes

#2

It depends. The javascript tmLanguage tries to highlight function parameters in six scenarios on quick observation.

  • foobar: function() { … }
  • “foo”: function
  • Sound.prototype.play = function() { … }
  • Sound.play = function() { … }
  • play = function() { … }
  • function myFunc(arg) { … }

Yours seems to fall in the last scenario where the regex is expressed as such:

\b(function)\s+([a-zA-Z_$]\w*)?\s*(\()(.*?)(\))

As you can see it requires at least one space after “function”. It could be rewritten, but you would have to play with it.

You could do:

\b(function)(\s+[a-zA-Z_$]\w*)?\s*(\()(.*?)(\))

But then the spaces would get include in the scoping of the function name.

You could try:

\b(function)(\s+([a-zA-Z_$]\w*))?\s*(\()(.*?)(\))

But then you will need to bump all of the selections by one except the first and not use the second selection (I am excluding selection 0 when I say first and second). I am not sure of the impact on the other highlighting if this change was made. Sometimes changing the rules can cause a cascade of unforeseen ripples due to the nature of how the theme files operate. You just have to play around with it.

0 Likes

#3

This should be possible with non-capturing groups (?: )

\b(function)(?:\s+([a-zA-Z_$]\w*))?\s*(\()(.*?)(\))

I’ll try it out and let you know how it goes. Thanks for the pointers so far!

0 Likes

#4

Yep, that worked.

It’d be good to get this modified in the distributed package.

Packages/Javascript/Javascript.tmLanguage
Line 260: \b(function)(?:\s+([a-zA-Z_$]\w*))?\s*(()(.*?)())

0 Likes

#5

Looks like I shut my brain off too early. Yep, non-capturing groups is what you need. Glad it worked. I will probably make this modification myself.

0 Likes