【问题标题】:Custom 'console.log' plugin Sublime Text 3自定义 'console.log' 插件 Sublime Text 3
【发布时间】:2018-02-09 12:42:21
【问题描述】:

我做了很多 JavaScript 项目,但我错过了 PHPStorm 的一个很棒的功能。现在我不太了解Python。所以希望你能帮助我。

这就是我想要的:

'test'.log => console.log('test');
test.log => console.log(test);

所以用一个 tabtrigger .log

我想检索.log 之前的任何内容。然后我会改造它。我该怎么做?

【问题讨论】:

    标签: python plugins sublimetext3


    【解决方案1】:

    您可以创建一个插件来检索.log 之前的文本并在视图中替换它:

    import re
    
    import sublime
    import sublime_plugin
    
    
    class PostSnippetLogCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            view = self.view
            for sel in view.sel():
                pos = sel.b
                text_before = view.substr(sublime.Region(view.line(pos).a, pos))
    
                # match the text before in reversed order
                m = re.match(r"gol\.(\S*)", text_before[::-1])
                if not m:
                    continue
                # retrieve the text before .log and reestablish the correct order
                text_content = m.group(1)[::-1]
                # create the replacements text and region
                replace_text = "console.log({});".format(text_content)
                replace_reg = sublime.Region(pos - len(m.group(0)), pos)
                # replace the text
                view.replace(edit, replace_reg, replace_text)
    

    如果在 javascript 文档中以 .log 为前缀,则添加此键绑定以触发命令。

    {
        "keys": ["tab"],
        "command": "post_snippet_log",
        "context":
        [
            { "key": "selector", "operand": "source.js" },
            { "key": "preceding_text", "operator": "regex_contains", "operand": "\\.log$" },
        ],
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多