如何在 VS Code 中使用 .editorconfig 控制大括号是否另起一行?

问题描述 投票:0回答:2

我使用 VS Code 作为我的编辑器。我已经安装了“EditorConfig for VS Code”扩展。我已将 .editorconfig 文件添加到我的根项目中。这是 .editorconfig 文件中的代码:

root = true

[*.c]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
brace_style = attach

以下是我想要通过使用指定的快捷方式将格式应用于C文件来格式化的C文件(称为main.c):

#include <stdio.h>
#include <math.h>

double fun(double x, double n)
{
    return pow(x, n);
}

int main()
{
    printf("Please write a base number:\n");
    double base, exponent;
    scanf("%lf", &base);
    printf("Write a exponent number:\n");
    scanf("%lf", &exponent);
    printf("The numbers %lf and %lf gives: %lf \n", base, exponent, fun(base, exponent));
}

C 文件(称为 main.c)位于根文件夹中,作为 .editorconfig 文件。因此,

Root
|-- .editorconfig
|-- Project A
    |-- main.c

我希望能够使左括号位于同一行。属性 brace_style 应确保括号位于同一行。不幸的是,他们不断被转移到新的线路。我可以将 indent_size 的属性更改为 8,然后这些更改将在 main.c 文件中格式化。因此,main.c 文件和 .editorconfig 文件之间存在“连接”。它只是不想将括号格式化为同一行。

c visual-studio-code editorconfig
2个回答
0
投票

我不知道 VS Code 的 EditorConfig 扩展支持

brace_style
属性。
EditorConfig.EditorConfig
记录它使用editorconfig npm包,(在撰写本文时),其唯一支持的属性是
indent_style
indent_size
tab_width
end_of_line
(保存时),
 insert_final_newline
(保存时)和
trim_trailing_whitespace
(保存时)。

我尝试用谷歌搜索

brace_style
属性并找到 这个 JetBrains 文档,所以我想知道您是否从 JetBrains IDE 获得它并假设它是您可以在任何编辑器中使用的东西,而事实上 - 它是一个编辑器扩展。另请参阅https://github.com/editorconfig/editorconfig/issues/199#issuecomment-72388652

如果 VS Code 的 EditorConfig 扩展是添加这些换行符,那就很奇怪了。如果你只是想让它“摆脱”它们(如果它们已经存在),因为你使用的 VS Code 扩展来自 EditorConfig 维护者,你要么需要找到一个不同的扩展,要求他们实现一些东西他们已经说过没有意义的通用性,或者等待他们的对提议的特定领域属性的想法curly_bracket_next_line”做出决定(您可能会在这里找到与此相关的问题票证

)。


0
投票

cpp_new_line_before_open_brace_block = same_line

我在
https://learn.microsoft.com/en-us/visualstudio/ide/cpp-editorconfig-properties

中找到了此记录,它对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.