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();

View File

@@ -31,7 +31,8 @@ public:
void autoIndent(const QTextCursor &cursor,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
Utils::Text::Replacements format(const TextEditor::RangesInLines &rangesInLines) override;
Utils::Text::Replacements format(const TextEditor::RangesInLines &rangesInLines,
FormattingMode mode = FormattingMode::Forced) override;
void indentBlock(const QTextBlock &block,
const QChar &typedChar,

View File

@@ -174,9 +174,9 @@ void ClangFormatForwardingIndenter::autoIndent(const QTextCursor &cursor,
}
Utils::Text::Replacements ClangFormatForwardingIndenter::format(
const TextEditor::RangesInLines &rangesInLines)
const TextEditor::RangesInLines &rangesInLines, FormattingMode mode)
{
return currentIndenter()->format(rangesInLines);
return currentIndenter()->format(rangesInLines, mode);
}

View File

@@ -40,7 +40,8 @@ public:
void autoIndent(const QTextCursor &cursor,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
Utils::Text::Replacements format(const TextEditor::RangesInLines &rangesInLines) override;
Utils::Text::Replacements format(const TextEditor::RangesInLines &rangesInLines,
FormattingMode mode) override;
bool formatOnSave() const override;
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
const TextEditor::TabSettings &tabSettings,

View File

@@ -128,6 +128,9 @@ bool CppCodeStyleSettings::equals(const CppCodeStyleSettings &rhs) const
&& extraPaddingForConditionsIfConfusingAlign == rhs.extraPaddingForConditionsIfConfusingAlign
&& alignAssignments == rhs.alignAssignments
&& preferGetterNameWithoutGetPrefix == rhs.preferGetterNameWithoutGetPrefix
#ifdef WITH_TESTS
&& forceFormatting == rhs.forceFormatting
#endif
;
}

View File

@@ -62,6 +62,10 @@ public:
// CppEditor/QuickFixSetting. Remove in 4.16
bool preferGetterNameWithoutGetPrefix = true;
#ifdef WITH_TESTS
bool forceFormatting = false;
#endif
QVariantMap toMap() const;
void fromMap(const QVariantMap &map);

View File

@@ -467,7 +467,7 @@ bool CppEditorDocument::saveImpl(QString *errorString, const FilePath &filePath,
if (!editedRanges.empty()) {
QTextCursor cursor(document());
cursor.joinPreviousEditBlock();
indenter()->format(editedRanges);
indenter()->format(editedRanges, Indenter::FormattingMode::Forced);
cursor.endEditBlock();
}

View File

@@ -5392,7 +5392,13 @@ SpaceBeforeParens: Always
CppTestDocument::create("file.cpp", origSource, expectedSource)});
InsertDefsFromDecls factory;
factory.setMode(InsertDefsFromDecls::Mode::Impl);
CppCodeStylePreferences * const prefs = CppToolsSettings::instance()->cppCodeStyle();
const CppCodeStyleSettings settings = prefs->codeStyleSettings();
CppCodeStyleSettings tempSettings = settings;
tempSettings.forceFormatting = true;
prefs->setCodeStyleSettings(tempSettings);
QuickFixOperationTest(testDocuments, &factory, {}, {}, {}, clangFormatSettings);
prefs->setCodeStyleSettings(settings);
}
// Function for one of InsertDeclDef section cases

View File

@@ -70,7 +70,9 @@ public:
indent(cursor, QChar::Null, tabSettings, cursorPositionInEditor);
}
virtual Utils::Text::Replacements format(const RangesInLines & /*rangesInLines*/)
enum class FormattingMode { Forced, Settings };
virtual Utils::Text::Replacements format(const RangesInLines &,
FormattingMode = FormattingMode::Forced)
{
return Utils::Text::Replacements();
}

View File

@@ -473,7 +473,13 @@ void RefactoringFile::doFormatting()
b = b.next();
}
}
m_editor->textDocument()->indenter()->format(formattingRanges);
// TODO: The proper solution seems to be to call formatOrIndent() here (and not
// use hardcoded indent regions anymore), but that would require intrusive changes
// to the C++ quickfixes and tests, where we rely on the built-in indenter behavior.
m_editor->textDocument()->indenter()->format(formattingRanges,
Indenter::FormattingMode::Settings);
for (QTextBlock b = m_editor->document()->findBlockByNumber(
formattingRanges.front().startLine - 1); b.isValid(); b = b.next()) {
QString blockText = b.text();