Skip to content
Snippets Groups Projects
Commit e8ccc81a authored by Nico Weber's avatar Nico Weber
Browse files

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
parent d147f8fa
No related branches found
No related tags found
No related merge requests found
...@@ -170,7 +170,7 @@ static void replacePPWhitespace( ...@@ -170,7 +170,7 @@ static void replacePPWhitespace(
/// \brief Checks whether the (remaining) \c UnwrappedLine starting with /// \brief Checks whether the (remaining) \c UnwrappedLine starting with
/// \p RootToken fits into \p Limit columns. /// \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; unsigned Columns = RootToken.FormatTok.TokenLength;
bool FitsOnALine = true; bool FitsOnALine = true;
const AnnotatedToken *Tok = &RootToken; const AnnotatedToken *Tok = &RootToken;
...@@ -188,6 +188,15 @@ bool fitsIntoLimit(const AnnotatedToken &RootToken, unsigned Limit) { ...@@ -188,6 +188,15 @@ bool fitsIntoLimit(const AnnotatedToken &RootToken, unsigned Limit) {
return FitsOnALine; 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 { class UnwrappedLineFormatter {
public: public:
UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
...@@ -479,6 +488,14 @@ private: ...@@ -479,6 +488,14 @@ private:
if (Left.is(tok::semi) || Left.is(tok::comma) || if (Left.is(tok::semi) || Left.is(tok::comma) ||
Left.ClosesTemplateDeclaration) Left.ClosesTemplateDeclaration)
return 0; 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)) if (Left.is(tok::l_paren))
return 20; return 20;
...@@ -1188,6 +1205,8 @@ private: ...@@ -1188,6 +1205,8 @@ private:
return false; return false;
if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr) if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
return true; return true;
if (isObjCSelectorName(Right))
return true;
if (Left.ClosesTemplateDeclaration) if (Left.ClosesTemplateDeclaration)
return true; return true;
if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser || if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
......
...@@ -1537,12 +1537,18 @@ TEST_F(FormatTest, FormatObjCMethodExpr) { ...@@ -1537,12 +1537,18 @@ TEST_F(FormatTest, FormatObjCMethodExpr) {
verifyFormat("throw [self errorFor:a];"); verifyFormat("throw [self errorFor:a];");
verifyFormat("@throw [self errorFor:a];"); verifyFormat("@throw [self errorFor:a];");
// The formatting of this isn't ideal yet. It tests that the formatter doesn't // This tests that the formatter doesn't break after "backing" but before ":",
// break after "backing" but before ":", which would be at 80 columns. // which would be at 80 columns.
verifyFormat( verifyFormat(
"void f() {\n" "void f() {\n"
" if ((self = [super initWithContentRect:contentRect styleMask:\n" " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n"
" styleMask backing:NSBackingStoreBuffered defer:YES]))"); " backing:NSBackingStoreBuffered defer:YES]))");
verifyFormat("[foo setasdfasdffffffffffffadfasdfasdf:\n"
" [bar dowith:asdfdsfasdfasdfasfasfasfsafasdfsfad]];");
verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n"
" [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
} }
...@@ -1582,6 +1588,7 @@ TEST_F(FormatTest, ObjCAt) { ...@@ -1582,6 +1588,7 @@ TEST_F(FormatTest, ObjCAt) {
verifyFormat("@'c'"); verifyFormat("@'c'");
verifyFormat("@true"); verifyFormat("@true");
verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
// FIXME: Array and dictionary literals need more work.
verifyFormat("@["); verifyFormat("@[");
verifyFormat("@{"); verifyFormat("@{");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment