forked from qt-creator/qt-creator
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:
37
src/libs/utils/genericconstants.h
Normal file
37
src/libs/utils/genericconstants.h
Normal 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
|
||||
@@ -132,6 +132,7 @@ win32:SOURCES += $$PWD/consoleprocess_win.cpp
|
||||
else:SOURCES += $$PWD/consoleprocess_unix.cpp
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/genericconstants.h \
|
||||
$$PWD/globalfilechangeblocker.h \
|
||||
$$PWD/benchmarker.h \
|
||||
$$PWD/environment.h \
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include <coreplugin/idocument.h>
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/fileutils.h>
|
||||
#include <utils/genericconstants.h>
|
||||
#include <utils/mimetypes/mimedatabase.h>
|
||||
|
||||
#include <QFile>
|
||||
@@ -204,7 +205,7 @@ void AbstractSettings::save()
|
||||
{
|
||||
// Save settings, except styles
|
||||
QSettings *s = Core::ICore::settings();
|
||||
s->beginGroup(Constants::SETTINGS_GROUP);
|
||||
s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP);
|
||||
s->beginGroup(m_name);
|
||||
QMap<QString, QVariant>::const_iterator iSettings = m_settings.constBegin();
|
||||
while (iSettings != m_settings.constEnd()) {
|
||||
@@ -276,7 +277,7 @@ void AbstractSettings::read()
|
||||
|
||||
// Read settings, except styles
|
||||
QSettings *s = Core::ICore::settings();
|
||||
s->beginGroup(Constants::SETTINGS_GROUP);
|
||||
s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP);
|
||||
s->beginGroup(m_name);
|
||||
const QStringList keys = s->allKeys();
|
||||
for (const QString &key : keys) {
|
||||
|
||||
@@ -34,7 +34,6 @@ const char ACTION_ID[] = "Beautifier.Action";
|
||||
const char MENU_ID[] = "Beautifier.Menu";
|
||||
const char OPTION_CATEGORY[] = "II.Beautifier";
|
||||
const char OPTION_GENERAL_ID[] = "aaa.General";
|
||||
const char SETTINGS_GROUP[] = "Beautifier";
|
||||
const char SETTINGS_DIRNAME[] = "beautifier";
|
||||
const char DOCUMENTATION_DIRNAME[] = "documentation";
|
||||
const char DOCUMENTATION_XMLROOT[] = "beautifier_documentation";
|
||||
|
||||
@@ -29,14 +29,13 @@
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/genericconstants.h>
|
||||
#include <utils/mimetypes/mimedatabase.h>
|
||||
|
||||
namespace Beautifier {
|
||||
namespace Internal {
|
||||
|
||||
namespace {
|
||||
const char GROUP[] = "General";
|
||||
const char AUTO_FORMAT_ON_SAVE[] = "autoFormatOnSave";
|
||||
const char AUTO_FORMAT_TOOL[] = "autoFormatTool";
|
||||
const char AUTO_FORMAT_MIME[] = "autoFormatMime";
|
||||
const char AUTO_FORMAT_ONLY_CURRENT_PROJECT[] = "autoFormatOnlyCurrentProject";
|
||||
@@ -50,9 +49,9 @@ GeneralSettings::GeneralSettings()
|
||||
void GeneralSettings::read()
|
||||
{
|
||||
QSettings *s = Core::ICore::settings();
|
||||
s->beginGroup(Constants::SETTINGS_GROUP);
|
||||
s->beginGroup(GROUP);
|
||||
m_autoFormatOnSave = s->value(AUTO_FORMAT_ON_SAVE, false).toBool();
|
||||
s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP);
|
||||
s->beginGroup(Utils::Constants::BEAUTIFIER_GENERAL_GROUP);
|
||||
m_autoFormatOnSave = s->value(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE, false).toBool();
|
||||
m_autoFormatTool = s->value(AUTO_FORMAT_TOOL, QString()).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();
|
||||
@@ -63,9 +62,9 @@ void GeneralSettings::read()
|
||||
void GeneralSettings::save()
|
||||
{
|
||||
QSettings *s = Core::ICore::settings();
|
||||
s->beginGroup(Constants::SETTINGS_GROUP);
|
||||
s->beginGroup(GROUP);
|
||||
s->setValue(AUTO_FORMAT_ON_SAVE, m_autoFormatOnSave);
|
||||
s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP);
|
||||
s->beginGroup(Utils::Constants::BEAUTIFIER_GENERAL_GROUP);
|
||||
s->setValue(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE, m_autoFormatOnSave);
|
||||
s->setValue(AUTO_FORMAT_TOOL, m_autoFormatTool);
|
||||
s->setValue(AUTO_FORMAT_MIME, autoFormatMimeAsString());
|
||||
s->setValue(AUTO_FORMAT_ONLY_CURRENT_PROJECT, m_autoFormatOnlyCurrentProject);
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -59,6 +59,7 @@ private:
|
||||
void onTableChanged();
|
||||
|
||||
bool eventFilter(QObject *object, QEvent *event) override;
|
||||
void showEvent(QShowEvent *event) override;
|
||||
|
||||
void showOrHideWidgets();
|
||||
void initChecksAndPreview();
|
||||
|
||||
@@ -40,6 +40,8 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_3">
|
||||
<item>
|
||||
<widget class="QCheckBox" name="formatOnSave">
|
||||
<property name="text">
|
||||
@@ -47,6 +49,28 @@
|
||||
</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">
|
||||
<property name="text">
|
||||
|
||||
Reference in New Issue
Block a user