我是那种会系统地检查每个可切换选项的人,这次是clang-format在 VSCode 中。我发现了这个,我假设它列出了所有可以塞进.clang-format文件供 VSCode 使用的东西。不幸的是,VSCode 似乎无法识别其中的一些:
BitFieldColonSpacingBreakBeforeConceptDeclarationsEmptyLineBeforeAccessModifierIndentAccessModifierIndentRequiresSortIncludesSpaceAroundPointerQualifiersSpaceBeforeCaseColon在 VSCode 无法加载的少数选项中,仅能SortIncludes完全识别,但它只接受 bool,而不接受参考文献所说的可用选项。其余选项完全无法识别:
YAML:xxx:xxx: error: unknown key 'xxx'
Error reading /path/to/.clang-format: Invalid argument
Run Code Online (Sandbox Code Playgroud)
我是否遗漏了某些内容,或者 VSCode 不支持这些选项?我正在使用 Microsoft 的 C/C++ 扩展,在 Linux 上使用 VSCode。
感谢您的时间。
我有以下格式错误的文件,test.cpp
#include "maininclude.h"
int main() {
class SIMPLE smpl;
for (int i = 1; i < 100;
i++) {
printf("Printing %d", i); // Trying out different stuff...
getchar();
}
}
Run Code Online (Sandbox Code Playgroud)
我想在上面运行clang-format。在运行此命令之前,为了直观地查看要修改的位置,我运行clang-format -n test.cpp. 这可以正确识别由于终端上的格式和输出错误而将更改的位置:
test.cpp:3:13: warning: code should be clang-formatted [-Wclang-format-violations]
int main() {
^
test.cpp:5:27: warning: code should be clang-formatted [-Wclang-format-violations]
for (int i = 1; i < 100;
^
test.cpp:6:9: warning: code should be clang-formatted [-Wclang-format-violations]
i++) {
^
test.cpp:7:64: warning: code should …Run Code Online (Sandbox Code Playgroud) 当我使用 clang 来格式化我的 C++ 代码时,我遇到了一个问题:
priority_queue<int, vector<int>, greater<int> > q;
Run Code Online (Sandbox Code Playgroud)
将自动格式化为:
priority_queue<int, vector<int>, greater<int>> q;
Run Code Online (Sandbox Code Playgroud)
两个单独的 '>' 将被格式化为一个移位 >>。
那么我应该如何配置.clang-format文件来避免这种情况呢?
我尝试在 Visual Studio Code 中进行垂直声明对齐。
这段代码:
struct A {
double a;
int b;
}
Run Code Online (Sandbox Code Playgroud)
必须转换成这样:
struct A {
double a;
int b;
}
Run Code Online (Sandbox Code Playgroud)
请注意,这里不是赋值,这只是具有对齐字段的结构声明。
是否有任何 Visual Studio Code 扩展可以执行此操作?
我正在使用version 8.0.0 (tags/google/stable/2019-01-18)带有样式文件的clang-format ( ),我在其中设置了
…
PointerAlignment: Left
…
Run Code Online (Sandbox Code Playgroud)
这成功地转换了这样的声明
const string &foo = "lorem ipsum";
Run Code Online (Sandbox Code Playgroud)
进入
const string& foo = "lorem ipsum";
Run Code Online (Sandbox Code Playgroud)
但是,当我还包含在我的样式文件中时
BasedOnStyle: Google
Run Code Online (Sandbox Code Playgroud)
选项不做任何事情。出于某种原因,它们被基本样式覆盖。这对我来说似乎是荒谬的——显式选项应该覆盖基本样式,不是吗?有人可以解释问题是什么以及如何同时使用BasedOnStyle和PointerAlignment: Left吗?
我正在尝试为短函数实现以下格式:
void shortFunctionDecl(int x, int y) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
但如果行太长,我希望它像这样溢出(而不是对参数进行装箱)
void longerFunctionDecl(
int longName,
int longName2,
int longName3
) { // I would also be ok if the paren were on the previous line and the brace on this one
// ...
}
Run Code Online (Sandbox Code Playgroud)
目前代码最终是这样的,读起来很烦人
void longerFunctionDecl(
int longName,
int longName2,
int longName3) {
// code is at the same indentation level as the args, hard to read.
}
Run Code Online (Sandbox Code Playgroud)
我一直在尝试使用一些 BraceWrapping 设置,但看起来控制语句只有多行设置。可以做我想做的事吗clang-format?FWIW,似乎括号中断是不可能的,但我想知道括号中断是否可能?真的不确定。
这是我的.clang-format文件。我已经标记了相关部分,但为了完整性而包含了整个内容。
BasedOnStyle: …Run Code Online (Sandbox Code Playgroud)