r222524 - clang-format: [Java] Basic lambda support.
Daniel Jasper
djasper at google.com
Fri Nov 21 04:14:13 PST 2014
Author: djasper
Date: Fri Nov 21 06:14:12 2014
New Revision: 222524
URL: http://llvm.org/viewvc/llvm-project?rev=222524&view=rev
Log:
clang-format: [Java] Basic lambda support.
Modified:
cfe/trunk/lib/Format/FormatToken.h
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTestJava.cpp
Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=222524&r1=222523&r2=222524&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Fri Nov 21 06:14:12 2014
@@ -48,6 +48,7 @@ enum TokenType {
TT_InheritanceColon,
TT_InlineASMColon,
TT_JavaAnnotation,
+ TT_LambdaArrow,
TT_LambdaLSquare,
TT_LeadingJavaAnnotation,
TT_LineComment,
@@ -271,6 +272,8 @@ struct FormatToken {
bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
+ bool is(TokenType TT) const { return Type == TT; }
+
bool is(const IdentifierInfo *II) const {
return II && II == Tok.getIdentifierInfo();
}
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=222524&r1=222523&r2=222524&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Fri Nov 21 06:14:12 2014
@@ -786,6 +786,9 @@ private:
Current.Type = TT_StartOfName;
} else if (Current.is(tok::kw_auto)) {
AutoFound = true;
+ } else if (Current.is(tok::arrow) &&
+ Style.Language == FormatStyle::LK_Java) {
+ Current.Type = TT_LambdaArrow;
} else if (Current.is(tok::arrow) && AutoFound &&
Line.MustBeDeclaration && Current.NestingLevel == 0) {
Current.Type = TT_TrailingReturnArrow;
@@ -1167,6 +1170,8 @@ private:
else if (NextNonComment && NextNonComment->is(tok::colon) &&
NextNonComment->Type == TT_DictLiteral)
return prec::Comma;
+ else if (Current->is(TT_LambdaArrow))
+ return prec::Comma;
else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon ||
Current->Type == TT_SelectorName ||
(Current->is(tok::comment) && NextNonComment &&
@@ -1701,6 +1706,8 @@ bool TokenAnnotator::spaceRequiredBefore
if (Left.is(Keywords.kw_var))
return true;
} else if (Style.Language == FormatStyle::LK_Java) {
+ if (Left.is(TT_LambdaArrow) || Right.is(TT_LambdaArrow))
+ return true;
if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren))
return Style.SpaceBeforeParens != FormatStyle::SBPO_Never;
if (Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
Modified: cfe/trunk/unittests/Format/FormatTestJava.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJava.cpp?rev=222524&r1=222523&r2=222524&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJava.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJava.cpp Fri Nov 21 06:14:12 2014
@@ -317,5 +317,20 @@ TEST_F(FormatTestJava, NeverAlignAfterRe
getStyleWithColumns(40));
}
+TEST_F(FormatTestJava, FormatsLambdas) {
+ verifyFormat("(aaaaaaaaaa, bbbbbbbbbb) -> aaaaaaaaaa + bbbbbbbbbb;");
+ verifyFormat("(aaaaaaaaaa, bbbbbbbbbb)\n"
+ " -> aaaaaaaaaa + bbbbbbbbbb;",
+ getStyleWithColumns(40));
+ verifyFormat("Runnable someLambda = () -> DoSomething();");
+ verifyFormat("Runnable someLambda = () -> {\n"
+ " DoSomething();\n"
+ "}");
+
+ verifyFormat("Runnable someLambda =\n"
+ " (int aaaaa) -> DoSomething(aaaaa);",
+ getStyleWithColumns(40));
+}
+
} // end namespace tooling
} // end namespace clang
More information about the cfe-commits
mailing list