forked from qt-creator/qt-creator
ClangFormat: Add parseConfigurationFile utility function
Change-Id: I7824e6bb77e9976aa212cdef478c4d40c4d8c56c Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -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<int>(clang::format::ParseError::Success),
|
||||
return currentQtStyle(preferences));
|
||||
bool success = parseConfigurationFile(filePath, currentSettingsStyle);
|
||||
QTC_ASSERT(success, return currentQtStyle(preferences));
|
||||
|
||||
return currentSettingsStyle;
|
||||
}
|
||||
|
@@ -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<int>(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);
|
||||
|
@@ -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<int>(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<int>(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<int>(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;
|
||||
|
@@ -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<int>(ParseError::Success);
|
||||
}
|
||||
|
||||
} // namespace ClangFormat
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user