From 44c9074c0bc77b407b2a0e3ac57ac4432657f775 Mon Sep 17 00:00:00 2001 From: Artem Sokolovskii Date: Mon, 5 Feb 2024 16:05:55 +0100 Subject: [PATCH] ClangFormat: Move clangformat output to infolabel Move clangformat output to infolabel in clangformatconfigwidget to reduce noiziness in cli and to enhance the convenience of editing the .clang-format file. Fixes: QTCREATORBUG-30269 Change-Id: I78053bde9791122172f1d3d4ba712a9954ea9c4e Reviewed-by: Eike Ziller Reviewed-by: --- .../clangformat/clangformatbaseindenter.cpp | 2 +- .../clangformat/clangformatconfigwidget.cpp | 52 +++++++------------ src/plugins/clangformat/clangformatutils.cpp | 26 ++++++++-- src/plugins/clangformat/clangformatutils.h | 6 ++- 4 files changed, 44 insertions(+), 42 deletions(-) diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index 9f9ea8ac94e..2de357b1124 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -849,7 +849,7 @@ clang::format::FormatStyle ClangFormatBaseIndenterPrivate::customSettingsStyle( return currentQtStyle(preferences); clang::format::FormatStyle currentSettingsStyle; - bool success = parseConfigurationFile(filePath, currentSettingsStyle); + const Utils::expected_str success = parseConfigurationFile(filePath, currentSettingsStyle); QTC_ASSERT(success, return currentQtStyle(preferences)); return currentSettingsStyle; diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index c546cc0d21f..98de3370228 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -37,6 +37,8 @@ #include #include #include +#include +#include #include #include @@ -105,8 +107,7 @@ private: Guard m_ignoreChanges; QLabel *m_clangVersion; - QLabel *m_clangFileIsCorrectText; - QLabel *m_clangFileIsCorrectIcon; + InfoLabel *m_clangFileIsCorrectText; ClangFormatIndenter *m_indenter; }; @@ -137,7 +138,7 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc Column { m_clangVersion, Row { m_editorScrollArea, m_preview }, - Row {m_clangFileIsCorrectIcon, m_clangFileIsCorrectText, st} + Row {m_clangFileIsCorrectText, st} }.attachTo(this); connect(codeStyle, &TextEditor::ICodeStylePreferences::currentPreferencesChanged, @@ -194,46 +195,28 @@ void ClangFormatConfigWidget::initEditor(TextEditor::ICodeStylePreferences *code m_editorScrollArea->setWidget(m_editor->widget()); m_editorScrollArea->setWidgetResizable(true); - m_clangFileIsCorrectText = new QLabel(Tr::tr("Clang-Format is configured correctly.")); - QPalette paletteCorrect = m_clangFileIsCorrectText->palette(); - paletteCorrect.setColor(QPalette::WindowText, Qt::darkGreen); - m_clangFileIsCorrectText->setPalette(paletteCorrect); - - m_clangFileIsCorrectIcon = new QLabel(this); - m_clangFileIsCorrectIcon->setPixmap(Icons::OK.icon().pixmap(16, 16)); + m_clangFileIsCorrectText = new InfoLabel("", Utils::InfoLabel::Ok); + m_clangFileIsCorrectText->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); + m_clangFileIsCorrectText->hide(); m_clangVersion = new QLabel(Tr::tr("Current ClangFormat version: %1.").arg(LLVM_VERSION_STRING), this); connect(m_editor->document(), &TextEditor::TextDocument::contentsChanged, this, [this] { clang::format::FormatStyle currentSettingsStyle; - const bool success + const Utils::expected_str success = parseConfigurationContent(m_editor->document()->contents().toStdString(), currentSettingsStyle); - QString text; - Qt::GlobalColor currentColor; - QPixmap pixmap; if (success) { - text = Tr::tr("Clang-Format is configured correctly."); - currentColor = Qt::darkGreen; - pixmap = Icons::OK.icon().pixmap(16, 16); - } else { - text = Tr::tr("Clang-Format is not configured correctly."); - currentColor = Qt::red; - pixmap = Icons::WARNING.icon().pixmap(16, 16); - } - - m_clangFileIsCorrectText->setText(text); - QPalette paletteCorrect = m_clangFileIsCorrectText->palette(); - paletteCorrect.setColor(QPalette::WindowText, currentColor); - m_clangFileIsCorrectText->setPalette(paletteCorrect); - m_clangFileIsCorrectIcon->setPixmap(pixmap); - - if (!success) + m_clangFileIsCorrectText->hide(); + m_indenter->setOverriddenStyle(currentSettingsStyle); + updatePreview(); return; - m_indenter->setOverriddenStyle(currentSettingsStyle); - updatePreview(); + } + m_clangFileIsCorrectText->show(); + m_clangFileIsCorrectText->setText(Tr::tr("Warning: ") + success.error()); + m_clangFileIsCorrectText->setType(Utils::InfoLabel::Warning); }); QShortcut *completionSC = new QShortcut(QKeySequence("Ctrl+Space"), this); @@ -347,8 +330,9 @@ void ClangFormatConfigWidget::apply() return; clang::format::FormatStyle currentSettingsStyle; - const bool success = parseConfigurationContent(m_editor->document()->contents().toStdString(), - currentSettingsStyle); + const Utils::expected_str success + = parseConfigurationContent(m_editor->document()->contents().toStdString(), + currentSettingsStyle); auto saveSettings = [this] { QString errorString; diff --git a/src/plugins/clangformat/clangformatutils.cpp b/src/plugins/clangformat/clangformatutils.cpp index b1e8090ece2..6d16ebe9aa1 100644 --- a/src/plugins/clangformat/clangformatutils.cpp +++ b/src/plugins/clangformat/clangformatutils.cpp @@ -19,6 +19,7 @@ #include #include +#include #include @@ -417,15 +418,30 @@ Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreference / QLatin1String(Constants::SETTINGS_FILE_NAME); } -bool parseConfigurationContent(const std::string &fileContent, clang::format::FormatStyle &style) +static QString s_errorMessage; +Utils::expected_str parseConfigurationContent(const std::string &fileContent, + clang::format::FormatStyle &style) { - style.Language = clang::format::FormatStyle::LK_Cpp; - const std::error_code error = parseConfiguration(fileContent, &style); + auto diagHandler = [](const llvm::SMDiagnostic &diag, void * /*context*/) { + s_errorMessage = QString::fromStdString(diag.getMessage().str()) + " " + + QString::number(diag.getLineNo()) + ":" + + QString::number(diag.getColumnNo()); + }; - return error.value() == static_cast(ParseError::Success); + style.Language = clang::format::FormatStyle::LK_Cpp; + const std::error_code error = parseConfiguration(llvm::MemoryBufferRef(fileContent, "YAML"), + &style, + false, + diagHandler, + nullptr); + + if (error) + return make_unexpected(s_errorMessage); + return {}; } -bool parseConfigurationFile(const Utils::FilePath &filePath, clang::format::FormatStyle &style) +Utils::expected_str parseConfigurationFile(const Utils::FilePath &filePath, + clang::format::FormatStyle &style) { return parseConfigurationContent(filePath.fileContents().value_or(QByteArray()).toStdString(), style); diff --git a/src/plugins/clangformat/clangformatutils.h b/src/plugins/clangformat/clangformatutils.h index a4ff429b3e8..019414b11f5 100644 --- a/src/plugins/clangformat/clangformatutils.h +++ b/src/plugins/clangformat/clangformatutils.h @@ -48,7 +48,9 @@ clang::format::FormatStyle currentQtStyle(const TextEditor::ICodeStylePreference Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle); -bool parseConfigurationContent(const std::string &fileContent, clang::format::FormatStyle &style); -bool parseConfigurationFile(const Utils::FilePath &filePath, clang::format::FormatStyle &style); +Utils::expected_str parseConfigurationContent(const std::string &fileContent, + clang::format::FormatStyle &style); +Utils::expected_str parseConfigurationFile(const Utils::FilePath &filePath, + clang::format::FormatStyle &style); } // ClangFormat