diff options
author | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2019-04-18 16:13:32 +0200 |
---|---|---|
committer | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2019-04-29 12:32:31 +0000 |
commit | 1ae98c35954e45a95ce1b76fdcb5120d9377392b (patch) | |
tree | 87c19f116398f46a92653b011e81af24c62c38ef /src/plugins/clangformat | |
parent | de1eed1e11f35af422d78c278cba686d2b2b1567 (diff) |
ClangFormat: Add the interaction with Beautifier
Do not allow ClangFormat plugin to format on file save
when the Beautifier has the same checkbox selected.
Change-Id: I655d77a888cc444ccb02c4e72f11a93b3ab9bbd5
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Diffstat (limited to 'src/plugins/clangformat')
-rw-r--r-- | src/plugins/clangformat/clangformatconfigwidget.cpp | 54 | ||||
-rw-r--r-- | src/plugins/clangformat/clangformatconfigwidget.h | 1 | ||||
-rw-r--r-- | src/plugins/clangformat/clangformatconfigwidget.ui | 34 |
3 files changed, 84 insertions, 5 deletions
diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index 61287bcb38b..3e311c1e4e2 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -37,6 +37,8 @@ #include <coreplugin/icore.h> #include <cppeditor/cpphighlighter.h> #include <cpptools/cppcodestylesnippets.h> +#include <extensionsystem/pluginmanager.h> +#include <extensionsystem/pluginspec.h> #include <projectexplorer/project.h> #include <projectexplorer/session.h> #include <texteditor/displaysettings.h> @@ -45,6 +47,7 @@ #include <texteditor/texteditorsettings.h> #include <utils/executeondestruction.h> #include <utils/qtcassert.h> +#include <utils/genericconstants.h> #include <QFile> #include <QMessageBox> @@ -55,6 +58,36 @@ using namespace ProjectExplorer; namespace ClangFormat { +static const char kFileSaveWarning[] + = "Disable formatting on file save in the Beautifier plugin to enable this check"; + +static bool isBeautifierPluginActivated() +{ + const QList<ExtensionSystem::PluginSpec *> specs = ExtensionSystem::PluginManager::plugins(); + return std::find_if(specs.begin(), + specs.end(), + [](ExtensionSystem::PluginSpec *spec) { + return spec->name() == "Beautifier"; + }) + != specs.end(); +} + +static bool isBeautifierOnSaveActivated() +{ + if (!isBeautifierPluginActivated()) + return false; + + QSettings *s = Core::ICore::settings(); + bool activated = false; + s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP); + s->beginGroup(Utils::Constants::BEAUTIFIER_GENERAL_GROUP); + if (s->value(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE, false).toBool()) + activated = true; + s->endGroup(); + s->endGroup(); + return activated; +} + bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event) { if (event->type() == QEvent::Wheel && qobject_cast<QComboBox *>(object)) { @@ -64,6 +97,22 @@ bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event) return QWidget::eventFilter(object, event); } +void ClangFormatConfigWidget::showEvent(QShowEvent *event) +{ + TextEditor::CodeStyleEditorWidget::showEvent(event); + if (isBeautifierOnSaveActivated()) { + bool wasEnabled = m_ui->formatOnSave->isEnabled(); + m_ui->formatOnSave->setChecked(false); + m_ui->formatOnSave->setEnabled(false); + m_ui->fileSaveWarning->setText(tr(kFileSaveWarning)); + if (wasEnabled) + apply(); + } else { + m_ui->formatOnSave->setEnabled(true); + m_ui->fileSaveWarning->setText(""); + } +} + ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *project, QWidget *parent) : CodeStyleEditorWidget(parent) , m_project(project) @@ -183,6 +232,11 @@ void ClangFormatConfigWidget::showGlobalCheckboxes() m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave()); m_ui->formatOnSave->show(); + if (isBeautifierOnSaveActivated()) { + m_ui->formatOnSave->setChecked(false); + m_ui->formatOnSave->setEnabled(false); + m_ui->fileSaveWarning->setText(tr(kFileSaveWarning)); + } } static bool projectConfigExists() diff --git a/src/plugins/clangformat/clangformatconfigwidget.h b/src/plugins/clangformat/clangformatconfigwidget.h index 5da2bd35f2e..869ddeccb78 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.h +++ b/src/plugins/clangformat/clangformatconfigwidget.h @@ -59,6 +59,7 @@ private: void onTableChanged(); bool eventFilter(QObject *object, QEvent *event) override; + void showEvent(QShowEvent *event) override; void showOrHideWidgets(); void initChecksAndPreview(); diff --git a/src/plugins/clangformat/clangformatconfigwidget.ui b/src/plugins/clangformat/clangformatconfigwidget.ui index d9126a4c269..45b9801d7ff 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.ui +++ b/src/plugins/clangformat/clangformatconfigwidget.ui @@ -41,11 +41,35 @@ </widget> </item> <item> - <widget class="QCheckBox" name="formatOnSave"> - <property name="text"> - <string>Format edited code on file save</string> - </property> - </widget> + <layout class="QHBoxLayout" name="horizontalLayout_3"> + <item> + <widget class="QCheckBox" name="formatOnSave"> + <property name="text"> + <string>Format edited code on file save</string> + </property> + </widget> + </item> + <item> + <widget class="QLabel" name="fileSaveWarning"> + <property name="text"> + <string/> + </property> + </widget> + </item> + <item> + <spacer name="horizontalSpacer_2"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + </layout> </item> <item> <widget class="QLabel" name="projectHasClangFormat"> |