forked from qt-creator/qt-creator
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:
@@ -288,7 +288,8 @@ void FakeVimOptionPage::copyTextEditorSettings()
|
|||||||
m_ui.checkBoxExpandTab->setChecked(ts.m_spacesForTabs);
|
m_ui.checkBoxExpandTab->setChecked(ts.m_spacesForTabs);
|
||||||
m_ui.spinBoxTabStop->setValue(ts.m_tabSize);
|
m_ui.spinBoxTabStop->setValue(ts.m_tabSize);
|
||||||
m_ui.spinBoxShiftWidth->setValue(ts.m_indentSize);
|
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.checkBoxAutoIndent->setChecked(true);
|
||||||
m_ui.checkBoxSmartIndent->setChecked(ts.m_autoIndent);
|
m_ui.checkBoxSmartIndent->setChecked(ts.m_autoIndent);
|
||||||
m_ui.checkBoxIncSearch->setChecked(true);
|
m_ui.checkBoxIncSearch->setChecked(true);
|
||||||
|
@@ -4529,12 +4529,12 @@ void BaseTextEditorWidget::handleBackspaceKey()
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
bool handled = false;
|
bool handled = false;
|
||||||
if (!tabSettings.m_smartBackspace) {
|
if (tabSettings.m_smartBackspaceBehavior == TabSettings::BackspaceNeverIndents) {
|
||||||
if (cursorWithinSnippet)
|
if (cursorWithinSnippet)
|
||||||
cursor.beginEditBlock();
|
cursor.beginEditBlock();
|
||||||
cursor.deletePreviousChar();
|
cursor.deletePreviousChar();
|
||||||
handled = true;
|
handled = true;
|
||||||
} else {
|
} else if (tabSettings.m_smartBackspaceBehavior == TabSettings::BackspaceFollowsPreviousIndents) {
|
||||||
QTextBlock currentBlock = cursor.block();
|
QTextBlock currentBlock = cursor.block();
|
||||||
int positionInBlock = pos - currentBlock.position();
|
int positionInBlock = pos - currentBlock.position();
|
||||||
const QString blockText = currentBlock.text();
|
const QString blockText = currentBlock.text();
|
||||||
@@ -4544,9 +4544,12 @@ void BaseTextEditorWidget::handleBackspaceKey()
|
|||||||
cursor.deletePreviousChar();
|
cursor.deletePreviousChar();
|
||||||
handled = true;
|
handled = true;
|
||||||
} else {
|
} else {
|
||||||
|
if (cursorWithinSnippet) {
|
||||||
|
d->m_snippetOverlay->clear();
|
||||||
|
cursorWithinSnippet = false;
|
||||||
|
}
|
||||||
int previousIndent = 0;
|
int previousIndent = 0;
|
||||||
const int indent = tabSettings.columnAt(blockText, positionInBlock);
|
const int indent = tabSettings.columnAt(blockText, positionInBlock);
|
||||||
|
|
||||||
for (QTextBlock previousNonEmptyBlock = currentBlock.previous();
|
for (QTextBlock previousNonEmptyBlock = currentBlock.previous();
|
||||||
previousNonEmptyBlock.isValid();
|
previousNonEmptyBlock.isValid();
|
||||||
previousNonEmptyBlock = previousNonEmptyBlock.previous()) {
|
previousNonEmptyBlock = previousNonEmptyBlock.previous()) {
|
||||||
@@ -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) {
|
if (!handled) {
|
||||||
|
@@ -42,7 +42,7 @@
|
|||||||
|
|
||||||
static const char spacesForTabsKey[] = "SpacesForTabs";
|
static const char spacesForTabsKey[] = "SpacesForTabs";
|
||||||
static const char autoSpacesForTabsKey[] = "AutoSpacesForTabs";
|
static const char autoSpacesForTabsKey[] = "AutoSpacesForTabs";
|
||||||
static const char smartBackspaceKey[] = "SmartBackspace";
|
static const char smartBackspaceBehaviorKey[] = "SmartBackspaceBehavior";
|
||||||
static const char autoIndentKey[] = "AutoIndent";
|
static const char autoIndentKey[] = "AutoIndent";
|
||||||
static const char tabSizeKey[] = "TabSize";
|
static const char tabSizeKey[] = "TabSize";
|
||||||
static const char indentSizeKey[] = "IndentSize";
|
static const char indentSizeKey[] = "IndentSize";
|
||||||
@@ -58,11 +58,11 @@ TabSettings::TabSettings() :
|
|||||||
m_spacesForTabs(true),
|
m_spacesForTabs(true),
|
||||||
m_autoSpacesForTabs(false),
|
m_autoSpacesForTabs(false),
|
||||||
m_autoIndent(true),
|
m_autoIndent(true),
|
||||||
m_smartBackspace(false),
|
|
||||||
m_tabSize(8),
|
m_tabSize(8),
|
||||||
m_indentSize(4),
|
m_indentSize(4),
|
||||||
m_tabKeyBehavior(TabNeverIndents),
|
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(spacesForTabsKey), m_spacesForTabs);
|
||||||
map->insert(prefix + QLatin1String(autoSpacesForTabsKey), m_autoSpacesForTabs);
|
map->insert(prefix + QLatin1String(autoSpacesForTabsKey), m_autoSpacesForTabs);
|
||||||
map->insert(prefix + QLatin1String(autoIndentKey), m_autoIndent);
|
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(tabSizeKey), m_tabSize);
|
||||||
map->insert(prefix + QLatin1String(indentSizeKey), m_indentSize);
|
map->insert(prefix + QLatin1String(indentSizeKey), m_indentSize);
|
||||||
map->insert(prefix + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior);
|
map->insert(prefix + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior);
|
||||||
@@ -96,8 +96,9 @@ void TabSettings::fromMap(const QString &prefix, const QVariantMap &map)
|
|||||||
m_autoSpacesForTabs =
|
m_autoSpacesForTabs =
|
||||||
map.value(prefix + QLatin1String(autoSpacesForTabsKey), m_autoSpacesForTabs).toBool();
|
map.value(prefix + QLatin1String(autoSpacesForTabsKey), m_autoSpacesForTabs).toBool();
|
||||||
m_autoIndent = map.value(prefix + QLatin1String(autoIndentKey), m_autoIndent).toBool();
|
m_autoIndent = map.value(prefix + QLatin1String(autoIndentKey), m_autoIndent).toBool();
|
||||||
m_smartBackspace =
|
m_smartBackspaceBehavior = (SmartBackspaceBehavior)
|
||||||
map.value(prefix + QLatin1String(smartBackspaceKey), m_smartBackspace).toBool();
|
map.value(prefix + QLatin1String(smartBackspaceBehaviorKey),
|
||||||
|
m_smartBackspaceBehavior).toInt();
|
||||||
m_tabSize = map.value(prefix + QLatin1String(tabSizeKey), m_tabSize).toInt();
|
m_tabSize = map.value(prefix + QLatin1String(tabSizeKey), m_tabSize).toInt();
|
||||||
m_indentSize = map.value(prefix + QLatin1String(indentSizeKey), m_indentSize).toInt();
|
m_indentSize = map.value(prefix + QLatin1String(indentSizeKey), m_indentSize).toInt();
|
||||||
m_tabKeyBehavior = (TabKeyBehavior)
|
m_tabKeyBehavior = (TabKeyBehavior)
|
||||||
@@ -396,7 +397,7 @@ bool TabSettings::equals(const TabSettings &ts) const
|
|||||||
return m_spacesForTabs == ts.m_spacesForTabs
|
return m_spacesForTabs == ts.m_spacesForTabs
|
||||||
&& m_autoSpacesForTabs == ts.m_autoSpacesForTabs
|
&& m_autoSpacesForTabs == ts.m_autoSpacesForTabs
|
||||||
&& m_autoIndent == ts.m_autoIndent
|
&& m_autoIndent == ts.m_autoIndent
|
||||||
&& m_smartBackspace == ts.m_smartBackspace
|
&& m_smartBackspaceBehavior == ts.m_smartBackspaceBehavior
|
||||||
&& m_tabSize == ts.m_tabSize
|
&& m_tabSize == ts.m_tabSize
|
||||||
&& m_indentSize == ts.m_indentSize
|
&& m_indentSize == ts.m_indentSize
|
||||||
&& m_tabKeyBehavior == ts.m_tabKeyBehavior
|
&& m_tabKeyBehavior == ts.m_tabKeyBehavior
|
||||||
|
@@ -63,6 +63,13 @@ public:
|
|||||||
ContinuationAlignWithIndent = 2
|
ContinuationAlignWithIndent = 2
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// This enum must match the indexes of smartBackspaceBehavior widget
|
||||||
|
enum SmartBackspaceBehavior {
|
||||||
|
BackspaceNeverIndents = 0,
|
||||||
|
BackspaceFollowsPreviousIndents = 1,
|
||||||
|
BackspaceUnindents = 2
|
||||||
|
};
|
||||||
|
|
||||||
TabSettings();
|
TabSettings();
|
||||||
|
|
||||||
void toSettings(const QString &category, QSettings *s) const;
|
void toSettings(const QString &category, QSettings *s) const;
|
||||||
@@ -96,11 +103,11 @@ public:
|
|||||||
bool m_spacesForTabs;
|
bool m_spacesForTabs;
|
||||||
bool m_autoSpacesForTabs;
|
bool m_autoSpacesForTabs;
|
||||||
bool m_autoIndent;
|
bool m_autoIndent;
|
||||||
bool m_smartBackspace;
|
|
||||||
int m_tabSize;
|
int m_tabSize;
|
||||||
int m_indentSize;
|
int m_indentSize;
|
||||||
TabKeyBehavior m_tabKeyBehavior;
|
TabKeyBehavior m_tabKeyBehavior;
|
||||||
ContinuationAlignBehavior m_continuationAlignBehavior;
|
ContinuationAlignBehavior m_continuationAlignBehavior;
|
||||||
|
SmartBackspaceBehavior m_smartBackspaceBehavior;
|
||||||
|
|
||||||
bool equals(const TabSettings &ts) const;
|
bool equals(const TabSettings &ts) const;
|
||||||
};
|
};
|
||||||
|
@@ -52,7 +52,7 @@ TabSettingsWidget::TabSettingsWidget(QWidget *parent) :
|
|||||||
this, SLOT(slotSettingsChanged()));
|
this, SLOT(slotSettingsChanged()));
|
||||||
connect(ui->autoIndent, SIGNAL(toggled(bool)),
|
connect(ui->autoIndent, SIGNAL(toggled(bool)),
|
||||||
this, SLOT(slotSettingsChanged()));
|
this, SLOT(slotSettingsChanged()));
|
||||||
connect(ui->smartBackspace, SIGNAL(toggled(bool)),
|
connect(ui->smartBackspaceBehavior, SIGNAL(currentIndexChanged(int)),
|
||||||
this, SLOT(slotSettingsChanged()));
|
this, SLOT(slotSettingsChanged()));
|
||||||
connect(ui->tabSize, SIGNAL(valueChanged(int)),
|
connect(ui->tabSize, SIGNAL(valueChanged(int)),
|
||||||
this, SLOT(slotSettingsChanged()));
|
this, SLOT(slotSettingsChanged()));
|
||||||
@@ -77,7 +77,7 @@ void TabSettingsWidget::setSettings(const TextEditor::TabSettings& s)
|
|||||||
ui->insertSpaces->setChecked(s.m_spacesForTabs);
|
ui->insertSpaces->setChecked(s.m_spacesForTabs);
|
||||||
ui->autoInsertSpaces->setChecked(s.m_autoSpacesForTabs);
|
ui->autoInsertSpaces->setChecked(s.m_autoSpacesForTabs);
|
||||||
ui->autoIndent->setChecked(s.m_autoIndent);
|
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->tabSize->setValue(s.m_tabSize);
|
||||||
ui->indentSize->setValue(s.m_indentSize);
|
ui->indentSize->setValue(s.m_indentSize);
|
||||||
ui->tabKeyBehavior->setCurrentIndex(s.m_tabKeyBehavior);
|
ui->tabKeyBehavior->setCurrentIndex(s.m_tabKeyBehavior);
|
||||||
@@ -94,11 +94,13 @@ TabSettings TabSettingsWidget::settings() const
|
|||||||
set.m_spacesForTabs = ui->insertSpaces->isChecked();
|
set.m_spacesForTabs = ui->insertSpaces->isChecked();
|
||||||
set.m_autoSpacesForTabs = ui->autoInsertSpaces->isChecked();
|
set.m_autoSpacesForTabs = ui->autoInsertSpaces->isChecked();
|
||||||
set.m_autoIndent = ui->autoIndent->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_tabSize = ui->tabSize->value();
|
||||||
set.m_indentSize = ui->indentSize->value();
|
set.m_indentSize = ui->indentSize->value();
|
||||||
set.m_tabKeyBehavior = (TabSettings::TabKeyBehavior)(ui->tabKeyBehavior->currentIndex());
|
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;
|
return set;
|
||||||
}
|
}
|
||||||
@@ -124,7 +126,7 @@ QString TabSettingsWidget::searchKeywords() const
|
|||||||
<< sep << ui->insertSpaces->text()
|
<< sep << ui->insertSpaces->text()
|
||||||
<< sep << ui->autoInsertSpaces->text()
|
<< sep << ui->autoInsertSpaces->text()
|
||||||
<< sep << ui->autoIndent->text()
|
<< sep << ui->autoIndent->text()
|
||||||
<< sep << ui->smartBackspace->text()
|
<< sep << ui->smartBackspaceLabel->text()
|
||||||
<< sep << ui->tabSizeLabel->text()
|
<< sep << ui->tabSizeLabel->text()
|
||||||
<< sep << ui->indentSizeLabel->text()
|
<< sep << ui->indentSizeLabel->text()
|
||||||
<< sep << ui->tabKeyBehaviorLabel->text()
|
<< sep << ui->tabKeyBehaviorLabel->text()
|
||||||
|
@@ -166,24 +166,14 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="4" column="0" colspan="3">
|
<item row="6" column="0" colspan="2">
|
||||||
<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>&Backspace follows indentation</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="5" column="0" colspan="2">
|
|
||||||
<widget class="QLabel" name="continuationAlignBehaviorLabel">
|
<widget class="QLabel" name="continuationAlignBehaviorLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Align continuation lines:</string>
|
<string>Align continuation lines:</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="6" column="1">
|
<item row="7" column="1">
|
||||||
<widget class="QComboBox" name="continuationAlignBehavior">
|
<widget class="QComboBox" name="continuationAlignBehavior">
|
||||||
<property name="toolTip">
|
<property name="toolTip">
|
||||||
<string><html><head/><body>
|
<string><html><head/><body>
|
||||||
@@ -229,14 +219,14 @@ Influences the indentation of continuation lines.
|
|||||||
</item>
|
</item>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="7" column="0" colspan="2">
|
<item row="8" column="0" colspan="2">
|
||||||
<widget class="QLabel" name="tabKeyBehaviorLabel">
|
<widget class="QLabel" name="tabKeyBehaviorLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Tab key performs auto-indent:</string>
|
<string>Tab key performs auto-indent:</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="8" column="1">
|
<item row="9" column="1">
|
||||||
<widget class="QComboBox" name="tabKeyBehavior">
|
<widget class="QComboBox" name="tabKeyBehavior">
|
||||||
<item>
|
<item>
|
||||||
<property name="text">
|
<property name="text">
|
||||||
@@ -255,7 +245,7 @@ Influences the indentation of continuation lines.
|
|||||||
</item>
|
</item>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="9" column="1">
|
<item row="10" column="1">
|
||||||
<spacer name="verticalSpacer_2">
|
<spacer name="verticalSpacer_2">
|
||||||
<property name="orientation">
|
<property name="orientation">
|
||||||
<enum>Qt::Vertical</enum>
|
<enum>Qt::Vertical</enum>
|
||||||
@@ -268,6 +258,48 @@ Influences the indentation of continuation lines.
|
|||||||
</property>
|
</property>
|
||||||
</spacer>
|
</spacer>
|
||||||
</item>
|
</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><html><head/><body>
|
||||||
|
Specifies how backspace interacts with indentation.
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>None: No interaction at all. Regular plain backspace behavior.
|
||||||
|
</li>
|
||||||
|
|
||||||
|
<li>Follows Previous Indents: In leading white space it will take the cursor back to the nearest indentation level used in previous lines.
|
||||||
|
</li>
|
||||||
|
|
||||||
|
<li>Unindents: If the character behind the cursor is a space it behaves as a backtab.
|
||||||
|
</li>
|
||||||
|
</ul></body></html>
|
||||||
|
</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>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
@@ -279,7 +311,6 @@ Influences the indentation of continuation lines.
|
|||||||
<tabstop>tabSize</tabstop>
|
<tabstop>tabSize</tabstop>
|
||||||
<tabstop>indentSize</tabstop>
|
<tabstop>indentSize</tabstop>
|
||||||
<tabstop>autoIndent</tabstop>
|
<tabstop>autoIndent</tabstop>
|
||||||
<tabstop>smartBackspace</tabstop>
|
|
||||||
<tabstop>continuationAlignBehavior</tabstop>
|
<tabstop>continuationAlignBehavior</tabstop>
|
||||||
<tabstop>tabKeyBehavior</tabstop>
|
<tabstop>tabKeyBehavior</tabstop>
|
||||||
</tabstops>
|
</tabstops>
|
||||||
|
Reference in New Issue
Block a user