diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index a0dd39d0ad5..ac788c8a4b1 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -792,13 +792,8 @@ clang::format::FormatStyle ClangFormatBaseIndenter::customSettingsStyle( return currentQtStyle(preferences); clang::format::FormatStyle currentSettingsStyle; - currentSettingsStyle.Language = clang::format::FormatStyle::LK_Cpp; - const std::error_code error = clang::format::parseConfiguration(filePath.fileContents() - .value_or(QByteArray()) - .toStdString(), - ¤tSettingsStyle); - QTC_ASSERT(error.value() == static_cast(clang::format::ParseError::Success), - return currentQtStyle(preferences)); + bool success = parseConfigurationFile(filePath, currentSettingsStyle); + QTC_ASSERT(success, return currentQtStyle(preferences)); return currentSettingsStyle; } diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index dc0d6837118..cc5b3083a09 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -305,10 +305,8 @@ std::string ClangFormatConfigWidget::readFile(const QString &path) file.close(); clang::format::FormatStyle style; - style.Language = clang::format::FormatStyle::LK_Cpp; - const std::error_code error = clang::format::parseConfiguration(content, &style); - QTC_ASSERT(error.value() == static_cast(clang::format::ParseError::Success), - return defaultStyle); + bool success = parseConfigurationFile(FilePath::fromString(path), style); + QTC_ASSERT(success, return defaultStyle); addQtcStatementMacros(style); std::string settings = clang::format::configurationAsText(style); diff --git a/src/plugins/clangformat/clangformatfile.cpp b/src/plugins/clangformat/clangformatfile.cpp index 66eb58d6eaf..395b7ba53b4 100644 --- a/src/plugins/clangformat/clangformatfile.cpp +++ b/src/plugins/clangformat/clangformatfile.cpp @@ -33,14 +33,8 @@ ClangFormatFile::ClangFormatFile(const TextEditor::ICodeStylePreferences *prefer return; } - m_style.Language = clang::format::FormatStyle::LK_Cpp; - const std::error_code error = clang::format::parseConfiguration(m_filePath.fileContents() - .value_or(QByteArray()) - .toStdString(), - &m_style); - if (error.value() != static_cast(clang::format::ParseError::Success)) { + if (!parseConfigurationFile(m_filePath, m_style)) resetStyleToQtC(preferences); - } } clang::format::FormatStyle ClangFormatFile::style() { @@ -143,10 +137,8 @@ CppEditor::CppCodeStyleSettings ClangFormatFile::toCppCodeStyleSettings( auto settings = CppEditor::CppCodeStyleSettings::getProjectCodeStyle(project); FormatStyle style; - style.Language = clang::format::FormatStyle::LK_Cpp; - const std::error_code error - = parseConfiguration(m_filePath.fileContents().value_or(QByteArray()).toStdString(), &style); - QTC_ASSERT(error.value() == static_cast(ParseError::Success), return settings); + bool success = parseConfigurationFile(m_filePath, style); + QTC_ASSERT(success, return settings); // Modifier offset should be opposite to indent width in order indentAccessSpecifiers // to be false @@ -197,10 +189,8 @@ TextEditor::TabSettings ClangFormatFile::toTabSettings(ProjectExplorer::Project auto settings = CppEditor::CppCodeStyleSettings::getProjectTabSettings(project); FormatStyle style; - style.Language = clang::format::FormatStyle::LK_Cpp; - const std::error_code error - = parseConfiguration(m_filePath.fileContents().value_or(QByteArray()).toStdString(), &style); - QTC_ASSERT(error.value() == static_cast(ParseError::Success), return settings); + bool success = parseConfigurationFile(m_filePath, style); + QTC_ASSERT(success, return settings); settings.m_indentSize = style.IndentWidth; settings.m_tabSize = style.TabWidth; diff --git a/src/plugins/clangformat/clangformatutils.cpp b/src/plugins/clangformat/clangformatutils.cpp index 183d9c9cd4f..5022325c5d0 100644 --- a/src/plugins/clangformat/clangformatutils.cpp +++ b/src/plugins/clangformat/clangformatutils.cpp @@ -416,4 +416,14 @@ Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreference / Utils::FileUtils::fileSystemFriendlyName(codeStyle->displayName()) / QLatin1String(Constants::SETTINGS_FILE_NAME); } + +bool parseConfigurationFile(const Utils::FilePath &filePath, clang::format::FormatStyle &style) +{ + style.Language = clang::format::FormatStyle::LK_Cpp; + const std::error_code error + = parseConfiguration(filePath.fileContents().value_or(QByteArray()).toStdString(), &style); + + return error.value() == static_cast(ParseError::Success); +} + } // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatutils.h b/src/plugins/clangformat/clangformatutils.h index e59046014f9..931385dc7cb 100644 --- a/src/plugins/clangformat/clangformatutils.h +++ b/src/plugins/clangformat/clangformatutils.h @@ -47,4 +47,7 @@ clang::format::FormatStyle qtcStyle(); clang::format::FormatStyle currentQtStyle(const TextEditor::ICodeStylePreferences *codeStyle); Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle); -} + +bool parseConfigurationFile(const Utils::FilePath &filePath, clang::format::FormatStyle &style); + +} // ClangFormat