forked from qt-creator/qt-creator
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:
@@ -8,6 +8,7 @@
|
|||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
|
|
||||||
#include <cppeditor/cppcodestylepreferences.h>
|
#include <cppeditor/cppcodestylepreferences.h>
|
||||||
|
#include <cppeditor/cpptoolssettings.h>
|
||||||
|
|
||||||
#include <projectexplorer/editorconfiguration.h>
|
#include <projectexplorer/editorconfiguration.h>
|
||||||
#include <projectexplorer/project.h>
|
#include <projectexplorer/project.h>
|
||||||
@@ -538,8 +539,16 @@ Utils::Text::Replacements ClangFormatBaseIndenter::replacements(QByteArray buffe
|
|||||||
}
|
}
|
||||||
|
|
||||||
Utils::Text::Replacements ClangFormatBaseIndenter::format(
|
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 {});
|
QTC_ASSERT(!m_fileName.isEmpty(), return {});
|
||||||
if (rangesInLines.empty())
|
if (rangesInLines.empty())
|
||||||
return Utils::Text::Replacements();
|
return Utils::Text::Replacements();
|
||||||
|
@@ -31,7 +31,8 @@ public:
|
|||||||
void autoIndent(const QTextCursor &cursor,
|
void autoIndent(const QTextCursor &cursor,
|
||||||
const TextEditor::TabSettings &tabSettings,
|
const TextEditor::TabSettings &tabSettings,
|
||||||
int cursorPositionInEditor = -1) override;
|
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,
|
void indentBlock(const QTextBlock &block,
|
||||||
const QChar &typedChar,
|
const QChar &typedChar,
|
||||||
|
@@ -174,9 +174,9 @@ void ClangFormatForwardingIndenter::autoIndent(const QTextCursor &cursor,
|
|||||||
}
|
}
|
||||||
|
|
||||||
Utils::Text::Replacements ClangFormatForwardingIndenter::format(
|
Utils::Text::Replacements ClangFormatForwardingIndenter::format(
|
||||||
const TextEditor::RangesInLines &rangesInLines)
|
const TextEditor::RangesInLines &rangesInLines, FormattingMode mode)
|
||||||
{
|
{
|
||||||
return currentIndenter()->format(rangesInLines);
|
return currentIndenter()->format(rangesInLines, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -40,7 +40,8 @@ public:
|
|||||||
void autoIndent(const QTextCursor &cursor,
|
void autoIndent(const QTextCursor &cursor,
|
||||||
const TextEditor::TabSettings &tabSettings,
|
const TextEditor::TabSettings &tabSettings,
|
||||||
int cursorPositionInEditor = -1) override;
|
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;
|
bool formatOnSave() const override;
|
||||||
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
|
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
|
||||||
const TextEditor::TabSettings &tabSettings,
|
const TextEditor::TabSettings &tabSettings,
|
||||||
|
@@ -128,6 +128,9 @@ bool CppCodeStyleSettings::equals(const CppCodeStyleSettings &rhs) const
|
|||||||
&& extraPaddingForConditionsIfConfusingAlign == rhs.extraPaddingForConditionsIfConfusingAlign
|
&& extraPaddingForConditionsIfConfusingAlign == rhs.extraPaddingForConditionsIfConfusingAlign
|
||||||
&& alignAssignments == rhs.alignAssignments
|
&& alignAssignments == rhs.alignAssignments
|
||||||
&& preferGetterNameWithoutGetPrefix == rhs.preferGetterNameWithoutGetPrefix
|
&& preferGetterNameWithoutGetPrefix == rhs.preferGetterNameWithoutGetPrefix
|
||||||
|
#ifdef WITH_TESTS
|
||||||
|
&& forceFormatting == rhs.forceFormatting
|
||||||
|
#endif
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -62,6 +62,10 @@ public:
|
|||||||
// CppEditor/QuickFixSetting. Remove in 4.16
|
// CppEditor/QuickFixSetting. Remove in 4.16
|
||||||
bool preferGetterNameWithoutGetPrefix = true;
|
bool preferGetterNameWithoutGetPrefix = true;
|
||||||
|
|
||||||
|
#ifdef WITH_TESTS
|
||||||
|
bool forceFormatting = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
QVariantMap toMap() const;
|
QVariantMap toMap() const;
|
||||||
void fromMap(const QVariantMap &map);
|
void fromMap(const QVariantMap &map);
|
||||||
|
|
||||||
|
@@ -467,7 +467,7 @@ bool CppEditorDocument::saveImpl(QString *errorString, const FilePath &filePath,
|
|||||||
if (!editedRanges.empty()) {
|
if (!editedRanges.empty()) {
|
||||||
QTextCursor cursor(document());
|
QTextCursor cursor(document());
|
||||||
cursor.joinPreviousEditBlock();
|
cursor.joinPreviousEditBlock();
|
||||||
indenter()->format(editedRanges);
|
indenter()->format(editedRanges, Indenter::FormattingMode::Forced);
|
||||||
cursor.endEditBlock();
|
cursor.endEditBlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -5392,7 +5392,13 @@ SpaceBeforeParens: Always
|
|||||||
CppTestDocument::create("file.cpp", origSource, expectedSource)});
|
CppTestDocument::create("file.cpp", origSource, expectedSource)});
|
||||||
InsertDefsFromDecls factory;
|
InsertDefsFromDecls factory;
|
||||||
factory.setMode(InsertDefsFromDecls::Mode::Impl);
|
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);
|
QuickFixOperationTest(testDocuments, &factory, {}, {}, {}, clangFormatSettings);
|
||||||
|
prefs->setCodeStyleSettings(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function for one of InsertDeclDef section cases
|
// Function for one of InsertDeclDef section cases
|
||||||
|
@@ -70,7 +70,9 @@ public:
|
|||||||
indent(cursor, QChar::Null, tabSettings, cursorPositionInEditor);
|
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();
|
return Utils::Text::Replacements();
|
||||||
}
|
}
|
||||||
|
@@ -473,7 +473,13 @@ void RefactoringFile::doFormatting()
|
|||||||
b = b.next();
|
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(
|
for (QTextBlock b = m_editor->document()->findBlockByNumber(
|
||||||
formattingRanges.front().startLine - 1); b.isValid(); b = b.next()) {
|
formattingRanges.front().startLine - 1); b.isValid(); b = b.next()) {
|
||||||
QString blockText = b.text();
|
QString blockText = b.text();
|
||||||
|
Reference in New Issue
Block a user