2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-08-16 10:45:23 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-08-16 10:45:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-08-16 10:45:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2011-08-16 10:45:23 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2011-08-16 10:45:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
#include "codestylepool.h"
|
|
|
|
|
#include "icodestylepreferencesfactory.h"
|
|
|
|
|
#include "icodestylepreferences.h"
|
|
|
|
|
#include "tabsettings.h"
|
2015-01-10 23:40:32 +02:00
|
|
|
|
2011-08-16 10:45:23 +02:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
2015-01-10 23:40:32 +02:00
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
#include <utils/persistentsettings.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QMap>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QDebug>
|
2015-01-10 23:40:32 +02:00
|
|
|
#include <QFileInfo>
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
2012-11-22 10:29:58 +02:00
|
|
|
static const char codeStyleDataKey[] = "CodeStyleData";
|
|
|
|
|
static const char displayNameKey[] = "DisplayName";
|
|
|
|
|
static const char codeStyleDocKey[] = "QtCreatorCodeStyle";
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
class CodeStylePoolPrivate
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-11-25 18:52:41 +01:00
|
|
|
CodeStylePoolPrivate() = default;
|
2012-07-19 22:26:45 +03:00
|
|
|
~CodeStylePoolPrivate();
|
2011-08-16 10:45:23 +02:00
|
|
|
|
2013-09-20 10:35:16 +02:00
|
|
|
QByteArray generateUniqueId(const QByteArray &id) const;
|
2011-08-16 10:45:23 +02:00
|
|
|
|
2018-11-25 18:52:41 +01:00
|
|
|
ICodeStylePreferencesFactory *m_factory = nullptr;
|
2011-08-16 10:45:23 +02:00
|
|
|
QList<ICodeStylePreferences *> m_pool;
|
|
|
|
|
QList<ICodeStylePreferences *> m_builtInPool;
|
|
|
|
|
QList<ICodeStylePreferences *> m_customPool;
|
2013-09-20 10:35:16 +02:00
|
|
|
QMap<QByteArray, ICodeStylePreferences *> m_idToCodeStyle;
|
2011-08-16 10:45:23 +02:00
|
|
|
QString m_settingsPath;
|
|
|
|
|
};
|
|
|
|
|
|
2012-07-19 22:26:45 +03:00
|
|
|
CodeStylePoolPrivate::~CodeStylePoolPrivate()
|
|
|
|
|
{
|
|
|
|
|
delete m_factory;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-20 10:35:16 +02:00
|
|
|
QByteArray CodeStylePoolPrivate::generateUniqueId(const QByteArray &id) const
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2012-03-02 13:47:04 +01:00
|
|
|
if (!id.isEmpty() && !m_idToCodeStyle.contains(id))
|
2011-08-16 10:45:23 +02:00
|
|
|
return id;
|
|
|
|
|
|
|
|
|
|
int idx = id.size();
|
|
|
|
|
while (idx > 0) {
|
2013-09-20 10:35:16 +02:00
|
|
|
if (!isdigit(id.at(idx - 1)))
|
2011-08-16 10:45:23 +02:00
|
|
|
break;
|
|
|
|
|
idx--;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-20 10:35:16 +02:00
|
|
|
const QByteArray baseName = id.left(idx);
|
|
|
|
|
QByteArray newName = baseName.isEmpty() ? "codestyle" : baseName;
|
2011-08-16 10:45:23 +02:00
|
|
|
int i = 2;
|
|
|
|
|
while (m_idToCodeStyle.contains(newName))
|
2013-09-20 10:35:16 +02:00
|
|
|
newName = baseName + QByteArray::number(i++);
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
return newName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QString customCodeStylesPath()
|
|
|
|
|
{
|
2012-01-24 15:36:40 +01:00
|
|
|
QString path = Core::ICore::userResourcePath();
|
2011-08-16 10:45:23 +02:00
|
|
|
path.append(QLatin1String("/codestyles/"));
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodeStylePool::CodeStylePool(ICodeStylePreferencesFactory *factory, QObject *parent)
|
|
|
|
|
: QObject(parent),
|
|
|
|
|
d(new Internal::CodeStylePoolPrivate)
|
|
|
|
|
{
|
|
|
|
|
d->m_factory = factory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodeStylePool::~CodeStylePool()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CodeStylePool::settingsDir() const
|
|
|
|
|
{
|
2012-11-21 21:47:17 +02:00
|
|
|
const QString suffix = d->m_factory ? d->m_factory->languageId().toString() : QLatin1String("default");
|
2011-08-16 10:45:23 +02:00
|
|
|
return customCodeStylesPath().append(suffix);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
Utils::FilePath CodeStylePool::settingsPath(const QByteArray &id) const
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2019-05-28 13:49:26 +02:00
|
|
|
return Utils::FilePath::fromString(settingsDir()).pathAppended(QString::fromUtf8(id + ".xml"));
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<ICodeStylePreferences *> CodeStylePool::codeStyles() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_pool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<ICodeStylePreferences *> CodeStylePool::builtInCodeStyles() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_builtInPool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<ICodeStylePreferences *> CodeStylePool::customCodeStyles() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_customPool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ICodeStylePreferences *CodeStylePool::cloneCodeStyle(ICodeStylePreferences *originalCodeStyle)
|
|
|
|
|
{
|
|
|
|
|
return createCodeStyle(originalCodeStyle->id(), originalCodeStyle->tabSettings(),
|
|
|
|
|
originalCodeStyle->value(), originalCodeStyle->displayName());
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-20 10:35:16 +02:00
|
|
|
ICodeStylePreferences *CodeStylePool::createCodeStyle(const QByteArray &id, const TabSettings &tabSettings,
|
2011-08-16 10:45:23 +02:00
|
|
|
const QVariant &codeStyleData, const QString &displayName)
|
|
|
|
|
{
|
|
|
|
|
if (!d->m_factory)
|
2018-09-20 01:16:01 +03:00
|
|
|
return nullptr;
|
2011-08-16 10:45:23 +02:00
|
|
|
|
2015-02-03 23:46:35 +02:00
|
|
|
ICodeStylePreferences *codeStyle = d->m_factory->createCodeStyle();
|
2011-08-16 10:45:23 +02:00
|
|
|
codeStyle->setId(id);
|
|
|
|
|
codeStyle->setTabSettings(tabSettings);
|
|
|
|
|
codeStyle->setValue(codeStyleData);
|
|
|
|
|
codeStyle->setDisplayName(displayName);
|
|
|
|
|
|
|
|
|
|
addCodeStyle(codeStyle);
|
|
|
|
|
|
|
|
|
|
saveCodeStyle(codeStyle);
|
|
|
|
|
|
|
|
|
|
return codeStyle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStylePool::addCodeStyle(ICodeStylePreferences *codeStyle)
|
|
|
|
|
{
|
2013-09-20 10:35:16 +02:00
|
|
|
const QByteArray newId = d->generateUniqueId(codeStyle->id());
|
2011-08-16 10:45:23 +02:00
|
|
|
codeStyle->setId(newId);
|
|
|
|
|
|
|
|
|
|
d->m_pool.append(codeStyle);
|
|
|
|
|
if (codeStyle->isReadOnly())
|
|
|
|
|
d->m_builtInPool.append(codeStyle);
|
|
|
|
|
else
|
|
|
|
|
d->m_customPool.append(codeStyle);
|
|
|
|
|
d->m_idToCodeStyle.insert(newId, codeStyle);
|
|
|
|
|
// take ownership
|
|
|
|
|
codeStyle->setParent(this);
|
|
|
|
|
|
2015-12-13 01:18:33 +02:00
|
|
|
connect(codeStyle, &ICodeStylePreferences::valueChanged,
|
|
|
|
|
this, &CodeStylePool::slotSaveCodeStyle);
|
|
|
|
|
connect(codeStyle, &ICodeStylePreferences::tabSettingsChanged,
|
|
|
|
|
this, &CodeStylePool::slotSaveCodeStyle);
|
|
|
|
|
connect(codeStyle, &ICodeStylePreferences::displayNameChanged,
|
|
|
|
|
this, &CodeStylePool::slotSaveCodeStyle);
|
2011-08-16 10:45:23 +02:00
|
|
|
emit codeStyleAdded(codeStyle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStylePool::removeCodeStyle(ICodeStylePreferences *codeStyle)
|
|
|
|
|
{
|
|
|
|
|
const int idx = d->m_customPool.indexOf(codeStyle);
|
|
|
|
|
if (idx < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (codeStyle->isReadOnly())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
emit codeStyleRemoved(codeStyle);
|
|
|
|
|
d->m_customPool.removeAt(idx);
|
|
|
|
|
d->m_pool.removeOne(codeStyle);
|
|
|
|
|
d->m_idToCodeStyle.remove(codeStyle->id());
|
|
|
|
|
|
|
|
|
|
QDir dir(settingsDir());
|
2015-01-10 23:40:32 +02:00
|
|
|
dir.remove(settingsPath(codeStyle->id()).fileName());
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
delete codeStyle;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-20 10:35:16 +02:00
|
|
|
ICodeStylePreferences *CodeStylePool::codeStyle(const QByteArray &id) const
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
|
|
|
|
return d->m_idToCodeStyle.value(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStylePool::loadCustomCodeStyles()
|
|
|
|
|
{
|
|
|
|
|
QDir dir(settingsDir());
|
|
|
|
|
const QStringList codeStyleFiles = dir.entryList(QStringList() << QLatin1String("*.xml"), QDir::Files);
|
|
|
|
|
for (int i = 0; i < codeStyleFiles.count(); i++) {
|
|
|
|
|
const QString codeStyleFile = codeStyleFiles.at(i);
|
|
|
|
|
// filter out styles which id is the same as one of built-in styles
|
2013-09-20 10:35:16 +02:00
|
|
|
if (!d->m_idToCodeStyle.contains(QFileInfo(codeStyleFile).completeBaseName().toUtf8()))
|
2019-05-28 13:49:26 +02:00
|
|
|
loadCodeStyle(Utils::FilePath::fromString(dir.absoluteFilePath(codeStyleFile)));
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
ICodeStylePreferences *CodeStylePool::importCodeStyle(const Utils::FilePath &fileName)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2015-02-03 23:46:35 +02:00
|
|
|
ICodeStylePreferences *codeStyle = loadCodeStyle(fileName);
|
2011-08-16 10:45:23 +02:00
|
|
|
if (codeStyle)
|
|
|
|
|
saveCodeStyle(codeStyle);
|
|
|
|
|
return codeStyle;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
ICodeStylePreferences *CodeStylePool::loadCodeStyle(const Utils::FilePath &fileName)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2018-09-20 01:16:01 +03:00
|
|
|
ICodeStylePreferences *codeStyle = nullptr;
|
2011-08-16 10:45:23 +02:00
|
|
|
Utils::PersistentSettingsReader reader;
|
2012-08-17 13:18:31 +02:00
|
|
|
reader.load(fileName);
|
2011-08-16 10:45:23 +02:00
|
|
|
QVariantMap m = reader.restoreValues();
|
|
|
|
|
if (m.contains(QLatin1String(codeStyleDataKey))) {
|
2013-09-20 10:35:16 +02:00
|
|
|
const QByteArray id = fileName.toFileInfo().completeBaseName().toUtf8();
|
2011-08-16 10:45:23 +02:00
|
|
|
const QString displayName = reader.restoreValue(QLatin1String(displayNameKey)).toString();
|
|
|
|
|
const QVariantMap map = reader.restoreValue(QLatin1String(codeStyleDataKey)).toMap();
|
|
|
|
|
if (d->m_factory) {
|
|
|
|
|
codeStyle = d->m_factory->createCodeStyle();
|
|
|
|
|
codeStyle->setId(id);
|
|
|
|
|
codeStyle->setDisplayName(displayName);
|
2013-03-08 16:09:03 +01:00
|
|
|
codeStyle->fromMap(QString(), map);
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
addCodeStyle(codeStyle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return codeStyle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStylePool::slotSaveCodeStyle()
|
|
|
|
|
{
|
2018-09-20 01:16:01 +03:00
|
|
|
auto codeStyle = qobject_cast<ICodeStylePreferences *>(sender());
|
2011-08-16 10:45:23 +02:00
|
|
|
if (!codeStyle)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
saveCodeStyle(codeStyle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStylePool::saveCodeStyle(ICodeStylePreferences *codeStyle) const
|
|
|
|
|
{
|
|
|
|
|
const QString codeStylesPath = customCodeStylesPath();
|
|
|
|
|
|
|
|
|
|
// Create the base directory when it doesn't exist
|
|
|
|
|
if (!QFile::exists(codeStylesPath) && !QDir().mkpath(codeStylesPath)) {
|
|
|
|
|
qWarning() << "Failed to create code style directory:" << codeStylesPath;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const QString languageCodeStylesPath = settingsDir();
|
|
|
|
|
// Create the base directory for the language when it doesn't exist
|
|
|
|
|
if (!QFile::exists(languageCodeStylesPath) && !QDir().mkpath(languageCodeStylesPath)) {
|
|
|
|
|
qWarning() << "Failed to create language code style directory:" << languageCodeStylesPath;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exportCodeStyle(settingsPath(codeStyle->id()), codeStyle);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
void CodeStylePool::exportCodeStyle(const Utils::FilePath &fileName, ICodeStylePreferences *codeStyle) const
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
|
|
|
|
QVariantMap map;
|
2013-03-08 16:09:03 +01:00
|
|
|
codeStyle->toMap(QString(), &map);
|
2012-08-31 17:01:15 +02:00
|
|
|
|
|
|
|
|
QVariantMap tmp;
|
|
|
|
|
tmp.insert(QLatin1String(displayNameKey), codeStyle->displayName());
|
|
|
|
|
tmp.insert(QLatin1String(codeStyleDataKey), map);
|
2012-08-17 13:18:31 +02:00
|
|
|
Utils::PersistentSettingsWriter writer(fileName, QLatin1String(codeStyleDocKey));
|
2013-11-26 12:53:11 +01:00
|
|
|
writer.save(tmp, Core::ICore::mainWindow());
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|