From: Daniel Jasper Date: Mon, 4 Jan 2016 07:29:07 +0000 (+0000) Subject: clang-format: Fix corner-case in ObjC method declaration formatting X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e62754299f84319c1ebe76e33194db6151bcbd2e;p=clang clang-format: Fix corner-case in ObjC method declaration formatting Before: - (void)shortf:(GTMFoo *)theFoo longKeyword:(NSRect)theRect longerKeyword:(float)theInterval error:(NSError **)theError { } After: - (void)shortf:(GTMFoo *)theFoo longKeyword:(NSRect)theRect longerKeyword:(float)theInterval error:(NSError **)theError { } git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@256738 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index d51121c118..ad8f798011 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -317,16 +317,16 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, if (Current.is(TT_SelectorName) && !State.Stack.back().ObjCSelectorNameFound) { + unsigned MinIndent = + std::max(State.FirstIndent + Style.ContinuationIndentWidth, + State.Stack.back().Indent); + unsigned FirstColonPos = State.Column + Spaces + Current.ColumnWidth; if (Current.LongestObjCSelectorName == 0) State.Stack.back().AlignColons = false; - else if (State.Stack.back().Indent + Current.LongestObjCSelectorName > - State.Column + Spaces + Current.ColumnWidth) - State.Stack.back().ColonPos = - std::max(State.FirstIndent + Style.ContinuationIndentWidth, - State.Stack.back().Indent) + - Current.LongestObjCSelectorName; + else if (MinIndent + Current.LongestObjCSelectorName > FirstColonPos) + State.Stack.back().ColonPos = MinIndent + Current.LongestObjCSelectorName; else - State.Stack.back().ColonPos = State.Column + Spaces + Current.ColumnWidth; + State.Stack.back().ColonPos = FirstColonPos; } // In "AlwaysBreak" mode, enforce wrapping directly after the parenthesis by diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 51c58786c7..a3a363107b 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -467,9 +467,8 @@ private: Tok->Type = TT_ObjCMethodExpr; Tok->Previous->Type = TT_SelectorName; if (Tok->Previous->ColumnWidth > - Contexts.back().LongestObjCSelectorName) { + Contexts.back().LongestObjCSelectorName) Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth; - } if (!Contexts.back().FirstObjCSelectorName) Contexts.back().FirstObjCSelectorName = Tok->Previous; } else if (Contexts.back().ColonIsForRangeExpr) { diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 3899eda406..22c469815b 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -7280,6 +7280,11 @@ TEST_F(FormatTest, FormatObjCMethodDeclarations) { " rect:(NSRect)theRect\n" " interval:(float)theInterval {\n" "}"); + verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" + " longKeyword:(NSRect)theRect\n" + " longerKeyword:(float)theInterval\n" + " error:(NSError **)theError {\n" + "}"); verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" " longKeyword:(NSRect)theRect\n" " evenLongerKeyword:(float)theInterval\n"