2017-08-31 105 views
2

我做了很多JavaScript项目,并且我错过了PHPStorm的一个很棒的功能。现在我不太了解Python。所以希望你能帮助我。自定义'console.log'插件崇高文本3

这就是我想要的:

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

因此,与单一tabtrigger .log

我要检索的.log任何事情之前。然后我会改变它。我怎样才能做到这一点?

回答

3

您只需创建一个插件.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) 

之后添加此键绑定如果前缀.log一个javascript文件内触发命令。

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