Sublime Forum

Syntax: Highlight the complete line of function declaration

#1

Specifically in python I want to achieve that the entire line of a function declartion [ i.e. def functionname(arg, arg, arg): ] is given another background color or any other marking that I can see on the mini map.

What I got so far:
Im familiar with the color schemes definitions and was able to make parts the text of the line bold with the help of Tools > Developer > Show Scope Name.
I then created a syntax file via Tools > Developer > New Syntax… But I’m stuck here, I read the docs but I don’t understand the push/pop concept and have absolutely no glue how to give the entire line a different background color if that’s even possible.

0 Likes

#2

Colors come from the color scheme, not from the syntax definition. Or more specifically, the syntax definition says what a thing is, and the color scheme specifies how the thing looks.

So in that regard the syntax needs to be able to detect the thing you want and apply a scope to it. The Python syntax already has the ability to do that, so all you need is a color scheme rule to color it somehow. All else being equal when it comes to augmenting a syntax (when it’s needed) it’s better to make an override on the original one and add things; if you make your own you need to apply rules for everything.

The color scheme rule you want needs to apply a background color to source.python meta.function, which is the scope applied to the line that defines a function (this assumes you’re using a recent build of Sublime though; if it doesn’t seem to work you might need to update).

For example, assuming that you were using the Monokai color scheme, then the following content in a Monokai.sublime-color-scheme file in your User package would highlight Python function definitions red:

{
    "rules":
    [
        {
            "scope": "source.python meta.function",
            "background": "color(var(red) alpha(0.67))",
        },
    ]
}

You would change the name of the file depending on your color scheme (keep the extension though, even if you’re using a tmTheme color scheme) and you would have to adjust the color line as well.

For me the result looks something like:

0 Likes

#4

Ok it works, thanks so much! Any chance to get the the red highlighting go further until the end of the editor to the edge of the window? I don’t want to get greedy though. A rough roadmap how to get this done would suffice, I will go from there.

0 Likes

#5

That’s an example of something that’s under the control of the syntax definition; specifically it needs to match the \n that ends the line where the function is defined in order to be captured.

I’m not sure offhand how simple or complex it might be to add that, though.

0 Likes

#6

In combination with your answer and what I read I was able to do this. Notice how the highlight goes until the end. Now I just need to know how extend the existing syntax specification.

0 Likes

#7

Note that you could also achieve this by including \n in the match instead of pushing to a context that pops on ^.

0 Likes

#8

That didnt work but the chances are high that I did something wrong. Anyways I didnt use it since changing the existing syntax file worked. Im fiddling around there now to achieve more highlighting.

0 Likes