Commit 3b0a4380 authored by Leandro Melo's avatar Leandro Melo Committed by hjk
Browse files

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: default avatarQt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: default avatarRobert Löhning <robert.loehning@nokia.com>
Reviewed-by: default avatarhjk <qthjk@ovi.com>
parent 7b90c8b0
......@@ -288,7 +288,8 @@ void FakeVimOptionPage::copyTextEditorSettings()
m_ui.checkBoxExpandTab->setChecked(ts.m_spacesForTabs);
m_ui.spinBoxTabStop->setValue(ts.m_tabSize);
m_ui.spinBoxShiftWidth->setValue(ts.m_indentSize);
m_ui.checkBoxSmartTab->setChecked(ts.m_smartBackspace);
m_ui.checkBoxSmartTab->setChecked(
ts.m_smartBackspaceBehavior == TabSettings::BackspaceFollowsPreviousIndents);
m_ui.checkBoxAutoIndent->setChecked(true);
m_ui.checkBoxSmartIndent->setChecked(ts.m_autoIndent);
m_ui.checkBoxIncSearch->setChecked(true);
......
......@@ -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) {
......
......@@ -42,7 +42,7 @@
static const char spacesForTabsKey[] = "SpacesForTabs";
static const char autoSpacesForTabsKey[] = "AutoSpacesForTabs";
static const char smartBackspaceKey[] = "SmartBackspace";
static const char smartBackspaceBehaviorKey[] = "SmartBackspaceBehavior";
static const char autoIndentKey[] = "AutoIndent";
static const char tabSizeKey[] = "TabSize";
static const char indentSizeKey[] = "IndentSize";
......@@ -58,11 +58,11 @@ TabSettings::TabSettings() :
m_spacesForTabs(true),
m_autoSpacesForTabs(false),
m_autoIndent(true),
m_smartBackspace(false),
m_tabSize(8),
m_indentSize(4),
m_tabKeyBehavior(TabNeverIndents),
m_continuationAlignBehavior(ContinuationAlignWithSpaces)
m_continuationAlignBehavior(ContinuationAlignWithSpaces),
m_smartBackspaceBehavior(BackspaceNeverIndents)
{
}
......@@ -82,7 +82,7 @@ void TabSettings::toMap(const QString &prefix, QVariantMap *map) const
map->insert(prefix + QLatin1String(spacesForTabsKey), m_spacesForTabs);
map->insert(prefix + QLatin1String(autoSpacesForTabsKey), m_autoSpacesForTabs);
map->insert(prefix + QLatin1String(autoIndentKey), m_autoIndent);
map->insert(prefix + QLatin1String(smartBackspaceKey), m_smartBackspace);
map->insert(prefix + QLatin1String(smartBackspaceBehaviorKey), m_smartBackspaceBehavior);
map->insert(prefix + QLatin1String(tabSizeKey), m_tabSize);
map->insert(prefix + QLatin1String(indentSizeKey), m_indentSize);
map->insert(prefix + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior);
......@@ -96,8 +96,9 @@ void TabSettings::fromMap(const QString &prefix, const QVariantMap &map)
m_autoSpacesForTabs =
map.value(prefix + QLatin1String(autoSpacesForTabsKey), m_autoSpacesForTabs).toBool();
m_autoIndent = map.value(prefix + QLatin1String(autoIndentKey), m_autoIndent).toBool();
m_smartBackspace =
map.value(prefix + QLatin1String(smartBackspaceKey), m_smartBackspace).toBool();
m_smartBackspaceBehavior = (SmartBackspaceBehavior)
map.value(prefix + QLatin1String(smartBackspaceBehaviorKey),
m_smartBackspaceBehavior).toInt();
m_tabSize = map.value(prefix + QLatin1String(tabSizeKey), m_tabSize).toInt();
m_indentSize = map.value(prefix + QLatin1String(indentSizeKey), m_indentSize).toInt();
m_tabKeyBehavior = (TabKeyBehavior)
......@@ -396,7 +397,7 @@ bool TabSettings::equals(const TabSettings &ts) const
return m_spacesForTabs == ts.m_spacesForTabs
&& m_autoSpacesForTabs == ts.m_autoSpacesForTabs
&& m_autoIndent == ts.m_autoIndent
&& m_smartBackspace == ts.m_smartBackspace
&& m_smartBackspaceBehavior == ts.m_smartBackspaceBehavior
&& m_tabSize == ts.m_tabSize
&& m_indentSize == ts.m_indentSize
&& m_tabKeyBehavior == ts.m_tabKeyBehavior
......
......@@ -63,6 +63,13 @@ public:
ContinuationAlignWithIndent = 2
};
// This enum must match the indexes of smartBackspaceBehavior widget
enum SmartBackspaceBehavior {
BackspaceNeverIndents = 0,
BackspaceFollowsPreviousIndents = 1,
BackspaceUnindents = 2
};
TabSettings();
void toSettings(const QString &category, QSettings *s) const;
......@@ -96,11 +103,11 @@ public:
bool m_spacesForTabs;
bool m_autoSpacesForTabs;
bool m_autoIndent;
bool m_smartBackspace;
int m_tabSize;
int m_indentSize;
TabKeyBehavior m_tabKeyBehavior;
ContinuationAlignBehavior m_continuationAlignBehavior;
SmartBackspaceBehavior m_smartBackspaceBehavior;
bool equals(const TabSettings &ts) const;
};
......
......@@ -52,7 +52,7 @@ TabSettingsWidget::TabSettingsWidget(QWidget *parent) :
this, SLOT(slotSettingsChanged()));
connect(ui->autoIndent, SIGNAL(toggled(bool)),
this, SLOT(slotSettingsChanged()));
connect(ui->smartBackspace, SIGNAL(toggled(bool)),
connect(ui->smartBackspaceBehavior, SIGNAL(currentIndexChanged(int)),
this, SLOT(slotSettingsChanged()));
connect(ui->tabSize, SIGNAL(valueChanged(int)),
this, SLOT(slotSettingsChanged()));
......@@ -77,7 +77,7 @@ void TabSettingsWidget::setSettings(const TextEditor::TabSettings& s)
ui->insertSpaces->setChecked(s.m_spacesForTabs);
ui->autoInsertSpaces->setChecked(s.m_autoSpacesForTabs);
ui->autoIndent->setChecked(s.m_autoIndent);
ui->smartBackspace->setChecked(s.m_smartBackspace);
ui->smartBackspaceBehavior->setCurrentIndex(s.m_smartBackspaceBehavior);
ui->tabSize->setValue(s.m_tabSize);
ui->indentSize->setValue(s.m_indentSize);
ui->tabKeyBehavior->setCurrentIndex(s.m_tabKeyBehavior);
......@@ -94,11 +94,13 @@ TabSettings TabSettingsWidget::settings() const
set.m_spacesForTabs = ui->insertSpaces->isChecked();
set.m_autoSpacesForTabs = ui->autoInsertSpaces->isChecked();
set.m_autoIndent = ui->autoIndent->isChecked();
set.m_smartBackspace = ui->smartBackspace->isChecked();
set.m_smartBackspaceBehavior =
(TabSettings::SmartBackspaceBehavior)ui->smartBackspaceBehavior->currentIndex();
set.m_tabSize = ui->tabSize->value();
set.m_indentSize = ui->indentSize->value();
set.m_tabKeyBehavior = (TabSettings::TabKeyBehavior)(ui->tabKeyBehavior->currentIndex());
set.m_continuationAlignBehavior = (TabSettings::ContinuationAlignBehavior)(ui->continuationAlignBehavior->currentIndex());
set.m_continuationAlignBehavior =
(TabSettings::ContinuationAlignBehavior)(ui->continuationAlignBehavior->currentIndex());
return set;
}
......@@ -124,7 +126,7 @@ QString TabSettingsWidget::searchKeywords() const
<< sep << ui->insertSpaces->text()
<< sep << ui->autoInsertSpaces->text()
<< sep << ui->autoIndent->text()
<< sep << ui->smartBackspace->text()
<< sep << ui->smartBackspaceLabel->text()
<< sep << ui->tabSizeLabel->text()
<< sep << ui->indentSizeLabel->text()
<< sep << ui->tabKeyBehaviorLabel->text()
......
......@@ -166,24 +166,14 @@
</property>
</widget>
</item>
<item row="4" column="0" colspan="3">
<widget class="QCheckBox" name="smartBackspace">
<property name="toolTip">
<string>Backspace will go back one indentation level instead of one space.</string>
</property>
<property name="text">
<string>&amp;Backspace follows indentation</string>
</property>
</widget>
</item>
<item row="5" column="0" colspan="2">
<item row="6" column="0" colspan="2">
<widget class="QLabel" name="continuationAlignBehaviorLabel">
<property name="text">
<string>Align continuation lines:</string>
</property>
</widget>
</item>
<item row="6" column="1">
<item row="7" column="1">
<widget class="QComboBox" name="continuationAlignBehavior">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;
......@@ -229,14 +219,14 @@ Influences the indentation of continuation lines.
</item>
</widget>
</item>
<item row="7" column="0" colspan="2">
<item row="8" column="0" colspan="2">
<widget class="QLabel" name="tabKeyBehaviorLabel">
<property name="text">
<string>Tab key performs auto-indent:</string>
</property>
</widget>
</item>
<item row="8" column="1">
<item row="9" column="1">
<widget class="QComboBox" name="tabKeyBehavior">
<item>
<property name="text">
......@@ -255,7 +245,7 @@ Influences the indentation of continuation lines.
</item>
</widget>
</item>
<item row="9" column="1">
<item row="10" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
......@@ -268,6 +258,48 @@ Influences the indentation of continuation lines.
</property>
</spacer>
</item>
<item row="4" column="0" colspan="2">
<widget class="QLabel" name="smartBackspaceLabel">
<property name="text">
<string>Backspace indentation:</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QComboBox" name="smartBackspaceBehavior">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;
Specifies how backspace interacts with indentation.
&lt;ul&gt;
&lt;li&gt;None: No interaction at all. Regular plain backspace behavior.
&lt;/li&gt;
&lt;li&gt;Follows Previous Indents: In leading white space it will take the cursor back to the nearest indentation level used in previous lines.
&lt;/li&gt;
&lt;li&gt;Unindents: If the character behind the cursor is a space it behaves as a backtab.
&lt;/li&gt;
&lt;/ul&gt;&lt;/body&gt;&lt;/html&gt;
</string>
</property>
<item>
<property name="text">
<string>None</string>
</property>
</item>
<item>
<property name="text">
<string>Follows Previous Indents</string>
</property>
</item>
<item>
<property name="text">
<string>Unindents</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
......@@ -279,7 +311,6 @@ Influences the indentation of continuation lines.
<tabstop>tabSize</tabstop>
<tabstop>indentSize</tabstop>
<tabstop>autoIndent</tabstop>
<tabstop>smartBackspace</tabstop>
<tabstop>continuationAlignBehavior</tabstop>
<tabstop>tabKeyBehavior</tabstop>
</tabstops>
......
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