sublime text中添加diff跳转代码

本文探讨了在不同语言编辑环境下,通过按键绑定实现快速定位到代码实现的方法,特别关注了diff语法视图下的回车键跳转功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

按键绑定

在编辑不同的语言时,使用的语法解析不同,需要根据语法进行过滤后加入按键绑定。
如下, 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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值