How to highlight calls to object methods and user-

2019-06-03 06:48发布

I tried using both Python3/PythonImproved .tmLanguage syntaxes, but neither does this for some reason. Are those actually supposed to be the same color as variables in most editors?

Here's a screenshot of some random function:

function calls are not highlighed

And here's how I want it to look like:

function calls are highlighed

To sum it up I just want everything called with parentheses like function() or an object.method() to be highlighted.

3条回答
再贱就再见
2楼-- · 2019-06-03 07:16

I used ScopeHunter to find out the scope of those calls and they are the same function-call.generic.python calls that are actually differentiated from object instances/variables in PythonImproved .tmLanguage, though not a single theme file did anything with this scope.

So I added this rule to my color scheme file (.tmTheme):

<dict>
    <key>name</key>
    <string>generic function call</string>
    <key>scope</key>
    <string>meta.function-call.generic.python</string>
    <key>settings</key>
    <dict>
        <key>foreground</key>
        <string>#66D9EF</string>
    </dict>
</dict>
查看更多
小情绪 Triste *
3楼-- · 2019-06-03 07:28

I'm the author of Python Improved. Sublime's default Monokai theme lacks the ability to highlight many of the scopes in PI (as well as a bunch of other languages - it's quite bare-bones). One of my other projects, the Neon Color Scheme, contains rules for all of PI's new scopes, as well as scopes for a number of other languages, both built-in and third-party from Package Control. If you don't like the colors, I've tried to keep Neon.tmTheme pretty organized, so you should be able to adapt the rules as you wish. If you have any questions or problems, feel free to submit an issue for Python Improved or Neon.

查看更多
beautiful°
4楼-- · 2019-06-03 07:30

Yes that is intended. In Python all variables you declare are bindings to objects. This means if you declare a variable varA = 1 then the object 1 will be created and the name varA will be bound to this object. When you declare next varB = 1 then varB will be bound to the same object as varA. This you can check with the built-in function id which returns the id of the object the variable is bound to: id(varA) == id(varB) # True.

The same is for functions and instance methods. If you declare a function

def foo():
    print 'foo'

then an object will be created which represents that function. At the same time the name foo is bound to that object. Declaring bar = foo will allow for a call of the previously created function object through bar: bar() will produce the same output as foo() (and call - or better use - the exactly same object in memory).

For instance methods this is similar. Declaring a class

class Foo:
    def foo(self):
        print 'foo'

will create the required objects and bind Foo to the class and foo to the instance method of that class. Declaring Foo.bar = Foo.foo will allow for

>>> f = Foo()
>>> f.foo()
foo
>>> f.bar()
foo
>>> Foo.foo
<unbound method Foo.foo>
>>> Foo.bar
<unbound method Foo.foo>
>>> f.foo
<bound method Foo.foo of <__main__.Foo instance at 0x7fd68e846fc8>>
>>> f.bar
<bound method Foo.foo of <__main__.Foo instance at 0x7fd68e846fc8>>

So every 'variable' in python is in fact the binding of a name to an object and this holds for everything, including functions and instance methods. This is why they have the same (missing) syntax highlighting in most editors.

查看更多
登录 后发表回答