Indenter: Add an option to control the padding style.

Reviewed-by: Thorbjorn Lindeijer
This commit is contained in:
Christian Kamm
2010-09-13 12:36:18 +02:00
parent 642b6fb0d3
commit 26114a23d0
4 changed files with 72 additions and 19 deletions

View File

@@ -158,6 +158,7 @@ void BehaviorSettingsPage::settingsFromUI(TabSettings &tabSettings,
tabSettings.m_doubleIndentBlocks = m_d->m_page.indentBlocksBehavior->currentIndex() >= 2; tabSettings.m_doubleIndentBlocks = m_d->m_page.indentBlocksBehavior->currentIndex() >= 2;
tabSettings.m_tabKeyBehavior = (TabSettings::TabKeyBehavior)m_d->m_page.tabKeyBehavior->currentIndex(); tabSettings.m_tabKeyBehavior = (TabSettings::TabKeyBehavior)m_d->m_page.tabKeyBehavior->currentIndex();
tabSettings.m_paddingMode = (TabSettings::PaddingMode)m_d->m_page.paddingMode->currentIndex();
storageSettings.m_cleanWhitespace = m_d->m_page.cleanWhitespace->isChecked(); storageSettings.m_cleanWhitespace = m_d->m_page.cleanWhitespace->isChecked();
storageSettings.m_inEntireDocument = m_d->m_page.inEntireDocument->isChecked(); storageSettings.m_inEntireDocument = m_d->m_page.inEntireDocument->isChecked();
@@ -181,6 +182,7 @@ void BehaviorSettingsPage::settingsToUI()
(tabSettings.m_doubleIndentBlocks ? 2 : 1) (tabSettings.m_doubleIndentBlocks ? 2 : 1)
: 0); : 0);
m_d->m_page.tabKeyBehavior->setCurrentIndex(tabSettings.m_tabKeyBehavior); m_d->m_page.tabKeyBehavior->setCurrentIndex(tabSettings.m_tabKeyBehavior);
m_d->m_page.paddingMode->setCurrentIndex(tabSettings.m_paddingMode);
const StorageSettings &storageSettings = m_d->m_storageSettings; const StorageSettings &storageSettings = m_d->m_storageSettings;
m_d->m_page.cleanWhitespace->setChecked(storageSettings.m_cleanWhitespace); m_d->m_page.cleanWhitespace->setChecked(storageSettings.m_cleanWhitespace);

View File

@@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>550</width> <width>576</width>
<height>464</height> <height>538</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
@@ -179,14 +179,14 @@
</item> </item>
</widget> </widget>
</item> </item>
<item row="1" column="0"> <item row="2" column="0">
<widget class="QLabel" name="tabKeyIndentLabel"> <widget class="QLabel" name="tabKeyIndentLabel">
<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="1" column="1"> <item row="2" column="1">
<widget class="QComboBox" name="tabKeyBehavior"> <widget class="QComboBox" name="tabKeyBehavior">
<item> <item>
<property name="text"> <property name="text">
@@ -205,6 +205,41 @@
</item> </item>
</widget> </widget>
</item> </item>
<item row="1" column="0">
<widget class="QLabel" name="paddingStyleLabel">
<property name="text">
<string>Padding style:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="paddingMode">
<property name="toolTip">
<string>Padding is the part of indentation exceeding logical indentation and is usually used to align consecutive lines of code.
Disable Padding: Do not pad at all. Lines will only be indented to the current logical indentation depth.
Pad With Spaces: Always use spaces for padding, regardless of the choice between tabs and spaces above.
Pad With Indentation: Padding will be part of the indentation and follow the settings above.</string>
</property>
<item>
<property name="text">
<string>Disable Padding</string>
</property>
</item>
<item>
<property name="text">
<string>Pad With Spaces</string>
</property>
</item>
<item>
<property name="text">
<string>Pad With Indentation</string>
</property>
</item>
</widget>
</item>
</layout> </layout>
</item> </item>
</layout> </layout>
@@ -339,10 +374,6 @@
</spacer> </spacer>
</item> </item>
</layout> </layout>
<zorder>groupBoxTabAndIndentSettings</zorder>
<zorder>groupBoxStorageSettings</zorder>
<zorder>groupBoxMouse</zorder>
<zorder>indentBlocksLabel</zorder>
</widget> </widget>
<tabstops> <tabstops>
<tabstop>insertSpaces</tabstop> <tabstop>insertSpaces</tabstop>
@@ -364,12 +395,12 @@
<slot>setEnabled(bool)</slot> <slot>setEnabled(bool)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>67</x> <x>87</x>
<y>252</y> <y>323</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>148</x> <x>205</x>
<y>281</y> <y>353</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>
@@ -380,12 +411,12 @@
<slot>setEnabled(bool)</slot> <slot>setEnabled(bool)</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>40</x> <x>60</x>
<y>251</y> <y>323</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>77</x> <x>134</x>
<y>310</y> <y>384</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>

View File

@@ -45,6 +45,7 @@ static const char *indentBracesKey = "IndentBraces";
static const char *doubleIndentBlocksKey = "DoubleIndentBlocks"; static const char *doubleIndentBlocksKey = "DoubleIndentBlocks";
static const char *tabKeyBehaviorKey = "TabKeyBehavior"; static const char *tabKeyBehaviorKey = "TabKeyBehavior";
static const char *groupPostfix = "TabSettings"; static const char *groupPostfix = "TabSettings";
static const char *paddingModeKey = "PaddingMode";
namespace TextEditor { namespace TextEditor {
@@ -57,7 +58,8 @@ TabSettings::TabSettings() :
m_indentSize(4), m_indentSize(4),
m_indentBraces(false), m_indentBraces(false),
m_doubleIndentBlocks(false), m_doubleIndentBlocks(false),
m_tabKeyBehavior(TabNeverIndents) m_tabKeyBehavior(TabNeverIndents),
m_paddingMode(PadWithSpaces)
{ {
} }
@@ -76,6 +78,7 @@ void TabSettings::toSettings(const QString &category, QSettings *s) const
s->setValue(QLatin1String(indentBracesKey), m_indentBraces); s->setValue(QLatin1String(indentBracesKey), m_indentBraces);
s->setValue(QLatin1String(doubleIndentBlocksKey), m_doubleIndentBlocks); s->setValue(QLatin1String(doubleIndentBlocksKey), m_doubleIndentBlocks);
s->setValue(QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior); s->setValue(QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior);
s->setValue(QLatin1String(paddingModeKey), m_paddingMode);
s->endGroup(); s->endGroup();
} }
@@ -99,6 +102,7 @@ void TabSettings::fromSettings(const QString &category, const QSettings *s)
= s->value(group + QLatin1String(doubleIndentBlocksKey), m_doubleIndentBlocks).toBool(); = s->value(group + QLatin1String(doubleIndentBlocksKey), m_doubleIndentBlocks).toBool();
m_tabKeyBehavior = (TabKeyBehavior)s->value(group + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior).toInt(); m_tabKeyBehavior = (TabKeyBehavior)s->value(group + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior).toInt();
m_paddingMode = (PaddingMode)s->value(group + QLatin1String(paddingModeKey), m_paddingMode).toInt();
} }
@@ -319,13 +323,20 @@ void TabSettings::indentLine(QTextBlock block, int newIndent, int padding) const
const QString text = block.text(); const QString text = block.text();
const int oldBlockLength = text.size(); const int oldBlockLength = text.size();
if (m_paddingMode == DisablePadding) {
newIndent -= padding;
padding = 0;
} else if (m_paddingMode == PadWithIndent) {
padding = 0;
}
// Quickly check whether indenting is required. // Quickly check whether indenting is required.
if (indentationColumn(text) == newIndent) if (indentationColumn(text) == newIndent)
return; return;
QString indentString; QString indentString;
if (!m_spacesForTabs && m_tabSize == m_indentSize) { if (!m_spacesForTabs) {
// user likes tabs for spaces and uses tabs for indentation, preserve padding // user likes tabs for spaces and uses tabs for indentation, preserve padding
indentString = indentationString(0, newIndent - padding, block); indentString = indentationString(0, newIndent - padding, block);
indentString += QString(padding, QLatin1Char(' ')); indentString += QString(padding, QLatin1Char(' '));
@@ -388,7 +399,8 @@ bool TabSettings::equals(const TabSettings &ts) const
&& m_indentSize == ts.m_indentSize && m_indentSize == ts.m_indentSize
&& m_indentBraces == ts.m_indentBraces && m_indentBraces == ts.m_indentBraces
&& m_doubleIndentBlocks == ts.m_doubleIndentBlocks && m_doubleIndentBlocks == ts.m_doubleIndentBlocks
&& m_tabKeyBehavior == ts.m_tabKeyBehavior; && m_tabKeyBehavior == ts.m_tabKeyBehavior
&& m_paddingMode == ts.m_paddingMode;
} }
} // namespace TextEditor } // namespace TextEditor

View File

@@ -52,6 +52,13 @@ public:
TabLeadingWhitespaceIndents = 2 TabLeadingWhitespaceIndents = 2
}; };
// This enum must match the indexes of paddingMode widget
enum PaddingMode {
DisablePadding = 0,
PadWithSpaces = 1,
PadWithIndent = 2
};
TabSettings(); TabSettings();
void toSettings(const QString &category, QSettings *s) const; void toSettings(const QString &category, QSettings *s) const;
@@ -89,6 +96,7 @@ public:
bool m_indentBraces; bool m_indentBraces;
bool m_doubleIndentBlocks; bool m_doubleIndentBlocks;
TabKeyBehavior m_tabKeyBehavior; TabKeyBehavior m_tabKeyBehavior;
PaddingMode m_paddingMode;
bool equals(const TabSettings &ts) const; bool equals(const TabSettings &ts) const;
}; };