From 72dd90e5120831f69c221761381f4809a4f9d6f0 Mon Sep 17 00:00:00 2001 From: Artem Sokolovskii Date: Fri, 8 Apr 2022 15:12:30 +0200 Subject: [PATCH] ClangFormat: Start to use new file management - Indenter uses .clang-format file from a dir of editing file or from a parent dir. If there is no such file then indenter starts to use file from a dir with the name current CodeStyle settings. - Test fixed ToDo: Add file absentness processing in case of import Change-Id: If09ef0c598899856b948c214e524bcfd5dad76e2 Reviewed-by: Reviewed-by: Christian Kandeler --- .../clangformat/clangformatbaseindenter.cpp | 20 ++++++++++++++++++- .../clangformat/clangformatbaseindenter.h | 3 ++- .../clangformat/clangformatconfigwidget.cpp | 7 ++----- .../clangformat/clangformatindenter.cpp | 5 ----- src/plugins/clangformat/clangformatindenter.h | 1 - src/plugins/clangformat/clangformatutils.cpp | 10 ++++++++++ src/plugins/clangformat/clangformatutils.h | 3 +++ .../clangformat/tests/clangformat-test.cpp | 2 +- 8 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index 2c1cdf1a647..a60ba13cbac 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -24,7 +24,11 @@ ****************************************************************************/ #include "clangformatbaseindenter.h" +#include "clangformatconstants.h" #include "clangformatutils.h" +#include +#include +#include #include @@ -728,8 +732,22 @@ void ClangFormatBaseIndenter::autoIndent(const QTextCursor &cursor, clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const { llvm::Expected style - = clang::format::getStyle("file", m_fileName.toString().toStdString(), "none"); + = clang::format::getStyle("file", m_fileName.path().toStdString(), "none"); + if (style) { + if (*style == clang::format::getNoStyle()) { + Utils::FilePath filePath = filePathToCurrentSettings( + TextEditor::TextEditorSettings::codeStyle("Cpp")->currentPreferences()); + + clang::format::FormatStyle style; + style.Language = clang::format::FormatStyle::LK_Cpp; + const std::error_code error + = clang::format::parseConfiguration(filePath.fileContents().toStdString(), &style); + QTC_ASSERT(error.value() == static_cast(clang::format::ParseError::Success), + return qtcStyle()); + + return style; + } addQtcStatementMacros(*style); return *style; } diff --git a/src/plugins/clangformat/clangformatbaseindenter.h b/src/plugins/clangformat/clangformatbaseindenter.h index 14c54db7609..0b96451aca0 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.h +++ b/src/plugins/clangformat/clangformatbaseindenter.h @@ -68,8 +68,9 @@ public: Utils::optional margin() const override; + clang::format::FormatStyle styleForFile() const; + protected: - virtual clang::format::FormatStyle styleForFile() const; virtual bool formatCodeInsteadOfIndent() const { return false; } virtual bool formatWhileTyping() const { return false; } virtual int lastSaveRevision() const { return 0; } diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index 2a6f3f9b6b5..1e1719e6089 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -113,11 +113,8 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc m_ui->setupUi(this); setEnabled(!codeStyle->isReadOnly()); - Utils::FilePath filePath = Core::ICore::userResourcePath(); - filePath = filePath / "clang-format/" - / Utils::FileUtils::fileSystemFriendlyName(codeStyle->displayName()) - / QLatin1String(Constants::SETTINGS_FILE_NAME); - m_config = std::make_unique(filePath, codeStyle->isReadOnly()); + m_config = std::make_unique(filePathToCurrentSettings(codeStyle), + codeStyle->isReadOnly()); initChecksAndPreview(); showCombobox(); diff --git a/src/plugins/clangformat/clangformatindenter.cpp b/src/plugins/clangformat/clangformatindenter.cpp index 381762ee7f2..7476c12041e 100644 --- a/src/plugins/clangformat/clangformatindenter.cpp +++ b/src/plugins/clangformat/clangformatindenter.cpp @@ -40,11 +40,6 @@ ClangFormatIndenter::ClangFormatIndenter(QTextDocument *doc) : ClangFormatBaseIndenter(doc) {} -FormatStyle ClangFormatIndenter::styleForFile() const -{ - return ClangFormat::styleForFile(m_fileName); -} - bool ClangFormatIndenter::formatCodeInsteadOfIndent() const { return ClangFormatSettings::instance().formatCodeInsteadOfIndent(); diff --git a/src/plugins/clangformat/clangformatindenter.h b/src/plugins/clangformat/clangformatindenter.h index 6d605eb5c91..9e1a65fb3d4 100644 --- a/src/plugins/clangformat/clangformatindenter.h +++ b/src/plugins/clangformat/clangformatindenter.h @@ -41,7 +41,6 @@ public: private: bool formatCodeInsteadOfIndent() const override; bool formatWhileTyping() const override; - clang::format::FormatStyle styleForFile() const override; int lastSaveRevision() const override; }; diff --git a/src/plugins/clangformat/clangformatutils.cpp b/src/plugins/clangformat/clangformatutils.cpp index 21fab5cc931..230078558e5 100644 --- a/src/plugins/clangformat/clangformatutils.cpp +++ b/src/plugins/clangformat/clangformatutils.cpp @@ -30,7 +30,9 @@ #include #include +#include #include +#include #include #include #include @@ -208,6 +210,7 @@ void saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath) const int pos = styleStr.find("# BasedOnStyle"); if (pos != int(std::string::npos)) styleStr.erase(pos, 2); + styleStr.append("\n"); filePath.writeFileContents(QByteArray::fromStdString(styleStr)); } @@ -382,6 +385,13 @@ void addQtcStatementMacros(clang::format::FormatStyle &style) } } +Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle) +{ + return Core::ICore::userResourcePath() / "clang-format/" + / Utils::FileUtils::fileSystemFriendlyName(codeStyle->displayName()) + / QLatin1String(Constants::SETTINGS_FILE_NAME); +} + std::string readFile(const QString &path) { const std::string defaultStyle = clang::format::configurationAsText(qtcStyle()); diff --git a/src/plugins/clangformat/clangformatutils.h b/src/plugins/clangformat/clangformatutils.h index aa7a8f30cc1..830ab42475b 100644 --- a/src/plugins/clangformat/clangformatutils.h +++ b/src/plugins/clangformat/clangformatutils.h @@ -34,6 +34,7 @@ #include +namespace TextEditor { class ICodeStylePreferences; } namespace ClangFormat { // Creates the style for the current project or the global style if needed. @@ -55,4 +56,6 @@ void saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath) void addQtcStatementMacros(clang::format::FormatStyle &style); clang::format::FormatStyle qtcStyle(); + +Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle); } diff --git a/src/plugins/clangformat/tests/clangformat-test.cpp b/src/plugins/clangformat/tests/clangformat-test.cpp index bc5e07328df..174a3d11705 100644 --- a/src/plugins/clangformat/tests/clangformat-test.cpp +++ b/src/plugins/clangformat/tests/clangformat-test.cpp @@ -52,7 +52,7 @@ public: ClangFormatExtendedTestIndenter(QTextDocument *doc) : ClangFormatTestIndenter(doc) {} private: - bool formatWhileTyping() const override { return true; } + bool formatCodeInsteadOfIndent() const override { return true; } }; ClangFormatTest::ClangFormatTest()