简体   繁体   English

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

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

By default this 默认情况下这个

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

are converting to this 正在转变为此

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

How can I disable this behavior? 如何禁用此行为?

UPD. UPD。 I am coding in JavaScript, last time in vuejs with vetur plugin 我使用JavaScript进行编码,上次使用vetur插件进行vuejs编码

UPD2. UPD2。 Code expample. 代码示例。

// 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. Maybe Prettier will be useful to solve this task? UPD 3.也许Prettier会对解决这个任务有用吗?

UPD 4. In my case, the problem was in the additional plugin for formatting. UPD 4.在我的情况下,问题在于格式化的附加插件。 Beautify was installed as the default option for formatting and all settings for vscode formatter was not working. 已安装Beautify作为格式化的默认选项,并且vscode formatter的所有设置均无效。

The solution is set vscode formatter or prettier by default. 解决方案默认设置为vscode formatter或更漂亮。

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

Thanks all. 谢谢大家。 Especially for maven87. 特别是对于maven87。

The settings you are looking for are: 您正在寻找的设置是:

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

Please refer to the User and Workspace Settings article. 请参阅“ 用户和工作区设置”一文。 It covers similar settings for other languages too. 它也涵盖了其他语言的类似设置。

If that doesn't provide enough control you may also choose to use Beautify and specify a .jsbeautifyrc 如果这不能提供足够的控制,您也可以选择使用Beautify并指定.jsbeautifyrc

with a setting for brace style as follows: 具有如下支撑样式的设置:

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

Please refer to this to see all possible values. 请参阅此处以查看所有可能的值。

UPDATE UPDATE

It was pointed out that there is another degree of control where if you would like to change the formatter completely, you do so by installing the correct VS Code plugin and then configuring the appropriate formatter (see User and Workspace Settings article). 有人指出,如果您想完全更改格式化程序,可以通过安装正确的VS Code插件,然后配置相应的格式化程序(请参阅“ 用户和工作区设置”一文)来进行另一种控制。 For example: 例如:

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

In this case the formatter being used is Prettier - Code formatter 在这种情况下,使用的格式化程序是Prettier - Code formatter

In VS Code go to settings: "File->Preferences->Settings" 在VS代码中转到设置:“文件 - >首选项 - >设置”

In settings search "curly". 在设置中搜索“卷曲”。 It will search below settings, unckeck them and verify if it works as expected. 它将搜索以下设置,解开它们并验证它是否按预期工作。 enter image description here 在此输入图像描述

In my case, the problem was in the additional plugin for formatting. 就我而言,问题在于格式化的附加插件。 Beautify was installed as the default option for formatting and all settings for vscode formatter was not working. 已安装Beautify作为格式化的默认选项,并且vscode formatter的所有设置均无效。

The solution is set vscode formatter or prettier by default. 解决方案默认设置为vscode formatter或更漂亮。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM