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>
This commit is contained in:
Ivan Donchevskii
2019-04-18 16:13:32 +02:00
parent de1eed1e11
commit 1ae98c3595
8 changed files with 132 additions and 16 deletions

View File

@@ -0,0 +1,37 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
namespace Utils {
namespace Constants {
const char BEAUTIFIER_SETTINGS_GROUP[] = "Beautifier";
const char BEAUTIFIER_GENERAL_GROUP[] = "General";
const char BEAUTIFIER_AUTO_FORMAT_ON_SAVE[] = "autoFormatOnSave";
} // namespace Constants
} // namespace Utils

View File

@@ -132,6 +132,7 @@ win32:SOURCES += $$PWD/consoleprocess_win.cpp
else:SOURCES += $$PWD/consoleprocess_unix.cpp else:SOURCES += $$PWD/consoleprocess_unix.cpp
HEADERS += \ HEADERS += \
$$PWD/genericconstants.h \
$$PWD/globalfilechangeblocker.h \ $$PWD/globalfilechangeblocker.h \
$$PWD/benchmarker.h \ $$PWD/benchmarker.h \
$$PWD/environment.h \ $$PWD/environment.h \

View File

@@ -32,6 +32,7 @@
#include <coreplugin/idocument.h> #include <coreplugin/idocument.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/genericconstants.h>
#include <utils/mimetypes/mimedatabase.h> #include <utils/mimetypes/mimedatabase.h>
#include <QFile> #include <QFile>
@@ -204,7 +205,7 @@ void AbstractSettings::save()
{ {
// Save settings, except styles // Save settings, except styles
QSettings *s = Core::ICore::settings(); QSettings *s = Core::ICore::settings();
s->beginGroup(Constants::SETTINGS_GROUP); s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP);
s->beginGroup(m_name); s->beginGroup(m_name);
QMap<QString, QVariant>::const_iterator iSettings = m_settings.constBegin(); QMap<QString, QVariant>::const_iterator iSettings = m_settings.constBegin();
while (iSettings != m_settings.constEnd()) { while (iSettings != m_settings.constEnd()) {
@@ -276,7 +277,7 @@ void AbstractSettings::read()
// Read settings, except styles // Read settings, except styles
QSettings *s = Core::ICore::settings(); QSettings *s = Core::ICore::settings();
s->beginGroup(Constants::SETTINGS_GROUP); s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP);
s->beginGroup(m_name); s->beginGroup(m_name);
const QStringList keys = s->allKeys(); const QStringList keys = s->allKeys();
for (const QString &key : keys) { for (const QString &key : keys) {

View File

@@ -34,7 +34,6 @@ const char ACTION_ID[] = "Beautifier.Action";
const char MENU_ID[] = "Beautifier.Menu"; const char MENU_ID[] = "Beautifier.Menu";
const char OPTION_CATEGORY[] = "II.Beautifier"; const char OPTION_CATEGORY[] = "II.Beautifier";
const char OPTION_GENERAL_ID[] = "aaa.General"; const char OPTION_GENERAL_ID[] = "aaa.General";
const char SETTINGS_GROUP[] = "Beautifier";
const char SETTINGS_DIRNAME[] = "beautifier"; const char SETTINGS_DIRNAME[] = "beautifier";
const char DOCUMENTATION_DIRNAME[] = "documentation"; const char DOCUMENTATION_DIRNAME[] = "documentation";
const char DOCUMENTATION_XMLROOT[] = "beautifier_documentation"; const char DOCUMENTATION_XMLROOT[] = "beautifier_documentation";

View File

@@ -29,14 +29,13 @@
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/genericconstants.h>
#include <utils/mimetypes/mimedatabase.h> #include <utils/mimetypes/mimedatabase.h>
namespace Beautifier { namespace Beautifier {
namespace Internal { namespace Internal {
namespace { namespace {
const char GROUP[] = "General";
const char AUTO_FORMAT_ON_SAVE[] = "autoFormatOnSave";
const char AUTO_FORMAT_TOOL[] = "autoFormatTool"; const char AUTO_FORMAT_TOOL[] = "autoFormatTool";
const char AUTO_FORMAT_MIME[] = "autoFormatMime"; const char AUTO_FORMAT_MIME[] = "autoFormatMime";
const char AUTO_FORMAT_ONLY_CURRENT_PROJECT[] = "autoFormatOnlyCurrentProject"; const char AUTO_FORMAT_ONLY_CURRENT_PROJECT[] = "autoFormatOnlyCurrentProject";
@@ -50,9 +49,9 @@ GeneralSettings::GeneralSettings()
void GeneralSettings::read() void GeneralSettings::read()
{ {
QSettings *s = Core::ICore::settings(); QSettings *s = Core::ICore::settings();
s->beginGroup(Constants::SETTINGS_GROUP); s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP);
s->beginGroup(GROUP); s->beginGroup(Utils::Constants::BEAUTIFIER_GENERAL_GROUP);
m_autoFormatOnSave = s->value(AUTO_FORMAT_ON_SAVE, false).toBool(); m_autoFormatOnSave = s->value(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE, false).toBool();
m_autoFormatTool = s->value(AUTO_FORMAT_TOOL, QString()).toString(); m_autoFormatTool = s->value(AUTO_FORMAT_TOOL, QString()).toString();
setAutoFormatMime(s->value(AUTO_FORMAT_MIME, "text/x-c++src;text/x-c++hdr").toString()); setAutoFormatMime(s->value(AUTO_FORMAT_MIME, "text/x-c++src;text/x-c++hdr").toString());
m_autoFormatOnlyCurrentProject = s->value(AUTO_FORMAT_ONLY_CURRENT_PROJECT, true).toBool(); m_autoFormatOnlyCurrentProject = s->value(AUTO_FORMAT_ONLY_CURRENT_PROJECT, true).toBool();
@@ -63,9 +62,9 @@ void GeneralSettings::read()
void GeneralSettings::save() void GeneralSettings::save()
{ {
QSettings *s = Core::ICore::settings(); QSettings *s = Core::ICore::settings();
s->beginGroup(Constants::SETTINGS_GROUP); s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP);
s->beginGroup(GROUP); s->beginGroup(Utils::Constants::BEAUTIFIER_GENERAL_GROUP);
s->setValue(AUTO_FORMAT_ON_SAVE, m_autoFormatOnSave); s->setValue(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE, m_autoFormatOnSave);
s->setValue(AUTO_FORMAT_TOOL, m_autoFormatTool); s->setValue(AUTO_FORMAT_TOOL, m_autoFormatTool);
s->setValue(AUTO_FORMAT_MIME, autoFormatMimeAsString()); s->setValue(AUTO_FORMAT_MIME, autoFormatMimeAsString());
s->setValue(AUTO_FORMAT_ONLY_CURRENT_PROJECT, m_autoFormatOnlyCurrentProject); s->setValue(AUTO_FORMAT_ONLY_CURRENT_PROJECT, m_autoFormatOnlyCurrentProject);

View File

@@ -37,6 +37,8 @@
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <cppeditor/cpphighlighter.h> #include <cppeditor/cpphighlighter.h>
#include <cpptools/cppcodestylesnippets.h> #include <cpptools/cppcodestylesnippets.h>
#include <extensionsystem/pluginmanager.h>
#include <extensionsystem/pluginspec.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <projectexplorer/session.h> #include <projectexplorer/session.h>
#include <texteditor/displaysettings.h> #include <texteditor/displaysettings.h>
@@ -45,6 +47,7 @@
#include <texteditor/texteditorsettings.h> #include <texteditor/texteditorsettings.h>
#include <utils/executeondestruction.h> #include <utils/executeondestruction.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/genericconstants.h>
#include <QFile> #include <QFile>
#include <QMessageBox> #include <QMessageBox>
@@ -55,6 +58,36 @@ using namespace ProjectExplorer;
namespace ClangFormat { 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) bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event)
{ {
if (event->type() == QEvent::Wheel && qobject_cast<QComboBox *>(object)) { 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); 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) ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *project, QWidget *parent)
: CodeStyleEditorWidget(parent) : CodeStyleEditorWidget(parent)
, m_project(project) , m_project(project)
@@ -183,6 +232,11 @@ void ClangFormatConfigWidget::showGlobalCheckboxes()
m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave()); m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave());
m_ui->formatOnSave->show(); 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() static bool projectConfigExists()

View File

@@ -59,6 +59,7 @@ private:
void onTableChanged(); void onTableChanged();
bool eventFilter(QObject *object, QEvent *event) override; bool eventFilter(QObject *object, QEvent *event) override;
void showEvent(QShowEvent *event) override;
void showOrHideWidgets(); void showOrHideWidgets();
void initChecksAndPreview(); void initChecksAndPreview();

View File

@@ -41,11 +41,35 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QCheckBox" name="formatOnSave"> <layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="text"> <item>
<string>Format edited code on file save</string> <widget class="QCheckBox" name="formatOnSave">
</property> <property name="text">
</widget> <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>
<item> <item>
<widget class="QLabel" name="projectHasClangFormat"> <widget class="QLabel" name="projectHasClangFormat">