From 6efe17611a988cf2f4a78ab34b04f24ea7fc7dd9 Mon Sep 17 00:00:00 2001 From: Ben Hamilton Date: Fri, 30 Mar 2018 15:38:45 +0000 Subject: [PATCH] [clang-format] Ensure wrapped ObjC selectors with 1 arg obey IndentWrappedFunctionNames Summary: In D43121, @Typz introduced logic to avoid indenting 2-or-more argument ObjC selectors too far to the right if the first component of the selector was longer than the others. This had a small side effect of causing wrapped ObjC selectors with exactly 1 argument to not obey IndentWrappedFunctionNames: ``` - (aaaaaaaaaa) aaaaaaaaaa; ``` This diff fixes the issue by ensuring we align wrapped 1-argument ObjC selectors correctly: ``` - (aaaaaaaaaa) aaaaaaaaaa; ``` Test Plan: New tests added. Test failed before change, passed after change. Ran tests with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper, klimek, Typz, jolesiak Reviewed By: djasper, jolesiak Subscribers: cfe-commits, Typz Differential Revision: https://reviews.llvm.org/D44994 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@328871 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/ContinuationIndenter.cpp | 20 ++++++++++++++------ unittests/Format/FormatTestObjC.cpp | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 9d19262397..167b8822a0 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -896,12 +896,20 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent); if (NextNonComment->is(TT_SelectorName)) { if (!State.Stack.back().ObjCSelectorNameFound) { - if (NextNonComment->LongestObjCSelectorName == 0) - return State.Stack.back().Indent; - return (Style.IndentWrappedFunctionNames - ? std::max(State.Stack.back().Indent, - State.FirstIndent + Style.ContinuationIndentWidth) - : State.Stack.back().Indent) + + unsigned MinIndent = State.Stack.back().Indent; + if (Style.IndentWrappedFunctionNames) + MinIndent = std::max(MinIndent, + State.FirstIndent + Style.ContinuationIndentWidth); + // If LongestObjCSelectorName is 0, we are indenting the first + // part of an ObjC selector (or a selector component which is + // not colon-aligned due to block formatting). + // + // Otherwise, we are indenting a subsequent part of an ObjC + // selector which should be colon-aligned to the longest + // component of the ObjC selector. + // + // In either case, we want to respect Style.IndentWrappedFunctionNames. + return MinIndent + std::max(NextNonComment->LongestObjCSelectorName, NextNonComment->ColumnWidth) - NextNonComment->ColumnWidth; diff --git a/unittests/Format/FormatTestObjC.cpp b/unittests/Format/FormatTestObjC.cpp index 2eeaa6947b..5c12911be1 100644 --- a/unittests/Format/FormatTestObjC.cpp +++ b/unittests/Format/FormatTestObjC.cpp @@ -537,6 +537,22 @@ TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) { " aShortf:(NSRect)theRect {\n" "}"); + // Make sure selectors with 0, 1, or more arguments are indented + // when IndentWrappedFunctionNames is true. + verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"); + verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n"); + verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n"); + verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n"); + verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n"); + // Format pairs correctly. Style.ColumnLimit = 80; verifyFormat("- (void)drawRectOn:(id)surface\n" -- 2.40.0