Index: lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- lib/Format/UnwrappedLineFormatter.cpp +++ lib/Format/UnwrappedLineFormatter.cpp @@ -1042,6 +1042,10 @@ (TheLine.Level == RangeMinLevel && !PreviousRBrace && !TheLine.startsWith(tok::r_brace)); + if (TheLine.First->Next && TheLine.First->Next->Tok.isObjCAtKeyword(tok::objc_end)) { + ContinueFormatting = false; + } + bool FixIndentation = (FixBadIndentation || ContinueFormatting) && Indent != TheLine.First->OriginalColumn; bool ShouldFormat = TheLine.Affected || FixIndentation; Index: test/Format/adjust-indent-objc.m =================================================================== --- /dev/null +++ test/Format/adjust-indent-objc.m @@ -0,0 +1,13 @@ +// RUN: grep -Ev "// *[A-Z-]+:" %s | clang-format -lines=4:4 \ +// RUN: | FileCheck -strict-whitespace %s + +@protocol A +// CHECK: @protocol A + @optional +// CHECK: {{^ @optional}} +- (void)f; +// CHECK: {{^ - \(void\)f;}} +@end +// CHECK: {{^@end}} +MACRO +// CHECK: {{^MACRO}} \ No newline at end of file Index: unittests/Format/FormatTestSelective.cpp =================================================================== --- unittests/Format/FormatTestSelective.cpp +++ unittests/Format/FormatTestSelective.cpp @@ -584,6 +584,20 @@ 15, 0)); } +TEST_F(FormatTestSelective, StopFormattingWhenLeavingObjCScope) { + EXPECT_EQ("@protocol A\n" + " - (void)f;\n" + " - (void)g;\n" + "@end\n" + "MACRO", + format("@protocol A\n" + " - (void)f;\n" + "- (void)g;\n" // Format here. + "@end\n" + "MACRO", + 25, 0)); +} + TEST_F(FormatTestSelective, SelectivelyRequoteJavaScript) { Style = getGoogleStyle(FormatStyle::LK_JavaScript); EXPECT_EQ(