简体   繁体   English

如何在 switch-case 语句中的 VSCode 中自动缩进?

[英]How to automatically indent in VSCode in a switch-case statement?

It's a bit frustrating that there are a lot of things that vscode does automatically but when using a switch-case statement it doesn't automatically indent after the colon.有点令人沮丧的是,vscode 会自动执行很多操作,但是在使用 switch-case 语句时,它不会在冒号后自动缩进。 This is what I get if I were to type without interfering如果我在不干扰的情况下打字,这就是我得到的

int x = 32;
switch (x){
    case 33:
    break;
    case 32:
    break;
    default:
}

And this is what I would like the to see这就是我希望看到的

int x = 32;
switch (x){
    case 33:
        break;
    case 32:
        break;
    default:
}

Clang Format for customizable formatting rules用于可自定义格式规则的 Clang 格式

For any C++ formatting needs I would recommend using Clang Format , which can be seamlessly integrated into VS Code.对于任何 C++ 格式化需求,我建议使用Clang Format ,它可以无缝集成到 VS Code 中。

In you example you may use the IndentCaseLabels style option :在您的示例中,您可以使用IndentCaseLabels样式选项

IndentCaseLabels (bool) IndentCaseLabels (bool)

Indent case labels one level from the switch statement.在 switch 语句中缩进 case 标签的一级。

When false , use the same indentation level as for the switch statement.false ,使用与 switch 语句相同的缩进级别。 Switch statement body is always indented one level more than case labels (except the first block following the case label, which itself indents the code - unless IndentCaseBlocks is enabled). Switch 语句主体总是比 case 标签缩进一级(除了 case 标签后面的第一个块,它本身缩进代码 - 除非启用了 IndentCaseBlocks)。

 false: true: switch (fool) { vs. switch (fool) { case 1: case 1: bar(); bar(); break; break; default: default: plop(); plop(); } }

Applied to your example:应用于您的示例:

//  IndentCaseLabels: true
int x = 32;
switch (x) {
  case 33:
    void();
    break;
  case 32:
    break;
  default:
}

//  IndentCaseLabels: false
int x = 32;
switch (x) {
case 33:
  void();
  break;
case 32:
  break;
default:
}

Integration of Clang Format into VS Code将 Clang 格式集成到 VS Code 中

Citing Edit C++ in Visual Studio Code from the VS Code documentation [ emphasis mine]:从 VS Code 文档中引用Edit C++ in Visual Studio Code [强调我的]:

[...] [...]

Code formatting代码格式化

The C/C++ extension for Visual Studio Code supports source code formatting using clang-format which is included with the extension . Visual Studio CodeC/C++ 扩展支持使用扩展中包含的clang-format进行源代码格式化。

You can format an entire file with Format Document (Ctrl+Shift+I) or just the current selection with Format Selection (Ctrl+K Ctrl+F) in right-click context menu.您可以使用格式化文档 (Ctrl+Shift+I) 格式化整个文件,或者使用右键单击上下文菜单中的格式化选择 (Ctrl+K Ctrl+F) 来格式化当前选择。 You can also configure auto-formatting with the following settings:您还可以使用以下设置配置自动格式化

  • editor.formatOnSave - to format when you save your file . editor.formatOnSave -保存文件时设置格式
  • editor.formatOnType - to format as you type (triggered on the ; character). editor.formatOnType - 在您键入时进行格式化(在;字符上触发)。

By default, the clang-format style is set to "file" which means it looks for a .clang-format file inside your workspace .默认情况下,clang-format 样式设置为“file”,这意味着它会在您的工作区中查找.clang-format文件 If the .clang-format file is found, formatting is applied according to the settings specified in the file.如果找到.clang-format文件,则根据文件中指定的设置应用格式。 If no .clang-format file is found in your workspace , formatting is applied based on a default style specified in the C_Cpp.clang_format_fallbackStyle setting instead.如果在您的工作区中找不到.clang-format文件,则将根据C_Cpp.clang_format_fallbackStyle设置中指定的默认样式应用格式。 Currently, the default formatting style is "Visual Studio" which is an approximation of the default code formatter in Visual Studio.当前,默认格式样式是“Visual Studio” ,它是Visual Studio 中默认代码格式器的近似值。

[...] [...]

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

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