CppEditor: Format quickfix code only if formatting is enabled

... in the ClangFormat settings (rather than the default of just
indenting).
As opposed to files generated by the wizard, quickfixes are often touching
existing code, and since ClangFormat works on line granularity, users
will experience unexpected re-formattings when ClangFormat is in indent-
only mode.
Therefore, do the formatting only if the user has enabled it in the
ClangFormat settings.

Change-Id: Icb30f166f2b6fb94113a8f25c4a5f92ff8bca9b0
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Christian Kandeler
2023-06-30 15:16:28 +02:00
parent 64c8c37bd4
commit a39119669c
10 changed files with 40 additions and 8 deletions

View File

@@ -8,6 +8,7 @@
#include <coreplugin/icore.h>
#include <cppeditor/cppcodestylepreferences.h>
#include <cppeditor/cpptoolssettings.h>
#include <projectexplorer/editorconfiguration.h>
#include <projectexplorer/project.h>
@@ -538,8 +539,16 @@ Utils::Text::Replacements ClangFormatBaseIndenter::replacements(QByteArray buffe
}
Utils::Text::Replacements ClangFormatBaseIndenter::format(
const TextEditor::RangesInLines &rangesInLines)
const TextEditor::RangesInLines &rangesInLines, FormattingMode mode)
{
bool doFormatting = mode == FormattingMode::Forced || formatCodeInsteadOfIndent();
#ifdef WITH_TESTS
doFormatting = doFormatting || CppEditor::CppToolsSettings::instance()->cppCodeStyle()
->codeStyleSettings().forceFormatting;
#endif
if (!doFormatting)
return {};
QTC_ASSERT(!m_fileName.isEmpty(), return {});
if (rangesInLines.empty())
return Utils::Text::Replacements();