简体   繁体   English

打字时如何设置 VSCode 以在 C# 和 C++ 中的新行上放置花括号?

[英]How do I set up VSCode to put curly braces on a new line in C# and C++ while typing?

I want VS Code to put curly braces on a new line in C# and C++我希望 VS Code 将花括号放在 C# 和 C++ 中的新行上

How it works now现在如何运作现在如何运作

How it should look它应该看起来如何它应该看起来如何

Tried C# FixFormat extension, but it works only after I push CTRL+K+F but I want VS Code to make curly braces on new lines while I'm coding, without additional steps like hotkeys and such尝试过 C# FixFormat 扩展,但它只有在我按下 CTRL+K+F 后才有效,但我希望 VS Code 在我编码时在新行上加上花括号,而不需要额外的步骤,如热键等

In my case, I was using the C# extension by Microsoft (3.5 stars).就我而言,我使用的是 MicrosoftC# 扩展(3.5 星)。 By default, this extension uses its default C# formatter.默认情况下,此扩展使用其默认的 C# 格式化程序。 Disable the formatting option as shown below, and you won't get any for formatting, including the annoying NewLinesForBracesInMethods .禁用如下所示的格式化选项,您将不会获得任何格式化选项,包括烦人的NewLinesForBracesInMethods Alternatively you can try to tweak the C# extension formatter.或者,您可以尝试调整 C# 扩展格式化程序。 More info here . 更多信息在这里

So I replaced it with the C# FixFormat (5 stars) extension.所以我用C# FixFormat (5 星)扩展替换了它。 This seems to work straight of the box for me.这对我来说似乎是直接的。

But then I realised, I wasn't getting autocomplete, so I reinstalled the C# extension by microsoft, and kept the FixFormat extension.但后来我意识到,我没有自动完成,所以我重新安装了微软的 C# 扩展,并保留了 FixFormat 扩展。 And it works great, no new lines for braces.它工作得很好,没有大括号的新行。

On VSCode, Go to File\\Preferences\\Settings and search for 'curly'.在 VSCode 上,转到 File\\Preferences\\Settings 并搜索“curly”。 Enable all four typescripts as given in the image.启用图像中给出的所有四个打字稿。

大括号

Now that C#FixFormat has been removed, try putting an omnisharp.json file in the root of your project with the following.现在 C#FixFormat 已被删除,请尝试使用以下内容将omnisharp.json文件放在项目的根目录中。

{
    "FormattingOptions": {
        "NewLinesForBracesInLambdaExpressionBody": false,
        "NewLinesForBracesInAnonymousMethods": false,
        "NewLinesForBracesInAnonymousTypes": false,
        "NewLinesForBracesInControlBlocks": false,
        "NewLinesForBracesInTypes": false,
        "NewLinesForBracesInMethods": false,
        "NewLinesForBracesInProperties": false,
        "NewLinesForBracesInObjectCollectionArrayInitializers": false,
        "NewLinesForBracesInAccessors": false,
        "NewLineForElse": false,
        "NewLineForCatch": false,
        "NewLineForFinally": false
    }
}

在此处输入图片说明

From Preferences/Settings, choose c# FixFormat and uncheck the checkbox (Braces: On Same Line).从首选项/设置中,选择 c# FixFormat 并取消选中复选框(大括号:在同一行)。

If you want to add this globally you could do the following:如果要全局添加它,您可以执行以下操作:

  1. open the directory: $HOME/.omnisharp (or create the directory if it does not already exist)打开目录: $HOME/.omnisharp (如果目录不存在,则创建该目录)
  2. create a file in said directory called: omnisharp.json在上述目录中创建一个名为: omnisharp.json的文件
  3. paste the content from @thornebrandt's answer and save the file粘贴@thornebrandt 答案中的内容并保存文件
  4. go into VS Code and format the code with alt + shift + f进入 VS Code 并使用alt + shift + f格式化代码
{
    "FormattingOptions": {
        "NewLinesForBracesInLambdaExpressionBody": false,
        "NewLinesForBracesInAnonymousMethods": false,
        "NewLinesForBracesInAnonymousTypes": false,
        "NewLinesForBracesInControlBlocks": false,
        "NewLinesForBracesInTypes": false,
        "NewLinesForBracesInMethods": false,
        "NewLinesForBracesInProperties": false,
        "NewLinesForBracesInObjectCollectionArrayInitializers": false,
        "NewLinesForBracesInAccessors": false,
        "NewLineForElse": false,
        "NewLineForCatch": false,
        "NewLineForFinally": false
    }
}

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

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