Fix some trailing whitespaces in query format

The following statements still has the trailing whitespace:
- WITH
- SELECT
- SELECT DISTINCT
- ARRAY JOIN
- GROUP BY
- ORDER BY
- LIMIT BY
This commit is contained in:
Azat Khuzhin 2020-09-08 23:34:10 +03:00
parent 4f1321daef
commit 014c7c02bd
5 changed files with 22 additions and 6 deletions

View File

@ -13,6 +13,9 @@ ASTPtr ASTExpressionList::clone() const
void ASTExpressionList::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const void ASTExpressionList::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
{ {
if (frame.expression_list_prepend_whitespace)
settings.ostr << ' ';
for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it) for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it)
{ {
if (it != children.begin()) if (it != children.begin())
@ -30,6 +33,12 @@ void ASTExpressionList::formatImplMultiline(const FormatSettings & settings, For
{ {
std::string indent_str = "\n" + std::string(4 * (frame.indent + 1), ' '); std::string indent_str = "\n" + std::string(4 * (frame.indent + 1), ' ');
if (frame.expression_list_prepend_whitespace)
{
if (!(children.size() > 1 || frame.expression_list_always_start_on_new_line))
settings.ostr << ' ';
}
++frame.indent; ++frame.indent;
for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it) for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it)
{ {

View File

@ -114,6 +114,7 @@ static bool highlightStringLiteralWithMetacharacters(const ASTPtr & node, const
void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
{ {
frame.expression_list_prepend_whitespace = false;
FormatStateStacked nested_need_parens = frame; FormatStateStacked nested_need_parens = frame;
FormatStateStacked nested_dont_need_parens = frame; FormatStateStacked nested_dont_need_parens = frame;
nested_need_parens.need_parens = true; nested_need_parens.need_parens = true;

View File

@ -72,18 +72,20 @@ void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, F
{ {
frame.current_select = this; frame.current_select = this;
frame.need_parens = false; frame.need_parens = false;
frame.expression_list_prepend_whitespace = true;
std::string indent_str = s.one_line ? "" : std::string(4 * frame.indent, ' '); std::string indent_str = s.one_line ? "" : std::string(4 * frame.indent, ' ');
if (with()) if (with())
{ {
s.ostr << (s.hilite ? hilite_keyword : "") << indent_str << "WITH " << (s.hilite ? hilite_none : ""); s.ostr << (s.hilite ? hilite_keyword : "") << indent_str << "WITH" << (s.hilite ? hilite_none : "");
s.one_line s.one_line
? with()->formatImpl(s, state, frame) ? with()->formatImpl(s, state, frame)
: with()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame); : with()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame);
s.ostr << s.nl_or_ws; s.ostr << s.nl_or_ws;
} }
s.ostr << (s.hilite ? hilite_keyword : "") << indent_str << "SELECT " << (distinct ? "DISTINCT " : "") << (s.hilite ? hilite_none : ""); s.ostr << (s.hilite ? hilite_keyword : "") << indent_str << "SELECT" << (distinct ? " DISTINCT" : "") << (s.hilite ? hilite_none : "");
s.one_line s.one_line
? select()->formatImpl(s, state, frame) ? select()->formatImpl(s, state, frame)
@ -109,7 +111,7 @@ void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, F
if (groupBy()) if (groupBy())
{ {
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "GROUP BY " << (s.hilite ? hilite_none : ""); s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "GROUP BY" << (s.hilite ? hilite_none : "");
s.one_line s.one_line
? groupBy()->formatImpl(s, state, frame) ? groupBy()->formatImpl(s, state, frame)
: groupBy()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame); : groupBy()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame);
@ -132,7 +134,7 @@ void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, F
if (orderBy()) if (orderBy())
{ {
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "ORDER BY " << (s.hilite ? hilite_none : ""); s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "ORDER BY" << (s.hilite ? hilite_none : "");
s.one_line s.one_line
? orderBy()->formatImpl(s, state, frame) ? orderBy()->formatImpl(s, state, frame)
: orderBy()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame); : orderBy()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame);
@ -147,7 +149,7 @@ void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, F
s.ostr << ", "; s.ostr << ", ";
} }
limitByLength()->formatImpl(s, state, frame); limitByLength()->formatImpl(s, state, frame);
s.ostr << (s.hilite ? hilite_keyword : "") << " BY " << (s.hilite ? hilite_none : ""); s.ostr << (s.hilite ? hilite_keyword : "") << " BY" << (s.hilite ? hilite_none : "");
s.one_line s.one_line
? limitBy()->formatImpl(s, state, frame) ? limitBy()->formatImpl(s, state, frame)
: limitBy()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame); : limitBy()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame);

View File

@ -210,6 +210,7 @@ void ASTTableJoin::formatImplBeforeTable(const FormatSettings & settings, Format
void ASTTableJoin::formatImplAfterTable(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const void ASTTableJoin::formatImplAfterTable(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
{ {
frame.need_parens = false; frame.need_parens = false;
frame.expression_list_prepend_whitespace = false;
if (using_expression_list) if (using_expression_list)
{ {
@ -236,8 +237,10 @@ void ASTTableJoin::formatImpl(const FormatSettings & settings, FormatState & sta
void ASTArrayJoin::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const void ASTArrayJoin::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
{ {
frame.expression_list_prepend_whitespace = true;
settings.ostr << (settings.hilite ? hilite_keyword : "") settings.ostr << (settings.hilite ? hilite_keyword : "")
<< (kind == Kind::Left ? "LEFT " : "") << "ARRAY JOIN " << (settings.hilite ? hilite_none : ""); << (kind == Kind::Left ? "LEFT " : "") << "ARRAY JOIN" << (settings.hilite ? hilite_none : "");
settings.one_line settings.one_line
? expression_list->formatImpl(settings, state, frame) ? expression_list->formatImpl(settings, state, frame)

View File

@ -203,6 +203,7 @@ public:
UInt8 indent = 0; UInt8 indent = 0;
bool need_parens = false; bool need_parens = false;
bool expression_list_always_start_on_new_line = false; /// Line feed and indent before expression list even if it's of single element. bool expression_list_always_start_on_new_line = false; /// Line feed and indent before expression list even if it's of single element.
bool expression_list_prepend_whitespace = false; /// Prepend whitespace (if it is required)
const IAST * current_select = nullptr; const IAST * current_select = nullptr;
}; };