From 2a58049e6a99acb5b1525776ee04189c38817d0d Mon Sep 17 00:00:00 2001 From: Roopesh Chander <roop@forwardbias.in> Date: Sat, 3 Oct 2009 23:15:17 +0530 Subject: [PATCH] Ability to auto-determine whether we want spaces instead of tabs or not MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Based on the previous (referrable) line Merge-request: 1766 Reviewed-by: Thorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com> --- src/plugins/texteditor/tabsettings.cpp | 28 ++++++++++++++++++++++---- src/plugins/texteditor/tabsettings.h | 4 +++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/plugins/texteditor/tabsettings.cpp b/src/plugins/texteditor/tabsettings.cpp index c15c8bceee8..2f3721ff6b9 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 4f2b4785a68..66a45c76368 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; -- GitLab