forked from qt-creator/qt-creator
Implement Code Style schemes
Task-number: QTCREATORBUG-5092 Change-Id: I218cef02f7c242e4dfae59b1b8021ea618e60d07 Reviewed-on: http://codereview.qt-project.org/5160 Reviewed-by: Leandro T. C. Melo <leandro.melo@nokia.com>
This commit is contained in:
@@ -33,10 +33,14 @@
|
||||
#include "cpptoolssettings.h"
|
||||
#include "cpptoolsconstants.h"
|
||||
#include "cppcodestylepreferences.h"
|
||||
#include "cppcodestylepreferencesfactory.h"
|
||||
|
||||
#include <texteditor/texteditorsettings.h>
|
||||
#include <texteditor/tabpreferences.h>
|
||||
#include <texteditor/texteditorsettings.h>
|
||||
#include <texteditor/tabsettings.h>
|
||||
#include <texteditor/codestylepool.h>
|
||||
|
||||
#include <utils/settingsutils.h>
|
||||
#include <utils/qtcassert.h>
|
||||
#include <coreplugin/icore.h>
|
||||
#include <QtCore/QSettings>
|
||||
@@ -44,15 +48,34 @@
|
||||
static const char *idKey = "CppGlobal";
|
||||
|
||||
using namespace CppTools;
|
||||
using TextEditor::TabSettings;
|
||||
|
||||
namespace CppTools {
|
||||
namespace Internal {
|
||||
|
||||
class LegacySettings
|
||||
{
|
||||
public:
|
||||
LegacySettings()
|
||||
: m_legacyTransformed(false)
|
||||
{ }
|
||||
void fromMap(const QString &prefix, const QVariantMap &map)
|
||||
{
|
||||
m_fallbackId = map.value(prefix + QLatin1String("CurrentFallback")).toString();
|
||||
m_legacyTransformed = map.value(prefix + QLatin1String("LegacyTransformed"), false).toBool();
|
||||
}
|
||||
void toMap(const QString &prefix, QVariantMap *map) const
|
||||
{
|
||||
map->insert(prefix + QLatin1String("LegacyTransformed"), true);
|
||||
}
|
||||
QString m_fallbackId;
|
||||
bool m_legacyTransformed;
|
||||
};
|
||||
|
||||
class CppToolsSettingsPrivate
|
||||
{
|
||||
public:
|
||||
CppCodeStylePreferences *m_cppCodeStylePreferences;
|
||||
TextEditor::TabPreferences *m_tabPreferences;
|
||||
CppCodeStylePreferences *m_globalCodeStyle;
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
@@ -66,27 +89,145 @@ CppToolsSettings::CppToolsSettings(QObject *parent)
|
||||
{
|
||||
QTC_ASSERT(!m_instance, return);
|
||||
m_instance = this;
|
||||
qRegisterMetaType<CppTools::CppCodeStyleSettings>("CppTools::CppCodeStyleSettings");
|
||||
|
||||
if (const QSettings *s = Core::ICore::instance()->settings()) {
|
||||
TextEditor::TextEditorSettings *textEditorSettings = TextEditor::TextEditorSettings::instance();
|
||||
TextEditor::TabPreferences *tabPrefs = textEditorSettings->tabPreferences();
|
||||
d->m_tabPreferences
|
||||
= new TextEditor::TabPreferences(QList<TextEditor::IFallbackPreferences *>()
|
||||
<< tabPrefs, this);
|
||||
d->m_tabPreferences->setCurrentFallback(tabPrefs);
|
||||
d->m_tabPreferences->setFallbackEnabled(tabPrefs, false);
|
||||
d->m_tabPreferences->fromSettings(CppTools::Constants::CPP_SETTINGS_ID, s);
|
||||
d->m_tabPreferences->setDisplayName(tr("Global C++", "Settings"));
|
||||
d->m_tabPreferences->setId(idKey);
|
||||
textEditorSettings->registerLanguageTabPreferences(CppTools::Constants::CPP_SETTINGS_ID, d->m_tabPreferences);
|
||||
TextEditor::TextEditorSettings *textEditorSettings = TextEditor::TextEditorSettings::instance();
|
||||
|
||||
d->m_cppCodeStylePreferences
|
||||
= new CppCodeStylePreferences(QList<TextEditor::IFallbackPreferences *>(), this);
|
||||
d->m_cppCodeStylePreferences->fromSettings(CppTools::Constants::CPP_SETTINGS_ID, s);
|
||||
d->m_cppCodeStylePreferences->setDisplayName(tr("Global C++", "Settings"));
|
||||
d->m_cppCodeStylePreferences->setId(idKey);
|
||||
textEditorSettings->registerLanguageCodeStylePreferences(CppTools::Constants::CPP_SETTINGS_ID, d->m_cppCodeStylePreferences);
|
||||
// code style factory
|
||||
TextEditor::ICodeStylePreferencesFactory *factory = new CppTools::CppCodeStylePreferencesFactory();
|
||||
textEditorSettings->registerCodeStyleFactory(factory);
|
||||
|
||||
// code style pool
|
||||
TextEditor::CodeStylePool *pool = new TextEditor::CodeStylePool(factory, this);
|
||||
textEditorSettings->registerCodeStylePool(Constants::CPP_SETTINGS_ID, pool);
|
||||
|
||||
// global code style settings
|
||||
d->m_globalCodeStyle = new CppCodeStylePreferences(this);
|
||||
d->m_globalCodeStyle->setDelegatingPool(pool);
|
||||
d->m_globalCodeStyle->setDisplayName(tr("Global", "Settings"));
|
||||
d->m_globalCodeStyle->setId(idKey);
|
||||
pool->addCodeStyle(d->m_globalCodeStyle);
|
||||
textEditorSettings->registerCodeStyle(CppTools::Constants::CPP_SETTINGS_ID, d->m_globalCodeStyle);
|
||||
|
||||
/*
|
||||
For every language we have exactly 1 pool. The pool contains:
|
||||
1) All built-in code styles (Qt/GNU)
|
||||
2) All custom code styles (which will be added dynamically)
|
||||
3) A global code style
|
||||
|
||||
If the code style gets a pool (setCodeStylePool()) it means it can behave
|
||||
like a proxy to one of the code styles from that pool
|
||||
(ICodeStylePreferences::setCurrentDelegate()).
|
||||
That's why the global code style gets a pool (it can point to any code style
|
||||
from the pool), while built-in and custom code styles don't get a pool
|
||||
(they can't point to any other code style).
|
||||
|
||||
The instance of the language pool is shared. The same instance of the pool
|
||||
is used for all project code style settings and for global one.
|
||||
Project code style can point to one of built-in or custom code styles
|
||||
or to the global one as well. That's why the global code style is added
|
||||
to the pool. The proxy chain can look like:
|
||||
ProjectCodeStyle -> GlobalCodeStyle -> BuildInCodeStyle (e.g. Qt).
|
||||
|
||||
With the global pool there is an exception - it gets a pool
|
||||
in which it exists itself. The case in which a code style point to itself
|
||||
is disallowed and is handled in ICodeStylePreferences::setCurrentDelegate().
|
||||
*/
|
||||
|
||||
// built-in settings
|
||||
// Qt style
|
||||
CppCodeStylePreferences *qtCodeStyle = new CppCodeStylePreferences();
|
||||
qtCodeStyle->setId(QLatin1String("qt"));
|
||||
qtCodeStyle->setDisplayName(tr("Qt"));
|
||||
qtCodeStyle->setReadOnly(true);
|
||||
TabSettings qtTabSettings;
|
||||
qtTabSettings.m_tabPolicy = TabSettings::SpacesOnlyTabPolicy;
|
||||
qtTabSettings.m_tabSize = 4;
|
||||
qtTabSettings.m_indentSize = 4;
|
||||
qtTabSettings.m_continuationAlignBehavior = TabSettings::ContinuationAlignWithIndent;
|
||||
qtCodeStyle->setTabSettings(qtTabSettings);
|
||||
pool->addCodeStyle(qtCodeStyle);
|
||||
|
||||
// GNU style
|
||||
CppCodeStylePreferences *gnuCodeStyle = new CppCodeStylePreferences();
|
||||
gnuCodeStyle->setId(QLatin1String("gnu"));
|
||||
gnuCodeStyle->setDisplayName(tr("GNU"));
|
||||
gnuCodeStyle->setReadOnly(true);
|
||||
TabSettings gnuTabSettings;
|
||||
gnuTabSettings.m_tabPolicy = TabSettings::MixedTabPolicy;
|
||||
gnuTabSettings.m_tabSize = 8;
|
||||
gnuTabSettings.m_indentSize = 2;
|
||||
gnuTabSettings.m_continuationAlignBehavior = TabSettings::ContinuationAlignWithIndent;
|
||||
gnuCodeStyle->setTabSettings(gnuTabSettings);
|
||||
CppCodeStyleSettings gnuCodeStyleSettings;
|
||||
gnuCodeStyleSettings.indentNamespaceBody = true;
|
||||
gnuCodeStyleSettings.indentBlockBraces = true;
|
||||
gnuCodeStyleSettings.indentSwitchLabels = true;
|
||||
gnuCodeStyleSettings.indentBlocksRelativeToSwitchLabels = true;
|
||||
gnuCodeStyle->setCodeStyleSettings(gnuCodeStyleSettings);
|
||||
pool->addCodeStyle(gnuCodeStyle);
|
||||
|
||||
// default delegate for global preferences
|
||||
d->m_globalCodeStyle->setCurrentDelegate(qtCodeStyle);
|
||||
|
||||
pool->loadCustomCodeStyles();
|
||||
|
||||
// load global settings (after built-in settings are added to the pool)
|
||||
if (QSettings *s = Core::ICore::instance()->settings()) {
|
||||
d->m_globalCodeStyle->fromSettings(CppTools::Constants::CPP_SETTINGS_ID, s);
|
||||
|
||||
// legacy handling start (Qt Creator <= 2.3)
|
||||
Internal::LegacySettings legacySettings;
|
||||
|
||||
TabSettings legacyTabSettings;
|
||||
Utils::fromSettings(QLatin1String("TabPreferences"),
|
||||
QLatin1String("Cpp"), s, &legacySettings);
|
||||
if (legacySettings.m_fallbackId == QLatin1String("CppGlobal")) {
|
||||
Utils::fromSettings(QLatin1String("TabPreferences"),
|
||||
QLatin1String("Cpp"), s, &legacyTabSettings);
|
||||
} else {
|
||||
legacyTabSettings = textEditorSettings->codeStyle()->currentTabSettings();
|
||||
}
|
||||
|
||||
CppCodeStyleSettings legacyCodeStyleSettings;
|
||||
Utils::fromSettings(QLatin1String("CodeStyleSettings"),
|
||||
QLatin1String("Cpp"), s, &legacySettings);
|
||||
if (!legacySettings.m_legacyTransformed
|
||||
&& legacySettings.m_fallbackId == QLatin1String("CppGlobal")) {
|
||||
Utils::fromSettings(QLatin1String("CodeStyleSettings"),
|
||||
QLatin1String("Cpp"), s, &legacyCodeStyleSettings);
|
||||
// create custom code style out of old settings
|
||||
QVariant v;
|
||||
v.setValue(legacyCodeStyleSettings);
|
||||
TextEditor::ICodeStylePreferences *oldCreator = pool->createCodeStyle(
|
||||
QLatin1String("legacy"), legacyTabSettings,
|
||||
v, tr("Old Creator"));
|
||||
// change the current delegate and save
|
||||
d->m_globalCodeStyle->setCurrentDelegate(oldCreator);
|
||||
d->m_globalCodeStyle->toSettings(CppTools::Constants::CPP_SETTINGS_ID, s);
|
||||
|
||||
// mark old settings as transformed,
|
||||
// we create only once "Old Creator" custom settings
|
||||
Utils::toSettings(QLatin1String("CodeStyleSettings"),
|
||||
QLatin1String("Cpp"), s, &legacySettings);
|
||||
}
|
||||
// legacy handling stop
|
||||
}
|
||||
|
||||
|
||||
// mimetypes to be handled
|
||||
textEditorSettings->registerMimeTypeForLanguageId(
|
||||
QLatin1String(Constants::C_SOURCE_MIMETYPE),
|
||||
Constants::CPP_SETTINGS_ID);
|
||||
textEditorSettings->registerMimeTypeForLanguageId(
|
||||
QLatin1String(Constants::C_HEADER_MIMETYPE),
|
||||
Constants::CPP_SETTINGS_ID);
|
||||
textEditorSettings->registerMimeTypeForLanguageId(
|
||||
QLatin1String(Constants::CPP_SOURCE_MIMETYPE),
|
||||
Constants::CPP_SETTINGS_ID);
|
||||
textEditorSettings->registerMimeTypeForLanguageId(
|
||||
QLatin1String(Constants::CPP_HEADER_MIMETYPE),
|
||||
Constants::CPP_SETTINGS_ID);
|
||||
}
|
||||
|
||||
CppToolsSettings::~CppToolsSettings()
|
||||
@@ -101,14 +242,8 @@ CppToolsSettings *CppToolsSettings::instance()
|
||||
return m_instance;
|
||||
}
|
||||
|
||||
CppCodeStylePreferences *CppToolsSettings::cppCodeStylePreferences() const
|
||||
CppCodeStylePreferences *CppToolsSettings::cppCodeStyle() const
|
||||
{
|
||||
return d->m_cppCodeStylePreferences;
|
||||
return d->m_globalCodeStyle;
|
||||
}
|
||||
|
||||
TextEditor::TabPreferences *CppToolsSettings::tabPreferences() const
|
||||
{
|
||||
return d->m_tabPreferences;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user