要制作将与包装一起发货的键绑定,请创建a Default.sublime-keymap
file in your package。
通常,Sublime Text会查看用于突出显示文档的语法,而不是使用的文件扩展名,以确定keybindings/plugins是否应该处于活动状态等。这主要是因为它可以处理那些没有“还没有保存。如果您想遵循此准则,则可以使用selector
键绑定上下文。对于XML文件,您可能需要使用source.xml
。否则,您将需要创建一个EventListener,它定义了一个on_query_context
方法来检查view.file_name()
。您可以使用os.path.splitext
方法来检索文件扩展名。
如果你是真正的XML处理,那么你可以使用默认的auto_indent_tag
键绑定为灵感:
{ "keys": ["enter"], "command": "auto_indent_tag", "context":
[
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "punctuation.definition.tag.begin", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": ">$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^</", "match_all": true },
]
},
建立类似:
{ "keys": ["enter"], "command": "insert_snippet", "args": { "contents": "\n\t$1\n" }, "context":
[
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.xml punctuation.definition.tag.begin", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": ">$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^</", "match_all": true },
]
},
这里使用的正则表达式是非常简单的,只要在插入符号为>
之前立即检查文本,在插入符号后立即插入文本即为</
。这是可能的,因为selector
检查以下内容:a)我们是否使用XML语法; b)紧随插入符号的范围为punctuation.definition.tag.begin
。 (您可以从工具菜单 - >开发人员 - >显示范围名称,直接在插入符右侧手动检查范围。)如果您使用的是自定义语法,则需要确保相应地调整这些内容。
在这种情况下,因为我们使用输入键的键绑定,所以在tmPreferences
文件中指定的缩进规则将被忽略。
可能重复[如何在ST3中设置括号缩进行为](http://stackoverflow.com/questions/41456641/how-to-set-bracket-indentation-behavior-in-st3) –
@KeithHall thanks for链接。这确实是非常相关的,但不是真正的直接答案。例如,在帖子中显示'xml'的情况下,我无法在'Packages'的'XML'文件夹中找到任何'.sublime-keymap'文件,所以我不知道该键绑定的位置。 – glS
你的用户密钥绑定总是在你的用户包中,它们不在与你想要使用它们的地方相关的包中。为了使绑定仅作为XML文件的链接工作,您只需添加一个额外的上下文来检查它是否为XML文件。查看默认密钥绑定文件中最后一个绑定的示例。 – OdatNurd