Look both forward and backward for auto-determining spaces vs tabs

This commit is contained in:
Roopesh Chander
2009-10-31 15:29:30 +05:30
committed by Thorbjørn Lindeijer
parent 9822fb65df
commit bf2289127f
2 changed files with 21 additions and 14 deletions

View File

@@ -50,10 +50,10 @@
<bool>false</bool>
</property>
<property name="toolTip">
<string>Automatically determine whether to insert spaces or tabs based on the previous line in the file</string>
<string>Automatically determine based on the nearest indented line (previous line preferred over next line)</string>
</property>
<property name="text">
<string>Based on the previous line</string>
<string>Based on the surrounding lines</string>
</property>
</widget>
</item>

View File

@@ -231,12 +231,16 @@ int TabSettings::indentedColumn(int column, bool doIndent) const
bool TabSettings::guessSpacesForTabs(const QTextBlock& _block) const {
if (m_autoSpacesForTabs && _block.isValid()) {
QTextBlock block = _block;
const QTextDocument* doc = block.document();
int maxLookBack = 100;
while (block.isValid() && block != doc->begin() && maxLookBack-- > 0) {
block = block.previous();
if (block.text().isEmpty())
QVector<QTextBlock> currentBlocks(2, _block); // [0] looks back; [1] looks forward
int maxLookAround = 100;
while (maxLookAround-- > 0) {
currentBlocks[0] = currentBlocks.at(0).previous();
currentBlocks[1] = currentBlocks.at(1).next();
bool done = true;
foreach(QTextBlock block, currentBlocks) {
if (block.isValid())
done = false;
if (!block.isValid() || block.text().isEmpty())
continue;
QChar firstChar = block.text().at(0);
if (firstChar == QLatin1Char(' ')) {
@@ -245,6 +249,9 @@ bool TabSettings::guessSpacesForTabs(const QTextBlock& _block) const {
return false;
}
}
if (done)
break;
}
}
return m_spacesForTabs;
}