Page MenuHomePhabricator

clang-format: keep ObjC colon alignment with short object name
ClosedPublic

Authored by Typz on Feb 9 2018, 4:47 AM.

Details

Summary

When the target object expression is short and the first selector name is long, clang-format used to break the colon alignment:

[I performSelectorOnMainThread:@selector(loadAccessories)
                     withObject:nil
                  waitUntilDone:false];

This happens because the colon is placed at ContinuationIndent +LongestObjCSelectorName, so that any selector can be wrapped. This is however not needed in case the longest selector is the first one, and not wrapped.

To overcome this, this patch does not include the first selector in LongestObjCSelectorName computation (in TokenAnnotator), and lets ContinuationIndenter decide how to account for the first selector when wrapping. (Note this was already partly the case, see line 521 of ContinuationIndenter.cpp)

This way, the code gets properly aligned whenever possible without breaking the continuation indent.

[I performSelectorOnMainThread:@selector(loadAccessories)
                    withObject:nil
                 waitUntilDone:false];
[I // force break
    performSelectorOnMainThread:@selector(loadAccessories)
                     withObject:nil
                  waitUntilDone:false];
[I perform:@selector(loadAccessories)
    withSelectorOnMainThread:true
               waitUntilDone:false];

Diff Detail

Repository
rC Clang

Event Timeline

Typz created this revision.Feb 9 2018, 4:47 AM
Typz edited the summary of this revision. (Show Details)Feb 9 2018, 4:49 AM
Typz edited the summary of this revision. (Show Details)
Typz updated this revision to Diff 133589.Feb 9 2018, 4:51 AM

fix type in commit message

djasper added inline comments.Feb 9 2018, 4:57 AM
lib/Format/ContinuationIndenter.cpp
905

I'd prefer to use std::max<unsigned>( .. )

(and we generally don't use c-style casts)

krasimir added inline comments.Feb 9 2018, 4:58 AM
unittests/Format/FormatTestObjC.cpp
794

Could you add an instance where the first one is the longest and is wrapped?

Typz updated this revision to Diff 133596.Feb 9 2018, 6:00 AM
Typz marked 2 inline comments as done.

Address review comments

lib/Format/ContinuationIndenter.cpp
905

this is C++-style C-style cast :-)

anyway changed to use ColumnWidth, as this is also what is used for initializing LongestObjCSelectorName.

djasper accepted this revision.Feb 9 2018, 6:27 AM

Looks good.

This revision is now accepted and ready to land.Feb 9 2018, 6:27 AM
Typz updated this revision to Diff 133619.Feb 9 2018, 7:42 AM

rebase on latest master.

This revision was automatically updated to reflect the committed changes.