forked from qt-creator/qt-creator
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:
@@ -842,7 +842,8 @@ bool CodeFormatter::tryDeclaration()
|
||||
if (tokenText.startsWith(QLatin1String("Q_"))
|
||||
|| tokenText.startsWith(QLatin1String("QT_"))
|
||||
|| tokenText.startsWith(QLatin1String("QML_"))
|
||||
|| tokenText.startsWith(QLatin1String("QDOC_"))) {
|
||||
|| tokenText.startsWith(QLatin1String("QDOC_"))
|
||||
|| m_statementMacros.contains(tokenText)) {
|
||||
enter(qt_like_macro);
|
||||
return true;
|
||||
}
|
||||
@@ -1118,6 +1119,7 @@ QtStyleCodeFormatter::QtStyleCodeFormatter(const TabSettings &tabSettings,
|
||||
, m_styleSettings(settings)
|
||||
{
|
||||
setTabSize(tabSettings.m_tabSize);
|
||||
setStatementMacros(m_styleSettings.statementMacros);
|
||||
}
|
||||
|
||||
void QtStyleCodeFormatter::setTabSettings(const TabSettings &tabSettings)
|
||||
@@ -1129,6 +1131,7 @@ void QtStyleCodeFormatter::setTabSettings(const TabSettings &tabSettings)
|
||||
void QtStyleCodeFormatter::setCodeStyleSettings(const CppCodeStyleSettings &settings)
|
||||
{
|
||||
m_styleSettings = settings;
|
||||
setStatementMacros(m_styleSettings.statementMacros);
|
||||
}
|
||||
|
||||
void QtStyleCodeFormatter::saveBlockData(QTextBlock *block, const BlockData &data) const
|
||||
|
@@ -40,6 +40,7 @@ public:
|
||||
void indentForNewLineAfter(const QTextBlock &block, int *indent, int *padding);
|
||||
|
||||
void setTabSize(int tabSize);
|
||||
void setStatementMacros(const QStringList ¯os) { m_statementMacros = macros; }
|
||||
|
||||
void invalidateCache(QTextDocument *document);
|
||||
|
||||
@@ -224,6 +225,7 @@ private:
|
||||
int m_paddingDepth = 0;
|
||||
|
||||
int m_tabSize = 4;
|
||||
QStringList m_statementMacros;
|
||||
|
||||
friend class Internal::CppCodeFormatterData;
|
||||
};
|
||||
|
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
static const char statementMacrosKey[] = "StatementMacros";
|
||||
static const char indentBlockBracesKey[] = "IndentBlockBraces";
|
||||
static const char indentBlockBodyKey[] = "IndentBlockBody";
|
||||
static const char indentClassBracesKey[] = "IndentClassBraces";
|
||||
@@ -50,6 +51,7 @@ CppCodeStyleSettings::CppCodeStyleSettings() = default;
|
||||
Store CppCodeStyleSettings::toMap() const
|
||||
{
|
||||
return {
|
||||
{statementMacrosKey, statementMacros},
|
||||
{indentBlockBracesKey, indentBlockBraces},
|
||||
{indentBlockBodyKey, indentBlockBody},
|
||||
{indentClassBracesKey, indentClassBraces},
|
||||
@@ -76,6 +78,7 @@ Store CppCodeStyleSettings::toMap() const
|
||||
|
||||
void CppCodeStyleSettings::fromMap(const Store &map)
|
||||
{
|
||||
statementMacros = map.value(statementMacrosKey, statementMacros).toStringList();
|
||||
indentBlockBraces = map.value(indentBlockBracesKey, indentBlockBraces).toBool();
|
||||
indentBlockBody = map.value(indentBlockBodyKey, indentBlockBody).toBool();
|
||||
indentClassBraces = map.value(indentClassBracesKey, indentClassBraces).toBool();
|
||||
@@ -128,6 +131,7 @@ bool CppCodeStyleSettings::equals(const CppCodeStyleSettings &rhs) const
|
||||
&& bindStarToRightSpecifier == rhs.bindStarToRightSpecifier
|
||||
&& extraPaddingForConditionsIfConfusingAlign == rhs.extraPaddingForConditionsIfConfusingAlign
|
||||
&& alignAssignments == rhs.alignAssignments
|
||||
&& statementMacros == rhs.statementMacros
|
||||
&& preferGetterNameWithoutGetPrefix == rhs.preferGetterNameWithoutGetPrefix
|
||||
#ifdef WITH_TESTS
|
||||
&& forceFormatting == rhs.forceFormatting
|
||||
|
@@ -18,6 +18,7 @@ class CPPEDITOR_EXPORT CppCodeStyleSettings
|
||||
public:
|
||||
CppCodeStyleSettings();
|
||||
|
||||
QStringList statementMacros;
|
||||
bool indentBlockBraces = false;
|
||||
bool indentBlockBody = true;
|
||||
bool indentClassBraces = false;
|
||||
|
@@ -163,8 +163,9 @@ public:
|
||||
, m_bindStarToRightSpecifier(createCheckBox(Tr::tr("Right const/volatile"),
|
||||
Tr::tr("This does not apply to references.")))
|
||||
, m_tabSettingsWidget(new TabSettingsWidget)
|
||||
, m_statementMacros(new QPlainTextEdit)
|
||||
{
|
||||
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
|
||||
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
||||
sizePolicy.setHorizontalStretch(0);
|
||||
sizePolicy.setVerticalStretch(0);
|
||||
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 {
|
||||
TabWidget {
|
||||
bindTo(&m_categoryTab),
|
||||
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("Braces"), Row { bracesGroup, createPreview(2) } },
|
||||
@@ -310,6 +325,8 @@ public:
|
||||
|
||||
QTabWidget *m_categoryTab = nullptr;
|
||||
TabSettingsWidget *m_tabSettingsWidget = nullptr;
|
||||
QPlainTextEdit * const m_statementMacros;
|
||||
bool m_handlingStatementMacroChange = false;
|
||||
};
|
||||
|
||||
CppCodeStylePreferencesWidget::CppCodeStylePreferencesWidget(QWidget *parent)
|
||||
@@ -361,6 +378,10 @@ CppCodeStyleSettings CppCodeStylePreferencesWidget::cppCodeStyleSettings() const
|
||||
{
|
||||
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.indentBlockBody = d->m_indentBlockBody->isChecked();
|
||||
set.indentClassBraces = d->m_indentClassBraces->isChecked();
|
||||
@@ -399,6 +420,8 @@ void CppCodeStylePreferencesWidget::setCodeStyleSettings(const CppCodeStyleSetti
|
||||
{
|
||||
const bool wasBlocked = m_blockUpdates;
|
||||
m_blockUpdates = true;
|
||||
if (!d->m_handlingStatementMacroChange)
|
||||
d->m_statementMacros->setPlainText(s.statementMacros.join('\n'));
|
||||
d->m_indentBlockBraces->setChecked(s.indentBlockBraces);
|
||||
d->m_indentBlockBody->setChecked(s.indentBlockBody);
|
||||
d->m_indentClassBraces->setChecked(s.indentClassBraces);
|
||||
|
@@ -108,6 +108,7 @@ private Q_SLOTS:
|
||||
void lambdaWithReturnType();
|
||||
void structuredBinding();
|
||||
void subscriptOperatorInFunctionCall();
|
||||
void statementMacros();
|
||||
};
|
||||
|
||||
struct Line {
|
||||
@@ -2213,6 +2214,23 @@ void tst_CodeFormatter::subscriptOperatorInFunctionCall()
|
||||
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)
|
||||
|
||||
#include "tst_codeformatter.moc"
|
||||
|
Reference in New Issue
Block a user