diff --git a/src/plugins/texteditor/tabsettings.cpp b/src/plugins/texteditor/tabsettings.cpp index c15c8bceee89562ea83ff7ac3410649a8d2ff356..2f3721ff6b9355f3e2d13796e3b06933611c9448 100644 --- a/src/plugins/texteditor/tabsettings.cpp +++ b/src/plugins/texteditor/tabsettings.cpp @@ -48,6 +48,7 @@ namespace TextEditor { TabSettings::TabSettings() : m_spacesForTabs(true), + m_autoSpacesForTabs(false), m_autoIndent(true), m_smartBackspace(false), m_tabSize(8), @@ -223,10 +224,29 @@ int TabSettings::indentedColumn(int column, bool doIndent) const return qMax(0, aligned - m_indentSize); } -QString TabSettings::indentationString(int startColumn, int targetColumn) const +bool TabSettings::guessSpacesForTabs(const QTextBlock& _block) const { + if (m_autoSpacesForTabs && _block.isValid()) { + QTextBlock block = _block; + const QTextDocument* doc = block.document(); + while (block.isValid() && block != doc->begin()) { + 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; + } + } + } + return m_spacesForTabs; +} + +QString TabSettings::indentationString(int startColumn, int targetColumn, const QTextBlock& block) const { targetColumn = qMax(startColumn, targetColumn); - if (m_spacesForTabs) + if (guessSpacesForTabs(block)) return QString(targetColumn - startColumn, QLatin1Char(' ')); QString s; @@ -252,7 +272,7 @@ void TabSettings::indentLine(QTextBlock block, int newIndent) const if (indentationColumn(text) == newIndent) return; - const QString indentString = indentationString(0, newIndent); + const QString indentString = indentationString(0, newIndent, block); newIndent = indentString.length(); if (oldBlockLength == indentString.length() && text == indentString) @@ -278,7 +298,7 @@ void TabSettings::reindentLine(QTextBlock block, int delta) const if (oldIndent == newIndent) return; - const QString indentString = indentationString(0, newIndent); + const QString indentString = indentationString(0, newIndent, block); newIndent = indentString.length(); if (oldBlockLength == indentString.length() && text == indentString) diff --git a/src/plugins/texteditor/tabsettings.h b/src/plugins/texteditor/tabsettings.h index 4f2b4785a681c9ddc325cc24190a18875155bb07..66a45c76368f414bd80a2ddceb36bb687eb89cea 100644 --- a/src/plugins/texteditor/tabsettings.h +++ b/src/plugins/texteditor/tabsettings.h @@ -62,7 +62,7 @@ struct TEXTEDITOR_EXPORT TabSettings int columnAt(const QString &text, int position) const; int spacesLeftFromPosition(const QString &text, int position) const; int indentedColumn(int column, bool doIndent = true) const; - QString indentationString(int startColumn, int targetColumn) const; + QString indentationString(int startColumn, int targetColumn, const QTextBlock& block = QTextBlock()) const; QString indentationString(const QString &text) const; int indentationColumn(const QString &text) const; @@ -74,8 +74,10 @@ struct TEXTEDITOR_EXPORT TabSettings int trailingWhitespaces(const QString &text) const; bool isIndentationClean(const QString &text) const; bool tabShouldIndent(const QTextDocument *document, QTextCursor cursor, int *suggestedPosition = 0) const; + bool guessSpacesForTabs(const QTextBlock& block) const; bool m_spacesForTabs; + bool m_autoSpacesForTabs; bool m_autoIndent; bool m_smartBackspace; int m_tabSize;