forked from qt-creator/qt-creator
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:
@@ -72,7 +72,6 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual bool formatCodeInsteadOfIndent() const { return false; }
|
virtual bool formatCodeInsteadOfIndent() const { return false; }
|
||||||
virtual bool formatWhileTyping() const { return false; }
|
|
||||||
virtual int lastSaveRevision() const { return 0; }
|
virtual int lastSaveRevision() const { return 0; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -62,36 +62,6 @@ using namespace ProjectExplorer;
|
|||||||
|
|
||||||
namespace ClangFormat {
|
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)
|
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)) {
|
||||||
@@ -210,25 +180,22 @@ void ClangFormatConfigWidget::onTableChanged()
|
|||||||
|
|
||||||
void ClangFormatConfigWidget::initIndentationOrFormattingCombobox()
|
void ClangFormatConfigWidget::initIndentationOrFormattingCombobox()
|
||||||
{
|
{
|
||||||
m_ui->indentingOrFormatting->insertItem(indentIndex(), tr("Indenting only"));
|
m_ui->indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Indenting),
|
||||||
m_ui->indentingOrFormatting->insertItem(formatIndex(), tr("Full formatting"));
|
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(
|
||||||
m_ui->indentingOrFormatting->setCurrentIndex(formatIndex());
|
static_cast<int>(ClangFormatSettings::instance().mode()));
|
||||||
else
|
|
||||||
m_ui->indentingOrFormatting->setCurrentIndex(indentIndex());
|
|
||||||
|
|
||||||
m_ui->indentingOrFormatting->show();
|
m_ui->indentingOrFormatting->show();
|
||||||
|
|
||||||
connect(m_ui->indentingOrFormatting, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
connect(m_ui->indentingOrFormatting, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||||
this, [](int index) {
|
this, [](int index) {
|
||||||
ClangFormatSettings &settings = ClangFormatSettings::instance();
|
ClangFormatSettings &settings = ClangFormatSettings::instance();
|
||||||
const bool isFormatting = index == formatIndex();
|
settings.setMode(static_cast<ClangFormatSettings::Mode>(index));
|
||||||
settings.setFormatCodeInsteadOfIndent(isFormatting);
|
|
||||||
|
|
||||||
if (!isBeautifierOnSaveActivated())
|
|
||||||
settings.setFormatOnSave(isFormatting);
|
|
||||||
|
|
||||||
settings.write();
|
settings.write();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,10 +31,8 @@ static const char SETTINGS_FILE_NAME[] = ".clang-format";
|
|||||||
static const char SETTINGS_FILE_ALT_NAME[] = "_clang-format";
|
static const char SETTINGS_FILE_ALT_NAME[] = "_clang-format";
|
||||||
static const char SAMPLE_FILE_NAME[] = "test.cpp";
|
static const char SAMPLE_FILE_NAME[] = "test.cpp";
|
||||||
static const char SETTINGS_ID[] = "ClangFormat";
|
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 OVERRIDE_FILE_ID[] = "ClangFormat.OverrideFile";
|
||||||
|
static const char MODE_ID[] = "ClangFormat.Mode";
|
||||||
static const char OPEN_CURRENT_CONFIG_ID[] = "ClangFormat.OpenCurrentConfig";
|
static const char OPEN_CURRENT_CONFIG_ID[] = "ClangFormat.OpenCurrentConfig";
|
||||||
} // namespace Constants
|
} // namespace Constants
|
||||||
} // namespace ClangFormat
|
} // namespace ClangFormat
|
||||||
|
|||||||
@@ -166,7 +166,7 @@ CppEditor::CppCodeStyleSettings ClangFormatFile::toCppCodeStyleSettings(
|
|||||||
settings.indentControlFlowRelativeToSwitchLabels = style.IndentCaseBlocks;
|
settings.indentControlFlowRelativeToSwitchLabels = style.IndentCaseBlocks;
|
||||||
#endif
|
#endif
|
||||||
if (style.DerivePointerAlignment
|
if (style.DerivePointerAlignment
|
||||||
&& ClangFormatSettings::instance().formatCodeInsteadOfIndent()) {
|
&& ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting) {
|
||||||
settings.bindStarToIdentifier = style.PointerAlignment == FormatStyle::PAS_Right;
|
settings.bindStarToIdentifier = style.PointerAlignment == FormatStyle::PAS_Right;
|
||||||
settings.bindStarToTypeName = style.PointerAlignment == FormatStyle::PAS_Left;
|
settings.bindStarToTypeName = style.PointerAlignment == FormatStyle::PAS_Left;
|
||||||
settings.bindStarToLeftSpecifier = style.PointerAlignment == FormatStyle::PAS_Left;
|
settings.bindStarToLeftSpecifier = style.PointerAlignment == FormatStyle::PAS_Left;
|
||||||
@@ -209,11 +209,11 @@ void ClangFormatFile::fromCppCodeStyleSettings(const CppEditor::CppCodeStyleSett
|
|||||||
|| settings.bindStarToRightSpecifier;
|
|| settings.bindStarToRightSpecifier;
|
||||||
|
|
||||||
if ((settings.bindStarToIdentifier || settings.bindStarToRightSpecifier)
|
if ((settings.bindStarToIdentifier || settings.bindStarToRightSpecifier)
|
||||||
&& ClangFormatSettings::instance().formatCodeInsteadOfIndent())
|
&& ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting)
|
||||||
m_style.PointerAlignment = FormatStyle::PAS_Right;
|
m_style.PointerAlignment = FormatStyle::PAS_Right;
|
||||||
|
|
||||||
if ((settings.bindStarToTypeName || settings.bindStarToLeftSpecifier)
|
if ((settings.bindStarToTypeName || settings.bindStarToLeftSpecifier)
|
||||||
&& ClangFormatSettings::instance().formatCodeInsteadOfIndent())
|
&& ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting)
|
||||||
m_style.PointerAlignment = FormatStyle::PAS_Left;
|
m_style.PointerAlignment = FormatStyle::PAS_Left;
|
||||||
|
|
||||||
saveNewFormat();
|
saveNewFormat();
|
||||||
|
|||||||
@@ -24,11 +24,16 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include "clangformatindenter.h"
|
#include "clangformatindenter.h"
|
||||||
|
#include "clangformatconstants.h"
|
||||||
#include "clangformatsettings.h"
|
#include "clangformatsettings.h"
|
||||||
#include "clangformatutils.h"
|
#include "clangformatutils.h"
|
||||||
|
|
||||||
|
#include <coreplugin/icore.h>
|
||||||
|
#include <extensionsystem/pluginmanager.h>
|
||||||
|
#include <extensionsystem/pluginspec.h>
|
||||||
#include <texteditor/tabsettings.h>
|
#include <texteditor/tabsettings.h>
|
||||||
#include <texteditor/textdocumentlayout.h>
|
#include <texteditor/textdocumentlayout.h>
|
||||||
|
#include <utils/genericconstants.h>
|
||||||
|
|
||||||
using namespace clang;
|
using namespace clang;
|
||||||
using namespace format;
|
using namespace format;
|
||||||
@@ -36,18 +41,40 @@ using namespace TextEditor;
|
|||||||
|
|
||||||
namespace ClangFormat {
|
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)
|
ClangFormatIndenter::ClangFormatIndenter(QTextDocument *doc)
|
||||||
: ClangFormatBaseIndenter(doc)
|
: ClangFormatBaseIndenter(doc)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
bool ClangFormatIndenter::formatCodeInsteadOfIndent() const
|
bool ClangFormatIndenter::formatCodeInsteadOfIndent() const
|
||||||
{
|
{
|
||||||
return ClangFormatSettings::instance().formatCodeInsteadOfIndent();
|
return ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting;
|
||||||
}
|
|
||||||
|
|
||||||
bool ClangFormatIndenter::formatWhileTyping() const
|
|
||||||
{
|
|
||||||
return ClangFormatSettings::instance().formatWhileTyping();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Utils::optional<TabSettings> ClangFormatIndenter::tabSettings() const
|
Utils::optional<TabSettings> ClangFormatIndenter::tabSettings() const
|
||||||
@@ -87,7 +114,7 @@ int ClangFormatIndenter::lastSaveRevision() const
|
|||||||
|
|
||||||
bool ClangFormatIndenter::formatOnSave() const
|
bool ClangFormatIndenter::formatOnSave() const
|
||||||
{
|
{
|
||||||
return ClangFormatSettings::instance().formatOnSave();
|
return !isBeautifierOnSaveActivated() && formatCodeInsteadOfIndent();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ClangFormat
|
} // namespace ClangFormat
|
||||||
|
|||||||
@@ -40,7 +40,6 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
bool formatCodeInsteadOfIndent() const override;
|
bool formatCodeInsteadOfIndent() const override;
|
||||||
bool formatWhileTyping() const override;
|
|
||||||
int lastSaveRevision() const override;
|
int lastSaveRevision() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@
|
|||||||
#include "clangformatconfigwidget.h"
|
#include "clangformatconfigwidget.h"
|
||||||
#include "clangformatconstants.h"
|
#include "clangformatconstants.h"
|
||||||
#include "clangformatindenter.h"
|
#include "clangformatindenter.h"
|
||||||
|
#include "clangformatsettings.h"
|
||||||
#include "clangformatutils.h"
|
#include "clangformatutils.h"
|
||||||
#include "tests/clangformat-test.h"
|
#include "tests/clangformat-test.h"
|
||||||
|
|
||||||
@@ -56,6 +57,7 @@
|
|||||||
#include <projectexplorer/target.h>
|
#include <projectexplorer/target.h>
|
||||||
|
|
||||||
#include <texteditor/icodestylepreferences.h>
|
#include <texteditor/icodestylepreferences.h>
|
||||||
|
#include <texteditor/textindenter.h>
|
||||||
#include <texteditor/texteditorsettings.h>
|
#include <texteditor/texteditorsettings.h>
|
||||||
|
|
||||||
#include <clang/Format/Format.h>
|
#include <clang/Format/Format.h>
|
||||||
@@ -81,6 +83,8 @@ class ClangFormatStyleFactory : public CppEditor::CppCodeStylePreferencesFactory
|
|||||||
public:
|
public:
|
||||||
TextEditor::Indenter *createIndenter(QTextDocument *doc) const override
|
TextEditor::Indenter *createIndenter(QTextDocument *doc) const override
|
||||||
{
|
{
|
||||||
|
if (ClangFormatSettings::instance().mode() == ClangFormatSettings::Disable)
|
||||||
|
return CppEditor::CppCodeStylePreferencesFactory::createIndenter(doc);
|
||||||
return new ClangFormatIndenter(doc);
|
return new ClangFormatIndenter(doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,14 +40,11 @@ ClangFormatSettings::ClangFormatSettings()
|
|||||||
{
|
{
|
||||||
QSettings *settings = Core::ICore::settings();
|
QSettings *settings = Core::ICore::settings();
|
||||||
settings->beginGroup(QLatin1String(Constants::SETTINGS_ID));
|
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)
|
m_overrideDefaultFile = settings->value(QLatin1String(Constants::OVERRIDE_FILE_ID), false)
|
||||||
.toBool();
|
.toBool();
|
||||||
|
m_mode = static_cast<ClangFormatSettings::Mode>(
|
||||||
|
settings->value(QLatin1String(Constants::MODE_ID), ClangFormatSettings::Mode::Indenting)
|
||||||
|
.toInt());
|
||||||
settings->endGroup();
|
settings->endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,44 +52,11 @@ void ClangFormatSettings::write() const
|
|||||||
{
|
{
|
||||||
QSettings *settings = Core::ICore::settings();
|
QSettings *settings = Core::ICore::settings();
|
||||||
settings->beginGroup(QLatin1String(Constants::SETTINGS_ID));
|
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::OVERRIDE_FILE_ID), m_overrideDefaultFile);
|
||||||
|
settings->setValue(QLatin1String(Constants::MODE_ID), static_cast<int>(m_mode));
|
||||||
settings->endGroup();
|
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)
|
void ClangFormatSettings::setOverrideDefaultFile(bool enable)
|
||||||
{
|
{
|
||||||
m_overrideDefaultFile = enable;
|
m_overrideDefaultFile = enable;
|
||||||
@@ -103,4 +67,14 @@ bool ClangFormatSettings::overrideDefaultFile() const
|
|||||||
return m_overrideDefaultFile;
|
return m_overrideDefaultFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ClangFormatSettings::setMode(Mode mode)
|
||||||
|
{
|
||||||
|
m_mode = mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
ClangFormatSettings::Mode ClangFormatSettings::mode() const
|
||||||
|
{
|
||||||
|
return m_mode;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace ClangFormat
|
} // namespace ClangFormat
|
||||||
|
|||||||
@@ -37,22 +37,21 @@ public:
|
|||||||
ClangFormatSettings();
|
ClangFormatSettings();
|
||||||
void write() const;
|
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);
|
void setOverrideDefaultFile(bool enable);
|
||||||
bool overrideDefaultFile() const;
|
bool overrideDefaultFile() const;
|
||||||
|
|
||||||
|
enum Mode {
|
||||||
|
Indenting = 0,
|
||||||
|
Formatting,
|
||||||
|
Disable
|
||||||
|
};
|
||||||
|
|
||||||
|
void setMode(Mode mode);
|
||||||
|
Mode mode() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_formatCodeInsteadOfIndent = false;
|
|
||||||
bool m_formatWhileTyping = false;
|
|
||||||
bool m_formatOnSave = false;
|
|
||||||
bool m_overrideDefaultFile = false;
|
bool m_overrideDefaultFile = false;
|
||||||
|
Mode m_mode;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ClangFormat
|
} // namespace ClangFormat
|
||||||
|
|||||||
Reference in New Issue
Block a user