CppEditor: Let users provide statement macros

Like ClangFormat has.

Fixes: QTCREATORBUG-15069
Fixes: QTCREATORBUG-18789
Change-Id: I0ffb70be502d1c73aaaf436484ddc6704f152621
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2024-02-23 14:31:30 +01:00
parent cdd85477fb
commit 2524e2adb3
6 changed files with 54 additions and 3 deletions

View File

@@ -842,7 +842,8 @@ bool CodeFormatter::tryDeclaration()
if (tokenText.startsWith(QLatin1String("Q_")) if (tokenText.startsWith(QLatin1String("Q_"))
|| tokenText.startsWith(QLatin1String("QT_")) || tokenText.startsWith(QLatin1String("QT_"))
|| tokenText.startsWith(QLatin1String("QML_")) || tokenText.startsWith(QLatin1String("QML_"))
|| tokenText.startsWith(QLatin1String("QDOC_"))) { || tokenText.startsWith(QLatin1String("QDOC_"))
|| m_statementMacros.contains(tokenText)) {
enter(qt_like_macro); enter(qt_like_macro);
return true; return true;
} }
@@ -1118,6 +1119,7 @@ QtStyleCodeFormatter::QtStyleCodeFormatter(const TabSettings &tabSettings,
, m_styleSettings(settings) , m_styleSettings(settings)
{ {
setTabSize(tabSettings.m_tabSize); setTabSize(tabSettings.m_tabSize);
setStatementMacros(m_styleSettings.statementMacros);
} }
void QtStyleCodeFormatter::setTabSettings(const TabSettings &tabSettings) void QtStyleCodeFormatter::setTabSettings(const TabSettings &tabSettings)
@@ -1129,6 +1131,7 @@ void QtStyleCodeFormatter::setTabSettings(const TabSettings &tabSettings)
void QtStyleCodeFormatter::setCodeStyleSettings(const CppCodeStyleSettings &settings) void QtStyleCodeFormatter::setCodeStyleSettings(const CppCodeStyleSettings &settings)
{ {
m_styleSettings = settings; m_styleSettings = settings;
setStatementMacros(m_styleSettings.statementMacros);
} }
void QtStyleCodeFormatter::saveBlockData(QTextBlock *block, const BlockData &data) const void QtStyleCodeFormatter::saveBlockData(QTextBlock *block, const BlockData &data) const

View File

@@ -40,6 +40,7 @@ public:
void indentForNewLineAfter(const QTextBlock &block, int *indent, int *padding); void indentForNewLineAfter(const QTextBlock &block, int *indent, int *padding);
void setTabSize(int tabSize); void setTabSize(int tabSize);
void setStatementMacros(const QStringList &macros) { m_statementMacros = macros; }
void invalidateCache(QTextDocument *document); void invalidateCache(QTextDocument *document);
@@ -224,6 +225,7 @@ private:
int m_paddingDepth = 0; int m_paddingDepth = 0;
int m_tabSize = 4; int m_tabSize = 4;
QStringList m_statementMacros;
friend class Internal::CppCodeFormatterData; friend class Internal::CppCodeFormatterData;
}; };

View File

@@ -17,6 +17,7 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
static const char statementMacrosKey[] = "StatementMacros";
static const char indentBlockBracesKey[] = "IndentBlockBraces"; static const char indentBlockBracesKey[] = "IndentBlockBraces";
static const char indentBlockBodyKey[] = "IndentBlockBody"; static const char indentBlockBodyKey[] = "IndentBlockBody";
static const char indentClassBracesKey[] = "IndentClassBraces"; static const char indentClassBracesKey[] = "IndentClassBraces";
@@ -50,6 +51,7 @@ CppCodeStyleSettings::CppCodeStyleSettings() = default;
Store CppCodeStyleSettings::toMap() const Store CppCodeStyleSettings::toMap() const
{ {
return { return {
{statementMacrosKey, statementMacros},
{indentBlockBracesKey, indentBlockBraces}, {indentBlockBracesKey, indentBlockBraces},
{indentBlockBodyKey, indentBlockBody}, {indentBlockBodyKey, indentBlockBody},
{indentClassBracesKey, indentClassBraces}, {indentClassBracesKey, indentClassBraces},
@@ -76,6 +78,7 @@ Store CppCodeStyleSettings::toMap() const
void CppCodeStyleSettings::fromMap(const Store &map) void CppCodeStyleSettings::fromMap(const Store &map)
{ {
statementMacros = map.value(statementMacrosKey, statementMacros).toStringList();
indentBlockBraces = map.value(indentBlockBracesKey, indentBlockBraces).toBool(); indentBlockBraces = map.value(indentBlockBracesKey, indentBlockBraces).toBool();
indentBlockBody = map.value(indentBlockBodyKey, indentBlockBody).toBool(); indentBlockBody = map.value(indentBlockBodyKey, indentBlockBody).toBool();
indentClassBraces = map.value(indentClassBracesKey, indentClassBraces).toBool(); indentClassBraces = map.value(indentClassBracesKey, indentClassBraces).toBool();
@@ -128,6 +131,7 @@ bool CppCodeStyleSettings::equals(const CppCodeStyleSettings &rhs) const
&& bindStarToRightSpecifier == rhs.bindStarToRightSpecifier && bindStarToRightSpecifier == rhs.bindStarToRightSpecifier
&& extraPaddingForConditionsIfConfusingAlign == rhs.extraPaddingForConditionsIfConfusingAlign && extraPaddingForConditionsIfConfusingAlign == rhs.extraPaddingForConditionsIfConfusingAlign
&& alignAssignments == rhs.alignAssignments && alignAssignments == rhs.alignAssignments
&& statementMacros == rhs.statementMacros
&& preferGetterNameWithoutGetPrefix == rhs.preferGetterNameWithoutGetPrefix && preferGetterNameWithoutGetPrefix == rhs.preferGetterNameWithoutGetPrefix
#ifdef WITH_TESTS #ifdef WITH_TESTS
&& forceFormatting == rhs.forceFormatting && forceFormatting == rhs.forceFormatting

View File

@@ -18,6 +18,7 @@ class CPPEDITOR_EXPORT CppCodeStyleSettings
public: public:
CppCodeStyleSettings(); CppCodeStyleSettings();
QStringList statementMacros;
bool indentBlockBraces = false; bool indentBlockBraces = false;
bool indentBlockBody = true; bool indentBlockBody = true;
bool indentClassBraces = false; bool indentClassBraces = false;

View File

@@ -163,8 +163,9 @@ public:
, m_bindStarToRightSpecifier(createCheckBox(Tr::tr("Right const/volatile"), , m_bindStarToRightSpecifier(createCheckBox(Tr::tr("Right const/volatile"),
Tr::tr("This does not apply to references."))) Tr::tr("This does not apply to references.")))
, m_tabSettingsWidget(new TabSettingsWidget) , m_tabSettingsWidget(new TabSettingsWidget)
, m_statementMacros(new QPlainTextEdit)
{ {
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
sizePolicy.setHorizontalStretch(0); sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0); sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(m_tabSettingsWidget->sizePolicy().hasHeightForWidth()); sizePolicy.setHeightForWidth(m_tabSettingsWidget->sizePolicy().hasHeightForWidth());
@@ -241,11 +242,25 @@ public:
} }
}; };
sizePolicy.setVerticalPolicy(QSizePolicy::Preferred);
m_statementMacros->setToolTip(
Tr::tr("Macros that can be used as statements without a trailing semicolon"));
m_statementMacros->setSizePolicy(sizePolicy);
const Group statementMacrosGroup {
title(Tr::tr("Statement macros")),
Column { m_statementMacros}
};
QObject::connect(m_statementMacros, &QPlainTextEdit::textChanged, q, [this] {
m_handlingStatementMacroChange = true;
q->slotCodeStyleSettingsChanged();
m_handlingStatementMacroChange = false;
});
Row { Row {
TabWidget { TabWidget {
bindTo(&m_categoryTab), bindTo(&m_categoryTab),
Tab { Tr::tr("General"), Tab { Tr::tr("General"),
Row { Column { m_tabSettingsWidget, st }, createPreview(0) } Row { Column { m_tabSettingsWidget, statementMacrosGroup }, createPreview(0) }
}, },
Tab { Tr::tr("Content"), Row { contentGroup, createPreview(1) } }, Tab { Tr::tr("Content"), Row { contentGroup, createPreview(1) } },
Tab { Tr::tr("Braces"), Row { bracesGroup, createPreview(2) } }, Tab { Tr::tr("Braces"), Row { bracesGroup, createPreview(2) } },
@@ -310,6 +325,8 @@ public:
QTabWidget *m_categoryTab = nullptr; QTabWidget *m_categoryTab = nullptr;
TabSettingsWidget *m_tabSettingsWidget = nullptr; TabSettingsWidget *m_tabSettingsWidget = nullptr;
QPlainTextEdit * const m_statementMacros;
bool m_handlingStatementMacroChange = false;
}; };
CppCodeStylePreferencesWidget::CppCodeStylePreferencesWidget(QWidget *parent) CppCodeStylePreferencesWidget::CppCodeStylePreferencesWidget(QWidget *parent)
@@ -361,6 +378,10 @@ CppCodeStyleSettings CppCodeStylePreferencesWidget::cppCodeStyleSettings() const
{ {
CppCodeStyleSettings set; CppCodeStyleSettings set;
set.statementMacros
= Utils::transform(d->m_statementMacros->toPlainText().trimmed().split('\n',
Qt::SkipEmptyParts),
[](const QString &line) { return line.trimmed(); });
set.indentBlockBraces = d->m_indentBlockBraces->isChecked(); set.indentBlockBraces = d->m_indentBlockBraces->isChecked();
set.indentBlockBody = d->m_indentBlockBody->isChecked(); set.indentBlockBody = d->m_indentBlockBody->isChecked();
set.indentClassBraces = d->m_indentClassBraces->isChecked(); set.indentClassBraces = d->m_indentClassBraces->isChecked();
@@ -399,6 +420,8 @@ void CppCodeStylePreferencesWidget::setCodeStyleSettings(const CppCodeStyleSetti
{ {
const bool wasBlocked = m_blockUpdates; const bool wasBlocked = m_blockUpdates;
m_blockUpdates = true; m_blockUpdates = true;
if (!d->m_handlingStatementMacroChange)
d->m_statementMacros->setPlainText(s.statementMacros.join('\n'));
d->m_indentBlockBraces->setChecked(s.indentBlockBraces); d->m_indentBlockBraces->setChecked(s.indentBlockBraces);
d->m_indentBlockBody->setChecked(s.indentBlockBody); d->m_indentBlockBody->setChecked(s.indentBlockBody);
d->m_indentClassBraces->setChecked(s.indentClassBraces); d->m_indentClassBraces->setChecked(s.indentClassBraces);

View File

@@ -108,6 +108,7 @@ private Q_SLOTS:
void lambdaWithReturnType(); void lambdaWithReturnType();
void structuredBinding(); void structuredBinding();
void subscriptOperatorInFunctionCall(); void subscriptOperatorInFunctionCall();
void statementMacros();
}; };
struct Line { struct Line {
@@ -2213,6 +2214,23 @@ void tst_CodeFormatter::subscriptOperatorInFunctionCall()
checkIndent(data); checkIndent(data);
} }
void tst_CodeFormatter::statementMacros()
{
QList<Line> data;
data << Line("MY_MACRO")
<< Line("template<int n = 0>")
<< Line("~ class C;");
checkIndent(data);
data.clear();
CppCodeStyleSettings settings;
settings.statementMacros << "MY_MACRO";
data << Line("MY_MACRO")
<< Line("template<int n = 0>")
<< Line("class C;");
checkIndent(data, settings);
}
QTEST_GUILESS_MAIN(tst_CodeFormatter) QTEST_GUILESS_MAIN(tst_CodeFormatter)
#include "tst_codeformatter.moc" #include "tst_codeformatter.moc"