From 3067f441df931771f859915633485eb050fa23f6 Mon Sep 17 00:00:00 2001 From: Christian Kamm <christian.d.kamm@nokia.com> Date: Wed, 21 Apr 2010 10:33:50 +0200 Subject: [PATCH] QmlJS: Fix switch statement indentation. Task-number: QTCREATORBUG-756 --- src/libs/qmljs/qmljsindenter.cpp | 19 ++++++++++++------- src/libs/qmljs/qmljsindenter.h | 1 + src/plugins/qmljseditor/qmljseditor.cpp | 3 ++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/libs/qmljs/qmljsindenter.cpp b/src/libs/qmljs/qmljsindenter.cpp index 3df0f63097d..7e1e50183c9 100644 --- a/src/libs/qmljs/qmljsindenter.cpp +++ b/src/libs/qmljs/qmljsindenter.cpp @@ -85,6 +85,12 @@ const int QmlJSIndenter::BigRoof = 400; QmlJSIndenter::QmlJSIndenter() : braceX(QRegExp(QLatin1String("^\\s*\\}\\s*(?:else|catch)\\b"))) + , caseOrDefault(QRegExp(QLatin1String( + "\\s*(?:" + "case\\b[^:]+|" + "default)" + "\\s*:.*"))) + { /* @@ -1007,12 +1013,16 @@ int QmlJSIndenter::indentForStandaloneLine() while (isContinuationLine()) readLine(); + int indentChange = - *yyBraceDepth; + if (caseOrDefault.exactMatch(*yyLine)) + ++indentChange; + /* Never trust lines containing only '{' or '}', as some people (Richard M. Stallman) format them weirdly. */ if (yyLine->trimmed().length() > 1) - return indentOfLine(*yyLine) - *yyBraceDepth * ppIndentSize; + return indentOfLine(*yyLine) + indentChange * ppIndentSize; } if (!readLine()) @@ -1071,12 +1081,7 @@ int QmlJSIndenter::indentForBottomLine(QTextBlock begin, QTextBlock end, QChar t */ indent -= ppIndentSize; } else if (okay(typedIn, QLatin1Char(':'))) { - QRegExp caseLabel( - QLatin1String("\\s*(?:case\\b(?:[^:]|::)+" - "|(?:default)\\s*" - ")?:.*")); - - if (caseLabel.exactMatch(bottomLine)) { + if (caseOrDefault.exactMatch(bottomLine)) { /* Move a case label (or the ':' in front of a constructor initialization list) one level to the diff --git a/src/libs/qmljs/qmljsindenter.h b/src/libs/qmljs/qmljsindenter.h index 52de5dfc089..f63d08106fd 100644 --- a/src/libs/qmljs/qmljsindenter.h +++ b/src/libs/qmljs/qmljsindenter.h @@ -134,6 +134,7 @@ private: const bool *yyLeftBraceFollows; QRegExp braceX; + QRegExp caseOrDefault; }; } // namespace QmlJS diff --git a/src/plugins/qmljseditor/qmljseditor.cpp b/src/plugins/qmljseditor/qmljseditor.cpp index 53b8d047b05..d2764ae2f96 100644 --- a/src/plugins/qmljseditor/qmljseditor.cpp +++ b/src/plugins/qmljseditor/qmljseditor.cpp @@ -932,7 +932,8 @@ QString QmlJSTextEditor::wordUnderCursor() const bool QmlJSTextEditor::isElectricCharacter(const QChar &ch) const { if (ch == QLatin1Char('}') - || ch == QLatin1Char(']')) + || ch == QLatin1Char(']') + || ch == QLatin1Char(':')) return true; return false; } -- GitLab