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

标签 c++ c visual-studio-code

有点令人沮丧的是,vscode 会自动执行很多操作,但是当使用 switch-case 语句时,它不会在冒号后自动缩进。 这就是如果我在不干扰的情况下打字时得到的结果

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

这就是我希望看到的

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

最佳答案

用于可自定义格式规则的 Clang Format

对于任何 C++ 格式化需求,我建议使用 Clang Format ,可以无缝集成到 VS Code 中。

在您的示例中,您可以使用 IndentCaseLabels style option :

IndentCaseLabels (bool)

Indent case labels one level from the switch statement.

When false, use the same indentation level as for the switch statement. 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).

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

应用于您的示例:

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

将 Clang 格式集成到 VS Code 中

引用Edit C++ in Visual Studio Code from the VS Code documentation [强调我的]:

[...]

Code formatting

The C/C++ extension for Visual Studio Code supports source code formatting using clang-format which is included with the extension.

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. You can also configure auto-formatting with the following settings:

  • editor.formatOnSave - to format when you save your file.
  • editor.formatOnType - to format as you type (triggered on the ; character).

By default, the clang-format style is set to "file" which means it looks for a .clang-format file inside your workspace. If the .clang-format file is found, formatting is applied according to the settings specified in the file. 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. Currently, the default formatting style is "Visual Studio" which is an approximation of the default code formatter in Visual Studio.

[...]

关于c++ - 如何在 VSCode 中的 switch-case 语句中自动缩进?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60727685/

相关文章:

c++ - 在 Arduino 中初始化 C++ 库

javascript - 在与 Webpack 捆绑在一起的 VS Code 中调试 Node JS 代码

python - Visual Studio Code 中的 IntelliSense 不适用于 python 包

visual-studio-code - 如何防止 VS Code 折叠尾随空行?

c++ - 如何在发行版本中包含声音片段

c++ - FP 数的指数字段不是我预期的,为什么?

字符变量[] = {0};和 char var[1];在 C 中等价?

c - openMPI 在服务器集群上运行期间无限等待?

c++ - 在 C++ 中预初始化 map

c - 为什么这个 C vector 循环不自动向量化?