Commit 27d2a129 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

New smart indenter for QML files.

parent 126b3de1
......@@ -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);
}
......
......@@ -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;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment