diff options
Diffstat (limited to 'Plugins/SqlEnterpriseFormatter/formatstatement.h')
| -rw-r--r-- | Plugins/SqlEnterpriseFormatter/formatstatement.h | 134 |
1 files changed, 78 insertions, 56 deletions
diff --git a/Plugins/SqlEnterpriseFormatter/formatstatement.h b/Plugins/SqlEnterpriseFormatter/formatstatement.h index 6bd0fea..427efe9 100644 --- a/Plugins/SqlEnterpriseFormatter/formatstatement.h +++ b/Plugins/SqlEnterpriseFormatter/formatstatement.h @@ -16,6 +16,58 @@ class FormatStatement { public: + struct FormatToken + { + enum Type + { + KEYWORD, + LINED_UP_KEYWORD, + ID, + STRING_OR_ID, + OPERATOR, + STAR, + FLOAT, + STRING, + INTEGER, + BLOB, + BIND_PARAM, + ID_DOT, + PAR_DEF_LEFT, + PAR_DEF_RIGHT, + PAR_EXPR_LEFT, + PAR_EXPR_RIGHT, + PAR_FUNC_LEFT, + PAR_FUNC_RIGHT, + SEMICOLON, + COMMA_LIST, + COMMA_OPER, // for example in LIMIT + FUNC_ID, + DATA_TYPE, + NEW_LINE, + INDENT_MARKER, + INCR_INDENT, + SET_INDENT, + DECR_INDENT, + MARK_KEYWORD_LINEUP + }; + + enum Flag + { + NO_FLAG = 0x00, + NO_SPACE_BEFORE = 0x01, + NO_SPACE_AFTER = 0x02, + NO_NEWLINE_BEFORE = 0x04, + NO_NEWLINE_AFTER = 0x08 + }; + + Q_DECLARE_FLAGS(Flags, Flag) + + Type type; + QVariant value; + QVariant additionalValue; + Flags flags; + }; + enum class ListSeparator { NONE, @@ -40,24 +92,24 @@ class FormatStatement FormatStatement& withLinedUpKeyword(const QString& kw, const QString& lineUpName = QString()); FormatStatement& withId(const QString& id); FormatStatement& withIdList(const QStringList& names, const QString& indentName = QString(), ListSeparator sep = ListSeparator::COMMA); - FormatStatement& withOperator(const QString& oper); + FormatStatement& withOperator(const QString& oper, FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); FormatStatement& withStringOrId(const QString& id); - FormatStatement& withIdDot(); - FormatStatement& withStar(); + FormatStatement& withIdDot(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); + FormatStatement& withStar(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); FormatStatement& withFloat(double value); FormatStatement& withInteger(qint64 value); FormatStatement& withString(const QString& value); FormatStatement& withBlob(const QString& value); FormatStatement& withBindParam(const QString& name); - FormatStatement& withParDefLeft(); - FormatStatement& withParDefRight(); - FormatStatement& withParExprLeft(); - FormatStatement& withParExprRight(); - FormatStatement& withParFuncLeft(); - FormatStatement& withParFuncRight(); - FormatStatement& withSemicolon(); - FormatStatement& withListComma(); - FormatStatement& withCommaOper(); + FormatStatement& withParDefLeft(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); + FormatStatement& withParDefRight(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); + FormatStatement& withParExprLeft(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); + FormatStatement& withParExprRight(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); + FormatStatement& withParFuncLeft(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); + FormatStatement& withParFuncRight(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); + FormatStatement& withSemicolon(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); + FormatStatement& withListComma(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); + FormatStatement& withCommaOper(FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); FormatStatement& withSortOrder(SqliteSortOrder sortOrder); FormatStatement& withConflict(SqliteConflictAlgo onConflict); FormatStatement& withFuncId(const QString& func); @@ -71,11 +123,17 @@ class FormatStatement FormatStatement& withIncrIndent(const QString& name = QString()); FormatStatement& withDecrIndent(); FormatStatement& markKeywordLineUp(const QString& keyword, const QString& lineUpName = QString()); - FormatStatement& withSeparator(ListSeparator sep); + FormatStatement& withSeparator(ListSeparator sep, FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); + + template <class T> + FormatStatement& withStatementList(QList<T*> stmtList, FormatToken::Flags flags) + { + return withStatementList(stmtList, QString(), ListSeparator::COMMA, nullptr, flags); + } template <class T> FormatStatement& withStatementList(QList<T*> stmtList, const QString& indentName = QString(), ListSeparator sep = ListSeparator::COMMA, - FormatStatementEnricher enricher = nullptr) + FormatStatementEnricher enricher = nullptr, FormatToken::Flags flags = FormatToken::Flag::NO_FLAG) { if (!indentName.isNull()) markAndKeepIndent(indentName); @@ -84,7 +142,7 @@ class FormatStatement foreach (T* stmt, stmtList) { if (!first) - withSeparator(sep); + withSeparator(sep, flags); withStatement(stmt, QString(), enricher); first = false; @@ -113,47 +171,8 @@ class FormatStatement Cfg::SqlEnterpriseFormatterConfig* cfg = nullptr; private: - struct FormatToken - { - enum Type - { - KEYWORD, - LINED_UP_KEYWORD, - ID, - STRING_OR_ID, - OPERATOR, - STAR, - FLOAT, - STRING, - INTEGER, - BLOB, - BIND_PARAM, - ID_DOT, - PAR_DEF_LEFT, - PAR_DEF_RIGHT, - PAR_EXPR_LEFT, - PAR_EXPR_RIGHT, - PAR_FUNC_LEFT, - PAR_FUNC_RIGHT, - SEMICOLON, - COMMA_LIST, - COMMA_OPER, // for example in LIMIT - FUNC_ID, - DATA_TYPE, - NEW_LINE, - INDENT_MARKER, - INCR_INDENT, - SET_INDENT, - DECR_INDENT, - MARK_KEYWORD_LINEUP - }; - - Type type; - QVariant value; - QVariant additionalValue; - }; - - void withToken(FormatToken::Type type, const QVariant& value, const QVariant& additionalValue = QVariant()); + FormatToken* withToken(FormatToken::Type type, const QVariant& value, const QVariant& additionalValue = QVariant(), FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); + FormatToken* withToken(FormatToken::Type type, const QVariant& value, FormatToken::Flags flags); void cleanup(); void buildTokens(); bool applyIndent(); @@ -166,6 +185,7 @@ class FormatStatement void setIndent(int newIndent); bool endsWithSpace(); FormatToken* getLastRealToken(bool skipNewLines = false); + FormatToken* getLastToken(); QString detokenize(); void detokenizeLeftPar(FormatToken* token, bool spaceBefore, bool spaceAfter, bool nlBefore, bool nlAfter); void detokenizeRightPar(FormatToken* token, bool spaceBefore, bool spaceAfter, bool nlBefore, bool nlAfter); @@ -197,4 +217,6 @@ class FormatStatement static const QString NEWLINE; }; +Q_DECLARE_OPERATORS_FOR_FLAGS(FormatStatement::FormatToken::Flags) + #endif // FORMATSTATEMENT_H |
