【问题标题】:SublimeText3 + EventListener - How to bind class method to on_query_completions methodSublimeText3 + EventListener - 如何将类方法绑定到 on_query_completions 方法
【发布时间】:2019-05-03 21:46:42
【问题描述】:

我正在使用 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'

【问题讨论】:

    标签: sublimetext3 sublime-text-plugin


    【解决方案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 'replace1'
    
        def replace2(self):
            return 'replace2'
    

    【讨论】:

    • def replace1(self): 应该返回 'replace1',假设 OP 也意味着这似乎很可能。
    • 最初我不考虑这个,只是为了说明代码为什么不起作用,但我同意最好有一个按预期工作的示例。
    猜你喜欢
    • 1970-01-01
    • 2018-05-27
    • 2015-06-25
    • 2011-04-07
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多