sublime text中添加diff跳转代码

来源:互联网 发布:ipadmini2淘宝卖家 编辑:程序博客网 时间:2024/05/21 02:01

按键绑定

在编辑不同的语言时,使用的语法解析不同,需要根据语法进行过滤后加入按键绑定。
如下, context中当view.scope_name() == ‘source.diff’时,才能激活使用按键绑定:

    {        // Navigate to results with enter key        "keys": ["enter"],        "command": "diff_visiter",        "context": [{"operand": "source.diff",                 "operator": "equal",                 "match_all": true,                 "key": "selector"                }]            }

diff跳转代码

当在diff语法的view中,直接按回车能够跳转到对应代码实现。

class DiffVisiterCommand(sublime_plugin.TextCommand):    def __init__(self, *args, **kwargs):        print("DiffVisiter init")        super(DiffVisiterCommand, self).__init__(*args, **kwargs)    def run(self, edit):        print("call diffvisitor")        if self.view.settings().get('syntax') == DIFF_SYNTAX_FILE:            print("diff visitor")            filepath = self.view.substr(self.view.line(0))            linenum_re = re.compile(r'^([0-9])*[ac]([0-9]+)')            for region in self.view.sel():                # Find anything looking like file in whole line at cursor                if not region.empty():                    break                match_line = self.view.substr(self.view.line(region))                print('match line: %s' % match_line)                re_match_linenum = linenum_re.search(match_line)                # if this line had a line number, use it and look up for the filename                if re_match_linenum:                    lineno = re_match_linenum.group(2)                    line_beg = self.view.line(region).begin()                    prev_line_bounds = self.view.line(sublime.Region(line_beg - 1, line_beg - 1))                    file_line = self.view.substr(prev_line_bounds)                else:                    lineno = '1'                print("Opening file '%s'" % (filepath + ":" + lineno))                sublime.active_window().open_file(filepath + ":" + lineno, sublime.ENCODED_POSITION)
0 0
原创粉丝点击