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;
/// 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) {
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;
}
Current = Current->Next;
}
+ const FormatStyle &Style;
FormatToken *Current;
};
if (Line.Type == LT_Invalid)
return;
- ExpressionParser ExprParser(Line);
+ ExpressionParser ExprParser(Style, Line);
ExprParser.parse();
if (Line.First->Type == TT_ObjCMethodSpecifier)
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;
}
}
// 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
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)) {
" }\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) {