aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/Format.cpp
diff options
context:
space:
mode:
authorNico Weber <nicolasweber@gmx.de>2013-01-12 22:48:47 +0000
committerNico Weber <nicolasweber@gmx.de>2013-01-12 22:48:47 +0000
commite8ccc81a9f66361450148338b8457dd2c6ad11de (patch)
treeb7204807089db8294ea82393a4117f97b0ab805f /lib/Format/Format.cpp
parentd147f8fa3ef5dbebd1f24b72e52dedaebeaaa4b5 (diff)
Formatter: Prefer breaking before ObjC selector names over breaking at their ':'
Before: if ((self = [super initWithContentRect:contentRect styleMask: styleMask backing:NSBackingStoreBuffered defer:YES])) { Now: if ((self = [super initWithContentRect:contentRect styleMask:styleMask backing:NSBackingStoreBuffered defer:YES])) { git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172333 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r--lib/Format/Format.cpp21
1 files changed, 20 insertions, 1 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 8ca0a823fe..de870900b4 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -170,7 +170,7 @@ static void replacePPWhitespace(
/// \brief Checks whether the (remaining) \c UnwrappedLine starting with
/// \p RootToken fits into \p Limit columns.
-bool fitsIntoLimit(const AnnotatedToken &RootToken, unsigned Limit) {
+static bool fitsIntoLimit(const AnnotatedToken &RootToken, unsigned Limit) {
unsigned Columns = RootToken.FormatTok.TokenLength;
bool FitsOnALine = true;
const AnnotatedToken *Tok = &RootToken;
@@ -188,6 +188,15 @@ bool fitsIntoLimit(const AnnotatedToken &RootToken, unsigned Limit) {
return FitsOnALine;
}
+/// \brief Returns if a token is an Objective-C selector name.
+///
+/// For example, "bar" is a selector name in [foo bar:(4 + 5)]
+static bool isObjCSelectorName(const AnnotatedToken &Tok) {
+ return Tok.is(tok::identifier) && !Tok.Children.empty() &&
+ Tok.Children[0].is(tok::colon) &&
+ Tok.Children[0].Type == TT_ObjCMethodExpr;
+}
+
class UnwrappedLineFormatter {
public:
UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
@@ -479,6 +488,14 @@ private:
if (Left.is(tok::semi) || Left.is(tok::comma) ||
Left.ClosesTemplateDeclaration)
return 0;
+
+ // In Objective-C method expressions, prefer breaking before "param:" over
+ // breaking after it.
+ if (isObjCSelectorName(Right))
+ return 0;
+ if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr)
+ return 20;
+
if (Left.is(tok::l_paren))
return 20;
@@ -1188,6 +1205,8 @@ private:
return false;
if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
return true;
+ if (isObjCSelectorName(Right))
+ return true;
if (Left.ClosesTemplateDeclaration)
return true;
if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||