Editors: Enhance smart backspace behavior

Adds a new smart backspace behavior option. Now it's also possible
to simply unindent (like a backtab). This is particularly useful
when the cursor is not inside an "indentation area" but the user
still wants to go backwards by indent levels when possible (for
example before a comment that appears after the code line). The
option also allows the user to reach a new indent level which has
not been seen so far in previous lines.

The original follows indentation user setting will be lost with this
patch, but we consider this ok for not very "significant" settings.

Change-Id: I49efb6b0309d9b7d7ff2a589413446bc16fb753c
Reviewed-on: http://codereview.qt.nokia.com/3105
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Robert Löhning <robert.loehning@nokia.com>
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
Leandro Melo
2011-08-17 12:31:15 +02:00
committed by hjk
parent 7b90c8b0c5
commit 3b0a43802e
6 changed files with 93 additions and 35 deletions

View File

@@ -4529,12 +4529,12 @@ void BaseTextEditorWidget::handleBackspaceKey()
return;
bool handled = false;
if (!tabSettings.m_smartBackspace) {
if (tabSettings.m_smartBackspaceBehavior == TabSettings::BackspaceNeverIndents) {
if (cursorWithinSnippet)
cursor.beginEditBlock();
cursor.deletePreviousChar();
handled = true;
} else {
} else if (tabSettings.m_smartBackspaceBehavior == TabSettings::BackspaceFollowsPreviousIndents) {
QTextBlock currentBlock = cursor.block();
int positionInBlock = pos - currentBlock.position();
const QString blockText = currentBlock.text();
@@ -4544,9 +4544,12 @@ void BaseTextEditorWidget::handleBackspaceKey()
cursor.deletePreviousChar();
handled = true;
} else {
if (cursorWithinSnippet) {
d->m_snippetOverlay->clear();
cursorWithinSnippet = false;
}
int previousIndent = 0;
const int indent = tabSettings.columnAt(blockText, positionInBlock);
for (QTextBlock previousNonEmptyBlock = currentBlock.previous();
previousNonEmptyBlock.isValid();
previousNonEmptyBlock = previousNonEmptyBlock.previous()) {
@@ -4554,8 +4557,8 @@ void BaseTextEditorWidget::handleBackspaceKey()
if (previousNonEmptyBlockText.trimmed().isEmpty())
continue;
previousIndent =
tabSettings.columnAt(previousNonEmptyBlockText,
tabSettings.firstNonSpace(previousNonEmptyBlockText));
tabSettings.columnAt(previousNonEmptyBlockText,
tabSettings.firstNonSpace(previousNonEmptyBlockText));
if (previousIndent < indent) {
cursor.beginEditBlock();
cursor.setPosition(currentBlock.position(), QTextCursor::KeepAnchor);
@@ -4566,6 +4569,19 @@ void BaseTextEditorWidget::handleBackspaceKey()
}
}
}
} else if (tabSettings.m_smartBackspaceBehavior == TabSettings::BackspaceUnindents) {
if (!pos || !characterAt(pos - 1).isSpace()) {
if (cursorWithinSnippet)
cursor.beginEditBlock();
cursor.deletePreviousChar();
} else {
if (cursorWithinSnippet) {
d->m_snippetOverlay->clear();
cursorWithinSnippet = false;
}
indentOrUnindent(false);
}
handled = true;
}
if (!handled) {