SublimeText3 + EventListener-如何将类方法绑定到on_query_completions方法

时间:2019-05-03 21:46:42

标签: sublimetext3 sublime-text-plugin

我正在使用sublime_plugin.EventListener类创建SublimeText3的插件。我想用on_query_completions方法将key1替换为replace1。但是我不能。如何知道将类方法绑定到on_query_completions方法?或者,如何选择替代方式?

class MyPlugin(sublime_plugin.EventListener):
    def on_query_completions(self, view, prefix, locations):
        """ success, but this is not to hoped """
        return [
            ['key1', 'replace1'],
            ['key2', 'replace2'],
        ]

    def on_query_completions(self, view, prefix, locations):
        """ not working, but this is I hoped """
        return [
            ['key1', self.replace1],
            ['key2', self.replace2],
        ]

    def replace1(self):
        return 'replace2'

    def replace2(self):
        return 'replace2'

1 个答案:

答案 0 :(得分:1)

您的代码无法正常运行,因为self.replace1self.replace2不是字符串,它们是函数(方法),并且on_query_completions的返回值需要字符串。

如果您打算获取方法的结果并将其用作替换文本,则应调用它们,而不仅仅是引用它们:

import sublime
import sublime_plugin


class MyPlugin(sublime_plugin.EventListener):
    def on_query_completions(self, view, prefix, locations):
        """ not working, but this is I hoped """
        return [
            ['key1', self.replace1()],
            ['key2', self.replace2()],
        ]

    def replace1(self):
        return 'replace2'

    def replace2(self):
        return 'replace2'