0

Say if I have the following code in Sublime:

if (condition) {
    // code
}

When my cursor is at the end of // code, I would like to set a key bind (e.g. Tab) that will exit the if-statement block and move it to the end of }. Thanks.

4

2 回答 2

1

BracketHighlighter插件可以原生地提供此功能......有点。在其示例快捷方式文件中Example.sublime-keymap,有一个“转到右括号”示例键绑定:

// Go to right bracket
{
    "keys": ["ctrl+alt+super+down"],
    "command": "bh_key",
    "args":
    {
        "lines" : true,
        "plugin":
        {
            "type": ["__all__"],
            "command": "bh_modules.bracketselect",
            "args": {"select": "right"}
        }
    }
},

唯一的问题是被调用的bracketselect命令将光标移动到右括号的左侧,需要另一个按键才能完全退出该块。我不认为那是你想要的。

不用担心!谢天谢地,BracketHighlighter 提供了一个非常直观的插件 API,我发现我可以修改bracketselect插件来创建一个从括号括起来的块中转义的命令 - 基本上与 相同bracketselect,但它将光标移动到关闭的右侧括号而不是左边,并且不需要任何额外的参数。

如果您还没有安装BracketHighlighter ,您首先需要安装它。

接下来,将blockescape.py(如果链接失效,请参见下文)保存到

Preferences -> Browse Packages... -> BracketHighlighter/bh_modules/blockescape.py

然后,将此条目添加到用户键绑定 ( Preferences -> Key Bindings — User) 的顶部:

{
    "keys": ["tab"],
    "command": "bh_key",
    "args":
    {
        "lines" : true,
        "plugin":
        {
            "type": ["__all__"],
            "command": "bh_modules.blockescape"
        }
    }
},

我不建议使用tab作为您的触发键,因为tab在扩展中已经发挥了重要作用。当然,您可以定义一个特殊的上下文来使用tab,但这取决于您。

如果 Github 出现故障,这是插件代码:

import bh_plugin
import sublime

DEFAULT_TAGS = ["cfml", "html", "angle"]


class BlockEscape(bh_plugin.BracketPluginCommand):
    def run(self, edit, name, tags=DEFAULT_TAGS):
        current_left, current_right = self.selection[0].begin(), self.selection[0].end()
        left, right = self.left, self.right
        first, last = left.end, right.begin
        if left.end != right.end:
            if name in tags and left.size() > 1:
                first, last = right.begin + 1, right.begin + 1
                if first == current_left and last == current_right:
                    first, last = right.end, right.end
            else:
                first, last = right.begin, right.begin
                if first == current_left and last == current_right:
                    first, last = right.end, right.end
        else:
            # There is no second bracket, so just select the first
            if name in tags and left.size() > 1:
                first, last = left.begin + 1, left.begin + 1
            else:
                first, last = right.end, right.end
                if first == current_left and last == current_right:
                    first, last = right.end, right.end

        self.selection = [sublime.Region(first+1, last+1)]


def plugin():
    return BlockEscape

由于我或多或少地破解了插件,它可能无法正常工作。在这种情况下,您可以自行编辑或在Gist 页面上发表评论。

于 2013-05-19T17:32:22.530 回答
0

您可以将$0片段添加到该位置的选项卡中:

<snippet>
    <description>If Condition</description>
    <content><![CDATA[if (${1:/* condition */}){
      ${2:/* code */}
    }${0}]]></content>
    <tabTrigger>if</tabTrigger>
    <scope>source.c</scope>
</snippet>
于 2013-05-18T17:28:36.020 回答