Commit a49395a0 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Made the indenter more Qt friendly. Added support for moc keywords.

parent 0bcbab86
...@@ -76,21 +76,9 @@ const SimpleToken &BackwardsScanner::fetchToken(int i) ...@@ -76,21 +76,9 @@ const SimpleToken &BackwardsScanner::fetchToken(int i)
QList<SimpleToken> adaptedTokens; QList<SimpleToken> adaptedTokens;
for (int i = 0; i < _tokens.size(); ++i) { for (int i = 0; i < _tokens.size(); ++i) {
SimpleToken t = _tokens.at(i); SimpleToken t = _tokens.at(i);
if (i == 0) {
Q_ASSERT(t.followsNewline());
}
t.setPosition(t.position() + blockText.length()); t.setPosition(t.position() + blockText.length());
t.setText(_text.midRef(t.position(), t.length())); t.setText(_text.midRef(t.position(), t.length()));
if (i == 0) {
Q_ASSERT(t.followsNewline());
}
adaptedTokens.append(t); adaptedTokens.append(t);
if (i == 0) {
Q_ASSERT(adaptedTokens.last().followsNewline());
}
} }
_tokens = _tokenize(blockText, previousBlockState(_block)); _tokens = _tokenize(blockText, previousBlockState(_block));
...@@ -111,18 +99,16 @@ int BackwardsScanner::startPosition() const ...@@ -111,18 +99,16 @@ int BackwardsScanner::startPosition() const
QString BackwardsScanner::text() const QString BackwardsScanner::text() const
{ return _text; } { return _text; }
QString BackwardsScanner::text(int begin, int end) const QString BackwardsScanner::text(int index) const
{ {
const SimpleToken &firstToken = _tokens.at(begin + _offset); const SimpleToken &firstToken = _tokens.at(index + _offset);
const SimpleToken &lastToken = _tokens.at(end + _offset - 1); return _text.mid(firstToken.begin(), firstToken.length());
return _text.mid(firstToken.begin(), lastToken.end() - firstToken.begin());
} }
QStringRef BackwardsScanner::textRef(int begin, int end) const QStringRef BackwardsScanner::textRef(int index) const
{ {
const SimpleToken &firstToken = _tokens.at(begin + _offset); const SimpleToken &firstToken = _tokens.at(index + _offset);
const SimpleToken &lastToken = _tokens.at(end + _offset - 1); return _text.midRef(firstToken.begin(), firstToken.length());
return _text.midRef(firstToken.begin(), lastToken.end() - firstToken.begin());
} }
int BackwardsScanner::previousBlockState(const QTextBlock &block) const int BackwardsScanner::previousBlockState(const QTextBlock &block) const
......
...@@ -51,8 +51,8 @@ public: ...@@ -51,8 +51,8 @@ public:
int startPosition() const; int startPosition() const;
QString text() const; QString text() const;
QString text(int begin, int end) const; QString text(int index) const;
QStringRef textRef(int begin, int end) const; QStringRef textRef(int index) const;
// 1-based // 1-based
SimpleToken LA(int index) const; SimpleToken LA(int index) const;
......
...@@ -150,7 +150,7 @@ QString ExpressionUnderCursor::operator()(const QTextCursor &cursor) ...@@ -150,7 +150,7 @@ QString ExpressionUnderCursor::operator()(const QTextCursor &cursor)
if (i == initialSize) if (i == initialSize)
return QString(); return QString();
return scanner.text(i, initialSize); return scanner.text(i);
} }
int ExpressionUnderCursor::startOfFunctionCall(const QTextCursor &cursor) const int ExpressionUnderCursor::startOfFunctionCall(const QTextCursor &cursor) const
......
...@@ -49,9 +49,9 @@ static bool shouldOverrideChar(const QChar &ch) ...@@ -49,9 +49,9 @@ static bool shouldOverrideChar(const QChar &ch)
} }
} }
static bool isCompleteStringLiteral(const BackwardsScanner &tk, int index, int startToken) static bool isCompleteStringLiteral(const BackwardsScanner &tk, int index)
{ {
const QStringRef text = tk.textRef(index, startToken); const QStringRef text = tk.textRef(index);
if (text.length() < 2) if (text.length() < 2)
return false; return false;
...@@ -62,9 +62,9 @@ static bool isCompleteStringLiteral(const BackwardsScanner &tk, int index, int s ...@@ -62,9 +62,9 @@ static bool isCompleteStringLiteral(const BackwardsScanner &tk, int index, int s
return false; return false;
} }
static bool isCompleteCharLiteral(const BackwardsScanner &tk, int index, int startToken) static bool isCompleteCharLiteral(const BackwardsScanner &tk, int index)
{ {
const QStringRef text = tk.textRef(index, startToken); const QStringRef text = tk.textRef(index);
if (text.length() < 2) if (text.length() < 2)
return false; return false;
...@@ -133,7 +133,7 @@ QString MatchingText::insertMatchingBrace(const QTextCursor &cursor, const QStri ...@@ -133,7 +133,7 @@ QString MatchingText::insertMatchingBrace(const QTextCursor &cursor, const QStri
if (text.length() != 1) if (text.length() != 1)
qWarning() << Q_FUNC_INFO << "handle event compression"; qWarning() << Q_FUNC_INFO << "handle event compression";
if (isCompleteStringLiteral(tk, index - 1, startToken)) if (isCompleteStringLiteral(tk, index - 1))
return QLatin1String("\""); return QLatin1String("\"");
return QString(); return QString();
...@@ -141,7 +141,7 @@ QString MatchingText::insertMatchingBrace(const QTextCursor &cursor, const QStri ...@@ -141,7 +141,7 @@ QString MatchingText::insertMatchingBrace(const QTextCursor &cursor, const QStri
if (text.length() != 1) if (text.length() != 1)
qWarning() << Q_FUNC_INFO << "handle event compression"; qWarning() << Q_FUNC_INFO << "handle event compression";
if (isCompleteCharLiteral(tk, index - 1, startToken)) if (isCompleteCharLiteral(tk, index - 1))
return QLatin1String("'"); return QLatin1String("'");
return QString(); return QString();
......
...@@ -139,27 +139,14 @@ QList<SimpleToken> SimpleLexer::operator()(const QString &text, int state) ...@@ -139,27 +139,14 @@ QList<SimpleToken> SimpleLexer::operator()(const QString &text, int state)
bool inPreproc = false; bool inPreproc = false;
bool first = true;
for (;;) { for (;;) {
Token tk; Token tk;
lex(&tk); lex(&tk);
if (tk.is(T_EOF_SYMBOL)) if (tk.is(T_EOF_SYMBOL))
break; break;
Q_ASSERT(lex.tokenOffset() == tk.begin());
Q_ASSERT(lex.tokenLength() == tk.f.length);
QStringRef spell = text.midRef(lex.tokenOffset(), lex.tokenLength()); QStringRef spell = text.midRef(lex.tokenOffset(), lex.tokenLength());
SimpleToken simpleTk(tk, spell); SimpleToken simpleTk(tk, spell);
if (first) {
first = false;
Q_ASSERT(tk.f.newline);
Q_ASSERT(simpleTk.followsNewline());
}
lex.setScanAngleStringLiteralTokens(false); lex.setScanAngleStringLiteralTokens(false);
if (tk.f.newline && tk.is(T_POUND)) if (tk.f.newline && tk.is(T_POUND))
......
...@@ -1472,9 +1472,6 @@ static void indentCPPBlock(const CPPEditor::TabSettings &ts, ...@@ -1472,9 +1472,6 @@ static void indentCPPBlock(const CPPEditor::TabSettings &ts,
void CPPEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar) void CPPEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar)
{ {
const TextEditor::TextBlockIterator begin(doc->begin());
const TextEditor::TextBlockIterator end(block.next());
QTextCursor tc(block); QTextCursor tc(block);
tc.movePosition(QTextCursor::EndOfBlock); tc.movePosition(QTextCursor::EndOfBlock);
...@@ -1509,6 +1506,26 @@ void CPPEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedCha ...@@ -1509,6 +1506,26 @@ void CPPEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedCha
} }
} }
if ((tokenCount == 0 || tk[0].isNot(T_POUND)) && typedChar.isNull() && (tk[-1].is(T_IDENTIFIER) || tk[-1].is(T_RPAREN))) {
int tokenIndex = -1;
if (tk[-1].is(T_RPAREN)) {
const int matchingBrace = tk.startOfMatchingBrace(0);
if (matchingBrace != 0 && tk[matchingBrace - 1].is(T_IDENTIFIER)) {
tokenIndex = matchingBrace - 1;
}
}
const QString spell = tk.text(tokenIndex);
if (tk[tokenIndex].followsNewline() && (spell.startsWith(QLatin1String("QT_")) ||
spell.startsWith(QLatin1String("Q_")))) {
const int indent = tk.indentation(tokenIndex);
tabSettings().indentLine(block, indent);
return;
}
}
const TextEditor::TextBlockIterator begin(doc->begin());
const TextEditor::TextBlockIterator end(block.next());
indentCPPBlock(tabSettings(), block, begin, end, typedChar); indentCPPBlock(tabSettings(), block, begin, end, typedChar);
} }
......
Markdown is supported
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