forked from qt-creator/qt-creator
Make indentation/tabbing more developer friendly:
- when doing backspace, preserve the previous indentation litterally - when having auto-indentation turned off, simply repeat the previous indentation litterally instead of jumping to 0
This commit is contained in:
@@ -887,13 +887,18 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
|
|||||||
QTextCursor cursor = textCursor();
|
QTextCursor cursor = textCursor();
|
||||||
if (d->m_inBlockSelectionMode)
|
if (d->m_inBlockSelectionMode)
|
||||||
cursor.clearSelection();
|
cursor.clearSelection();
|
||||||
if (d->m_document->tabSettings().m_autoIndent) {
|
const TabSettings &ts = d->m_document->tabSettings();
|
||||||
|
if (ts.m_autoIndent) {
|
||||||
cursor.beginEditBlock();
|
cursor.beginEditBlock();
|
||||||
cursor.insertBlock();
|
cursor.insertBlock();
|
||||||
indent(document(), cursor, QChar::Null);
|
indent(document(), cursor, QChar::Null);
|
||||||
cursor.endEditBlock();
|
cursor.endEditBlock();
|
||||||
} else {
|
} else {
|
||||||
|
cursor.beginEditBlock();
|
||||||
|
QString previousBlockText = cursor.block().text();
|
||||||
cursor.insertBlock();
|
cursor.insertBlock();
|
||||||
|
cursor.insertText(ts.indentationString(previousBlockText));
|
||||||
|
cursor.endEditBlock();
|
||||||
}
|
}
|
||||||
e->accept();
|
e->accept();
|
||||||
setTextCursor(cursor);
|
setTextCursor(cursor);
|
||||||
@@ -3189,17 +3194,14 @@ void BaseTextEditor::handleBackspaceKey()
|
|||||||
continue;
|
continue;
|
||||||
previousIndent = tabSettings.columnAt(previousNonEmptyBlockText,
|
previousIndent = tabSettings.columnAt(previousNonEmptyBlockText,
|
||||||
tabSettings.firstNonSpace(previousNonEmptyBlockText));
|
tabSettings.firstNonSpace(previousNonEmptyBlockText));
|
||||||
if (previousIndent < indent)
|
if (previousIndent < indent) {
|
||||||
|
cursor.beginEditBlock();
|
||||||
|
cursor.setPosition(currentBlock.position(), QTextCursor::KeepAnchor);
|
||||||
|
cursor.insertText(tabSettings.indentationString(previousNonEmptyBlockText));
|
||||||
|
cursor.endEditBlock();
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (previousIndent >= indent)
|
|
||||||
previousIndent = 0;
|
|
||||||
|
|
||||||
cursor.beginEditBlock();
|
|
||||||
cursor.setPosition(currentBlock.position(), QTextCursor::KeepAnchor);
|
|
||||||
cursor.insertText(tabSettings.indentationString(0, previousIndent));
|
|
||||||
cursor.endEditBlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseTextEditor::wheelEvent(QWheelEvent *e)
|
void BaseTextEditor::wheelEvent(QWheelEvent *e)
|
||||||
|
|||||||
@@ -110,6 +110,12 @@ int TabSettings::firstNonSpace(const QString &text) const
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString TabSettings::indentationString(const QString &text) const
|
||||||
|
{
|
||||||
|
return text.left(firstNonSpace(text));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int TabSettings::trailingWhitespaces(const QString &text) const
|
int TabSettings::trailingWhitespaces(const QString &text) const
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ struct TEXTEDITOR_EXPORT TabSettings
|
|||||||
int spacesLeftFromPosition(const QString &text, int position) const;
|
int spacesLeftFromPosition(const QString &text, int position) const;
|
||||||
int indentedColumn(int column, bool doIndent = true) const;
|
int indentedColumn(int column, bool doIndent = true) const;
|
||||||
QString indentationString(int startColumn, int targetColumn) const;
|
QString indentationString(int startColumn, int targetColumn) const;
|
||||||
|
QString indentationString(const QString &text) const;
|
||||||
|
|
||||||
void indentLine(QTextBlock block, int newIndent) const;
|
void indentLine(QTextBlock block, int newIndent) const;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user