如何设置VSCode将花括号放在同一行上?

时间:2019-03-07 13:14:25

标签: visual-studio-code vscode-settings

默认情况下

{path: '/post/:postId', component: Post},

正在转换为此

{
    path: '/post/:postId',
    component: Post
},

如何禁用此行为?

3 个答案:

答案 0 :(得分:8)

您要查找的设置是:

{
  // Defines whether an open brace is put onto a new line for control blocks or not.
  "javascript.format.placeOpenBraceOnNewLineForControlBlocks": false,

  // Defines whether an open brace is put onto a new line for functions or not.
  "javascript.format.placeOpenBraceOnNewLineForFunctions": false
}

请参阅User and Workspace Settings文章。它也涵盖了其他语言的类似设置。

如果这不能提供足够的控制,您也可以选择使用Beautify 并指定一个.jsbeautifyrc

具有大括号样式的设置,如下所示:

{
   "js": {
       "brace_style": "collapse,preserve-inline"
    }
}

请参阅this以查看所有可能的值。

更新

有人指出,还有一种控制程度,如果您想完全更改格式器,可以通过安装正确的VS Code插件然后配置适当的格式器来实现(请参阅User and Workspace Settings文章) 。例如:

"[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode",
}

在这种情况下,使用的格式化程序为Prettier - Code formatter

答案 1 :(得分:1)

在VS Code中进入设置: “文件->首选项->设置”

在设置中搜索“ curly”。 它将搜索以下设置,取消设置并验证其是否按预期工作。 enter image description here

答案 2 :(得分:0)

就我而言,问题出在用于格式化的其他插件中。 Beautify已安装为格式化的默认选项,并且vscode格式化程序的所有设置均无效。

默认情况下,此解决方案设置为vscode格式化程序或更漂亮。

"[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode",
},