【问题标题】:After pressing Enter not getting extra line Sublime Text 3按下 Enter 后没有额外的行 Sublime Text 3
【发布时间】:2021-08-25 18:58:37
【问题描述】:

我有这个初始场景,| 表示我的光标:

for(int i=0;i<n;i++){|}

回车后的结果:

for(int i=0;i<n;i++){
|}

想要的结果:

for(int i=0;i<n;i++){
    |
}

我尝试了this,但没有解决我的问题。

我使用了以下键绑定,但它不起作用:

[
   { "keys": ["enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\n"}, "context":
        [
            { "key": "setting.auto_indent", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "preceding_text", "operator": "regex_contains", "operand": "{$", "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^}", "match_all": true }
        ]
    }
]

我的这些键绑定也设置如下:

"auto_indent": true, 
"smart_indent": true, 
"indent_to_bracket": true,
"trim_automatic_white_space": true,

我该如何解决?
编辑:- 我卸载了 Sublime Text4 并安装了 Sublime Text3。它工作正常。我会坚持下去的。

【问题讨论】:

  • 在链接的解决方案中,您是否将 () 更改为 {} 以匹配您的用例?
  • @KabirKanhaArora 是的,我做到了。但是没有用:(
  • 已经有一个默认的键绑定可以做到这一点,但它需要打开auto_indent(就像您喜欢的解决方案中的键绑定一样)。你启用了吗? (如果没有,打开它并删除您添加的绑定)。
  • @lazy_propagator 您能否请edit 提出您的问题并粘贴您尝试但没有成功的键绑定代码?谢谢。
  • @MattDMo 我已经添加了。

标签: sublimetext3 newline indentation key-bindings sublime-text-plugin


【解决方案1】:

花括号是正则表达式中的特殊字符,因此您需要更改 "regex_contains" "operand" 以反映这一点。完整的键绑定应该是:

[
   { "keys": ["enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\n"}, "context":
        [
            { "key": "setting.auto_indent", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }
        ]
    }
]

如果您希望方括号 [ ] 出现相同的行为,您需要以相同的方式对其进行转义,因为它们也是正则表达式中的特殊字符。

编辑

这里是直接从 Sublime Text 3 复制的键绑定,在 ST4 中也是一样的:

[
{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, "context":
    [
        { "key": "setting.auto_indent", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }
    ]
},
]

【讨论】:

  • 我不知道为什么,但它仍然没有解决我的问题:(
  • 我不知道我的崇高文本是否有问题或什么。这对我也不起作用。我希望我可以在这里上传实时视频。我得到了同样的东西。
  • 如果您已升级到 Sublime Text 4,请在 Safe Mode 中重新启动它,并仅添加此键绑定。
  • 是的,我已经升级到 sublime 4,即使我在安全模式下启动它并执行了这个过程它给出了相同的结果。顺便说一句,在安全模式下,会弹出一个窗口,说所有数据/包/安装将在关闭后被删除。那么,如果我必须再次进行此更改,那么这样做有什么意义。无论如何,它仍然没有工作。告诉我一件事,如果我回到 Sublime Text3,这个问题会消失吗?我应该尝试一次。
  • @lazy_propagator 你是否包括了开始和结束的方括号?这是我现在唯一能想到的。就安全模式而言,它旨在为调试单个问题提供全新的解决方案 - 例如,您可以只运行一个插件而不受您可能已安装的其他插件的干扰。一旦你关闭它,它就会恢复到新的状态。请记住,您可以同时运行多个 Sublime 实例,因此如果需要,您可以将一个处于安全模式,另一个处于常规模式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 2014-01-05
  • 2014-10-29
  • 2016-04-13
相关资源
最近更新 更多