From bf2289127fcb527c45f28c13f8a54229bc73b332 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Sat, 31 Oct 2009 15:29:30 +0530 Subject: [PATCH] Look both forward and backward for auto-determining spaces vs tabs --- .../texteditor/behaviorsettingspage.ui | 4 +-- src/plugins/texteditor/tabsettings.cpp | 31 ++++++++++++------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/plugins/texteditor/behaviorsettingspage.ui b/src/plugins/texteditor/behaviorsettingspage.ui index eddd849c261..349d272a949 100644 --- a/src/plugins/texteditor/behaviorsettingspage.ui +++ b/src/plugins/texteditor/behaviorsettingspage.ui @@ -50,10 +50,10 @@ false - Automatically determine whether to insert spaces or tabs based on the previous line in the file + Automatically determine based on the nearest indented line (previous line preferred over next line) - Based on the previous line + Based on the surrounding lines diff --git a/src/plugins/texteditor/tabsettings.cpp b/src/plugins/texteditor/tabsettings.cpp index efc40526947..f5bf66eecd9 100644 --- a/src/plugins/texteditor/tabsettings.cpp +++ b/src/plugins/texteditor/tabsettings.cpp @@ -231,19 +231,26 @@ 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()) - continue; - QChar firstChar = block.text().at(0); - if (firstChar == QLatin1Char(' ')) { - return true; - } else if (firstChar == QLatin1Char('\t')) { - return false; + QVector 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(' ')) { + return true; + } else if (firstChar == QLatin1Char('\t')) { + return false; + } } + if (done) + break; } } return m_spacesForTabs;