diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index fa95514ca32..d9e36e3717a 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -24,6 +24,7 @@ ****************************************************************************/ #include "clangformatbaseindenter.h" +#include "clangformatutils.h" #include @@ -728,14 +729,16 @@ clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const { llvm::Expected style = clang::format::getStyle("file", m_fileName.toString().toStdString(), "none"); - if (style) + if (style) { + addQtcStatementMacros(*style); return *style; + } handleAllErrors(style.takeError(), [](const llvm::ErrorInfoBase &) { // do nothing }); - return clang::format::getLLVMStyle(); + return qtcStyle(); } } // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatutils.cpp b/src/plugins/clangformat/clangformatutils.cpp index ff184ea737c..e59a151f0b2 100644 --- a/src/plugins/clangformat/clangformatutils.cpp +++ b/src/plugins/clangformat/clangformatutils.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -46,7 +47,7 @@ using namespace Utils; namespace ClangFormat { -static clang::format::FormatStyle qtcStyle() +clang::format::FormatStyle qtcStyle() { clang::format::FormatStyle style = getLLVMStyle(); style.Language = FormatStyle::LK_Cpp; @@ -355,18 +356,38 @@ clang::format::FormatStyle styleForFile(Utils::FilePath fileName) return styleForFile(fileName, true); } +void addQtcStatementMacros(clang::format::FormatStyle &style) +{ + static const std::vector macros = {"Q_OBJECT", + "QT_BEGIN_NAMESPACE", + "QT_END_NAMESPACE"}; + for (const std::string ¯o : macros) { + if (std::find(style.StatementMacros.begin(), style.StatementMacros.end(), macro) + == style.StatementMacros.end()) + style.StatementMacros.emplace_back(macro); + } +} + static std::string readFile(const QString &path) { + const std::string defaultStyle = clang::format::configurationAsText(qtcStyle()); + QFile file(path); - if (!file.open(QFile::ReadOnly)) { - clang::format::FormatStyle defaultStyle = qtcStyle(); - return clang::format::configurationAsText(defaultStyle); - } + if (!file.open(QFile::ReadOnly)) + return defaultStyle; const QByteArray content = file.readAll(); file.close(); - return content.toStdString(); + clang::format::FormatStyle style; + style.Language = clang::format::FormatStyle::LK_Cpp; + const std::error_code error = clang::format::parseConfiguration(content.toStdString(), &style); + + QTC_ASSERT(error.value() == static_cast(ParseError::Success), return defaultStyle); + + addQtcStatementMacros(style); + + return clang::format::configurationAsText(style); } std::string currentProjectConfigText() diff --git a/src/plugins/clangformat/clangformatutils.h b/src/plugins/clangformat/clangformatutils.h index 74fd84c9c6b..3caf6c9d6fc 100644 --- a/src/plugins/clangformat/clangformatutils.h +++ b/src/plugins/clangformat/clangformatutils.h @@ -51,4 +51,6 @@ clang::format::FormatStyle currentGlobalStyle(); QString configForFile(Utils::FilePath fileName); clang::format::FormatStyle styleForFile(Utils::FilePath fileName); +void addQtcStatementMacros(clang::format::FormatStyle &style); +clang::format::FormatStyle qtcStyle(); } diff --git a/src/plugins/clangformat/tests/clangformat-test.cpp b/src/plugins/clangformat/tests/clangformat-test.cpp index 3bef6506795..bc5e07328df 100644 --- a/src/plugins/clangformat/tests/clangformat-test.cpp +++ b/src/plugins/clangformat/tests/clangformat-test.cpp @@ -646,4 +646,12 @@ void ClangFormatTest::testCommentBlock() "****************************************************************************/"})); } +void ClangFormatTest::testClassIndentStructure() +{ + insertLines({"class test {", " Q_OBJECT", " public:", "};"}); + m_indenter->indent(*m_cursor, QChar::Null, TextEditor::TabSettings()); + QCOMPARE(documentLines(), + (std::vector{"class test {", " Q_OBJECT", "public:", "};"})); +} + } // namespace ClangFormat::Internal diff --git a/src/plugins/clangformat/tests/clangformat-test.h b/src/plugins/clangformat/tests/clangformat-test.h index e34a1491168..094657d1b01 100644 --- a/src/plugins/clangformat/tests/clangformat-test.h +++ b/src/plugins/clangformat/tests/clangformat-test.h @@ -109,6 +109,7 @@ private slots: void testSortIncludes(); void testChainedMemberFunctionCalls(); void testCommentBlock(); + void testClassIndentStructure(); private: void insertLines(const std::vector &lines);