forked from qt-creator/qt-creator
Fixed autoindent when using tabs instead of spaces
Several special cases that are handled by the CPPEditor did not take into account code that was using tab characters. Task-number: QTCREATORBUG-292 Reviewed-by: Roberto Raggi
This commit is contained in:
@@ -70,13 +70,14 @@ const SimpleToken &BackwardsScanner::fetchToken(int i)
|
|||||||
} else {
|
} else {
|
||||||
++_blocksTokenized;
|
++_blocksTokenized;
|
||||||
|
|
||||||
const QString blockText = _block.text();
|
QString blockText = _block.text();
|
||||||
|
_text.prepend(QLatin1Char('\n'));
|
||||||
_text.prepend(blockText);
|
_text.prepend(blockText);
|
||||||
|
|
||||||
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);
|
||||||
t.setPosition(t.position() + blockText.length());
|
t.setPosition(t.position() + blockText.length() + 1);
|
||||||
t.setText(_text.midRef(t.position(), t.length()));
|
t.setText(_text.midRef(t.position(), t.length()));
|
||||||
adaptedTokens.append(t);
|
adaptedTokens.append(t);
|
||||||
}
|
}
|
||||||
@@ -247,8 +248,9 @@ int BackwardsScanner::startOfBlock(int index) const
|
|||||||
return start;
|
return start;
|
||||||
}
|
}
|
||||||
|
|
||||||
int BackwardsScanner::indentation(int index) const
|
QString BackwardsScanner::indentationString(int index) const
|
||||||
{
|
{
|
||||||
SimpleToken newline = operator[](startOfLine(index + 1));
|
const SimpleToken tokenAfterNewline = operator[](startOfLine(index + 1));
|
||||||
return newline.position();
|
const int newlinePos = qMax(0, _text.lastIndexOf(QLatin1Char('\n'), tokenAfterNewline.position()));
|
||||||
|
return _text.mid(newlinePos, tokenAfterNewline.position() - newlinePos);
|
||||||
}
|
}
|
||||||
|
@@ -61,7 +61,7 @@ public:
|
|||||||
// n-la token is [startToken - n]
|
// n-la token is [startToken - n]
|
||||||
SimpleToken operator[](int index) const; // ### deprecate
|
SimpleToken operator[](int index) const; // ### deprecate
|
||||||
|
|
||||||
int indentation(int index) const;
|
QString indentationString(int index) const;
|
||||||
|
|
||||||
int startOfLine(int index) const;
|
int startOfLine(int index) const;
|
||||||
int startOfMatchingBrace(int index) const;
|
int startOfMatchingBrace(int index) const;
|
||||||
|
@@ -1592,37 +1592,44 @@ static void indentCPPBlock(const CPPEditor::TabSettings &ts,
|
|||||||
ts.indentLine(block, indent);
|
ts.indentLine(block, indent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int indentationColumn(const TextEditor::TabSettings &tabSettings,
|
||||||
|
const BackwardsScanner &scanner,
|
||||||
|
int index)
|
||||||
|
{
|
||||||
|
return tabSettings.indentationColumn(scanner.indentationString(index));
|
||||||
|
}
|
||||||
|
|
||||||
void CPPEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar)
|
void CPPEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar)
|
||||||
{
|
{
|
||||||
QTextCursor tc(block);
|
QTextCursor tc(block);
|
||||||
tc.movePosition(QTextCursor::EndOfBlock);
|
tc.movePosition(QTextCursor::EndOfBlock);
|
||||||
|
|
||||||
|
const TabSettings &ts = tabSettings();
|
||||||
|
|
||||||
BackwardsScanner tk(tc, QString(), 400);
|
BackwardsScanner tk(tc, QString(), 400);
|
||||||
const int tokenCount = tk.startToken();
|
const int tokenCount = tk.startToken();
|
||||||
const int indentSize = tabSettings().m_indentSize;
|
|
||||||
|
|
||||||
if (tokenCount != 0) {
|
if (tokenCount != 0) {
|
||||||
const SimpleToken firstToken = tk[0];
|
const SimpleToken firstToken = tk[0];
|
||||||
|
|
||||||
if (firstToken.is(T_COLON)) {
|
if (firstToken.is(T_COLON)) {
|
||||||
const int indent = tk.indentation(-1) + // indentation of the previous newline
|
const int previousLineIndent = indentationColumn(ts, tk, -1);
|
||||||
indentSize;
|
ts.indentLine(block, previousLineIndent + ts.m_indentSize);
|
||||||
tabSettings().indentLine(block, indent);
|
|
||||||
return;
|
return;
|
||||||
} else if ((firstToken.is(T_PUBLIC) || firstToken.is(T_PROTECTED) || firstToken.is(T_PRIVATE) ||
|
} else if ((firstToken.is(T_PUBLIC) || firstToken.is(T_PROTECTED) || firstToken.is(T_PRIVATE) ||
|
||||||
firstToken.is(T_Q_SIGNALS) || firstToken.is(T_Q_SLOTS)) &&
|
firstToken.is(T_Q_SIGNALS) || firstToken.is(T_Q_SLOTS)) &&
|
||||||
tk.size() > 1 && tk[1].is(T_COLON)) {
|
tk.size() > 1 && tk[1].is(T_COLON)) {
|
||||||
const int startOfBlock = tk.startOfBlock(0);
|
const int startOfBlock = tk.startOfBlock(0);
|
||||||
if (startOfBlock != 0) {
|
if (startOfBlock != 0) {
|
||||||
const int indent = tk.indentation(startOfBlock);
|
const int indent = indentationColumn(ts, tk, startOfBlock);
|
||||||
tabSettings().indentLine(block, indent);
|
ts.indentLine(block, indent);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if (firstToken.is(T_CASE) || firstToken.is(T_DEFAULT)) {
|
} else if (firstToken.is(T_CASE) || firstToken.is(T_DEFAULT)) {
|
||||||
const int startOfBlock = tk.startOfBlock(0);
|
const int startOfBlock = tk.startOfBlock(0);
|
||||||
if (startOfBlock != 0) {
|
if (startOfBlock != 0) {
|
||||||
const int indent = tk.indentation(startOfBlock);
|
const int indent = indentationColumn(ts, tk, startOfBlock);
|
||||||
tabSettings().indentLine(block, indent);
|
ts.indentLine(block, indent);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -1641,15 +1648,15 @@ void CPPEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedCha
|
|||||||
const QString spell = tk.text(tokenIndex);
|
const QString spell = tk.text(tokenIndex);
|
||||||
if (tk[tokenIndex].followsNewline() && (spell.startsWith(QLatin1String("QT_")) ||
|
if (tk[tokenIndex].followsNewline() && (spell.startsWith(QLatin1String("QT_")) ||
|
||||||
spell.startsWith(QLatin1String("Q_")))) {
|
spell.startsWith(QLatin1String("Q_")))) {
|
||||||
const int indent = tk.indentation(tokenIndex);
|
const int indent = indentationColumn(ts, tk, tokenIndex);
|
||||||
tabSettings().indentLine(block, indent);
|
ts.indentLine(block, indent);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const TextEditor::TextBlockIterator begin(doc->begin());
|
const TextEditor::TextBlockIterator begin(doc->begin());
|
||||||
const TextEditor::TextBlockIterator end(block.next());
|
const TextEditor::TextBlockIterator end(block.next());
|
||||||
indentCPPBlock(tabSettings(), block, begin, end, typedChar);
|
indentCPPBlock(ts, block, begin, end, typedChar);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CPPEditor::event(QEvent *e)
|
bool CPPEditor::event(QEvent *e)
|
||||||
@@ -1682,9 +1689,6 @@ void CPPEditor::contextMenuEvent(QContextMenuEvent *e)
|
|||||||
foreach (QAction *action, contextMenu->actions())
|
foreach (QAction *action, contextMenu->actions())
|
||||||
menu->addAction(action);
|
menu->addAction(action);
|
||||||
|
|
||||||
const QList<QTextEdit::ExtraSelection> selections =
|
|
||||||
extraSelections(BaseTextEditor::CodeSemanticsSelection);
|
|
||||||
|
|
||||||
appendStandardContextMenuActions(menu);
|
appendStandardContextMenuActions(menu);
|
||||||
|
|
||||||
menu->exec(e->globalPos());
|
menu->exec(e->globalPos());
|
||||||
|
Reference in New Issue
Block a user