【问题标题】:Sublime text 3 plugin development for custom autocomplete like emmet?用于自定义自动完成的 Sublime text 3 插件开发,如 emmet?
【发布时间】:2017-02-28 15:06:00
【问题描述】:

我想创建我的自定义插件,如 emmet 用于自动完成和 html 标签的标签扩展,如h2>span .myclass 应该导致<div class="myclass"></div>

我开始使用但没有找到任何用于跟踪用户类型事件以及如何定义插件范围以仅应用于 html 文件的文档。

当我尝试在我的类中使用 print 语句时,它会抛出语法错误

def run(self, edit):
  print "i am in run"
  self.view.insert(edit, 0, "Hello, World!")

如何在没有 print 语句的情况下调试我的插件代码,或者有什么替代 sublime 插件的方法?

【问题讨论】:

    标签: python sublimetext3


    【解决方案1】:

    通常,不会编写插件来跟踪用户在 Sublime Text 中键入的内容,而是将命令绑定到键绑定。然后,当用户在特定条件下(在键绑定上下文中定义)按下该特定键时,该命令将执行并查看选择插入符号附近的文本。

    Sublime Text 插件是在 Python 3 中开发的,其中print 不是一个语句,而是一个函数。因此,需要使用print('I am in "run"')将调试信息输出到ST控制台。

    例如,如果这是您的插件代码:

    import sublime
    import sublime_plugin
    
    
    class ThisIsAnExampleCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            print('I am in the "run" method of "ThisIsAnExampleCommand"')
            self.view.insert(edit, 0, "Hello, World!")
    

    那么您可以定义一个键绑定,例如:

    { "keys": ["tab"], "command": "this_is_an_example",
        "context":
        [
            { "key": "selector", "operator": "equal", "operand": "text.html", "match_all": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        ]
    },
    

    当用户按下 Tab 时会运行,但前提是所有选择都为空,并且当前正在编辑的文件的语法是 HTML。

    您的插件可以查看self.view.sel() 以获取选择/插入符号位置。

    【讨论】:

      猜你喜欢
      • 2016-02-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 2013-07-05
      • 1970-01-01
      • 2017-10-13
      相关资源
      最近更新 更多