forked from qt-creator/qt-creator
[ClangFormat] Add test checkking indentation after Q_OBJECT
Added test checking behavior when after Q_OBJECT all class structure have correct indenting and redundent tabs doesn't appear before key words such as public:, private: , etc. Made automatic addition Qt defines to StatementMacro to .clang-format files. Fixes: QTCREATORBUG-26776 Change-Id: I3490421a9caf2831b593939597940358f7ce8f01 Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "clangformatbaseindenter.h"
|
||||
#include "clangformatutils.h"
|
||||
|
||||
#include <clang/Tooling/Core/Replacement.h>
|
||||
|
||||
@@ -728,14 +729,16 @@ clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const
|
||||
{
|
||||
llvm::Expected<clang::format::FormatStyle> 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
|
||||
|
@@ -33,6 +33,7 @@
|
||||
#include <texteditor/tabsettings.h>
|
||||
#include <projectexplorer/project.h>
|
||||
#include <projectexplorer/session.h>
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
#include <QCryptographicHash>
|
||||
|
||||
@@ -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<std::string> 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<int>(ParseError::Success), return defaultStyle);
|
||||
|
||||
addQtcStatementMacros(style);
|
||||
|
||||
return clang::format::configurationAsText(style);
|
||||
}
|
||||
|
||||
std::string currentProjectConfigText()
|
||||
|
@@ -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();
|
||||
}
|
||||
|
@@ -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<QString>{"class test {", " Q_OBJECT", "public:", "};"}));
|
||||
}
|
||||
|
||||
} // namespace ClangFormat::Internal
|
||||
|
@@ -109,6 +109,7 @@ private slots:
|
||||
void testSortIncludes();
|
||||
void testChainedMemberFunctionCalls();
|
||||
void testCommentBlock();
|
||||
void testClassIndentStructure();
|
||||
|
||||
private:
|
||||
void insertLines(const std::vector<QString> &lines);
|
||||
|
Reference in New Issue
Block a user