From 42c3e88f95c7dba17564cca1b82b437867672eac Mon Sep 17 00:00:00 2001 From: Artem Sokolovskii Date: Mon, 16 Jan 2023 12:33:43 +0100 Subject: [PATCH] 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 --- .../clangformat/clangformatconfigwidget.cpp | 89 +++++++++++++++- .../clangformat/clangformatconfigwidget.h | 4 + src/plugins/clangformat/clangformatfile.cpp | 12 +++ src/plugins/clangformat/clangformatfile.h | 1 + src/plugins/clangformat/clangformatutils.cpp | 100 ++---------------- src/plugins/clangformat/clangformatutils.h | 10 +- 6 files changed, 118 insertions(+), 98 deletions(-) diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index ccfb981492c..c5b5de64133 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -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 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(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(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); diff --git a/src/plugins/clangformat/clangformatconfigwidget.h b/src/plugins/clangformat/clangformatconfigwidget.h index e81fd1fbcef..f3f05cdedbb 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.h +++ b/src/plugins/clangformat/clangformatconfigwidget.h @@ -48,11 +48,15 @@ public: private: bool eventFilter(QObject *object, QEvent *event) override; + Utils::FilePath globalPath(); + Utils::FilePath projectPath(); + void createStyleFileIfNeeded(bool isGlobal); void showOrHideWidgets(); void initChecksAndPreview(); void connectChecks(); void fillTable(); + std::string readFile(const QString &path); void saveChanges(QObject *sender); void updatePreview(); diff --git a/src/plugins/clangformat/clangformatfile.cpp b/src/plugins/clangformat/clangformatfile.cpp index 82da1c24481..7023645cc80 100644 --- a/src/plugins/clangformat/clangformatfile.cpp +++ b/src/plugins/clangformat/clangformatfile.cpp @@ -118,6 +118,18 @@ void ClangFormatFile::saveNewFormat(QByteArray style) m_filePath.writeFileContents(style); } +void ClangFormatFile::saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath) +{ + std::string styleStr = clang::format::configurationAsText(style); + + // workaround: configurationAsText() add comment "# " before BasedOnStyle line + const int pos = styleStr.find("# BasedOnStyle"); + if (pos != int(std::string::npos)) + styleStr.erase(pos, 2); + styleStr.append("\n"); + filePath.writeFileContents(QByteArray::fromStdString(styleStr)); +} + CppEditor::CppCodeStyleSettings ClangFormatFile::toCppCodeStyleSettings( ProjectExplorer::Project *project) const { diff --git a/src/plugins/clangformat/clangformatfile.h b/src/plugins/clangformat/clangformatfile.h index 1f219b645d7..6e2267befc9 100644 --- a/src/plugins/clangformat/clangformatfile.h +++ b/src/plugins/clangformat/clangformatfile.h @@ -38,6 +38,7 @@ public: private: void saveNewFormat(); void saveNewFormat(QByteArray style); + void saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath); private: Utils::FilePath m_filePath; diff --git a/src/plugins/clangformat/clangformatutils.cpp b/src/plugins/clangformat/clangformatutils.cpp index 860ba6239de..72464ac88dc 100644 --- a/src/plugins/clangformat/clangformatutils.cpp +++ b/src/plugins/clangformat/clangformatutils.cpp @@ -6,12 +6,17 @@ #include "clangformatconstants.h" #include + #include + #include #include #include + +#include #include #include + #include #include @@ -178,7 +183,11 @@ static bool useGlobalOverriddenSettings() QString currentProjectUniqueId() { - const Project *project = SessionManager::startupProject(); + return projectUniqueId(SessionManager::startupProject()); +} + +QString projectUniqueId(ProjectExplorer::Project *project) +{ if (!project) return QString(); @@ -187,18 +196,6 @@ QString currentProjectUniqueId() .toHex(0)); } -void saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath) -{ - std::string styleStr = clang::format::configurationAsText(style); - - // workaround: configurationAsText() add comment "# " before BasedOnStyle line - const int pos = styleStr.find("# BasedOnStyle"); - if (pos != int(std::string::npos)) - styleStr.erase(pos, 2); - styleStr.append("\n"); - filePath.writeFileContents(QByteArray::fromStdString(styleStr)); -} - static bool useProjectOverriddenSettings() { const Project *project = SessionManager::startupProject(); @@ -307,52 +304,6 @@ QString configForFile(Utils::FilePath fileName) return configForFile(fileName, true); } -static clang::format::FormatStyle constructStyle(const QByteArray &baseStyle = QByteArray()) -{ - if (!baseStyle.isEmpty()) { - // Try to get the style for this base style. - Expected style = getStyle(baseStyle.toStdString(), - "dummy.cpp", - baseStyle.toStdString()); - if (style) - return *style; - - handleAllErrors(style.takeError(), [](const ErrorInfoBase &) { - // do nothing - }); - // Fallthrough to the default style. - } - - return qtcStyle(); -} - -void 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) { - const Project *project = SessionManager::startupProject(); - FilePath possibleProjectConfig = 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 addQtcStatementMacros(clang::format::FormatStyle &style) { static const std::vector macros = {"Q_OBJECT", @@ -371,35 +322,4 @@ Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreference / Utils::FileUtils::fileSystemFriendlyName(codeStyle->displayName()) / QLatin1String(Constants::SETTINGS_FILE_NAME); } - -std::string 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(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; -} } // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatutils.h b/src/plugins/clangformat/clangformatutils.h index 5e8ac3d9bb6..de796c75dd0 100644 --- a/src/plugins/clangformat/clangformatutils.h +++ b/src/plugins/clangformat/clangformatutils.h @@ -18,12 +18,7 @@ namespace TextEditor { class ICodeStylePreferences; } namespace ProjectExplorer { class Project; } namespace ClangFormat { -// Creates the style for the current project or the global style if needed. -void createStyleFileIfNeeded(bool isGlobal); - -QString currentProjectUniqueId(); - -std::string readFile(const QString &path); +QString projectUniqueId(ProjectExplorer::Project *project); bool getProjectUseGlobalSettings(const ProjectExplorer::Project *project); @@ -36,7 +31,8 @@ ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils: // Is the style from the matching .clang-format file or global one if it's not found. QString configForFile(Utils::FilePath fileName); -void saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath); + +bool getProjectOverriddenSettings(const ProjectExplorer::Project *project); void addQtcStatementMacros(clang::format::FormatStyle &style); clang::format::FormatStyle qtcStyle();