当光标在括号中时,如何在按 Enter 时禁用插入新的空行?

问题描述

重现步骤:

  • 简单代码
    if () {}

  • 光标在 {}

    之间
  • 我按回车

  • 结果:

if () {
  
}
  • 预期结果:
if () {
}

我不想插入一个空行。

可能是认情况下(添加空行),而当Alt+Enter时它不添加空行。

我在 vscode 中没有找到设置。我在谷歌上没有找到任何东西。


我试过了:

{
  "key": "alt+enter","command": "type","args": { 
    "text": "\n" 
  },"when": "editorTextFocus"
}

因为 Alt+Enter 认什么都不做。 但是,与 onEnterRules 选项一起使用的 editor.autoIndent 函数会检测到 \n 字符的添加,并且无论如何都会添加一个额外的空行。 :(

我想使用 editor.autoIndent。但我想使用快捷方式 Alt+Enter 关闭(不打开)。


最糟糕的选择:寻找与 editor.autoIndent 完全相同但能够创建快捷方式 Alt+Enter 以按照我想要的方式工作的扩展程序。

解决方法

您可以使用扩展名 multi-command 并构建一个执行您想要的命令。

将此添加到您的 settings.json(全局或工作区)

  "multiCommand.commands": {
    "multiCommand.lineBreakNoEmptyline": {
      "sequence": [
        "lineBreakInsert","deleteWordRight","cursorRight","cursorHome"
      ]
    }
  }

将此添加到您的 keybindings.json

  {
    "key": "alt+enter","command": "multiCommand.lineBreakNoEmptyline","when": "editorTextFocus"
  }

或仅使用键绑定方法

  {
    "key": "alt+enter","command": "extension.multiCommand.execute","args": { 
      "sequence": [
        "lineBreakInsert","cursorHome"
      ]
    },"when": "editorTextFocus"
  }