From b1329216350e7d3278e3d37618904db47f8ea041 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 22 Oct 2014 09:12:44 +0000 Subject: [PATCH] clang-format: Use AllowShortBlocksOnASingleLine for ObjC blocks, too. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@220375 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 3 +++ unittests/Format/FormatTest.cpp | 45 ++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 0f4c2e2d50..285c23139c 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1782,6 +1782,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, } else if (Style.Language == FormatStyle::LK_Proto && Left.isNot(tok::l_brace) && Right.Type == TT_SelectorName) { return true; + } else if (Left.Type == TT_ObjCBlockLBrace && + !Style.AllowShortBlocksOnASingleLine) { + return true; } // If the last token before a '}' is a comma or a trailing comment, the diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index b19e3cb44b..7c2e6c396e 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -9154,24 +9154,35 @@ TEST_F(FormatTest, FormatsLambdas) { } TEST_F(FormatTest, FormatsBlocks) { - verifyFormat("int (^Block)(int, int);"); - verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); - verifyFormat("void (^block)(int) = ^(id test) { int i; };"); - verifyFormat("void (^block)(int) = ^(int test) { int i; };"); - verifyFormat("void (^block)(int) = ^id(int test) { int i; };"); - verifyFormat("void (^block)(int) = ^int(int test) { int i; };"); - - verifyFormat("foo(^{ bar(); });"); - verifyFormat("foo(a, ^{ bar(); });"); - verifyFormat("{ void (^block)(Object *x); }"); - - verifyFormat("[operation setCompletionBlock:^{ [self onOperationDone]; }];"); - verifyFormat("int i = {[operation setCompletionBlock:^{ [self " - "onOperationDone]; }]};"); - verifyFormat("[operation setCompletionBlock:^(int *i) { f(); }];"); - verifyFormat("int a = [operation block:^int(int *i) { return 1; }];"); + FormatStyle ShortBlocks = getLLVMStyle(); + ShortBlocks.AllowShortBlocksOnASingleLine = true; + verifyFormat("int (^Block)(int, int);", ShortBlocks); + verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); + verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); + verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); + verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); + verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); + + verifyFormat("foo(^{ bar(); });", ShortBlocks); + verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); + verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); + + verifyFormat("[operation setCompletionBlock:^{\n" + " [self onOperationDone];\n" + "}];"); + verifyFormat("int i = {[operation setCompletionBlock:^{\n" + " [self onOperationDone];\n" + "}]};"); + verifyFormat("[operation setCompletionBlock:^(int *i) {\n" + " f();\n" + "}];"); + verifyFormat("int a = [operation block:^int(int *i) {\n" + " return 1;\n" + "}];"); verifyFormat("[myObject doSomethingWith:arg1\n" - " aaa:^int(int *a) { return 1; }\n" + " aaa:^int(int *a) {\n" + " return 1;\n" + " }\n" " bbb:f(a * bbbbbbbb)];"); verifyFormat("[operation setCompletionBlock:^{\n" -- 2.40.0