r220274 - clang-format: [Java] Wrap after each function annotation.
Daniel Jasper
djasper at google.com
Tue Oct 21 01:24:18 PDT 2014
Author: djasper
Date: Tue Oct 21 03:24:18 2014
New Revision: 220274
URL: http://llvm.org/viewvc/llvm-project?rev=220274&view=rev
Log:
clang-format: [Java] Wrap after each function annotation.
Before:
@Override public String toString() { .. }
After:
@Override
public String toString() { .. }
Modified:
cfe/trunk/lib/Format/ContinuationIndenter.cpp
cfe/trunk/lib/Format/FormatToken.h
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTestJava.cpp
Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=220274&r1=220273&r2=220274&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Tue Oct 21 03:24:18 2014
@@ -458,6 +458,7 @@ unsigned ContinuationIndenter::addTokenO
!PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
PreviousNonComment->Type != TT_TemplateCloser &&
PreviousNonComment->Type != TT_BinaryOperator &&
+ PreviousNonComment->Type != TT_JavaAnnotation &&
Current.Type != TT_BinaryOperator && !PreviousNonComment->opensScope())
State.Stack.back().BreakBeforeParameter = true;
@@ -533,7 +534,8 @@ unsigned ContinuationIndenter::getNewLin
if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0)
return State.Stack.back().VariablePos;
if ((PreviousNonComment && (PreviousNonComment->ClosesTemplateDeclaration ||
- PreviousNonComment->Type == TT_AttributeParen)) ||
+ PreviousNonComment->Type == TT_AttributeParen ||
+ PreviousNonComment->Type == TT_JavaAnnotation)) ||
(!Style.IndentWrappedFunctionNames &&
(NextNonComment->is(tok::kw_operator) ||
NextNonComment->Type == TT_FunctionDeclarationName)))
Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=220274&r1=220273&r2=220274&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Tue Oct 21 03:24:18 2014
@@ -46,6 +46,7 @@ enum TokenType {
TT_ImplicitStringLiteral,
TT_InheritanceColon,
TT_InlineASMColon,
+ TT_JavaAnnotation,
TT_LambdaLSquare,
TT_LineComment,
TT_ObjCBlockLBrace,
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=220274&r1=220273&r2=220274&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue Oct 21 03:24:18 2014
@@ -826,6 +826,9 @@ private:
// Line.MightBeFunctionDecl can only be true after the parentheses of a
// function declaration have been found.
Current.Type = TT_TrailingAnnotation;
+ } else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
+ Current.Previous->is(tok::at)) {
+ Current.Type = TT_JavaAnnotation;
}
}
}
@@ -1787,6 +1790,9 @@ bool TokenAnnotator::mustBreakBefore(con
if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
Left.Previous->is(tok::char_constant))
return true;
+ } else if (Style.Language == FormatStyle::LK_Java) {
+ if (Left.Type == TT_JavaAnnotation && Line.MightBeFunctionDecl)
+ return true;
}
return false;
Modified: cfe/trunk/unittests/Format/FormatTestJava.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJava.cpp?rev=220274&r1=220273&r2=220274&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJava.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJava.cpp Tue Oct 21 03:24:18 2014
@@ -64,5 +64,14 @@ TEST_F(FormatTestJava, ThrowsDeclaration
" throws LooooooooooooooooooooooooooooongException {}");
}
+TEST_F(FormatTestJava, Annotations) {
+ verifyFormat("@Override\n"
+ "public String toString() {\n}");
+ verifyFormat("@Override\n"
+ "@Nullable\n"
+ "public String getNameIfPresent() {\n}");
+ verifyFormat("@Partial @Mock DataLoader loader;");
+}
+
} // end namespace tooling
} // end namespace clang
More information about the cfe-commits
mailing list