Sublime Forum

Highlight method arguments in Python

#1

Hello,
I was used to editing Python with Netbeans, because it was the only editor I knew that could edit Python and even added a little support.

Some time ago, I found that Sublime Text 2 suits my needs better, so I made the step.

However, there is one thing in Netbeans that is really handy, which is the highlighting of the arguments of the method.

http://img12.imageshack.us/img12/3519/sublimetext2vsnetbeans.png

How to change the theme of Sublime Text 2 so, that ‘self’, ‘playerId’, ‘text’, ‘channel’ and ‘flags’ are highlighted as well?

Regards,
Aart
P.S. I sure hope this is the right section…

0 Likes

#2

If you click into one of the parameters and press Ctrl-Alt-Shift-P you can read the scope in the status-bar. Alternatively, the **ScopeHunter **package is very useful.

<dict> <key>name</key> <string>Function params (Python)</string> <key>scope</key> <string>variable.parameter.function.python</string> <key>settings</key> <dict> <key>fontStyle</key> <string></string> <key>foreground</key> <string>#0AC800</string> </dict> </dict>

0 Likes

#3

Thanks, but that’s not really what I meant.

I would like it to highlight the arguments of the method INSIDE the method, so I can see what variables are given when you call the method.

So if you would call this particular method, you could do something like this:

self.onChatMessage(1, "text", 5, 6)

Then, def onChatMessage would do something with that, but I would like to see which variables are given when the method is called: “self, playerId, text, channel, flags”, like so:

def onChatMessage(self, playerId, text, channel, flags):
…command, parameter = self.core.decodeChatMessage(text)
…text = “@” + command.lower()]
…if parameter: text.append(parameter)
…self.core.coreChatMessage(playerId, " ".join(text), channel, flags)

This is not only for Python, by the way, but also very handy in Java, PHP and so on.

0 Likes

#4

Should be possible but would require a Python plugin because you can’t figure out which identifiers have been used in the function definition (at least not with an undefined number) using only regular expressions.

You just have to analyze the Python code (probably using the already defined scopes) and then highlight the variables manually using view.add_regions.

0 Likes

#5

Oh boy… Could you give an example?

0 Likes

#6

Giving an example would be hard because I can either tell you which methods can be used or I can just write the whole thing. I’ll try the first because that requires less time:

Thankfully, ST2 provides some nice parsing using the syntax definition from Python. You can adapt to this essentially using view.find_by_selector. For examples with add_region, see “BracketHighlighter” or “SublimeLinter” plugins. The code could be quite complex but their usage is really exemplary.

0 Likes