ClangFormat: Move the functions from clangformatutils

- Moved the functions from clangformatutils to the places where
it is belong

Change-Id: Ia4108c5b096610170e0f9d16d15d40d5538ffbdc
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2023-01-16 12:33:43 +01:00
parent 32d71c6da7
commit 42c3e88f95
6 changed files with 118 additions and 98 deletions

View File

@@ -6,7 +6,6 @@
#include "clangformatconstants.h"
#include "clangformatindenter.h"
#include "clangformatfile.h"
#include "clangformatsettings.h"
#include "clangformatutils.h"
// the file was generated by scripts/generateClangFormatChecksLayout.py
@@ -150,6 +149,62 @@ void ClangFormatConfigWidget::connectChecks()
}
}
static clang::format::FormatStyle constructStyle(const QByteArray &baseStyle = QByteArray())
{
if (!baseStyle.isEmpty()) {
// Try to get the style for this base style.
llvm::Expected<clang::format::FormatStyle> style
= clang::format::getStyle(baseStyle.toStdString(), "dummy.cpp", baseStyle.toStdString());
if (style)
return *style;
handleAllErrors(style.takeError(), [](const llvm::ErrorInfoBase &) {
// do nothing
});
// Fallthrough to the default style.
}
return qtcStyle();
}
Utils::FilePath ClangFormatConfigWidget::globalPath()
{
return Core::ICore::userResourcePath();
}
Utils::FilePath ClangFormatConfigWidget::projectPath()
{
if (m_project)
return globalPath().pathAppended("clang-format/" + projectUniqueId(m_project));
return Utils::FilePath();
}
void ClangFormatConfigWidget::createStyleFileIfNeeded(bool isGlobal)
{
const FilePath path = isGlobal ? globalPath() : projectPath();
const FilePath configFile = path / Constants::SETTINGS_FILE_NAME;
if (configFile.exists())
return;
QDir().mkpath(path.toString());
if (!isGlobal) {
FilePath possibleProjectConfig = m_project->rootProjectDirectory()
/ Constants::SETTINGS_FILE_NAME;
if (possibleProjectConfig.exists()) {
// Just copy th .clang-format if current project has one.
possibleProjectConfig.copyFile(configFile);
return;
}
}
std::fstream newStyleFile(configFile.toString().toStdString(), std::fstream::out);
if (newStyleFile.is_open()) {
newStyleFile << clang::format::configurationAsText(constructStyle());
newStyleFile.close();
}
}
void ClangFormatConfigWidget::showOrHideWidgets()
{
auto verticalLayout = qobject_cast<QVBoxLayout *>(layout());
@@ -244,6 +299,38 @@ static void fillComboBoxOrLineEdit(QObject *object, const std::string &text, siz
lineEdit->setText(QString::fromStdString(value));
}
std::string ClangFormatConfigWidget::readFile(const QString &path)
{
const std::string defaultStyle = clang::format::configurationAsText(qtcStyle());
QFile file(path);
if (!file.open(QFile::ReadOnly))
return defaultStyle;
const std::string content = file.readAll().toStdString();
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);
addQtcStatementMacros(style);
std::string settings = 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;
}
void ClangFormatConfigWidget::fillTable()
{
Utils::GuardLocker locker(m_ignoreChanges);