如何突出显示对对象方法和用户创建的函数的调用? (崇高文本,python)

How to highlight calls to object methods and user-created functions? (SublimeText, python)

我尝试同时使用 Python3/PythonImproved .tmLanguage 语法,但出于某种原因两者都没有。在大多数编辑器中,这些实际上应该与变量颜色相同吗?

这是一些随机函数的屏幕截图:

我希望它看起来像这样:

总而言之,我只想突出显示用括号调用的所有内容,例如 function() 或 object.method()。

是的,这是有意的。在 Python 中,您声明的所有变量都是对象的绑定。这意味着如果你声明一个变量 varA = 1 那么对象 1 将被创建并且名称 varA 将被绑定到这个对象。当您声明下一个 varB = 1 时,varB 将绑定到与 varA 相同的对象。您可以使用内置函数 id 来检查 returns 变量绑定到的对象的 ID:id(varA) == id(varB) # True.

函数和实例方法也是如此。如果你声明一个函数

def foo():
    print 'foo'

然后将创建一个代表该函数的对象。同时名称 foo 绑定到该对象。声明 bar = foo 将允许通过 bar 调用先前创建的函数对象:bar() 将产生与 foo() 相同的输出(并调用 - 或更好地使用 - 内存中完全相同的对象).

对于实例方法,这是相似的。声明一个 class

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

将创建所需的对象并将 Foo 绑定到 class 并将 foo 绑定到 class 的实例方法。声明 Foo.bar = Foo.foo 将允许

>>> 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>>

因此 python 中的每个 'variable' 实际上都是名称与对象的绑定,这适用于所有内容,包括函数和实例方法。这就是为什么它们在大多数编辑器中都有相同(缺失)的语法突出显示。

我使用了 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,尽管没有一个主题文件对这个范围有任何作用。

所以我将这条规则添加到我的配色方案文件 (.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>

我是 Python 改进版的作者。 Sublime 的默认 Monokai 主题无法突出显示 PI 中的许多范围(以及许多其他语言 - 它非常简单)。我的其他项目之一,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.