TextEditor: Fix tab detection for short+simpler files

It was only detecting the usage of tabs if there were at least 3
*different* indentations with tabs. So a file that had all lines
starting with tab wasn't detected to have tab indentation.

In the case that we checked the whole file, it makes sense to assume
that any indentation that we find should be considered for the setting
for the whole file.

If we only took a sample of the whole file, we do not want to make a
guess for the whole file from only one or two indented lines.

Amends 08a66b7780

Change-Id: I615a35c7ad4e7e9ab3537eb76aad544603b47c4c
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2025-02-14 12:49:35 +01:00
parent d3c1fe6b6d
commit 1ca2ecdb3c

View File

@@ -91,10 +91,13 @@ TabSettings TabSettings::autoDetect(const QTextDocument *document) const
};
const int blockCount = document->blockCount();
bool useDefault = true;
if (blockCount < 200) {
// check the indentation of all blocks if the document is shorter than 200 lines
for (QTextBlock block = document->firstBlock(); block.isValid(); block = block.next())
checkText(block);
// We checked all, so if we find any indented line, it makes sense to use it:
useDefault = totalIndentations == 0;
} else {
// scanning the first and last 25 lines specifically since those most probably contain
// different indentations
@@ -115,9 +118,12 @@ TabSettings TabSettings::autoDetect(const QTextDocument *document) const
const int blockNummer = gen.bounded(startEndDelta + 1, blockCount - startEndDelta - 2);
checkText(document->findBlockByNumber(blockNummer));
}
// Don't determine indentation for the whole file from few actually indented lines that we
// managed to find:
useDefault = totalIndentations < 3;
}
if (indentCount.size() < 3)
if (useDefault)
return *this;
// find the most common indent