当插入符在括号内(或其他符号)时如何使 Sublime Text 3 缩进换行符
How to make Sublime Text 3 indent newlines when caret is inside brackets (or other symbols)
要根据当前行自定义 Sublime Text 何时缩进换行的行为,可以更改 whatever.tmPreferences
文件适当地设置 increaseIndentPattern
和 decreaseIndentPattern
选项,如图所示例如 this other answer.
但是,我似乎无法弄清楚如何生成以下行为:给定一行
[<cursor here>]
将光标放在方括号之间,按 enter
我想要以下结果:
[
<cursor here>
]
例如,在修改 xml
文件时会发生这种情况,在两个括号之间按回车键,例如 <sometag><cursor here></sometag>
.
我试图查看 xml
的 tmPreferences
文件,但无济于事。
有人问过类似的问题 ,但由于以下几个原因,现在的问题有所不同:
- 我希望仅针对特定文件扩展名实现此行为,和 随包一起提供。所以我也在问我应该把这个自定义键绑定的说明放在哪里。
- 在链接的问题中,事情更简单:只需在某种大括号之间正确添加和缩进换行符。 (对我来说)如何概括这种行为并不简单,就像上面引用的例子一样,我们想要在 XML-like 标签之间换行,因为在这种情况下我们将不得不以某种方式处理正则表达式 and验证左右模式匹配。
如何实现这种行为?
要创建将随包一起提供的键绑定,请创建 a Default.sublime-keymap
file in your package。
通常 Sublime Text 查看用于突出显示文档的语法,而不是使用的文件扩展名,以确定 keybindings/plugins 是否应该激活等。这主要是为了它可以处理文件尚未保存。如果您想遵循此准则,可以使用 selector
键绑定上下文。对于 XML 个文件,您可能希望使用 source.xml
。否则,您将需要创建一个 EventListener that defines a on_query_context
method to check the view.file_name()
. You could use the 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\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
。 (您可以从工具菜单 -> 开发人员 -> 显示范围名称手动检查插入符号右侧的范围。)如果您使用的是自定义语法,则需要确保相应地调整这些。
在这种情况下,因为我们在 Enter 键上使用键绑定,所以 tmPreferences
文件中指定的缩进规则将被忽略。
要根据当前行自定义 Sublime Text 何时缩进换行的行为,可以更改 whatever.tmPreferences
文件适当地设置 increaseIndentPattern
和 decreaseIndentPattern
选项,如图所示例如 this other answer.
但是,我似乎无法弄清楚如何生成以下行为:给定一行
[<cursor here>]
将光标放在方括号之间,按 enter
我想要以下结果:
[
<cursor here>
]
例如,在修改 xml
文件时会发生这种情况,在两个括号之间按回车键,例如 <sometag><cursor here></sometag>
.
我试图查看 xml
的 tmPreferences
文件,但无济于事。
有人问过类似的问题
- 我希望仅针对特定文件扩展名实现此行为,和 随包一起提供。所以我也在问我应该把这个自定义键绑定的说明放在哪里。
- 在链接的问题中,事情更简单:只需在某种大括号之间正确添加和缩进换行符。 (对我来说)如何概括这种行为并不简单,就像上面引用的例子一样,我们想要在 XML-like 标签之间换行,因为在这种情况下我们将不得不以某种方式处理正则表达式 and验证左右模式匹配。
如何实现这种行为?
要创建将随包一起提供的键绑定,请创建 a Default.sublime-keymap
file in your package。
通常 Sublime Text 查看用于突出显示文档的语法,而不是使用的文件扩展名,以确定 keybindings/plugins 是否应该激活等。这主要是为了它可以处理文件尚未保存。如果您想遵循此准则,可以使用 selector
键绑定上下文。对于 XML 个文件,您可能希望使用 source.xml
。否则,您将需要创建一个 EventListener that defines a on_query_context
method to check the view.file_name()
. You could use the 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\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
。 (您可以从工具菜单 -> 开发人员 -> 显示范围名称手动检查插入符号右侧的范围。)如果您使用的是自定义语法,则需要确保相应地调整这些。
在这种情况下,因为我们在 Enter 键上使用键绑定,所以 tmPreferences
文件中指定的缩进规则将被忽略。