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

Ability to auto-determine whether we want spaces instead of tabs or not



Based on the previous (referrable) line

Merge-request: 1766
Reviewed-by: default avatarThorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com>
parent 7391ba38
...@@ -48,6 +48,7 @@ namespace TextEditor { ...@@ -48,6 +48,7 @@ namespace TextEditor {
TabSettings::TabSettings() : TabSettings::TabSettings() :
m_spacesForTabs(true), m_spacesForTabs(true),
m_autoSpacesForTabs(false),
m_autoIndent(true), m_autoIndent(true),
m_smartBackspace(false), m_smartBackspace(false),
m_tabSize(8), m_tabSize(8),
...@@ -223,10 +224,29 @@ int TabSettings::indentedColumn(int column, bool doIndent) const ...@@ -223,10 +224,29 @@ int TabSettings::indentedColumn(int column, bool doIndent) const
return qMax(0, aligned - m_indentSize); 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); targetColumn = qMax(startColumn, targetColumn);
if (m_spacesForTabs) if (guessSpacesForTabs(block))
return QString(targetColumn - startColumn, QLatin1Char(' ')); return QString(targetColumn - startColumn, QLatin1Char(' '));
QString s; QString s;
...@@ -252,7 +272,7 @@ void TabSettings::indentLine(QTextBlock block, int newIndent) const ...@@ -252,7 +272,7 @@ void TabSettings::indentLine(QTextBlock block, int newIndent) const
if (indentationColumn(text) == newIndent) if (indentationColumn(text) == newIndent)
return; return;
const QString indentString = indentationString(0, newIndent); const QString indentString = indentationString(0, newIndent, block);
newIndent = indentString.length(); newIndent = indentString.length();
if (oldBlockLength == indentString.length() && text == indentString) if (oldBlockLength == indentString.length() && text == indentString)
...@@ -278,7 +298,7 @@ void TabSettings::reindentLine(QTextBlock block, int delta) const ...@@ -278,7 +298,7 @@ void TabSettings::reindentLine(QTextBlock block, int delta) const
if (oldIndent == newIndent) if (oldIndent == newIndent)
return; return;
const QString indentString = indentationString(0, newIndent); const QString indentString = indentationString(0, newIndent, block);
newIndent = indentString.length(); newIndent = indentString.length();
if (oldBlockLength == indentString.length() && text == indentString) if (oldBlockLength == indentString.length() && text == indentString)
......
...@@ -62,7 +62,7 @@ struct TEXTEDITOR_EXPORT TabSettings ...@@ -62,7 +62,7 @@ struct TEXTEDITOR_EXPORT TabSettings
int columnAt(const QString &text, int position) const; int columnAt(const QString &text, int position) const;
int spacesLeftFromPosition(const QString &text, int position) const; int spacesLeftFromPosition(const QString &text, int position) const;
int indentedColumn(int column, bool doIndent = true) 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; QString indentationString(const QString &text) const;
int indentationColumn(const QString &text) const; int indentationColumn(const QString &text) const;
...@@ -74,8 +74,10 @@ struct TEXTEDITOR_EXPORT TabSettings ...@@ -74,8 +74,10 @@ struct TEXTEDITOR_EXPORT TabSettings
int trailingWhitespaces(const QString &text) const; int trailingWhitespaces(const QString &text) const;
bool isIndentationClean(const QString &text) const; bool isIndentationClean(const QString &text) const;
bool tabShouldIndent(const QTextDocument *document, QTextCursor cursor, int *suggestedPosition = 0) const; bool tabShouldIndent(const QTextDocument *document, QTextCursor cursor, int *suggestedPosition = 0) const;
bool guessSpacesForTabs(const QTextBlock& block) const;
bool m_spacesForTabs; bool m_spacesForTabs;
bool m_autoSpacesForTabs;
bool m_autoIndent; bool m_autoIndent;
bool m_smartBackspace; bool m_smartBackspace;
int m_tabSize; int m_tabSize;
......
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