diff options
author | zhvv117 <[email protected]> | 2024-11-26 23:03:38 +0300 |
---|---|---|
committer | zhvv117 <[email protected]> | 2024-11-26 23:13:41 +0300 |
commit | 40f133ee2bbedc81d314e9804f0a39910489a3e6 (patch) | |
tree | e9a197896c5dca30340283149abcbbbc908b1a90 /yql/essentials/sql/v1/format/sql_format.cpp | |
parent | 60419ef1ed276c3a1f4269720382826a2b85e912 (diff) |
YQL-formatter improvements (part 1)
Список изменений:
1. Не делается перенос строки перед `WITH` с одним хинтом (например, перед `WITH TRUNCATE`).
2. Бинарные операторы ставятся в начале, а не конце строки.
3. Убрана лишняя пустая строка в конце файла.
4. Внутри выражений меняем `=` на `==` и `<>` на `!=`.
5. Улучшено форматирование комментариев.
6. Перечисление символов в `EXPORT` записывается в виде вертикально развёрнутого списка вместо одной длинной строки.
7. Убран отступ для блока `WITHOUT`.
commit_hash:a407628a023fbd1b9eb98eb41ac92b1ca1593298
Diffstat (limited to 'yql/essentials/sql/v1/format/sql_format.cpp')
-rw-r--r-- | yql/essentials/sql/v1/format/sql_format.cpp | 296 |
1 files changed, 136 insertions, 160 deletions
diff --git a/yql/essentials/sql/v1/format/sql_format.cpp b/yql/essentials/sql/v1/format/sql_format.cpp index 3cc18b141bb..7422b37a938 100644 --- a/yql/essentials/sql/v1/format/sql_format.cpp +++ b/yql/essentials/sql/v1/format/sql_format.cpp @@ -43,6 +43,17 @@ TTokenIterator SkipWSOrComment(TTokenIterator curr, TTokenIterator end) { return curr; } +TParsedToken TransformTokenForValidate(TParsedToken token) { + if (token.Name == "EQUALS2") { + token.Name = "EQUALS"; + token.Content = "="; + } else if (token.Name == "NOT_EQUALS2") { + token.Name = "NOT_EQUALS"; + token.Content = "!="; + } + return token; +} + bool Validate(const TParsedTokenList& query, const TParsedTokenList& formattedQuery) { auto in = query.begin(); auto out = formattedQuery.begin(); @@ -53,15 +64,17 @@ bool Validate(const TParsedTokenList& query, const TParsedTokenList& formattedQu in = SkipWS(in, inEnd); out = SkipWS(out, outEnd); if (in != inEnd && out != outEnd) { - if (in->Name != out->Name) { + auto inToken = TransformTokenForValidate(*in); + auto outToken = TransformTokenForValidate(*out); + if (inToken.Name != outToken.Name) { return false; } - if (IsProbablyKeyword(*in)) { - if (!AsciiEqualsIgnoreCase(in->Content, out->Content)) { + if (IsProbablyKeyword(inToken)) { + if (!AsciiEqualsIgnoreCase(inToken.Content, outToken.Content)) { return false; } } else { - if (in->Content != out->Content) { + if (inToken.Content != outToken.Content) { return false; } } @@ -456,14 +469,17 @@ public: MarkTokens(msg); Y_ENSURE(MarkTokenStack.empty()); Y_ENSURE(TokenIndex == ParsedTokens.size()); + TokenIndex = 0; Visit(msg); Y_ENSURE(TokenIndex == ParsedTokens.size()); Y_ENSURE(MarkTokenStack.empty()); + for (; LastComment < Comments.size(); ++LastComment) { const auto text = Comments[LastComment].Content; AddComment(text); } + addLine = AddLine.GetOrElse(true); return SB; @@ -508,17 +524,34 @@ private: } void NewLine() { + if (TokenIndex >= ParsedTokens.size() || ParsedTokens[TokenIndex].Line > LastLine) { + WriteComments(true); + } + if (OutColumn) { Out('\n'); } } void AddComment(TStringBuf text) { - if (text.StartsWith("--") && !SB.empty() && SB.back() == '-') { + if (!AfterComment && OutLine > BlockFirstLine && OutColumn == 0) { + Out('\n'); + } + AfterComment = true; + + if (!SB.empty() && SB.back() != ' ' && SB.back() != '\n') { Out(' '); } Out(text); + + if (!text.StartsWith("--") && + TokenIndex < ParsedTokens.size() && + Comments[LastComment].Line < ParsedTokens[TokenIndex].Line && + (LastComment + 1 >= Comments.size() || Comments[LastComment].Line < Comments[LastComment + 1].Line) + ) { + Out('\n'); + } } void MarkTokens(const NProtoBuf::Message& msg) { @@ -753,7 +786,6 @@ private: } void VisitPragma(const TRule_pragma_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitKeyword(msg.GetToken1()); auto prefix = msg.GetRule_opt_id_prefix_or_type2(); @@ -776,44 +808,7 @@ private: } } - void PosFromPartial(const TRule_select_kind_partial& partial) { - const auto& kind = partial.GetRule_select_kind1(); - if (kind.HasBlock1()) { // DISCARD - PosFromToken(kind.GetBlock1().GetToken1()); - } else { - switch (kind.GetBlock2().Alt_case()) { - case TRule_select_kind_TBlock2::kAlt1: - PosFromToken(kind.GetBlock2().GetAlt1().GetRule_process_core1().GetToken1()); - break; - case TRule_select_kind_TBlock2::kAlt2: - PosFromToken(kind.GetBlock2().GetAlt2().GetRule_reduce_core1().GetToken1()); - break; - case TRule_select_kind_TBlock2::kAlt3: { - const auto& selCore = kind.GetBlock2().GetAlt3().GetRule_select_core1(); - if (selCore.HasBlock1()) { - PosFromToken(selCore.GetBlock1().GetToken1()); - } else { - PosFromToken(selCore.GetToken2()); - } - - break; - } - - default: - ythrow yexception() << "Alt is not supported"; - } - } - } - void VisitSelect(const TRule_select_stmt& msg) { - const auto& paren = msg.GetRule_select_kind_parenthesis1(); - if (paren.Alt_case() == TRule_select_kind_parenthesis::kAltSelectKindParenthesis1) { - const auto& partial = paren.GetAlt_select_kind_parenthesis1().GetRule_select_kind_partial1(); - PosFromPartial(partial); - } else { - PosFromToken(paren.GetAlt_select_kind_parenthesis2().GetToken1()); - } - NewLine(); Visit(msg.GetRule_select_kind_parenthesis1()); for (const auto& block : msg.GetBlock2()) { @@ -825,8 +820,6 @@ private: } void VisitSelectUnparenthesized(const TRule_select_unparenthesized_stmt& msg) { - const auto& partial = msg.GetRule_select_kind_partial1(); - PosFromPartial(partial); NewLine(); Visit(msg.GetRule_select_kind_partial1()); for (const auto& block : msg.GetBlock2()) { @@ -838,10 +831,11 @@ private: } void VisitNamedNodes(const TRule_named_nodes_stmt& msg) { - PosFromToken(msg.GetRule_bind_parameter_list1().GetRule_bind_parameter1().GetToken1()); NewLine(); Visit(msg.GetRule_bind_parameter_list1()); Visit(msg.GetToken2()); + ExprLineIndent = CurrentIndent; + switch (msg.GetBlock3().Alt_case()) { case TRule_named_nodes_stmt::TBlock3::kAlt1: { const auto& alt = msg.GetBlock3().GetAlt1(); @@ -884,10 +878,11 @@ private: default: ythrow yexception() << "Alt is not supported"; } + + ExprLineIndent = 0; } void VisitCreateTable(const TRule_create_table_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); Visit(msg.GetToken1()); Visit(msg.GetBlock2()); @@ -933,31 +928,26 @@ private: } void VisitDropTable(const TRule_drop_table_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_drop_table_stmt::GetDescriptor(), msg); } void VisitAnalyze(const TRule_analyze_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_analyze_stmt::GetDescriptor(), msg); } void VisitBackup(const TRule_backup_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_backup_stmt::GetDescriptor(), msg); } void VisitRestore(const TRule_restore_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_restore_stmt::GetDescriptor(), msg); } void VisitUse(const TRule_use_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_use_stmt::GetDescriptor(), msg); } @@ -969,41 +959,16 @@ private: } void VisitIntoTable(const TRule_into_table_stmt& msg) { - switch (msg.GetBlock1().Alt_case()) { - case TRule_into_table_stmt_TBlock1::AltCase::kAlt1: - PosFromToken(msg.GetBlock1().GetAlt1().GetToken1()); - break; - case TRule_into_table_stmt_TBlock1::AltCase::kAlt2: - PosFromToken(msg.GetBlock1().GetAlt2().GetToken1()); - break; - case TRule_into_table_stmt_TBlock1::AltCase::kAlt3: - PosFromToken(msg.GetBlock1().GetAlt3().GetToken1()); - break; - case TRule_into_table_stmt_TBlock1::AltCase::kAlt4: - PosFromToken(msg.GetBlock1().GetAlt4().GetToken1()); - break; - case TRule_into_table_stmt_TBlock1::AltCase::kAlt5: - PosFromToken(msg.GetBlock1().GetAlt5().GetToken1()); - break; - case TRule_into_table_stmt_TBlock1::AltCase::kAlt6: - PosFromToken(msg.GetBlock1().GetAlt6().GetToken1()); - break; - default: - ythrow yexception() << "Alt is not supported"; - } - NewLine(); VisitAllFields(TRule_into_table_stmt::GetDescriptor(), msg); } void VisitCommit(const TRule_commit_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_commit_stmt::GetDescriptor(), msg); } void VisitUpdate(const TRule_update_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); Visit(msg.GetToken1()); Visit(msg.GetRule_simple_table_ref2()); @@ -1103,7 +1068,6 @@ private: } void VisitDelete(const TRule_delete_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); Visit(msg.GetToken1()); Visit(msg.GetToken2()); @@ -1129,31 +1093,39 @@ private: } void VisitRollback(const TRule_rollback_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_rollback_stmt::GetDescriptor(), msg); } void VisitDeclare(const TRule_declare_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_declare_stmt::GetDescriptor(), msg); } void VisitImport(const TRule_import_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_import_stmt::GetDescriptor(), msg); } void VisitExport(const TRule_export_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); - VisitAllFields(TRule_export_stmt::GetDescriptor(), msg); + VisitKeyword(msg.GetToken1()); + + NewLine(); + PushCurrentIndent(); + + const auto& list = msg.GetRule_bind_parameter_list2(); + Visit(list.GetRule_bind_parameter1()); + for (auto& b : list.GetBlock2()) { + Visit(b.GetToken1()); + NewLine(); + Visit(b.GetRule_bind_parameter2()); + } + + PopCurrentIndent(); } void VisitAlterTable(const TRule_alter_table_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitKeyword(msg.GetToken1()); VisitKeyword(msg.GetToken2()); @@ -1171,13 +1143,11 @@ private: } void VisitAlterTableStore(const TRule_alter_table_store_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_alter_table_store_stmt::GetDescriptor(), msg); } void VisitAlterExternalTable(const TRule_alter_external_table_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitKeyword(msg.GetToken1()); VisitKeyword(msg.GetToken2()); @@ -1196,7 +1166,6 @@ private: } void VisitDo(const TRule_do_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitKeyword(msg.GetToken1()); switch (msg.GetBlock2().Alt_case()) { @@ -1230,7 +1199,6 @@ private: } void VisitAction(const TRule_define_action_or_subquery_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitKeyword(msg.GetToken1()); VisitKeyword(msg.GetToken2()); @@ -1253,12 +1221,6 @@ private: } void VisitIf(const TRule_if_stmt& msg) { - if (msg.HasBlock1()) { - PosFromToken(msg.GetBlock1().GetToken1()); - } else { - PosFromToken(msg.GetToken2()); - } - NewLine(); if (msg.HasBlock1()) { Visit(msg.GetBlock1()); @@ -1281,14 +1243,6 @@ private: } void VisitFor(const TRule_for_stmt& msg) { - if (msg.HasBlock1()) { - PosFromToken(msg.GetBlock1().GetToken1()); - } else if (msg.HasBlock2()) { - PosFromToken(msg.GetBlock2().GetToken1()); - } else { - PosFromToken(msg.GetToken3()); - } - NewLine(); if (msg.HasBlock1()) { Visit(msg.GetBlock1()); @@ -1317,7 +1271,6 @@ private: } void VisitValues(const TRule_values_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitKeyword(msg.GetToken1()); const auto& rowList = msg.GetRule_values_source_row_list2(); @@ -1334,73 +1287,61 @@ private: } void VisitGrantPermissions(const TRule_grant_permissions_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_grant_permissions_stmt::GetDescriptor(), msg); } void VisitRevokePermissions(const TRule_revoke_permissions_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_revoke_permissions_stmt::GetDescriptor(), msg); } void VisitCreateUser(const TRule_create_user_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_create_user_stmt::GetDescriptor(), msg); } void VisitAlterUser(const TRule_alter_user_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_alter_user_stmt::GetDescriptor(), msg); } void VisitCreateGroup(const TRule_create_group_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_create_group_stmt::GetDescriptor(), msg); } void VisitAlterGroup(const TRule_alter_group_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_alter_group_stmt::GetDescriptor(), msg); } void VisitDropRole(const TRule_drop_role_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_drop_role_stmt::GetDescriptor(), msg); } void VisitUpsertObject(const TRule_upsert_object_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_upsert_object_stmt::GetDescriptor(), msg); } void VisitCreateObject(const TRule_create_object_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_create_object_stmt::GetDescriptor(), msg); } void VisitAlterObject(const TRule_alter_object_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_alter_object_stmt::GetDescriptor(), msg); } void VisitDropObject(const TRule_drop_object_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_drop_object_stmt::GetDescriptor(), msg); } void VisitCreateTopic(const TRule_create_topic_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitKeyword(msg.GetToken1()); VisitKeyword(msg.GetToken2()); @@ -1435,7 +1376,6 @@ private: } void VisitAlterTopic(const TRule_alter_topic_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitKeyword(msg.GetToken1()); VisitKeyword(msg.GetToken2()); @@ -1454,19 +1394,16 @@ private: } void VisitDropTopic(const TRule_drop_topic_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_drop_topic_stmt::GetDescriptor(), msg); } void VisitCreateExternalDataSource(const TRule_create_external_data_source_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_create_external_data_source_stmt::GetDescriptor(), msg); } void VisitAlterExternalDataSource(const TRule_alter_external_data_source_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitToken(msg.GetToken1()); VisitToken(msg.GetToken2()); @@ -1487,49 +1424,41 @@ private: } void VisitDropExternalDataSource(const TRule_drop_external_data_source_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_drop_external_data_source_stmt::GetDescriptor(), msg); } void VisitCreateView(const TRule_create_view_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_create_view_stmt::GetDescriptor(), msg); } void VisitDropView(const TRule_drop_view_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_drop_view_stmt::GetDescriptor(), msg); } void VisitCreateAsyncReplication(const TRule_create_replication_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_create_replication_stmt::GetDescriptor(), msg); } void VisitAlterAsyncReplication(const TRule_alter_replication_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_alter_replication_stmt::GetDescriptor(), msg); } void VisitDropAsyncReplication(const TRule_drop_replication_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_drop_replication_stmt::GetDescriptor(), msg); } void VisitCreateResourcePool(const TRule_create_resource_pool_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_create_resource_pool_stmt::GetDescriptor(), msg); } void VisitAlterResourcePool(const TRule_alter_resource_pool_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitToken(msg.GetToken1()); VisitToken(msg.GetToken2()); @@ -1549,19 +1478,16 @@ private: } void VisitDropResourcePool(const TRule_drop_resource_pool_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_drop_resource_pool_stmt::GetDescriptor(), msg); } void VisitCreateBackupCollection(const TRule_create_backup_collection_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_create_backup_collection_stmt::GetDescriptor(), msg); } void VisitAlterBackupCollection(const TRule_alter_backup_collection_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitToken(msg.GetToken1()); Visit(msg.GetRule_backup_collection2()); @@ -1595,19 +1521,16 @@ private: } void VisitDropBackupCollection(const TRule_drop_backup_collection_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_drop_backup_collection_stmt::GetDescriptor(), msg); } void VisitCreateResourcePoolClassifier(const TRule_create_resource_pool_classifier_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_create_resource_pool_classifier_stmt::GetDescriptor(), msg); } void VisitAlterResourcePoolClassifier(const TRule_alter_resource_pool_classifier_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitToken(msg.GetToken1()); VisitToken(msg.GetToken2()); @@ -1628,7 +1551,6 @@ private: } void VisitDropResourcePoolClassifier(const TRule_drop_resource_pool_classifier_stmt& msg) { - PosFromToken(msg.GetToken1()); NewLine(); VisitAllFields(TRule_drop_resource_pool_classifier_stmt::GetDescriptor(), msg); } @@ -1637,10 +1559,10 @@ private: VisitAllFieldsImpl<TPrettyVisitor, &TPrettyVisitor::Visit>(this, descr, msg); } - void WriteComments() { + void WriteComments(bool completeLine) { while (LastComment < Comments.size()) { const auto& c = Comments[LastComment]; - if (c.Line > LastLine || c.Line == LastLine && c.LinePos > LastColumn) { + if (c.Line > LastLine || !completeLine && c.Line == LastLine && c.LinePos > LastColumn) { break; } @@ -1652,13 +1574,7 @@ private: void PosFromToken(const TToken& token) { LastLine = token.GetLine(); LastColumn = token.GetColumn(); - WriteComments(); - } - - void PosFromParsedToken(const TParsedToken& token) { - LastLine = token.Line; - LastColumn = token.LinePos; - WriteComments(); + WriteComments(false); } void VisitToken(const TToken& token) { @@ -1713,6 +1629,7 @@ private: AfterQuestion = (str == "?"); AfterLess = (str == "<"); AfterKeyExpr = false; + AfterComment = false; if (forceKeyword) { str = to_upper(str); @@ -1737,9 +1654,22 @@ private: MarkTokenStack.pop_back(); } + if (InCondExpr) { + if (str == "=") { + str = "=="; + } else if (str == "<>") { + str = "!="; + } + } + Out(str); + + if (TokenIndex + 1 >= ParsedTokens.size() || ParsedTokens[TokenIndex + 1].Line > LastLine) { + WriteComments(true); + } + if (str == ";") { - Out('\n'); + NewLine(); } if (markedInfo.OpeningBracket) { @@ -1941,12 +1871,13 @@ private: Visit(msg.GetBlock7()); } + PopCurrentIndent(); + if (msg.HasBlock8()) { NewLine(); Visit(msg.GetBlock8()); } - PopCurrentIndent(); if (msg.HasBlock9()) { NewLine(); Visit(msg.GetBlock9()); @@ -2082,13 +2013,37 @@ private: } } + void VisitTableHints(const TRule_table_hints& msg) { + Visit(msg.GetToken1()); + const auto& block2 = msg.GetBlock2(); + if (block2.Alt_case() == TRule_table_hints::TBlock2::kAlt2) { + const auto& alt = block2.GetAlt2(); + + Visit(alt.GetToken1()); + + NewLine(); + PushCurrentIndent(); + + Visit(alt.GetRule_table_hint2()); + for (const auto& block : alt.GetBlock3()) { + Visit(block.GetToken1()); + NewLine(); + Visit(block.GetRule_table_hint2()); + } + + NewLine(); + PopCurrentIndent(); + + Visit(alt.GetToken4()); + } else { + Visit(block2); + } + } + void VisitSimpleTableRef(const TRule_simple_table_ref& msg) { Visit(msg.GetRule_simple_table_ref_core1()); if (msg.HasBlock2()) { - NewLine(); - PushCurrentIndent(); Visit(msg.GetBlock2()); - PopCurrentIndent(); } } @@ -2357,9 +2312,13 @@ private: } Visit(msg.GetToken3()); + ExprLineIndent = CurrentIndent; + Visit(msg.GetRule_expr4()); VisitRepeated(msg.GetBlock5()); + ExprLineIndent = 0; + PopCurrentIndent(); NewLine(); } @@ -2572,6 +2531,19 @@ private: VisitBinaryOp(orExpr.GetRule_or_subexpr1(), getOp, getExpr, orExpr.GetBlock2().begin(), orExpr.GetBlock2().end()); } + void VisitCondExpr(const TRule_cond_expr& msg) { + if (msg.Alt_case() == TRule_cond_expr::kAltCondExpr5) { + for (const auto& block : msg.GetAlt_cond_expr5().GetBlock1()) { + InCondExpr = true; + Visit(block.GetBlock1()); + InCondExpr = false; + Visit(block.GetRule_eq_subexpr2()); + } + } else { + VisitAllFields(TRule_cond_expr::GetDescriptor(), msg); + } + } + void VisitOrSubexpr(const TRule_or_subexpr& msg) { auto getExpr = [](const TRule_or_subexpr::TBlock2& b) -> const TRule_and_subexpr& { return b.GetRule_and_subexpr2(); }; auto getOp = [](const TRule_or_subexpr::TBlock2& b) -> const TToken& { return b.GetToken1(); }; @@ -2609,7 +2581,6 @@ private: const bool hasSecondNewline = ParsedTokens[TokenIndex].Line != ParsedTokens[TokenIndex + 2].Line; const ui32 currentOutLine = OutLine; - PosFromParsedToken(ParsedTokens[TokenIndex]); if (currentOutLine != OutLine || (hasFirstNewline && hasSecondNewline)) { NewLine(); if (!pushedIndent) { @@ -2619,7 +2590,6 @@ private: } Visit(alt.GetRule_double_question1()); - PosFromParsedToken(ParsedTokens[TokenIndex]); if (hasFirstNewline || hasSecondNewline) { NewLine(); if (!pushedIndent) { @@ -2701,8 +2671,8 @@ private: template <typename TExpr, typename TGetOp, typename TGetExpr, typename TIter> void VisitBinaryOp(const TExpr& expr, TGetOp getOp, TGetExpr getExpr, TIter begin, TIter end) { Visit(expr); - bool pushedIndent = false; + bool pushedIndent = false; for (; begin != end; ++begin) { const auto op = getOp(*begin); const auto opSize = BinaryOpTokenSize(op); @@ -2710,23 +2680,18 @@ private: const bool hasSecondNewline = ParsedTokens[TokenIndex].Line != ParsedTokens[TokenIndex + opSize].Line; const ui32 currentOutLine = OutLine; - PosFromParsedToken(ParsedTokens[TokenIndex]); - if (currentOutLine != OutLine || (hasFirstNewline && hasSecondNewline)) { + if (currentOutLine != OutLine || hasFirstNewline || hasSecondNewline) { NewLine(); - if (!pushedIndent) { + if (!pushedIndent && CurrentIndent == ExprLineIndent) { PushCurrentIndent(); pushedIndent = true; } } + Visit(op); - PosFromParsedToken(ParsedTokens[TokenIndex]); - if (hasFirstNewline || hasSecondNewline) { + if (hasFirstNewline && hasSecondNewline) { NewLine(); - if (!pushedIndent) { - PushCurrentIndent(); - pushedIndent = true; - } } Visit(getExpr(*begin)); @@ -2739,6 +2704,11 @@ private: void PushCurrentIndent() { CurrentIndent += OneIndent; + + BlockFirstLine = OutLine; + if (OutColumn > 0) { + ++BlockFirstLine; + } } void PopCurrentIndent() { @@ -2770,9 +2740,13 @@ private: bool AfterQuestion = false; bool AfterLess = false; bool AfterKeyExpr = false; + bool AfterComment = false; bool InMultiTokenOp = false; + bool InCondExpr = false; ui32 ForceExpandedLine = 0; ui32 ForceExpandedColumn = 0; + ui32 BlockFirstLine = 1; + i32 ExprLineIndent = 0; ui32 TokenIndex = 0; TMarkTokenStack MarkTokenStack; @@ -2826,6 +2800,7 @@ TStaticData::TStaticData() {TRule_single_source::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSingleSource)}, {TRule_flatten_source::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitFlattenSource)}, {TRule_named_single_source::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitNamedSingleSource)}, + {TRule_table_hints::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitTableHints)}, {TRule_simple_table_ref::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSimpleTableRef)}, {TRule_into_simple_table_ref::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitIntoSimpleTableRef)}, {TRule_select_kind_partial::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectKindPartial)}, @@ -2853,6 +2828,7 @@ TStaticData::TStaticData() {TRule_ttl_tier_action::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitTtlTierAction)}, {TRule_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitExpr)}, + {TRule_cond_expr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCondExpr)}, {TRule_or_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitOrSubexpr)}, {TRule_and_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitAndSubexpr)}, {TRule_eq_subexpr::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitEqSubexpr)}, |