Skip to content

Commit 208661b

Browse files
committedOct 5, 2018
clang-format: Don't insert spaces in front of :: for Java 8 Method References.
The existing code kept the space if it was there for identifiers, and it didn't handle `this`. After this patch, for Java `this` is handled in addition to identifiers, and existing space is always stripped between identifier and `::`. Also accept `::` in addition to `.` in front of `<` in `foo::<T>bar` generic calls. Differential Revision: https://reviews.llvm.org/D52842 llvm-svn: 343872
1 parent 0ed892d commit 208661b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed
 

‎clang/lib/Format/TokenAnnotator.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -2555,8 +2555,11 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
25552555
return false;
25562556
if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
25572557
Left.MatchingParen->Previous &&
2558-
Left.MatchingParen->Previous->is(tok::period))
2558+
(Left.MatchingParen->Previous->is(tok::period) ||
2559+
Left.MatchingParen->Previous->is(tok::coloncolon)))
2560+
// Java call to generic function with explicit type:
25592561
// A.<B<C<...>>>DoSomething();
2562+
// A::<B<C<...>>>DoSomething(); // With a Java 8 method reference.
25602563
return false;
25612564
if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
25622565
return false;
@@ -2776,6 +2779,9 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
27762779
if (!Style.SpaceBeforeAssignmentOperators &&
27772780
Right.getPrecedence() == prec::Assignment)
27782781
return false;
2782+
if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) &&
2783+
(Left.is(tok::identifier) || Left.is(tok::kw_this)))
2784+
return false;
27792785
if (Right.is(tok::coloncolon) && Left.is(tok::identifier))
27802786
// Generally don't remove existing spaces between an identifier and "::".
27812787
// The identifier might actually be a macro name such as ALWAYS_INLINE. If

‎clang/unittests/Format/FormatTestJava.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,22 @@ TEST_F(FormatTestJava, MethodDeclarations) {
443443
getStyleWithColumns(40));
444444
}
445445

446+
TEST_F(FormatTestJava, MethodReference) {
447+
EXPECT_EQ(
448+
"private void foo() {\n"
449+
" f(this::methodReference);\n"
450+
" f(C.super::methodReference);\n"
451+
" Consumer<String> c = System.out::println;\n"
452+
" Iface<Integer> mRef = Ty::<Integer>meth;\n"
453+
"}",
454+
format("private void foo() {\n"
455+
" f(this ::methodReference);\n"
456+
" f(C.super ::methodReference);\n"
457+
" Consumer<String> c = System.out ::println;\n"
458+
" Iface<Integer> mRef = Ty :: <Integer> meth;\n"
459+
"}"));
460+
}
461+
446462
TEST_F(FormatTestJava, CppKeywords) {
447463
verifyFormat("public void union(Type a, Type b);");
448464
verifyFormat("public void struct(Object o);");

0 commit comments

Comments
 (0)
Please sign in to comment.