From 8e640722c62692818ab840d50b3758f89a41a54e Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Wed, 25 Nov 2015 16:48:41 -0500 Subject: Imported Upstream version 3.0.7 --- .../SqlEnterpriseFormatter.pro | 4 +- .../SqlEnterpriseFormatter_de.ts | 31 ++- .../SqlEnterpriseFormatter_es.ts | 31 ++- .../SqlEnterpriseFormatter_fr.ts | 31 ++- .../SqlEnterpriseFormatter_it.ts | 233 +++++++++++++++++++++ .../SqlEnterpriseFormatter_pl.ts | 31 ++- .../SqlEnterpriseFormatter_pt_BR.ts | 31 ++- .../SqlEnterpriseFormatter_ru.ts | 33 ++- .../SqlEnterpriseFormatter_sk.ts | 31 ++- .../SqlEnterpriseFormatter_zh_CN.ts | 31 ++- Plugins/SqlEnterpriseFormatter/formatempty.cpp | 5 +- Plugins/SqlEnterpriseFormatter/formatempty.h | 3 - .../sqlenterpriseformatter.cpp | 206 +++++++++++++++++- .../sqlenterpriseformatter.h | 23 ++ .../sqlenterpriseformatter.ui | 69 +++++- 15 files changed, 754 insertions(+), 39 deletions(-) create mode 100644 Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_it.ts (limited to 'Plugins/SqlEnterpriseFormatter') diff --git a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter.pro b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter.pro index 965767b..5af5bfa 100644 --- a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter.pro +++ b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter.pro @@ -100,7 +100,8 @@ RESOURCES += \ sqlenterpriseformatter.qrc -TRANSLATIONS += SqlEnterpriseFormatter_zh_CN.ts \ +TRANSLATIONS += SqlEnterpriseFormatter_it.ts \ + SqlEnterpriseFormatter_zh_CN.ts \ SqlEnterpriseFormatter_sk.ts \ SqlEnterpriseFormatter_de.ts \ SqlEnterpriseFormatter_ru.ts \ @@ -120,3 +121,4 @@ TRANSLATIONS += SqlEnterpriseFormatter_zh_CN.ts \ + diff --git a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_de.ts b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_de.ts index a83f5df..37751d0 100644 --- a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_de.ts +++ b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_de.ts @@ -4,8 +4,8 @@ QObject - - + + name example name wrapper @@ -200,7 +200,32 @@ - + + Comments + + + + + Preferred comment marker (where possible): + + + + + SqlEnterpriseFormatter.CommentMarkers + + + + + Move all comments to the line end + + + + + Line up comments at the line end + + + + Preview diff --git a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_es.ts b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_es.ts index 4d873e5..b0b5eed 100644 --- a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_es.ts +++ b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_es.ts @@ -4,8 +4,8 @@ QObject - - + + name example name wrapper @@ -200,7 +200,32 @@ - + + Comments + + + + + Preferred comment marker (where possible): + + + + + SqlEnterpriseFormatter.CommentMarkers + + + + + Move all comments to the line end + + + + + Line up comments at the line end + + + + Preview diff --git a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_fr.ts b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_fr.ts index 8a9178b..d0e62f7 100644 --- a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_fr.ts +++ b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_fr.ts @@ -4,8 +4,8 @@ QObject - - + + name example name wrapper Nom @@ -200,7 +200,32 @@ Mots clé en majuscule - + + Comments + + + + + Preferred comment marker (where possible): + + + + + SqlEnterpriseFormatter.CommentMarkers + + + + + Move all comments to the line end + + + + + Line up comments at the line end + + + + Preview Aperçu diff --git a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_it.ts b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_it.ts new file mode 100644 index 0000000..9e721a4 --- /dev/null +++ b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_it.ts @@ -0,0 +1,233 @@ + + + + + QObject + + + + name + example name wrapper + + + + + SqlEnterpriseFormatter + + + Indentation + + + + + Line up keywords in multi-line queries + + + + + Indent contents of parenthesis block + + + + + Tab size: + + + + + New lines + + + + + Before opening parenthesis in column definitions + + + + + After opening parenthesis in column definitions + + + + + Before closing parenthesis in column definitions + + + + + After closing parenthesis in column definitions + + + + + Before opening parenthesis in expressions + + + + + After opening parenthesis in expressions + + + + + Before closing parenthesis in expressions + + + + + After closing parenthesis in expressions + + + + + After JOIN keywords in FROM clause + + + + + Put each column constraint in CREATE TABLE into new line + + + + + After comma + + + + + After comma in expressions + + + + + After semicolon + + + + + + Never before semicolon + + + + + White spaces + + + + + Before comma in lists + + + + + After comma in lists + + + + + Before opening parenthesis + + + + + After opening parenthesis + + + + + Before closing parenthesis + + + + + After closing parenthesis + + + + + No space between SQL function name and opening parenthesis + + + + + Before dot operator (in path to database object) + + + + + After dot operator (in path to database object) + + + + + Before mathematical operator + + + + + After mathematical operator + + + + + Never before comma + + + + + Names + + + + + Preferred name wrapper + + + + + Always use name wrapping + + + + + Uppercase data type names + + + + + Uppercase keywords + + + + + Comments + + + + + Preferred comment marker (where possible): + + + + + SqlEnterpriseFormatter.CommentMarkers + + + + + Move all comments to the line end + + + + + Line up comments at the line end + + + + + Preview + + + + diff --git a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_pl.ts b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_pl.ts index 5cfbf32..2c03a46 100644 --- a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_pl.ts +++ b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_pl.ts @@ -4,8 +4,8 @@ QObject - - + + name example name wrapper nazwa @@ -201,7 +201,32 @@ Zmieniaj litery słów kluczowych na duże - + + Comments + + + + + Preferred comment marker (where possible): + + + + + SqlEnterpriseFormatter.CommentMarkers + + + + + Move all comments to the line end + + + + + Line up comments at the line end + + + + Preview Podgląd diff --git a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_pt_BR.ts b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_pt_BR.ts index b89804a..a408ffc 100644 --- a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_pt_BR.ts +++ b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_pt_BR.ts @@ -4,8 +4,8 @@ QObject - - + + name example name wrapper @@ -200,7 +200,32 @@ - + + Comments + + + + + Preferred comment marker (where possible): + + + + + SqlEnterpriseFormatter.CommentMarkers + + + + + Move all comments to the line end + + + + + Line up comments at the line end + + + + Preview diff --git a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_ru.ts b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_ru.ts index a5656d8..4e5e98d 100644 --- a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_ru.ts +++ b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_ru.ts @@ -1,11 +1,11 @@ - + QObject - - + + name example name wrapper имя @@ -200,7 +200,32 @@ Приводить ключевые слова к верхнему регистру - + + Comments + Комментарии + + + + Preferred comment marker (where possible): + Предпочитаемый символ комментирования (где применимо): + + + + SqlEnterpriseFormatter.CommentMarkers + SqlEnterpriseFormatter.CommentMarkers + + + + Move all comments to the line end + Перемещать все комментарии в конец строки + + + + Line up comments at the line end + Выравнивать комментарии в конце строки + + + Preview Предпросмотр diff --git a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_sk.ts b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_sk.ts index 407cc6e..37a42a4 100644 --- a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_sk.ts +++ b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_sk.ts @@ -4,8 +4,8 @@ QObject - - + + name example name wrapper @@ -200,7 +200,32 @@ - + + Comments + + + + + Preferred comment marker (where possible): + + + + + SqlEnterpriseFormatter.CommentMarkers + + + + + Move all comments to the line end + + + + + Line up comments at the line end + + + + Preview diff --git a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_zh_CN.ts b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_zh_CN.ts index 3c80603..d1a361b 100644 --- a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_zh_CN.ts +++ b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter_zh_CN.ts @@ -4,8 +4,8 @@ QObject - - + + name example name wrapper @@ -200,7 +200,32 @@ - + + Comments + + + + + Preferred comment marker (where possible): + + + + + SqlEnterpriseFormatter.CommentMarkers + + + + + Move all comments to the line end + + + + + Line up comments at the line end + + + + Preview diff --git a/Plugins/SqlEnterpriseFormatter/formatempty.cpp b/Plugins/SqlEnterpriseFormatter/formatempty.cpp index 976694e..d0696e0 100644 --- a/Plugins/SqlEnterpriseFormatter/formatempty.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatempty.cpp @@ -1,8 +1,9 @@ #include "formatempty.h" +#include "common/unused.h" -FormatEmpty::FormatEmpty(SqliteEmptyQuery* eq) : - eq(eq) +FormatEmpty::FormatEmpty(SqliteEmptyQuery* eq) { + UNUSED(eq); } void FormatEmpty::formatInternal() diff --git a/Plugins/SqlEnterpriseFormatter/formatempty.h b/Plugins/SqlEnterpriseFormatter/formatempty.h index 3279925..2fa3d01 100644 --- a/Plugins/SqlEnterpriseFormatter/formatempty.h +++ b/Plugins/SqlEnterpriseFormatter/formatempty.h @@ -12,9 +12,6 @@ class FormatEmpty : public FormatStatement protected: void formatInternal(); - - private: - SqliteEmptyQuery* eq = nullptr; }; #endif // FORMATEMPTY_H diff --git a/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.cpp b/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.cpp index 8f75960..a08f95c 100644 --- a/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.cpp +++ b/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.cpp @@ -3,6 +3,7 @@ #include "common/unused.h" #include "common/global.h" #include +#include #include SqlEnterpriseFormatter::SqlEnterpriseFormatter() @@ -11,6 +12,8 @@ SqlEnterpriseFormatter::SqlEnterpriseFormatter() QString SqlEnterpriseFormatter::format(SqliteQueryPtr query) { + QList comments = collectComments(query->tokens); + int wrapperIdx = cfg.SqlEnterpriseFormatter.Wrappers.get().indexOf(cfg.SqlEnterpriseFormatter.PrefferedWrapper.get()); NameWrapper wrapper = getAllNameWrappers()[wrapperIdx]; @@ -24,7 +27,11 @@ QString SqlEnterpriseFormatter::format(SqliteQueryPtr query) QString formatted = formatStmt->format(); delete formatStmt; - return formatted; + QString formattedWithComments = applyComments(formatted, comments, query->dialect); + for (Comment* c : comments) + delete c; + + return formattedWithComments; } bool SqlEnterpriseFormatter::init() @@ -34,7 +41,9 @@ bool SqlEnterpriseFormatter::init() static_qstring(query1, "SELECT (2 + 4) AND (3 + 5), 4 NOT IN (SELECT t1.'some[_]name' + t2.[some'name2] FROM xyz t1 JOIN zxc t2 ON (t1.aaa = t2.aaa)) " "FROM a, (SELECT id FROM table2);"); static_qstring(query2, "INSERT INTO table1 (id, value1, value2) VALUES (1, (2 + 5), (SELECT id FROM table2));"); - static_qstring(query3, "CREATE TABLE tab (id INTEGER PRIMARY KEY, value1 VARCHAR(6), value2 NUMBER(8,2) NOT NULL DEFAULT 1.0);"); + static_qstring(query3, "CREATE TABLE tab (id INTEGER PRIMARY KEY, /*a primary key column*/ value1 VARCHAR(6), " + "value2 /*column with constraints*/ NUMBER(8,2) NOT NULL DEFAULT 1.0" + ");"); static_qstring(query4, "CREATE UNIQUE INDEX IF NOT EXISTS dbName.idx1 ON [messages column] (id COLLATE x ASC, lang DESC, description);"); Parser parser(Dialect::Sqlite3); @@ -110,3 +119,196 @@ void SqlEnterpriseFormatter::configDialogClosed() { disconnect(&cfg.SqlEnterpriseFormatter, SIGNAL(changed(CfgEntry*)), this, SLOT(configModified(CfgEntry*))); } + +QList SqlEnterpriseFormatter::collectComments(const TokenList &tokens) +{ + QList results; + + QList tokensInLines = tokensByLines(tokens); + Comment* prevCommentInThisLine = nullptr; + Comment* cmt = nullptr; + bool tokensBefore = false; + int pos = 0; + int line = 0; + for (const TokenList& tokensInLine : tokensInLines) + { + tokensBefore = true; + prevCommentInThisLine = nullptr; + for (const TokenPtr& token : tokensInLine) + { + if (token->type == Token::Type::SPACE) + continue; + + if (prevCommentInThisLine) + prevCommentInThisLine->tokensAfter = true; + + if (token->type == Token::Type::COMMENT) + { + cmt = new Comment; + cmt->tokensBefore = tokensBefore; + cmt->position = pos; + cmt->multiline = token->value.startsWith("/*"); + if (cmt->multiline) + cmt->contents = token->value.mid(2, token->value.length() - 4).trimmed(); + else + cmt->contents = token->value.mid(2).trimmed(); + + results << cmt; + prevCommentInThisLine = cmt; + continue; + } + + tokensBefore = true; + pos++; + } + line++; + } + + return results; +} + +QList SqlEnterpriseFormatter::tokensByLines(const TokenList &tokens, bool includeSpaces) +{ + QList tokensInLines; + TokenList tokensInLine; + for (const TokenPtr& token : tokens) + { + if (includeSpaces || token->type != Token::Type::SPACE) + tokensInLine << token; + + if (token->type == Token::Type::SPACE && token->value.contains('\n')) + { + tokensInLines << tokensInLine; + tokensInLine.clear(); + } + } + if (tokensInLine.size() > 0) + tokensInLines << tokensInLine; + + return tokensInLines; +} + +TokenList SqlEnterpriseFormatter::adjustCommentsToEnd(const TokenList &inputTokens) +{ + QList tokensInLines = tokensByLines(inputTokens, true); + TokenList newTokens; + TokenList commentTokensForLine; + TokenPtr newLineToken; + for (const TokenList& tokensInLine : tokensInLines) + { + commentTokensForLine.clear(); + newLineToken.clear(); + for (const TokenPtr& token : tokensInLine) + { + if (token->type == Token::Type::COMMENT) + { + wrapComment(token, true); + //token->value = " " + endLineCommentTpl.arg(token->value); + commentTokensForLine << token; + } + else if (token->type == Token::Type::SPACE && token->value.contains("\n")) + newLineToken = token; + else + newTokens << token; + } + + newTokens += commentTokensForLine; + if (newLineToken) + newTokens << newLineToken; + } + return newTokens; +} + +TokenList SqlEnterpriseFormatter::wrapOnlyComments(const TokenList &inputTokens) +{ + QList tokensInLines = tokensByLines(inputTokens, true); + TokenList newTokens; + bool lineEnd = true; + for (const TokenList& tokensInLine : reverse(tokensInLines)) + { + lineEnd = true; + for (const TokenPtr& token : reverse(tokensInLine)) + { + if (!token->isWhitespace()) + lineEnd = false; + + if (token->type == Token::Type::COMMENT) + wrapComment(token, lineEnd); + + newTokens << token; + } + } + return reverse(newTokens); +} + +TokenList SqlEnterpriseFormatter::optimizeInnerComments(const TokenList &inputTokens) +{ + // TODO + return inputTokens; +} + +TokenList SqlEnterpriseFormatter::optimizeEndLineComments(const TokenList &inputTokens) +{ + // TODO + return inputTokens; +} + +void SqlEnterpriseFormatter::indentMultiLineComments(const TokenList &inputTokens) +{ + // TODO +} + +void SqlEnterpriseFormatter::wrapComment(const TokenPtr &token, bool isAtLineEnd) +{ + static_qstring(multiCommentTpl, "/* %1 */"); + static_qstring(endLineCommentTpl, "-- %1"); + + bool isMultiLine = token->value.contains("\n"); + if (isAtLineEnd && !isMultiLine && cfg.SqlEnterpriseFormatter.PreferredCommentMarker.get() == "--") + token->value = endLineCommentTpl.arg(token->value); + else + token->value = multiCommentTpl.arg(token->value); +} + +QString SqlEnterpriseFormatter::applyComments(const QString& formatted, QList comments, Dialect dialect) +{ + if (comments.size() == 0) + return formatted; + + int currentCommentPosition = comments.first()->position; + + TokenList allTokens = Lexer::tokenize(formatted, dialect); + TokenList newTokens; + int currentTokenPosition = 0; + for (const TokenPtr& token : allTokens) + { + if (currentTokenPosition == currentCommentPosition) + { + newTokens << TokenPtr::create(Token::Type::COMMENT, comments.first()->contents); + comments.removeFirst(); + if (comments.size() > 0) + currentCommentPosition = comments.first()->position; + else + currentCommentPosition = -1; + } + + newTokens << token; + if (token->type != Token::Type::SPACE) + currentTokenPosition++; + } + + // Any remaining comments + for (Comment* cmt : comments) + newTokens << TokenPtr::create(Token::Type::COMMENT, cmt->contents); + + if (cfg.SqlEnterpriseFormatter.MoveAllCommentsToLineEnd.get()) + newTokens = adjustCommentsToEnd(newTokens); + else + newTokens = wrapOnlyComments(newTokens); + + newTokens = optimizeInnerComments(newTokens); + newTokens = optimizeEndLineComments(newTokens); + indentMultiLineComments(newTokens); + + return newTokens.detokenize(); +} diff --git a/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.h b/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.h index 2701745..1f9b6d8 100644 --- a/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.h +++ b/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.h @@ -53,6 +53,10 @@ CFG_CATEGORIES(SqlEnterpriseFormatterConfig, CFG_ENTRY(QString, PrefferedWrapper, getNameWrapperStr(NameWrapper::BRACKET)) CFG_ENTRY(QStringList, Wrappers, getNameWrapperStrings(), false) CFG_ENTRY(QString, PreviewCode, QString(), false) + CFG_ENTRY(bool, MoveAllCommentsToLineEnd, false) + CFG_ENTRY(bool, LineUpCommentsAtLineEnd, true) + CFG_ENTRY(QString, PreferredCommentMarker, "--") + CFG_ENTRY(QStringList, CommentMarkers, QStringList({"--", "/* */"})) ) ) @@ -73,6 +77,25 @@ class SQLENTERPRISEFORMATTERSHARED_EXPORT SqlEnterpriseFormatter : public Generi void configDialogClosed(); private: + struct Comment + { + int position = 0; + QString contents; + bool tokensBefore = false; + bool tokensAfter = false; + bool multiline = false; + }; + + QList collectComments(const TokenList& tokens); + QString applyComments(const QString& formatted, QList comments, Dialect dialect); + QList tokensByLines(const TokenList& tokens, bool includeSpaces = false); + TokenList adjustCommentsToEnd(const TokenList& inputTokens); + TokenList wrapOnlyComments(const TokenList& inputTokens); + TokenList optimizeInnerComments(const TokenList& inputTokens); + TokenList optimizeEndLineComments(const TokenList& inputTokens); + void indentMultiLineComments(const TokenList& inputTokens); + void wrapComment(const TokenPtr& token, bool isAtLineEnd); + QList previewQueries; CFG_LOCAL_PERSISTABLE(SqlEnterpriseFormatterConfig, cfg) diff --git a/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.ui b/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.ui index 2ebfbdf..b21de64 100644 --- a/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.ui +++ b/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.ui @@ -39,7 +39,7 @@ - 1 + 0 @@ -133,9 +133,9 @@ 0 - -166 - 578 - 350 + 0 + 390 + 299 @@ -366,8 +366,8 @@ 0 0 - 424 - 325 + 418 + 278 @@ -626,6 +626,63 @@ + + + Comments + + + + + + Preferred comment marker (where possible): + + + + + + + SqlEnterpriseFormatter.PreferredCommentMarker + + + SqlEnterpriseFormatter.CommentMarkers + + + + + + + Move all comments to the line end + + + SqlEnterpriseFormatter.MoveAllCommentsToLineEnd + + + + + + + Line up comments at the line end + + + SqlEnterpriseFormatter.LineUpCommentsAtLineEnd + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + -- cgit v1.2.3