简体   繁体   English

在 C++ VSCode 中自动格式化同一行的大括号

[英]Automatically format Curly Braces on Same Line in C++ VSCode

I have a problem with the way intellisense autocompletes snippets in VSCode, currently, when I write an if statement, it autocompletes to:我对 VSCode 中 intellisense 自动完成片段的方式有疑问,目前,当我编写 if 语句时,它会自动完成为:

if (condition)
{
   // some code
}

However, I would like it to autocomplete to:但是,我希望它自动完成:

if (condition) {
  // some code
}

If I use my formatter in VSCode, it formats it to the convention above, however I would like to avoid having to press a separate command for this to occur.如果我在 VSCode 中使用我的格式化程序,它会将其格式化为上面的约定,但是我想避免为此按下一个单独的命令。 Optimally the intellisense snippet would just format it like this by default.最佳情况下,智能感知代码段默认情况下会像这样格式化。

Any help with this would be appreciated.对此有任何帮助,我们将不胜感激。

This is a bigger subject than you might think这是一个比你想象的更大的主题

So typically with C++, the preferred formatter is the CLang Formatter .因此,通常对于 C++,首选格式化程序是CLang Formatter CLang's tool is so good, ECMAScript's ESLint has been taking notes, and has implemented many of the same rules found in the CLang formatter, specifically the rules that have to do with brackets. CLang 的工具非常好,ECMAScript 的ESLint一直在做笔记,并实现了许多在 CLang 格式化程序中发现的相同规则,特别是与括号有关的规则。

VSCode has a default formatter that can be used for C++. And this really comes down to what your using. VSCode 有一个默认的格式化程序,可用于 C++。这实际上取决于您使用的是什么。



Problem:问题:

What your asking for, essentially, is for your brackets to be inserted using a certain style, rather than to be auto formatted to a specific style.您基本上要求的是使用某种样式插入括号,而不是将其自动格式化为特定样式。



Solution解决方案

The snippets you are using are obviously predefined, and VSCode supports writing your own snippets.您使用的片段显然是预定义的,VSCode 支持编写您自己的片段。

Like this:像这样:

  // FILE: @(".../.config/code/user/snippets/cpp.json")

    {
        "Print to console": {
            "prefix": "if(",
            "body": ["if($1){\n$2\n}", "$2"],
            "description": "If Condition"
        }
    }

The problem with snippets is: You would need to defined every snippet you use, styling each one.片段的问题是:您需要定义您使用的每个片段,为每个片段设置样式。

VSCode Snippets Documentation (official) VSCode 片段文档(官方)

Snippet files are extensible, meaning they can be turned into VSCode extensions.片段文件是可扩展的,这意味着它们可以变成 VSCode 扩展。 Because a snippet library is quick & easy to write in comparison to most other extensions, there are a lot of them available in the Visual Studio Marketplace, and are all 100% Free to download.由于与大多数其他扩展相比,代码片段库编写起来既快捷又容易,因此 Visual Studio Marketplace 中提供了很多,而且都 100% 免费下载。

You could download a pack that contains the snippets you wont, and then just ad the '\n' line character where needed, throughout the document.您可以下载一个包,其中包含您不会使用的片段,然后在整个文档中需要的地方添加 '\n' 行字符。


Preferred Solution首选解决方案

It may not be what you want.它可能不是你想要的。 But the best way to do this, is to do what 90% of C/Cpp developers do who write c/cpp in VSCode;但最好的方法是做 90% 在 VSCode 中编写 c/cpp 的 C/Cpp 开发人员所做的事情; use CLang formatter, and configure VSCode to format your code when you save.使用 CLang 格式化程序,并配置 VSCode 在保存时格式化代码。 I am always hitting CTRL + S because I don't like throwing work down the drain because I forgot to save.我总是按 CTRL + S,因为我不喜欢因为忘记保存而把工作付诸东流。 And my if statements are all auto formatted exactly as you displayed the example in your question.我的 if 语句都是自动格式化的,与您在问题中显示的示例完全相同。

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

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