From 27d2a129c2538bc0b241ddc35ddf7bed78460af5 Mon Sep 17 00:00:00 2001 From: Roberto Raggi <roberto.raggi@nokia.com> Date: Tue, 12 Jan 2010 18:01:39 +0100 Subject: [PATCH] New smart indenter for QML files. --- src/plugins/qmleditor/qmleditor.cpp | 84 +++++++------------ .../qscripthighlighter/qscriptindenter.cpp | 15 ++-- 2 files changed, 36 insertions(+), 63 deletions(-) diff --git a/src/plugins/qmleditor/qmleditor.cpp b/src/plugins/qmleditor/qmleditor.cpp index 8986f1eaf12..a83f18423a7 100644 --- a/src/plugins/qmleditor/qmleditor.cpp +++ b/src/plugins/qmleditor/qmleditor.cpp @@ -37,6 +37,8 @@ #include "qmllookupcontext.h" #include "qmlresolveexpression.h" +#include <qscripthighlighter/qscriptindenter.h> + #include <qml/metatype/qmltypesystem.h> #include <qml/parser/qmljsastvisitor_p.h> #include <qml/parser/qmljsast_p.h> @@ -74,6 +76,27 @@ using namespace QmlJS; using namespace QmlJS::AST; using namespace SharedTools; +namespace { +int blockBraceDepth(const QTextBlock &block) +{ + int state = block.userState(); + if (state == -1) + return 0; + + return (state >> 8) & 0xFF; +} + +int blockStartState(const QTextBlock &block) +{ + int state = block.userState(); + + if (state == -1) + return 0; + else + return state & 0xff; +} +} // end of anonymous namespace + namespace QmlEditor { namespace Internal { @@ -548,65 +571,14 @@ bool QmlTextEditor::isClosingBrace(const QList<QScriptIncrementalScanner::Token> return false; } -static int blockBraceDepth(const QTextBlock &block) -{ - int state = block.userState(); - if (state == -1) - return 0; - - return (state >> 8) & 0xFF; -} - -static int blockStartState(const QTextBlock &block) -{ - int state = block.userState(); - - if (state == -1) - return 0; - else - return state & 0xff; -} - -void QmlTextEditor::indentBlock(QTextDocument *, QTextBlock block, QChar /*typedChar*/) +void QmlTextEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar) { TextEditor::TabSettings ts = tabSettings(); + SharedTools::QScriptIndenter indenter; + indenter.setTabSize(ts.m_tabSize); + indenter.setIndentSize(ts.m_indentSize); - QTextCursor tc(block); - - const QString blockText = block.text(); - int startState = blockStartState(block.previous()); - - QScriptIncrementalScanner scanner; - const QList<QScriptIncrementalScanner::Token> tokens = scanner(blockText, startState); - - if (! tokens.isEmpty()) { - const QScriptIncrementalScanner::Token tk = tokens.first(); - - if (tk.is(QScriptIncrementalScanner::Token::RightBrace) - || tk.is(QScriptIncrementalScanner::Token::RightBracket)) { - if (TextEditor::TextBlockUserData::findPreviousBlockOpenParenthesis(&tc)) { - const QString text = tc.block().text(); - int indent = ts.columnAt(text, ts.firstNonSpace(text)); - ts.indentLine(block, indent); - return; - } - } - } - - int initialIndent = 0; - for (QTextBlock it = block.previous(); it.isValid(); it = it.previous()) { - const QString text = it.text(); - - if (! text.isEmpty()) { - initialIndent = ts.columnAt(text, ts.firstNonSpace(text)); - break; - } - } - - const int braceDepth = blockBraceDepth(block.previous()); - const int previousBraceDepth = blockBraceDepth(block.previous().previous()); - const int delta = qMax(0, braceDepth - previousBraceDepth); - int indent = initialIndent + (delta * ts.m_indentSize); + const int indent = indenter.indentForBottomLine(doc->begin(), block.next(), typedChar); ts.indentLine(block, indent); } diff --git a/src/shared/qscripthighlighter/qscriptindenter.cpp b/src/shared/qscripthighlighter/qscriptindenter.cpp index 45f4fa2235e..4f8d5ab9e6c 100644 --- a/src/shared/qscripthighlighter/qscriptindenter.cpp +++ b/src/shared/qscripthighlighter/qscriptindenter.cpp @@ -87,7 +87,7 @@ QScriptIndenter::QScriptIndenter() inlineCComment(QRegExp(QLatin1String("/\\*.*\\*/"))), braceX(QRegExp(QLatin1String("^\\s*\\}\\s*(?:else|catch)\\b"))), iflikeKeyword(QRegExp(QLatin1String("\\b(?:catch|do|for|if|while|with)\\b"))), - propertylikeKeyword(QRegExp(QLatin1String("^\\s*\\b(?:property|signal|import)\\b"))) + propertylikeKeyword(QRegExp(QLatin1String("^\\s*\\b(?:property|signal|import|readonly|return)\\b"))) { /* @@ -278,13 +278,14 @@ QString QScriptIndenter::trimmedCodeLine(const QString &t) const const QString e = trimmed.trimmed(); - if (insertSemicolon - || e.endsWith(QLatin1Char(',')) - || e.endsWith(QLatin1Char(']')) - || trimmed.indexOf(propertylikeKeyword) != -1) + if (insertSemicolon || e.endsWith(QLatin1Char(',')) || e.endsWith(QLatin1Char(']'))) trimmed.append(QLatin1Char(';')); - - //qDebug() << trimmed; + else if (trimmed.indexOf(propertylikeKeyword) != -1) { + const QChar ch = trimmed.at(trimmed.length() - 1); + if (ch.isLetterOrNumber() || ch == QLatin1Char(')') || ch == QLatin1Char(']') + || ch == QLatin1Char('"') || ch == QLatin1Char('\'')) + trimmed.append(QLatin1Char(';')); + } return trimmed; } -- GitLab