aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/clangformat
diff options
context:
space:
mode:
authorArtem Sokolovskii <artem.sokolovskii@qt.io>2022-05-12 12:51:51 +0200
committerArtem Sokolovskii <artem.sokolovskii@qt.io>2022-05-19 11:58:45 +0000
commit0b6fca2293f38dd359fc4e02bcec25706567c649 (patch)
tree29777f34d8d6353fc0c7985c5e1954b74c48dece /src/plugins/clangformat
parent10dd3706fac7028d91edce641fc62eff12fb08a8 (diff)
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>
Diffstat (limited to 'src/plugins/clangformat')
-rw-r--r--src/plugins/clangformat/clangformatbaseindenter.h1
-rw-r--r--src/plugins/clangformat/clangformatconfigwidget.cpp51
-rw-r--r--src/plugins/clangformat/clangformatconstants.h4
-rw-r--r--src/plugins/clangformat/clangformatfile.cpp6
-rw-r--r--src/plugins/clangformat/clangformatindenter.cpp41
-rw-r--r--src/plugins/clangformat/clangformatindenter.h1
-rw-r--r--src/plugins/clangformat/clangformatplugin.cpp4
-rw-r--r--src/plugins/clangformat/clangformatsettings.cpp50
-rw-r--r--src/plugins/clangformat/clangformatsettings.h21
9 files changed, 73 insertions, 106 deletions
diff --git a/src/plugins/clangformat/clangformatbaseindenter.h b/src/plugins/clangformat/clangformatbaseindenter.h
index 0b96451aca..bcfd4ca4a4 100644
--- a/src/plugins/clangformat/clangformatbaseindenter.h
+++ b/src/plugins/clangformat/clangformatbaseindenter.h
@@ -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:
diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp
index 79ca3b5a1c..ddf0415ef4 100644
--- a/src/plugins/clangformat/clangformatconfigwidget.cpp
+++ b/src/plugins/clangformat/clangformatconfigwidget.cpp
@@ -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();
});
}
diff --git a/src/plugins/clangformat/clangformatconstants.h b/src/plugins/clangformat/clangformatconstants.h
index 2635dbedea..74e84efa17 100644
--- a/src/plugins/clangformat/clangformatconstants.h
+++ b/src/plugins/clangformat/clangformatconstants.h
@@ -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
diff --git a/src/plugins/clangformat/clangformatfile.cpp b/src/plugins/clangformat/clangformatfile.cpp
index b439dc5698..192bb47cb8 100644
--- a/src/plugins/clangformat/clangformatfile.cpp
+++ b/src/plugins/clangformat/clangformatfile.cpp
@@ -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();
diff --git a/src/plugins/clangformat/clangformatindenter.cpp b/src/plugins/clangformat/clangformatindenter.cpp
index 7476c12041..6f7bb32ca8 100644
--- a/src/plugins/clangformat/clangformatindenter.cpp
+++ b/src/plugins/clangformat/clangformatindenter.cpp
@@ -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
diff --git a/src/plugins/clangformat/clangformatindenter.h b/src/plugins/clangformat/clangformatindenter.h
index 9e1a65fb3d..83f0b912f6 100644
--- a/src/plugins/clangformat/clangformatindenter.h
+++ b/src/plugins/clangformat/clangformatindenter.h
@@ -40,7 +40,6 @@ public:
private:
bool formatCodeInsteadOfIndent() const override;
- bool formatWhileTyping() const override;
int lastSaveRevision() const override;
};
diff --git a/src/plugins/clangformat/clangformatplugin.cpp b/src/plugins/clangformat/clangformatplugin.cpp
index 69ef7235fe..0b2c2fd5a6 100644
--- a/src/plugins/clangformat/clangformatplugin.cpp
+++ b/src/plugins/clangformat/clangformatplugin.cpp
@@ -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);
}
diff --git a/src/plugins/clangformat/clangformatsettings.cpp b/src/plugins/clangformat/clangformatsettings.cpp
index 8f70130ae3..66c2076003 100644
--- a/src/plugins/clangformat/clangformatsettings.cpp
+++ b/src/plugins/clangformat/clangformatsettings.cpp
@@ -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,52 +52,29 @@ 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)
+void ClangFormatSettings::setOverrideDefaultFile(bool enable)
{
- m_formatOnSave = enable;
+ m_overrideDefaultFile = enable;
}
-bool ClangFormatSettings::formatOnSave() const
+bool ClangFormatSettings::overrideDefaultFile() const
{
- return m_formatOnSave;
+ return m_overrideDefaultFile;
}
-void ClangFormatSettings::setOverrideDefaultFile(bool enable)
+void ClangFormatSettings::setMode(Mode mode)
{
- m_overrideDefaultFile = enable;
+ m_mode = mode;
}
-bool ClangFormatSettings::overrideDefaultFile() const
+ClangFormatSettings::Mode ClangFormatSettings::mode() const
{
- return m_overrideDefaultFile;
+ return m_mode;
}
} // namespace ClangFormat
diff --git a/src/plugins/clangformat/clangformatsettings.h b/src/plugins/clangformat/clangformatsettings.h
index 9344cf34a9..88b37d393e 100644
--- a/src/plugins/clangformat/clangformatsettings.h
+++ b/src/plugins/clangformat/clangformatsettings.h
@@ -37,22 +37,21 @@ public:
ClangFormatSettings();
void write() const;
- void setFormatCodeInsteadOfIndent(bool enable);
- bool formatCodeInsteadOfIndent() const;
+ void setOverrideDefaultFile(bool enable);
+ bool overrideDefaultFile() const;
- void setFormatWhileTyping(bool enable);
- bool formatWhileTyping() const;
+ enum Mode {
+ Indenting = 0,
+ Formatting,
+ Disable
+ };
- void setFormatOnSave(bool enable);
- bool formatOnSave() const;
+ void setMode(Mode mode);
+ Mode mode() const;
- void setOverrideDefaultFile(bool enable);
- bool overrideDefaultFile() const;
private:
- bool m_formatCodeInsteadOfIndent = false;
- bool m_formatWhileTyping = false;
- bool m_formatOnSave = false;
bool m_overrideDefaultFile = false;
+ Mode m_mode;
};
} // namespace ClangFormat