This is an archive of the discontinued LLVM Phabricator instance.

Format closing braces when reformatting the line containing theopening brace.
ClosedPublic

Authored by klimek on Apr 17 2018, 9:10 AM.

Details

Summary

This required a couple of yaks to be shaved:1. MatchingOpeningBlockLineIndex was misused to also store the closing index; instead, use a second variable, as this doesn't work correctly for } else {.

  1. We needed to change the API of AffectedRangeManager to not use iterators; we always passed in begin / end for the whole container before, so there was no mismatch in generality.
  2. We need an extra check to discontinue formatting at the top level, as we now sometimes change the indent of the closing brace, but want to bail out immediately afterwards, for example:
void f() {
  if (a) {
}
void g();
Previously:
void f() {
  if (a) {
}
void g();
Now:
void f() {
  if (a) {
  }
void g();

Format closing braces.

Diff Detail

Event Timeline

klimek created this revision.Apr 17 2018, 9:10 AM

Great patch!

lib/Format/AffectedRangeManager.cpp
144

nit: replace -1 with UnwrappedLine::kInvalidIndex

lib/Format/UnwrappedLineParser.h
58

This comment could be improved. I interpret it as: start with the line at MatchingOpeningBlockLineIndex, and store the closing block line index of it, which will result in just the index of the current line being stored here if it closes a block.

krasimir retitled this revision from Format closing braces when reformatting the line containing the opening brace. This required a couple of yaks to be shaved: 1. MatchingOpeningBlockLineIndex was misused to also store the closing index; instead, use a second variable, as this... to Format closing braces when reformatting the line containing theopening brace..Apr 19 2018, 3:13 AM
krasimir edited the summary of this revision. (Show Details)
krasimir edited the summary of this revision. (Show Details)
krasimir edited the summary of this revision. (Show Details)

Another point: for the example in the summary about bailing-out early, is there a test for this already? If not, we should add one.

klimek updated this revision to Diff 143274.Apr 20 2018, 3:04 AM
klimek marked 2 inline comments as done.

Address comments.

Another point: for the example in the summary about bailing-out early, is there a test for this already? If not, we should add one.

Yep, there already is one - I regressed that with my change at first ;)

krasimir accepted this revision.Apr 23 2018, 1:48 AM
This revision is now accepted and ready to land.Apr 23 2018, 1:48 AM
This revision was automatically updated to reflect the committed changes.