forked from qt-creator/qt-creator
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).
This commit is contained in:
@@ -229,8 +229,10 @@ int TabSettings::indentedColumn(int column, bool doIndent) const
|
|||||||
return qMax(0, aligned - m_indentSize);
|
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()) {
|
if (m_spacesForTabs && m_autoSpacesForTabs && _block.isValid()) {
|
||||||
|
const QTextDocument *doc = _block.document();
|
||||||
QVector<QTextBlock> currentBlocks(2, _block); // [0] looks back; [1] looks forward
|
QVector<QTextBlock> currentBlocks(2, _block); // [0] looks back; [1] looks forward
|
||||||
int maxLookAround = 100;
|
int maxLookAround = 100;
|
||||||
while (maxLookAround-- > 0) {
|
while (maxLookAround-- > 0) {
|
||||||
@@ -239,12 +241,12 @@ bool TabSettings::guessSpacesForTabs(const QTextBlock& _block) const {
|
|||||||
if (currentBlocks.at(1).isValid())
|
if (currentBlocks.at(1).isValid())
|
||||||
currentBlocks[1] = currentBlocks.at(1).next();
|
currentBlocks[1] = currentBlocks.at(1).next();
|
||||||
bool done = true;
|
bool done = true;
|
||||||
foreach(QTextBlock block, currentBlocks) {
|
foreach (const QTextBlock &block, currentBlocks) {
|
||||||
if (block.isValid())
|
if (block.isValid())
|
||||||
done = false;
|
done = false;
|
||||||
if (!block.isValid() || block.text().isEmpty())
|
if (!block.isValid() || block.length() == 0)
|
||||||
continue;
|
continue;
|
||||||
QChar firstChar = block.text().at(0);
|
const QChar firstChar = doc->characterAt(block.position());
|
||||||
if (firstChar == QLatin1Char(' ')) {
|
if (firstChar == QLatin1Char(' ')) {
|
||||||
return true;
|
return true;
|
||||||
} else if (firstChar == QLatin1Char('\t')) {
|
} else if (firstChar == QLatin1Char('\t')) {
|
||||||
@@ -258,7 +260,7 @@ bool TabSettings::guessSpacesForTabs(const QTextBlock& _block) const {
|
|||||||
return m_spacesForTabs;
|
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);
|
targetColumn = qMax(startColumn, targetColumn);
|
||||||
if (guessSpacesForTabs(block))
|
if (guessSpacesForTabs(block))
|
||||||
|
|||||||
@@ -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 QTextBlock& currentBlock = QTextBlock()) const;
|
QString indentationString(int startColumn, int targetColumn, const QTextBlock ¤tBlock = 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,7 +74,7 @@ struct TEXTEDITOR_EXPORT TabSettings
|
|||||||
int trailingWhitespaces(const QString &text) const;
|
int trailingWhitespaces(const QString &text) const;
|
||||||
bool isIndentationClean(const QTextBlock &block) const;
|
bool isIndentationClean(const QTextBlock &block) 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 guessSpacesForTabs(const QTextBlock &block) const;
|
||||||
|
|
||||||
bool m_spacesForTabs;
|
bool m_spacesForTabs;
|
||||||
bool m_autoSpacesForTabs;
|
bool m_autoSpacesForTabs;
|
||||||
|
|||||||
Reference in New Issue
Block a user