r221103 - clang-format: [Java] Don't break after extends/implements.
Daniel Jasper
djasper at google.com
Sun Nov 2 11:16:42 PST 2014
Author: djasper
Date: Sun Nov 2 13:16:41 2014
New Revision: 221103
URL: http://llvm.org/viewvc/llvm-project?rev=221103&view=rev
Log:
clang-format: [Java] Don't break after extends/implements.
Before:
abstract class SomeClass extends SomeOtherClass implements
SomeInterface {}
After:
abstract class SomeClass extends SomeOtherClass
implements SomeInterface {}
Modified:
cfe/trunk/lib/Format/ContinuationIndenter.cpp
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/lib/Format/UnwrappedLineParser.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=221103&r1=221102&r2=221103&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Sun Nov 2 13:16:41 2014
@@ -503,6 +503,13 @@ unsigned ContinuationIndenter::getNewLin
const FormatToken *NextNonComment = Previous.getNextNonComment();
if (!NextNonComment)
NextNonComment = &Current;
+
+ // Java specific bits.
+ if (Style.Language == FormatStyle::LK_Java && Current.is(tok::identifier) &&
+ (Current.TokenText == "implements" || Current.TokenText == "extends"))
+ return std::max(State.Stack.back().LastSpace,
+ State.Stack.back().Indent + Style.ContinuationIndentWidth);
+
if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block)
return Current.NestingLevel == 0 ? State.FirstIndent
: State.Stack.back().Indent;
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=221103&r1=221102&r2=221103&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Sun Nov 2 13:16:41 2014
@@ -1064,7 +1064,8 @@ static int PrecedenceArrowAndPeriod = pr
/// operator precedence.
class ExpressionParser {
public:
- ExpressionParser(AnnotatedLine &Line) : Current(Line.First) {}
+ ExpressionParser(const FormatStyle &Style, AnnotatedLine &Line)
+ : Style(Style), Current(Line.First) {}
/// \brief Parse expressions with the given operatore precedence.
void parse(int Precedence = 0) {
@@ -1167,6 +1168,11 @@ private:
return Current->getPrecedence();
else if (Current->isOneOf(tok::period, tok::arrow))
return PrecedenceArrowAndPeriod;
+ else if (Style.Language == FormatStyle::LK_Java &&
+ Current->is(tok::identifier) &&
+ (Current->TokenText == "extends" ||
+ Current->TokenText == "implements"))
+ return 0;
}
return -1;
}
@@ -1224,6 +1230,7 @@ private:
Current = Current->Next;
}
+ const FormatStyle &Style;
FormatToken *Current;
};
@@ -1256,7 +1263,7 @@ void TokenAnnotator::annotate(AnnotatedL
if (Line.Type == LT_Invalid)
return;
- ExpressionParser ExprParser(Line);
+ ExpressionParser ExprParser(Style, Line);
ExprParser.parse();
if (Line.First->Type == TT_ObjCMethodSpecifier)
@@ -1837,7 +1844,9 @@ bool TokenAnnotator::canBreakBefore(cons
const FormatToken &Left = *Right.Previous;
if (Style.Language == FormatStyle::LK_Java) {
- if (Left.is(tok::identifier) && Left.TokenText == "throws")
+ if (Left.is(tok::identifier) &&
+ (Left.TokenText == "throws" || Left.TokenText == "extends" ||
+ Left.TokenText == "implements"))
return false;
}
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=221103&r1=221102&r2=221103&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Sun Nov 2 13:16:41 2014
@@ -1375,10 +1375,10 @@ void UnwrappedLineParser::parseRecord()
}
// The actual identifier can be a nested name specifier, and in macros
// it is often token-pasted.
- while (
- FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
- FormatTok->is(tok::hashhash) ||
- (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::period)))
+ while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
+ FormatTok->is(tok::hashhash) ||
+ (Style.Language == FormatStyle::LK_Java &&
+ FormatTok->isOneOf(tok::period, tok::comma)))
nextToken();
// Note that parsing away template declarations here leads to incorrectly
Modified: cfe/trunk/unittests/Format/FormatTestJava.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJava.cpp?rev=221103&r1=221102&r2=221103&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJava.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJava.cpp Sun Nov 2 13:16:41 2014
@@ -37,6 +37,12 @@ protected:
return format(Code, 0, Code.size(), Style);
}
+ static FormatStyle getStyleWithColumns(unsigned ColumnLimit) {
+ FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java);
+ Style.ColumnLimit = ColumnLimit;
+ return Style;
+ }
+
static void verifyFormat(
llvm::StringRef Code,
const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
@@ -58,6 +64,19 @@ TEST_F(FormatTestJava, ClassDeclarations
" }\n"
"}");
verifyFormat("public class A extends B.C {}");
+
+ verifyFormat("abstract class SomeClass extends SomeOtherClass\n"
+ " implements SomeInterface {}",
+ getStyleWithColumns(60));
+ verifyFormat("abstract class SomeClass\n"
+ " extends SomeOtherClass\n"
+ " implements SomeInterface {}",
+ getStyleWithColumns(40));
+ verifyFormat("abstract class SomeClass\n"
+ " extends SomeOtherClass\n"
+ " implements SomeInterface,\n"
+ " AnotherInterface {}",
+ getStyleWithColumns(40));
}
TEST_F(FormatTestJava, ThrowsDeclarations) {
More information about the cfe-commits
mailing list