ClangFormat: Fix autoindentation for QML_ and Q_ macros

Fixes: QTCREATORBUG-29086
Change-Id: Ie23e46baf2c802799818cb724aacd71776480b06
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2023-04-25 16:04:30 +02:00
parent 65a1637d61
commit 7c1f2ea16d
2 changed files with 46 additions and 7 deletions

View File

@@ -171,9 +171,7 @@ clang::format::FormatStyle qtcStyle()
style.SpacesInCStyleCastParentheses = false; style.SpacesInCStyleCastParentheses = false;
style.SpacesInParentheses = false; style.SpacesInParentheses = false;
style.SpacesInSquareBrackets = false; style.SpacesInSquareBrackets = false;
style.StatementMacros.emplace_back("Q_OBJECT"); addQtcStatementMacros(style);
style.StatementMacros.emplace_back("QT_BEGIN_NAMESPACE");
style.StatementMacros.emplace_back("QT_END_NAMESPACE");
style.Standard = FormatStyle::LS_Cpp11; style.Standard = FormatStyle::LS_Cpp11;
style.TabWidth = 4; style.TabWidth = 4;
style.UseTab = FormatStyle::UT_Never; style.UseTab = FormatStyle::UT_Never;
@@ -278,9 +276,44 @@ Utils::FilePath configForFile(const Utils::FilePath &fileName)
void addQtcStatementMacros(clang::format::FormatStyle &style) void addQtcStatementMacros(clang::format::FormatStyle &style)
{ {
static const std::vector<std::string> macros = {"Q_OBJECT", static const std::vector<std::string> macros = {"Q_CLASSINFO",
"Q_ENUM",
"Q_ENUM_NS",
"Q_FLAG",
"Q_FLAG_NS",
"Q_GADGET",
"Q_GADGET_EXPORT",
"Q_INTERFACES",
"Q_MOC_INCLUDE",
"Q_NAMESPACE",
"Q_NAMESPACE_EXPORT",
"Q_OBJECT",
"Q_PROPERTY",
"Q_REVISION",
"Q_DISABLE_COPY",
"Q_SET_OBJECT_NAME",
"QT_BEGIN_NAMESPACE", "QT_BEGIN_NAMESPACE",
"QT_END_NAMESPACE"}; "QT_END_NAMESPACE",
"QML_ADDED_IN_MINOR_VERSION",
"QML_ANONYMOUS",
"QML_ATTACHED",
"QML_DECLARE_TYPE",
"QML_DECLARE_TYPEINFO",
"QML_ELEMENT",
"QML_EXTENDED",
"QML_EXTENDED_NAMESPACE",
"QML_EXTRA_VERSION",
"QML_FOREIGN",
"QML_FOREIGN_NAMESPACE",
"QML_IMPLEMENTS_INTERFACES",
"QML_INTERFACE",
"QML_NAMED_ELEMENT",
"QML_REMOVED_IN_MINOR_VERSION",
"QML_SINGLETON",
"QML_UNAVAILABLE",
"QML_UNCREATABLE",
"QML_VALUE_TYPE"};
for (const std::string &macro : macros) { for (const std::string &macro : macros) {
if (std::find(style.StatementMacros.begin(), style.StatementMacros.end(), macro) if (std::find(style.StatementMacros.begin(), style.StatementMacros.end(), macro)
== style.StatementMacros.end()) == style.StatementMacros.end())

View File

@@ -628,10 +628,16 @@ void ClangFormatTest::testCommentBlock()
void ClangFormatTest::testClassIndentStructure() void ClangFormatTest::testClassIndentStructure()
{ {
insertLines({"class test {", " Q_OBJECT", " public:", "};"}); insertLines(
{"class test {", " Q_OBJECT", " QML_ELEMENT", " QML_SINGLETON", " public:", "};"});
m_indenter->indent(*m_cursor, QChar::Null, TextEditor::TabSettings()); m_indenter->indent(*m_cursor, QChar::Null, TextEditor::TabSettings());
QCOMPARE(documentLines(), QCOMPARE(documentLines(),
(std::vector<QString>{"class test {", " Q_OBJECT", "public:", "};"})); (std::vector<QString>{"class test {",
" Q_OBJECT",
" QML_ELEMENT",
" QML_SINGLETON",
"public:",
"};"}));
} }
void ClangFormatTest::testIndentInitializeVector() void ClangFormatTest::testIndentInitializeVector()