标签: clang-format

Clang-Format块和ColumnLimit 0

我在使用clang-format和blocks时遇到了一些麻烦.我想使用尾随块维护以下格式的方法调用:

[self presentViewController:alertController animated:YES completion:^{
    NSLog(@"Output");
    // Do something
}];
Run Code Online (Sandbox Code Playgroud)

我已将ColumnLimit设置为0,它可以在任何地方使用,但它的副作用是不在块内格式化任何内容(if语句,其他调用等).我可以找到格式化块内部代码的唯一方法是将ColumnLimit设置为> 0,但是,即使我将其设置为像100000这样的大小,它也会为每个参数添加中断,这是我不想要的:

[self presentViewController:alertController
               animated:YES
             completion:^{
                 NSLog(@"Output");
                 // Do something
             }];
Run Code Online (Sandbox Code Playgroud)

因此,我想要的组合是对块内的代码进行适当格式化,而不涉及方法调用的任何其他内容.

我的clang格式配置:

Language: Cpp

ColumnLimit: 0
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortLoopsOnASingleLine: false
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
IndentWidth: 4
TabWidth: 4
UseTab: Never
PointerAlignment: Right
DerivePointerAlignment: false
ObjCSpaceAfterProperty: true
BreakBeforeBraces: Stroustrup
AllowShortIfStatementsOnASingleLine: false
BreakBeforeTernaryOperators: false
IndentCaseLabels: true
AllowShortCaseLabelsOnASingleLine: false
AlignTrailingComments: true
BinPackParameters: false
BinPackArguments: false
AllowShortFunctionsOnASingleLine: false
IndentWrappedFunctionNames: false
ObjCSpaceBeforeProtocolList: true
SpacesInParentheses: false
SpacesInAngles:  false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: …
Run Code Online (Sandbox Code Playgroud)

xcode objective-c ios clang-format

8
推荐指数
0
解决办法
295
查看次数

括号和访问修饰符的Clang格式样式规则

我试着编写自己的clang格式样式文件.有两个方面我无法让他们正确.

我如何让它留着以后一个空行public:,private:,protected:?例如,我想拥有

public :

ctor () {} 
Run Code Online (Sandbox Code Playgroud)

代替

public :
ctor () {}
Run Code Online (Sandbox Code Playgroud)

第二个问题是,当它跟随并控制语句函数定义时,有一种方法可以使它在括号前插入一个空格.但是在函数调用之前没有空格.例如,我想要,

void func () {}
func()
Run Code Online (Sandbox Code Playgroud)

SpaceBeforeParens只能是一个Never,Always,ControlStatements.最后一个最接近我想要的,但它仍然不能按我想要的方式工作.一个次要的相关问题是,如果它遵循一元运算符,它总是在括号之前删除空格

C &operator=(const C &);
Run Code Online (Sandbox Code Playgroud)

我比较习惯

C &operator= (const C &);
Run Code Online (Sandbox Code Playgroud)

clang-format

8
推荐指数
0
解决办法
621
查看次数

我怎么能告诉clang-format遵循这个约定?

我想要这个:

if (!enabled)
{
    return;
}
Run Code Online (Sandbox Code Playgroud)

转向这个:

if (!enabled) { return; }
Run Code Online (Sandbox Code Playgroud)

(换句话说,我想在单行上使用简短的if语句但保留{}它们)

目前我正在使用以下配置:

AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortBlocksOnASingleLine: true
BreakBeforeBraces: Allman
Run Code Online (Sandbox Code Playgroud)

但是,我得到的输出是:

if (!enabled) 
{
    return;
}
Run Code Online (Sandbox Code Playgroud)

是否可以使用clang格式完成上述格式化?

c++ clang-format

8
推荐指数
1
解决办法
594
查看次数

如何防止clang-format在注释char后添加空格?

我的代码中有一些注释:

//asdf
Run Code Online (Sandbox Code Playgroud)

当我在它上面使用clang-format时,它会在//字符后添加一个空格:

// asdf
Run Code Online (Sandbox Code Playgroud)

如何防止在clang格式配置中发生这种情况?

谢谢

c++ clang-format

8
推荐指数
1
解决办法
1155
查看次数

ReactiveCocoa的ClangFormat样式

我正在使用ClangFormat.

我希望从中更新我的ReactiveCocoa代码的样式

   [[self.myService indexCase] subscribeNext:^(id response) {
        DDLogDebug(@"response : %@", response);
    }
        error:^(NSError *error) {
            [self.dataManager sendError:error];
        }];
Run Code Online (Sandbox Code Playgroud)

对此

  [[self.myService indexCase]
     subscribeNext:^(id response) {
        DDLogDebug(@"response : %@", response);
     } error:^(NSError *error) {
            [self.dataManager sendError:error];
        }];
Run Code Online (Sandbox Code Playgroud)

我应该考虑使用什么ClangFormat属性来实现这一目标?


我当前的.clang-format档案:

BasedOnStyle: WebKit
AlignTrailingComments: true
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: true
BreakBeforeBraces: Linux
ColumnLimit: 120
IndentCaseLabels: true
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 2
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PointerBindsToType: false
SpacesBeforeTrailingComments: 1
TabWidth: 4
UseTab: Never
Run Code Online (Sandbox Code Playgroud)

static-analysis objective-c clang reactive-cocoa clang-format

8
推荐指数
1
解决办法
131
查看次数

clang格式的流输出

clang-format格式化我的代码如下:

std::cout << 1 << 1 << 1 << 1 << 1 << 1
          << 1 << 1 << 1 << 1 << 1 << 1;
Run Code Online (Sandbox Code Playgroud)

但我的风格指南要求:

std::cout << 1 << 1 << 1 << 1 << 1 << 1 <<
    1 << 1 << 1 << 1 << 1 << 1;
Run Code Online (Sandbox Code Playgroud)

我尝试设置BreakBeforeBinaryOperators:None和AlignOperands:false,但它没有帮助我.还有其他选择吗?

c++ clang-format

8
推荐指数
0
解决办法
461
查看次数

如何防止clang-format将单个分号添加到新行?

我在C++中有这行代码

while (fread(pixel_array++, sizeof(byte), 3, fp));
Run Code Online (Sandbox Code Playgroud)

但是当我使用clang-format时,它会分割分号并将其添加到新行中

while (fread(pixel_array++, sizeof(byte), 3, fp))
    ;
Run Code Online (Sandbox Code Playgroud)

我不喜欢这种风格,我只想保留原来的风格.

我该如何修改我的clang格式配置?谢谢.

c++ coding-style clang clang-format

8
推荐指数
2
解决办法
910
查看次数

clang-formatter:阻止函数声明中的新行

Objective-C如何防止新线路:

- (NSInteger)tableView:(UITableView *)tableView
    numberOfRowsInSection:(NSInteger)section {
Run Code Online (Sandbox Code Playgroud)

看到这个:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
Run Code Online (Sandbox Code Playgroud)

试过这个,但显然不适用于Objective-C:

AllowAllParametersOfDeclarationOnNextLine: false
Run Code Online (Sandbox Code Playgroud)

code-formatting objective-c ios clang-format

8
推荐指数
0
解决办法
82
查看次数

获取clang格式以将多行函数调用的右括号放在单独的行上?

我一直在使用clang格式来帮助保持我的代码干净.对于多行函数调用,有没有办法让clang将克隆括号放在它自己的行上?

例:

现在正在做什么:

increment_and_call_on_match(
    clique_colors,
    0,
    max_clique_color,
    [&](int clique_color) { 
        comms.emplace_back(context.split_by_color(clique_color)); 
    },
    [&](int) { context.split_by_color(); });
Run Code Online (Sandbox Code Playgroud)

我想要的是:

increment_and_call_on_match(
    clique_colors,
    0,
    max_clique_color,
    [&](int clique_color) { 
        comms.emplace_back(context.split_by_color(clique_color)); 
    },
    [&](int) { context.split_by_color(); }
); //Closing paren on new line
Run Code Online (Sandbox Code Playgroud)

c++ clang-format

8
推荐指数
1
解决办法
179
查看次数

clang-format:宏的缩进

我正在尝试将 clang-format 应用到现有的代码库并遇到以下问题:

简化(并格式化)的示例代码:

#define QUERY_BEGIN()
#define QUERY_NORESULT()
#define QUERY_END()

void foo()
{
   int a = 0;

   QUERY_BEGIN()
      a = 1;
      QUERY_NORESULT()
      a = 2;
   QUERY_END()
}
Run Code Online (Sandbox Code Playgroud)

我设置了以下选项:

MacroBlockEnd:   'QUERY_END'
MacroBlockBegin: 'QUERY_BEGIN'
Run Code Online (Sandbox Code Playgroud)

我想要实现的是宏部分的以下格式:

   QUERY_BEGIN()
      a = 1;
   QUERY_NORESULT()
      a = 2;
   QUERY_END()
Run Code Online (Sandbox Code Playgroud)

我的第一个猜测是设置QUERY_NORESULTMacroBlockEndMacroBlockBegin但这没有帮助。其结果如下:

   QUERY_BEGIN()
      a = 1;
      QUERY_NORESULT
         a = 2;
      QUERY_END()
Run Code Online (Sandbox Code Playgroud)

目前有没有办法实现如上所示的缩进?

c++ macros clang-format

8
推荐指数
1
解决办法
8195
查看次数