clang-format有很多关于空白的配置选项,还有一些关于代码顺序(包括的顺序).是否可以重新排序const限定符,以便将它们放在相应类型的右侧?
示例:声明const int x = 0;应格式化为int const x = 0;.
clang-format打破了80列的界限.有没有办法让止损线?文档在
http://clang.llvm.org/docs/ClangFormatStyleOptions.html
似乎没有解决这个问题.
使用具有默认设置的clang-format,如下所示:
if ((exprA) &&
(exprB))
Run Code Online (Sandbox Code Playgroud)
变成:
if ((exprA) && (exprB))
Run Code Online (Sandbox Code Playgroud)
我试图阻止将条件折叠成一条线,但没有成功.
目前有办法实现这一目标吗?
在clang格式的文档中,BreakBeforeBinaryOperators参数似乎与我之后的最接近:
BreakBeforeBinaryOperators(BinaryOperatorStyle)
包装二元运算符的方法.
- BOS_None(在配置中:)
None断开运算符.
但它似乎只在需要包装时才会启动(超出列限制),这不是通常的情况.
在尝试使用Objective-C为项目设置.clang格式文件时,我遇到了一个问题,即使最大行宽为0,长Objective-C方法也会被切割成多行.例如,这个:
AFHTTPRequestOperation *returnOperation = [self POST:endpoint parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:[[secureStore valueForKey:storeKey] dataUsingEncoding:NSUTF8StringEncoding] name:tokenKey];
if ([provider isEqualToString:kTwitterKey]) {
[formData appendPartWithFormData:[[secureStore twitterSecretToken] dataUsingEncoding:NSUTF8StringEncoding] name:kTwitterSecretKey];
[formData appendPartWithFormData:[email dataUsingEncoding:NSUTF8StringEncoding] name:kEmailKey];
}
[formData appendPartWithFormData:[[secureStore token] dataUsingEncoding:NSUTF8StringEncoding] name:tokenKey];
} success:^(AFHTTPRequestOperation *operation, NSDictionary *response) {
[secureStore setToken:response[kTokenKey]];
if (completionHandler) {
completionHandler(nil, response);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (completionHandler) {
completionHandler(error, nil);
}
}];
Run Code Online (Sandbox Code Playgroud)
变成了这个:
AFHTTPRequestOperation *returnOperation = [self POST:endpoint
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:[[secureStore valueForKey:storeKey] dataUsingEncoding:NSUTF8StringEncoding] name:tokenKey];
if ([provider isEqualToString:kTwitterKey]) { …Run Code Online (Sandbox Code Playgroud) clang-format 似乎从这样的块中弄得一团糟:
desc.add_options()("help", "output usage")
("inputDirectory", po::value<boost::filesystem::path>()->required(), "The input path")
("outputDirectory", po::value<boost::filesystem::path>()->required(), "The output path");
Run Code Online (Sandbox Code Playgroud)
我知道// clang-format off明确不要格式化一个块,但有没有一套配置规则可以让它做一些合理的事情呢?
我们在C/C++的代码库中使用lint,我也试图在我的工作流程中开始集成clang-format.
不幸的是,lint偶尔需要注释来忽略特定的检查,格式如下:
/*lint -[annotation] */
Run Code Online (Sandbox Code Playgroud)
要么
//lint -[annotation]
Run Code Online (Sandbox Code Playgroud)
具体来说,如果注释的开始标记和'lint'之间有空格,则它不会将其识别为注释指令.不幸的是,我对clang-format的默认设置将其视为错误并有助于插入空格.
有没有办法让clang格式识别匹配该模式的评论并让它们孤立?现在我使用3.4,但如果需要可以升级.
有没有办法强制参数和参数为单行 - 如果一个或多个字符超过溢出?
例如:
例如,这个:
if(value != "course" || value != "module" || value != "lesson"
)
Run Code Online (Sandbox Code Playgroud)
应该是这样的:
if(value != "course" ||
value != "module" ||
value != "lesson")
Run Code Online (Sandbox Code Playgroud)
或这个:
if(value != "course"
|| value != "module"
|| value != "lesson")
Run Code Online (Sandbox Code Playgroud)
还有这个:
void some_class::some_func(const std:string s, const std::string t
)
Run Code Online (Sandbox Code Playgroud)
应该是这样的:
void some_class::some_func(const std:string s,
const std::string t)
Run Code Online (Sandbox Code Playgroud)
或这个:
void some_class::some_func(const std:string s
, const std::string t)
Run Code Online (Sandbox Code Playgroud)
编辑:
#http://clang.llvm.org/docs/ClangFormatStyleOptions.html
# The style used for all options not specifically …Run Code Online (Sandbox Code Playgroud) 我正在使用vim-autoformat,它clang-format用作外部格式化程序.
似乎clang-format不会缩进C++ #pragma.例如:
#include <omp.h>
#include <cstdio>
int main()
{
#pragma omp parallel for
for (int i = 0; i < 10; ++i)
{
puts("demo");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想把它格式化为:
#include <omp.h>
#include <cstdio>
int main()
{
#pragma omp parallel for
for (int i = 0; i < 10; ++i)
{
puts("demo");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我检查了clangformat,但没有找到我可以使用的选项.
C++20(以及带有 的 23 std::ranges::to<T>())惯用地使用operator|来创建如下所示的转换管道:
return numbers
| std::views::filter([](int n) { return n % 2 == 0; })
| std::views::transform([](int n) { return n * 2; })
| std::ranges::to<std::vector>();
Run Code Online (Sandbox Code Playgroud)
根据我的项目的当前情况.clang-format,看起来像
return numbers | std::views::filter([](int n) { return n % 2 == 0; }) |
std::views::transform([](int n) { return n * 2; }) | std::ranges::to<std::vector>();
Run Code Online (Sandbox Code Playgroud)
我觉得很难读。如果我设置BreakBeforeBinaryOperators: All我得到
return numbers | std::views::filter([](int n) { return n % 2 == 0; })
| std::views::transform([](int n) { return …Run Code Online (Sandbox Code Playgroud) 是否有可能告诉Clang-Format忽略换行操作的注释?我们的想法是遵循"代码格式正确,即使评论超出换行余量"的风格.代码不应分成多行,如果它不超过保证金,但注释会.
例如
//desired behaviour:
short code = shortCode +
longlonglongCode;
short code = shortCode; //long comment without a line break
//not desired behaviour:
short code =
shortCode; //long comment without a line break
Run Code Online (Sandbox Code Playgroud) clang-format ×10
c++ ×7
clang ×2
c++20 ×1
coding-style ×1
lint ×1
objective-c ×1
std-ranges ×1