[ClangFormat] Fix bax with BasedOnStyle field

Behavior was: The field became "LLVM" after a change to another style.
Fixed behavior: BasedOnStyle field becomes and stays chosen style.

Change-Id: I0d41a216f7f06c5681cc20a52c736406307a3724
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2022-02-25 14:14:20 +01:00
parent 99f73bf37f
commit 72991ea5c1

View File

@@ -376,18 +376,27 @@ static std::string readFile(const QString &path)
if (!file.open(QFile::ReadOnly)) if (!file.open(QFile::ReadOnly))
return defaultStyle; return defaultStyle;
const QByteArray content = file.readAll(); const std::string content = file.readAll().toStdString();
file.close(); file.close();
clang::format::FormatStyle style; clang::format::FormatStyle style;
style.Language = clang::format::FormatStyle::LK_Cpp; style.Language = clang::format::FormatStyle::LK_Cpp;
const std::error_code error = clang::format::parseConfiguration(content.toStdString(), &style); const std::error_code error = clang::format::parseConfiguration(content, &style);
QTC_ASSERT(error.value() == static_cast<int>(ParseError::Success), return defaultStyle); QTC_ASSERT(error.value() == static_cast<int>(ParseError::Success), return defaultStyle);
addQtcStatementMacros(style); addQtcStatementMacros(style);
std::string settings = clang::format::configurationAsText(style);
return clang::format::configurationAsText(style); // Needed workaround because parseConfiguration remove BasedOnStyle field
// ToDo: standardize this behavior for future
const size_t index = content.find("BasedOnStyle");
if (index != std::string::npos) {
const size_t size = content.find("\n", index) - index;
const size_t insert_index = settings.find("\n");
settings.insert(insert_index, "\n" + content.substr(index, size));
}
return settings;
} }
std::string currentProjectConfigText() std::string currentProjectConfigText()