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