From 1ca2ecdb3c812ece0cb0f50de21f8ed3a8f7e438 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Fri, 14 Feb 2025 12:49:35 +0100 Subject: [PATCH] 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 08a66b778064ece5f01ee3323d8a5b5171b02c09 Change-Id: I615a35c7ad4e7e9ab3537eb76aad544603b47c4c Reviewed-by: David Schulz --- src/plugins/texteditor/tabsettings.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/plugins/texteditor/tabsettings.cpp b/src/plugins/texteditor/tabsettings.cpp index debc9e199ac..ea982bad9ef 100644 --- a/src/plugins/texteditor/tabsettings.cpp +++ b/src/plugins/texteditor/tabsettings.cpp @@ -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