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

How can I set up VSCode to put curly braces on the same line?

默认这个

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

正在转换成这个

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

如何禁用此行为?

更新。我在 JavaScript 中编码,最后一次在 vuejs 中使用 vetur 插件

UPD2。代码示例。

// before
export default new Router({
  routes: [
    { path: '/', component: Home },
    { path: '/about', component: About },
    { path: '/todo', component: Todo },
  ]
})
// after formatting (curly braces are moved on new line)
export default new Router({
    routes: [{
            path: '/',
            component: Home
        },
        {
            path: '/about',
            component: About
        },
        {
            path: '/todo',
            component: Todo
        },
    ]
})

UPD 3. 也许 Prettier 对解决这个任务有用?

UPD 4. 就我而言,问题出在用于格式化的附加插件中。 Beautify 已作为格式化的默认选项安装,vscode 格式化程序的所有设置均无效。

解决方案默认设置vscode formatter or prettier

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

谢谢大家。尤其是maven87.

您要查找的设置是:

{
  // 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

在 VS Code 中转到设置: "File->Preferences->Settings"

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

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

解决方法是默认设置vscode formatter or prettier

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