ClangFormat: Add parseConfigurationFile utility function

Change-Id: I7824e6bb77e9976aa212cdef478c4d40c4d8c56c
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2024-01-08 16:00:54 +01:00
parent 910126867d
commit 2d96d1a39f
5 changed files with 23 additions and 27 deletions

View File

@@ -792,13 +792,8 @@ clang::format::FormatStyle ClangFormatBaseIndenter::customSettingsStyle(
return currentQtStyle(preferences); return currentQtStyle(preferences);
clang::format::FormatStyle currentSettingsStyle; clang::format::FormatStyle currentSettingsStyle;
currentSettingsStyle.Language = clang::format::FormatStyle::LK_Cpp; bool success = parseConfigurationFile(filePath, currentSettingsStyle);
const std::error_code error = clang::format::parseConfiguration(filePath.fileContents() QTC_ASSERT(success, return currentQtStyle(preferences));
.value_or(QByteArray())
.toStdString(),
&currentSettingsStyle);
QTC_ASSERT(error.value() == static_cast<int>(clang::format::ParseError::Success),
return currentQtStyle(preferences));
return currentSettingsStyle; return currentSettingsStyle;
} }

View File

@@ -305,10 +305,8 @@ std::string ClangFormatConfigWidget::readFile(const QString &path)
file.close(); file.close();
clang::format::FormatStyle style; clang::format::FormatStyle style;
style.Language = clang::format::FormatStyle::LK_Cpp; bool success = parseConfigurationFile(FilePath::fromString(path), style);
const std::error_code error = clang::format::parseConfiguration(content, &style); QTC_ASSERT(success, return defaultStyle);
QTC_ASSERT(error.value() == static_cast<int>(clang::format::ParseError::Success),
return defaultStyle);
addQtcStatementMacros(style); addQtcStatementMacros(style);
std::string settings = clang::format::configurationAsText(style); std::string settings = clang::format::configurationAsText(style);

View File

@@ -33,14 +33,8 @@ ClangFormatFile::ClangFormatFile(const TextEditor::ICodeStylePreferences *prefer
return; return;
} }
m_style.Language = clang::format::FormatStyle::LK_Cpp; if (!parseConfigurationFile(m_filePath, m_style))
const std::error_code error = clang::format::parseConfiguration(m_filePath.fileContents()
.value_or(QByteArray())
.toStdString(),
&m_style);
if (error.value() != static_cast<int>(clang::format::ParseError::Success)) {
resetStyleToQtC(preferences); resetStyleToQtC(preferences);
}
} }
clang::format::FormatStyle ClangFormatFile::style() { clang::format::FormatStyle ClangFormatFile::style() {
@@ -143,10 +137,8 @@ CppEditor::CppCodeStyleSettings ClangFormatFile::toCppCodeStyleSettings(
auto settings = CppEditor::CppCodeStyleSettings::getProjectCodeStyle(project); auto settings = CppEditor::CppCodeStyleSettings::getProjectCodeStyle(project);
FormatStyle style; FormatStyle style;
style.Language = clang::format::FormatStyle::LK_Cpp; bool success = parseConfigurationFile(m_filePath, style);
const std::error_code error QTC_ASSERT(success, return settings);
= parseConfiguration(m_filePath.fileContents().value_or(QByteArray()).toStdString(), &style);
QTC_ASSERT(error.value() == static_cast<int>(ParseError::Success), return settings);
// Modifier offset should be opposite to indent width in order indentAccessSpecifiers // Modifier offset should be opposite to indent width in order indentAccessSpecifiers
// to be false // to be false
@@ -197,10 +189,8 @@ TextEditor::TabSettings ClangFormatFile::toTabSettings(ProjectExplorer::Project
auto settings = CppEditor::CppCodeStyleSettings::getProjectTabSettings(project); auto settings = CppEditor::CppCodeStyleSettings::getProjectTabSettings(project);
FormatStyle style; FormatStyle style;
style.Language = clang::format::FormatStyle::LK_Cpp; bool success = parseConfigurationFile(m_filePath, style);
const std::error_code error QTC_ASSERT(success, return settings);
= parseConfiguration(m_filePath.fileContents().value_or(QByteArray()).toStdString(), &style);
QTC_ASSERT(error.value() == static_cast<int>(ParseError::Success), return settings);
settings.m_indentSize = style.IndentWidth; settings.m_indentSize = style.IndentWidth;
settings.m_tabSize = style.TabWidth; settings.m_tabSize = style.TabWidth;

View File

@@ -416,4 +416,14 @@ Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreference
/ Utils::FileUtils::fileSystemFriendlyName(codeStyle->displayName()) / Utils::FileUtils::fileSystemFriendlyName(codeStyle->displayName())
/ QLatin1String(Constants::SETTINGS_FILE_NAME); / 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<int>(ParseError::Success);
}
} // namespace ClangFormat } // namespace ClangFormat

View File

@@ -47,4 +47,7 @@ clang::format::FormatStyle qtcStyle();
clang::format::FormatStyle currentQtStyle(const TextEditor::ICodeStylePreferences *codeStyle); clang::format::FormatStyle currentQtStyle(const TextEditor::ICodeStylePreferences *codeStyle);
Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle); Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle);
}
bool parseConfigurationFile(const Utils::FilePath &filePath, clang::format::FormatStyle &style);
} // ClangFormat