Commit ea0394a8 authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer
Browse files

Style fixes and small optimizations to guessSpacesForTabs

Save some cycles by not using QTextBlock::text unnecessarily (since only
its length and the character at the block position were used).
parent e64c255b
......@@ -229,8 +229,10 @@ int TabSettings::indentedColumn(int column, bool doIndent) const
return qMax(0, aligned - m_indentSize);
}
bool TabSettings::guessSpacesForTabs(const QTextBlock& _block) const {
bool TabSettings::guessSpacesForTabs(const QTextBlock &_block) const
{
if (m_spacesForTabs && m_autoSpacesForTabs && _block.isValid()) {
const QTextDocument *doc = _block.document();
QVector<QTextBlock> currentBlocks(2, _block); // [0] looks back; [1] looks forward
int maxLookAround = 100;
while (maxLookAround-- > 0) {
......@@ -239,12 +241,12 @@ bool TabSettings::guessSpacesForTabs(const QTextBlock& _block) const {
if (currentBlocks.at(1).isValid())
currentBlocks[1] = currentBlocks.at(1).next();
bool done = true;
foreach(QTextBlock block, currentBlocks) {
foreach (const QTextBlock &block, currentBlocks) {
if (block.isValid())
done = false;
if (!block.isValid() || block.text().isEmpty())
if (!block.isValid() || block.length() == 0)
continue;
QChar firstChar = block.text().at(0);
const QChar firstChar = doc->characterAt(block.position());
if (firstChar == QLatin1Char(' ')) {
return true;
} else if (firstChar == QLatin1Char('\t')) {
......@@ -258,7 +260,7 @@ bool TabSettings::guessSpacesForTabs(const QTextBlock& _block) const {
return m_spacesForTabs;
}
QString TabSettings::indentationString(int startColumn, int targetColumn, const QTextBlock& block) const
QString TabSettings::indentationString(int startColumn, int targetColumn, const QTextBlock &block) const
{
targetColumn = qMax(startColumn, targetColumn);
if (guessSpacesForTabs(block))
......
......@@ -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 QTextBlock& currentBlock = QTextBlock()) const;
QString indentationString(int startColumn, int targetColumn, const QTextBlock &currentBlock = QTextBlock()) const;
QString indentationString(const QString &text) const;
int indentationColumn(const QString &text) const;
......@@ -74,7 +74,7 @@ struct TEXTEDITOR_EXPORT TabSettings
int trailingWhitespaces(const QString &text) const;
bool isIndentationClean(const QTextBlock &block) const;
bool tabShouldIndent(const QTextDocument *document, QTextCursor cursor, int *suggestedPosition = 0) const;
bool guessSpacesForTabs(const QTextBlock& block) const;
bool guessSpacesForTabs(const QTextBlock &block) const;
bool m_spacesForTabs;
bool m_autoSpacesForTabs;
......
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