2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
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>
|
|
|
|
|
|
2022-09-22 16:26:39 +02:00
|
|
|
#include <utils/filepath.h>
|
2015-01-10 23:40:32 +02:00
|
|
|
#include <utils/persistentsettings.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QMap>
|
|
|
|
|
#include <QDebug>
|
2011-08-16 10:45:23 +02:00
|
|
|
|
2022-09-22 16:26:39 +02:00
|
|
|
using namespace Utils;
|
2011-08-16 10:45:23 +02:00
|
|
|
|
2022-09-22 16:26:39 +02:00
|
|
|
const char codeStyleDataKey[] = "CodeStyleData";
|
|
|
|
|
const char displayNameKey[] = "DisplayName";
|
|
|
|
|
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);
|
2020-06-30 15:11:15 +02:00
|
|
|
QByteArray newName = baseName.isEmpty() ? QByteArray("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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 16:26:39 +02:00
|
|
|
} // Internal
|
2011-08-16 10:45:23 +02:00
|
|
|
|
2022-09-22 16:26:39 +02:00
|
|
|
static FilePath customCodeStylesPath()
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2021-07-15 17:11:53 +02:00
|
|
|
return Core::ICore::userResourcePath("codestyles");
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodeStylePool::CodeStylePool(ICodeStylePreferencesFactory *factory, QObject *parent)
|
|
|
|
|
: QObject(parent),
|
|
|
|
|
d(new Internal::CodeStylePoolPrivate)
|
|
|
|
|
{
|
|
|
|
|
d->m_factory = factory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodeStylePool::~CodeStylePool()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 16:26:39 +02:00
|
|
|
FilePath CodeStylePool::settingsDir() const
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2012-11-21 21:47:17 +02:00
|
|
|
const QString suffix = d->m_factory ? d->m_factory->languageId().toString() : QLatin1String("default");
|
2022-09-22 16:26:39 +02:00
|
|
|
return customCodeStylesPath().pathAppended(suffix);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-22 16:26:39 +02:00
|
|
|
FilePath CodeStylePool::settingsPath(const QByteArray &id) const
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2022-09-22 16:26:39 +02:00
|
|
|
return 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);
|
|
|
|
|
|
2022-07-20 18:55:58 +02:00
|
|
|
auto doSaveStyle = [this, codeStyle] { saveCodeStyle(codeStyle); };
|
|
|
|
|
connect(codeStyle, &ICodeStylePreferences::valueChanged, this, doSaveStyle);
|
|
|
|
|
connect(codeStyle, &ICodeStylePreferences::tabSettingsChanged, this, doSaveStyle);
|
|
|
|
|
connect(codeStyle, &ICodeStylePreferences::displayNameChanged, this, doSaveStyle);
|
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());
|
|
|
|
|
|
2022-09-22 16:26:39 +02:00
|
|
|
settingsPath(codeStyle->id()).removeFile();
|
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()
|
|
|
|
|
{
|
2022-09-22 16:26:39 +02:00
|
|
|
FilePath dir = settingsDir();
|
|
|
|
|
const FilePaths codeStyleFiles = dir.dirEntries({QStringList(QLatin1String("*.xml")), QDir::Files});
|
|
|
|
|
for (const FilePath &codeStyleFile : codeStyleFiles) {
|
2011-08-16 10:45:23 +02:00
|
|
|
// filter out styles which id is the same as one of built-in styles
|
2022-09-22 16:26:39 +02:00
|
|
|
if (!d->m_idToCodeStyle.contains(codeStyleFile.completeBaseName().toUtf8()))
|
|
|
|
|
loadCodeStyle(codeStyleFile);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 16:26:39 +02:00
|
|
|
ICodeStylePreferences *CodeStylePool::importCodeStyle(const 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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 16:26:39 +02:00
|
|
|
ICodeStylePreferences *CodeStylePool::loadCodeStyle(const FilePath &fileName)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2018-09-20 01:16:01 +03:00
|
|
|
ICodeStylePreferences *codeStyle = nullptr;
|
2022-09-22 16:26:39 +02:00
|
|
|
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))) {
|
2021-06-04 07:59:00 +02:00
|
|
|
const QByteArray id = fileName.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);
|
2021-12-01 15:41:12 +02:00
|
|
|
codeStyle->fromMap(map);
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
addCodeStyle(codeStyle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return codeStyle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeStylePool::saveCodeStyle(ICodeStylePreferences *codeStyle) const
|
|
|
|
|
{
|
2022-09-22 16:26:39 +02:00
|
|
|
const FilePath codeStylesPath = customCodeStylesPath();
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
// Create the base directory when it doesn't exist
|
2022-09-22 16:26:39 +02:00
|
|
|
if (!codeStylesPath.exists() && !codeStylesPath.createDir()) {
|
2011-08-16 10:45:23 +02:00
|
|
|
qWarning() << "Failed to create code style directory:" << codeStylesPath;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-09-22 16:26:39 +02:00
|
|
|
const FilePath languageCodeStylesPath = settingsDir();
|
2011-08-16 10:45:23 +02:00
|
|
|
// Create the base directory for the language when it doesn't exist
|
2022-09-22 16:26:39 +02:00
|
|
|
if (!languageCodeStylesPath.exists() && !languageCodeStylesPath.createDir()) {
|
2011-08-16 10:45:23 +02:00
|
|
|
qWarning() << "Failed to create language code style directory:" << languageCodeStylesPath;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exportCodeStyle(settingsPath(codeStyle->id()), codeStyle);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 16:26:39 +02:00
|
|
|
void CodeStylePool::exportCodeStyle(const FilePath &fileName, ICodeStylePreferences *codeStyle) const
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2021-12-01 15:41:12 +02:00
|
|
|
const QVariantMap map = codeStyle->toMap();
|
|
|
|
|
const QVariantMap tmp = {
|
|
|
|
|
{displayNameKey, codeStyle->displayName()},
|
|
|
|
|
{codeStyleDataKey, map}
|
|
|
|
|
};
|
2022-09-22 16:26:39 +02:00
|
|
|
PersistentSettingsWriter writer(fileName, QLatin1String(codeStyleDocKey));
|
2020-06-02 09:10:40 +02:00
|
|
|
writer.save(tmp, Core::ICore::dialogParent());
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-22 16:26:39 +02:00
|
|
|
} // TextEditor
|