ClangFormat: Add disable option

- Added a possibility to disable ClangFormat plugin
- Removed unneeded properties from clangformatsettings

Change-Id: If71f46670e4fd3d2dac6d18c97df5a811504ed5e
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2022-05-12 12:51:51 +02:00
parent 10dd3706fa
commit 0b6fca2293
9 changed files with 76 additions and 109 deletions

View File

@@ -72,7 +72,6 @@ public:
protected:
virtual bool formatCodeInsteadOfIndent() const { return false; }
virtual bool formatWhileTyping() const { return false; }
virtual int lastSaveRevision() const { return 0; }
private:

View File

@@ -62,36 +62,6 @@ using namespace ProjectExplorer;
namespace ClangFormat {
static bool isBeautifierPluginActivated()
{
const QVector<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;
}
static int indentIndex() { return 0; }
static int formatIndex() { return 1; }
bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::Wheel && qobject_cast<QComboBox *>(object)) {
@@ -210,25 +180,22 @@ void ClangFormatConfigWidget::onTableChanged()
void ClangFormatConfigWidget::initIndentationOrFormattingCombobox()
{
m_ui->indentingOrFormatting->insertItem(indentIndex(), tr("Indenting only"));
m_ui->indentingOrFormatting->insertItem(formatIndex(), tr("Full formatting"));
m_ui->indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Indenting),
tr("Indenting only"));
m_ui->indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Formatting),
tr("Full formatting"));
m_ui->indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Disable),
tr("Disable"));
if (ClangFormatSettings::instance().formatCodeInsteadOfIndent())
m_ui->indentingOrFormatting->setCurrentIndex(formatIndex());
else
m_ui->indentingOrFormatting->setCurrentIndex(indentIndex());
m_ui->indentingOrFormatting->setCurrentIndex(
static_cast<int>(ClangFormatSettings::instance().mode()));
m_ui->indentingOrFormatting->show();
connect(m_ui->indentingOrFormatting, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, [](int index) {
ClangFormatSettings &settings = ClangFormatSettings::instance();
const bool isFormatting = index == formatIndex();
settings.setFormatCodeInsteadOfIndent(isFormatting);
if (!isBeautifierOnSaveActivated())
settings.setFormatOnSave(isFormatting);
settings.setMode(static_cast<ClangFormatSettings::Mode>(index));
settings.write();
});
}

View File

@@ -31,10 +31,8 @@ static const char SETTINGS_FILE_NAME[] = ".clang-format";
static const char SETTINGS_FILE_ALT_NAME[] = "_clang-format";
static const char SAMPLE_FILE_NAME[] = "test.cpp";
static const char SETTINGS_ID[] = "ClangFormat";
static const char FORMAT_CODE_INSTEAD_OF_INDENT_ID[] = "ClangFormat.FormatCodeInsteadOfIndent";
static const char FORMAT_WHILE_TYPING_ID[] = "ClangFormat.FormatWhileTyping";
static const char FORMAT_CODE_ON_SAVE_ID[] = "ClangFormat.FormatCodeOnSave";
static const char OVERRIDE_FILE_ID[] = "ClangFormat.OverrideFile";
static const char MODE_ID[] = "ClangFormat.Mode";
static const char OPEN_CURRENT_CONFIG_ID[] = "ClangFormat.OpenCurrentConfig";
} // namespace Constants
} // namespace ClangFormat

View File

@@ -166,7 +166,7 @@ CppEditor::CppCodeStyleSettings ClangFormatFile::toCppCodeStyleSettings(
settings.indentControlFlowRelativeToSwitchLabels = style.IndentCaseBlocks;
#endif
if (style.DerivePointerAlignment
&& ClangFormatSettings::instance().formatCodeInsteadOfIndent()) {
&& ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting) {
settings.bindStarToIdentifier = style.PointerAlignment == FormatStyle::PAS_Right;
settings.bindStarToTypeName = style.PointerAlignment == FormatStyle::PAS_Left;
settings.bindStarToLeftSpecifier = style.PointerAlignment == FormatStyle::PAS_Left;
@@ -209,11 +209,11 @@ void ClangFormatFile::fromCppCodeStyleSettings(const CppEditor::CppCodeStyleSett
|| settings.bindStarToRightSpecifier;
if ((settings.bindStarToIdentifier || settings.bindStarToRightSpecifier)
&& ClangFormatSettings::instance().formatCodeInsteadOfIndent())
&& ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting)
m_style.PointerAlignment = FormatStyle::PAS_Right;
if ((settings.bindStarToTypeName || settings.bindStarToLeftSpecifier)
&& ClangFormatSettings::instance().formatCodeInsteadOfIndent())
&& ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting)
m_style.PointerAlignment = FormatStyle::PAS_Left;
saveNewFormat();

View File

@@ -24,11 +24,16 @@
****************************************************************************/
#include "clangformatindenter.h"
#include "clangformatconstants.h"
#include "clangformatsettings.h"
#include "clangformatutils.h"
#include <coreplugin/icore.h>
#include <extensionsystem/pluginmanager.h>
#include <extensionsystem/pluginspec.h>
#include <texteditor/tabsettings.h>
#include <texteditor/textdocumentlayout.h>
#include <utils/genericconstants.h>
using namespace clang;
using namespace format;
@@ -36,18 +41,40 @@ using namespace TextEditor;
namespace ClangFormat {
static bool isBeautifierPluginActivated()
{
const QVector<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;
}
ClangFormatIndenter::ClangFormatIndenter(QTextDocument *doc)
: ClangFormatBaseIndenter(doc)
{}
bool ClangFormatIndenter::formatCodeInsteadOfIndent() const
{
return ClangFormatSettings::instance().formatCodeInsteadOfIndent();
}
bool ClangFormatIndenter::formatWhileTyping() const
{
return ClangFormatSettings::instance().formatWhileTyping();
return ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting;
}
Utils::optional<TabSettings> ClangFormatIndenter::tabSettings() const
@@ -87,7 +114,7 @@ int ClangFormatIndenter::lastSaveRevision() const
bool ClangFormatIndenter::formatOnSave() const
{
return ClangFormatSettings::instance().formatOnSave();
return !isBeautifierOnSaveActivated() && formatCodeInsteadOfIndent();
}
} // namespace ClangFormat

View File

@@ -40,7 +40,6 @@ public:
private:
bool formatCodeInsteadOfIndent() const override;
bool formatWhileTyping() const override;
int lastSaveRevision() const override;
};

View File

@@ -28,6 +28,7 @@
#include "clangformatconfigwidget.h"
#include "clangformatconstants.h"
#include "clangformatindenter.h"
#include "clangformatsettings.h"
#include "clangformatutils.h"
#include "tests/clangformat-test.h"
@@ -56,6 +57,7 @@
#include <projectexplorer/target.h>
#include <texteditor/icodestylepreferences.h>
#include <texteditor/textindenter.h>
#include <texteditor/texteditorsettings.h>
#include <clang/Format/Format.h>
@@ -81,6 +83,8 @@ class ClangFormatStyleFactory : public CppEditor::CppCodeStylePreferencesFactory
public:
TextEditor::Indenter *createIndenter(QTextDocument *doc) const override
{
if (ClangFormatSettings::instance().mode() == ClangFormatSettings::Disable)
return CppEditor::CppCodeStylePreferencesFactory::createIndenter(doc);
return new ClangFormatIndenter(doc);
}

View File

@@ -40,14 +40,11 @@ ClangFormatSettings::ClangFormatSettings()
{
QSettings *settings = Core::ICore::settings();
settings->beginGroup(QLatin1String(Constants::SETTINGS_ID));
m_formatCodeInsteadOfIndent
= settings->value(QLatin1String(Constants::FORMAT_CODE_INSTEAD_OF_INDENT_ID), false).toBool();
m_formatWhileTyping = settings->value(QLatin1String(Constants::FORMAT_WHILE_TYPING_ID), false)
.toBool();
m_formatOnSave = settings->value(QLatin1String(Constants::FORMAT_CODE_ON_SAVE_ID), false)
.toBool();
m_overrideDefaultFile = settings->value(QLatin1String(Constants::OVERRIDE_FILE_ID), false)
.toBool();
m_mode = static_cast<ClangFormatSettings::Mode>(
settings->value(QLatin1String(Constants::MODE_ID), ClangFormatSettings::Mode::Indenting)
.toInt());
settings->endGroup();
}
@@ -55,44 +52,11 @@ void ClangFormatSettings::write() const
{
QSettings *settings = Core::ICore::settings();
settings->beginGroup(QLatin1String(Constants::SETTINGS_ID));
settings->setValue(QLatin1String(Constants::FORMAT_CODE_INSTEAD_OF_INDENT_ID),
m_formatCodeInsteadOfIndent);
settings->setValue(QLatin1String(Constants::FORMAT_WHILE_TYPING_ID), m_formatWhileTyping);
settings->setValue(QLatin1String(Constants::FORMAT_CODE_ON_SAVE_ID), m_formatOnSave);
settings->setValue(QLatin1String(Constants::OVERRIDE_FILE_ID), m_overrideDefaultFile);
settings->setValue(QLatin1String(Constants::MODE_ID), static_cast<int>(m_mode));
settings->endGroup();
}
void ClangFormatSettings::setFormatCodeInsteadOfIndent(bool enable)
{
m_formatCodeInsteadOfIndent = enable;
}
bool ClangFormatSettings::formatCodeInsteadOfIndent() const
{
return m_formatCodeInsteadOfIndent;
}
void ClangFormatSettings::setFormatWhileTyping(bool enable)
{
m_formatWhileTyping = enable;
}
bool ClangFormatSettings::formatWhileTyping() const
{
return m_formatWhileTyping;
}
void ClangFormatSettings::setFormatOnSave(bool enable)
{
m_formatOnSave = enable;
}
bool ClangFormatSettings::formatOnSave() const
{
return m_formatOnSave;
}
void ClangFormatSettings::setOverrideDefaultFile(bool enable)
{
m_overrideDefaultFile = enable;
@@ -103,4 +67,14 @@ bool ClangFormatSettings::overrideDefaultFile() const
return m_overrideDefaultFile;
}
void ClangFormatSettings::setMode(Mode mode)
{
m_mode = mode;
}
ClangFormatSettings::Mode ClangFormatSettings::mode() const
{
return m_mode;
}
} // namespace ClangFormat

View File

@@ -37,22 +37,21 @@ public:
ClangFormatSettings();
void write() const;
void setFormatCodeInsteadOfIndent(bool enable);
bool formatCodeInsteadOfIndent() const;
void setFormatWhileTyping(bool enable);
bool formatWhileTyping() const;
void setFormatOnSave(bool enable);
bool formatOnSave() const;
void setOverrideDefaultFile(bool enable);
bool overrideDefaultFile() const;
enum Mode {
Indenting = 0,
Formatting,
Disable
};
void setMode(Mode mode);
Mode mode() const;
private:
bool m_formatCodeInsteadOfIndent = false;
bool m_formatWhileTyping = false;
bool m_formatOnSave = false;
bool m_overrideDefaultFile = false;
Mode m_mode;
};
} // namespace ClangFormat