forked from qt-creator/qt-creator
Ability to auto-determine whether we want spaces instead of tabs or not
Based on the previous (referrable) line Merge-request: 1766 Reviewed-by: Thorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com>
This commit is contained in:
committed by
Thorbjørn Lindeijer
parent
7391ba388c
commit
2a58049e6a
@@ -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
|
|||||||
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
|
|||||||
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
|
|||||||
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
|
|||||||
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
|
|||||||
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user