Commit bf228912 authored by Roopesh Chander's avatar Roopesh Chander Committed by Thorbjørn Lindeijer
Browse files

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

parent 9822fb65
...@@ -50,10 +50,10 @@ ...@@ -50,10 +50,10 @@
<bool>false</bool> <bool>false</bool>
</property> </property>
<property name="toolTip"> <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>
<property name="text"> <property name="text">
<string>Based on the previous line</string> <string>Based on the surrounding lines</string>
</property> </property>
</widget> </widget>
</item> </item>
......
...@@ -231,19 +231,26 @@ int TabSettings::indentedColumn(int column, bool doIndent) const ...@@ -231,19 +231,26 @@ int TabSettings::indentedColumn(int column, bool doIndent) const
bool TabSettings::guessSpacesForTabs(const QTextBlock& _block) const { bool TabSettings::guessSpacesForTabs(const QTextBlock& _block) const {
if (m_autoSpacesForTabs && _block.isValid()) { if (m_autoSpacesForTabs && _block.isValid()) {
QTextBlock block = _block; QVector<QTextBlock> currentBlocks(2, _block); // [0] looks back; [1] looks forward
const QTextDocument* doc = block.document(); int maxLookAround = 100;
int maxLookBack = 100; while (maxLookAround-- > 0) {
while (block.isValid() && block != doc->begin() && maxLookBack-- > 0) { currentBlocks[0] = currentBlocks.at(0).previous();
block = block.previous(); currentBlocks[1] = currentBlocks.at(1).next();
if (block.text().isEmpty()) bool done = true;
continue; foreach(QTextBlock block, currentBlocks) {
QChar firstChar = block.text().at(0); if (block.isValid())
if (firstChar == QLatin1Char(' ')) { done = false;
return true; if (!block.isValid() || block.text().isEmpty())
} else if (firstChar == QLatin1Char('\t')) { continue;
return false; QChar firstChar = block.text().at(0);
if (firstChar == QLatin1Char(' ')) {
return true;
} else if (firstChar == QLatin1Char('\t')) {
return false;
}
} }
if (done)
break;
} }
} }
return m_spacesForTabs; return m_spacesForTabs;
......
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