Commit 22c9b652 authored by Leandro Melo's avatar Leandro Melo
Browse files

Snippets: Move code from the manager to the collection.

Reloading and synchronizing are now directly accessible from
the collection interface.
parent 4a8bdaf8
......@@ -28,7 +28,18 @@
**************************************************************************/
#include "snippetscollection.h"
#include "reuse.h"
#include <coreplugin/icore.h>
#include <QtCore/QLatin1String>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <QtCore/QHash>
#include <QtCore/QDebug>
#include <QtCore/QXmlStreamReader>
#include <QtCore/QXmlStreamWriter>
#include <QtAlgorithms>
#include <iterator>
......@@ -65,6 +76,15 @@ RemovedSnippetPred removedSnippetPred;
} // Anonymous
const QLatin1String SnippetsCollection::kSnippet("snippet");
const QLatin1String SnippetsCollection::kSnippets("snippets");
const QLatin1String SnippetsCollection::kTrigger("trigger");
const QLatin1String SnippetsCollection::kId("id");
const QLatin1String SnippetsCollection::kComplement("complement");
const QLatin1String SnippetsCollection::kGroup("group");
const QLatin1String SnippetsCollection::kRemoved("removed");
const QLatin1String SnippetsCollection::kModified("modified");
// Hint
SnippetsCollection::Hint::Hint(int index) : m_index(index)
{}
......@@ -80,7 +100,10 @@ int SnippetsCollection::Hint::index() const
// SnippetsCollection
SnippetsCollection::SnippetsCollection() :
m_snippets(Snippet::GroupSize),
m_activeSnippetsEnd(Snippet::GroupSize)
m_activeSnippetsEnd(Snippet::GroupSize),
m_builtInSnippetsPath(QLatin1String(":/texteditor/snippets/")),
m_userSnippetsPath(Core::ICore::instance()->userResourcePath() + QLatin1String("/snippets/")),
m_snippetsFileName(QLatin1String("snippets.xml"))
{
for (Snippet::Group group = Snippet::Cpp; group < Snippet::GroupSize; ++group)
m_activeSnippetsEnd[group] = m_snippets[group].end();
......@@ -210,3 +233,111 @@ void SnippetsCollection::updateActiveSnippetsEnd(Snippet::Group group)
m_snippets[group].end(),
removedSnippetPred);
}
void SnippetsCollection::reload()
{
clear();
QHash<QString, Snippet> activeBuiltInSnippets;
const QList<Snippet> &builtInSnippets = readXML(m_builtInSnippetsPath + m_snippetsFileName);
foreach (const Snippet &snippet, builtInSnippets)
activeBuiltInSnippets.insert(snippet.id(), snippet);
const QList<Snippet> &userSnippets = readXML(m_userSnippetsPath + m_snippetsFileName);
foreach (const Snippet &snippet, userSnippets) {
if (snippet.isBuiltIn())
// This user snippet overrides the corresponding built-in snippet.
activeBuiltInSnippets.remove(snippet.id());
insertSnippet(snippet, snippet.group());
}
foreach (const Snippet &snippet, activeBuiltInSnippets)
insertSnippet(snippet, snippet.group());
}
void SnippetsCollection::synchronize()
{
if (QFile::exists(m_userSnippetsPath) || QDir().mkpath(m_userSnippetsPath)) {
QFile file(m_userSnippetsPath + m_snippetsFileName);
if (file.open(QFile::WriteOnly | QFile::Truncate)) {
QXmlStreamWriter writer(&file);
writer.setAutoFormatting(true);
writer.writeStartDocument();
writer.writeStartElement(kSnippets);
for (Snippet::Group group = Snippet::Cpp; group < Snippet::GroupSize; ++group) {
const int size = totalSnippets(group);
for (int i = 0; i < size; ++i) {
const Snippet &current = snippet(i, group);
if (!current.isBuiltIn() ||
(current.isBuiltIn() && (current.isRemoved() || current.isModified()))) {
writeSnippetXML(current, &writer);
}
}
}
writer.writeEndElement();
writer.writeEndDocument();
file.close();
}
}
reload();
}
void SnippetsCollection::writeSnippetXML(const Snippet &snippet, QXmlStreamWriter *writer)
{
writer->writeStartElement(kSnippet);
writer->writeAttribute(kGroup, fromSnippetGroup(snippet.group()));
writer->writeAttribute(kTrigger, snippet.trigger());
writer->writeAttribute(kId, snippet.id());
writer->writeAttribute(kComplement, snippet.complement());
writer->writeAttribute(kRemoved, fromBool(snippet.isRemoved()));
writer->writeAttribute(kModified, fromBool(snippet.isModified()));
writer->writeCharacters(snippet.content());
writer->writeEndElement();
}
QList<Snippet> SnippetsCollection::readXML(const QString &fileName)
{
QList<Snippet> snippets;
QFile file(fileName);
if (file.exists() && file.open(QIODevice::ReadOnly)) {
QXmlStreamReader xml(&file);
if (xml.readNextStartElement()) {
if (xml.name() == kSnippets) {
while (xml.readNextStartElement()) {
if (xml.name() == kSnippet) {
const QXmlStreamAttributes &atts = xml.attributes();
Snippet snippet(atts.value(kId).toString());
snippet.setTrigger(atts.value(kTrigger).toString());
snippet.setComplement(atts.value(kComplement).toString());
snippet.setGroup(toSnippetGroup(atts.value(kGroup).toString()));
snippet.setIsRemoved(toBool(atts.value(kRemoved).toString()));
snippet.setIsModified(toBool(atts.value(kModified).toString()));
QString content;
while (!xml.atEnd()) {
xml.readNext();
if (xml.isCharacters()) {
content += xml.text();
} else if (xml.isEndElement()) {
snippet.setContent(content);
snippets.append(snippet);
break;
}
}
} else {
xml.skipCurrentElement();
}
}
} else {
xml.skipCurrentElement();
}
}
if (xml.hasError())
qWarning() << fileName << xml.errorString() << xml.lineNumber() << xml.columnNumber();
file.close();
}
return snippets;
}
......@@ -35,6 +35,8 @@
#include <QtCore/QVector>
#include <QtCore/QList>
QT_FORWARD_DECLARE_CLASS(QXmlStreamWriter)
namespace TextEditor {
namespace Internal {
......@@ -82,13 +84,31 @@ public:
int totalActiveSnippets(Snippet::Group group) const;
int totalSnippets(Snippet::Group group) const;
void clear();
void reload();
void synchronize();
private:
void clear();
void updateActiveSnippetsEnd(Snippet::Group group);
static QList<Snippet> readXML(const QString &fileName);
static void writeSnippetXML(const Snippet &snippet, QXmlStreamWriter *writer);
static const QLatin1String kSnippet;
static const QLatin1String kSnippets;
static const QLatin1String kTrigger;
static const QLatin1String kId;
static const QLatin1String kComplement;
static const QLatin1String kGroup;
static const QLatin1String kRemoved;
static const QLatin1String kModified;
QVector<QList<Snippet> > m_snippets;
QVector<QList<Snippet>::iterator> m_activeSnippetsEnd;
QString m_builtInSnippetsPath;
QString m_userSnippetsPath;
QString m_snippetsFileName;
};
} // Internal
......
......@@ -28,40 +28,16 @@
**************************************************************************/
#include "snippetsmanager.h"
#include "isnippeteditordecorator.h"
#include "snippetscollection.h"
#include "reuse.h"
#include <coreplugin/icore.h>
#include <QtCore/QLatin1String>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <QtCore/QHash>
#include <QtCore/QDebug>
#include <QtCore/QXmlStreamReader>
#include <QtCore/QXmlStreamWriter>
using namespace TextEditor;
using namespace Internal;
const QLatin1String SnippetsManager::kSnippet("snippet");
const QLatin1String SnippetsManager::kSnippets("snippets");
const QLatin1String SnippetsManager::kTrigger("trigger");
const QLatin1String SnippetsManager::kId("id");
const QLatin1String SnippetsManager::kComplement("complement");
const QLatin1String SnippetsManager::kGroup("group");
const QLatin1String SnippetsManager::kRemoved("removed");
const QLatin1String SnippetsManager::kModified("modified");
SnippetsManager::SnippetsManager() :
m_collectionLoaded(false),
m_collection(new SnippetsCollection),
m_builtInSnippetsPath(QLatin1String(":/texteditor/snippets/")),
m_userSnippetsPath(Core::ICore::instance()->userResourcePath() + QLatin1String("/snippets/")),
m_snippetsFileName(QLatin1String("snippets.xml"))
{}
m_collection(new SnippetsCollection)
{
m_collection->reload();
}
SnippetsManager::~SnippetsManager()
{}
......@@ -72,121 +48,7 @@ SnippetsManager *SnippetsManager::instance()
return &manager;
}
void SnippetsManager::loadSnippetsCollection()
{
QHash<QString, Snippet> activeBuiltInSnippets;
const QList<Snippet> &builtInSnippets = readXML(m_builtInSnippetsPath + m_snippetsFileName);
foreach (const Snippet &snippet, builtInSnippets)
activeBuiltInSnippets.insert(snippet.id(), snippet);
const QList<Snippet> &userSnippets = readXML(m_userSnippetsPath + m_snippetsFileName);
foreach (const Snippet &snippet, userSnippets) {
if (snippet.isBuiltIn())
// This user snippet overrides the corresponding built-in snippet.
activeBuiltInSnippets.remove(snippet.id());
m_collection->insertSnippet(snippet, snippet.group());
}
foreach (const Snippet &snippet, activeBuiltInSnippets)
m_collection->insertSnippet(snippet, snippet.group());
}
void SnippetsManager::reloadSnippetsCollection()
{
m_collection->clear();
loadSnippetsCollection();
}
void SnippetsManager::persistSnippetsCollection()
{
if (QFile::exists(m_userSnippetsPath) || QDir().mkpath(m_userSnippetsPath)) {
QFile file(m_userSnippetsPath + m_snippetsFileName);
if (file.open(QFile::WriteOnly | QFile::Truncate)) {
QXmlStreamWriter writer(&file);
writer.setAutoFormatting(true);
writer.writeStartDocument();
writer.writeStartElement(kSnippets);
for (Snippet::Group group = Snippet::Cpp; group < Snippet::GroupSize; ++group) {
const int size = m_collection->totalSnippets(group);
for (int i = 0; i < size; ++i) {
const Snippet &snippet = m_collection->snippet(i, group);
if (!snippet.isBuiltIn() ||
(snippet.isBuiltIn() && (snippet.isRemoved() || snippet.isModified()))) {
writeSnippetXML(snippet, &writer);
}
}
}
writer.writeEndElement();
writer.writeEndDocument();
file.close();
}
}
}
void SnippetsManager::writeSnippetXML(const Snippet &snippet, QXmlStreamWriter *writer)
{
writer->writeStartElement(kSnippet);
writer->writeAttribute(kGroup, fromSnippetGroup(snippet.group()));
writer->writeAttribute(kTrigger, snippet.trigger());
writer->writeAttribute(kId, snippet.id());
writer->writeAttribute(kComplement, snippet.complement());
writer->writeAttribute(kRemoved, fromBool(snippet.isRemoved()));
writer->writeAttribute(kModified, fromBool(snippet.isModified()));
writer->writeCharacters(snippet.content());
writer->writeEndElement();
}
QList<Snippet> SnippetsManager::readXML(const QString &fileName)
{
QList<Snippet> snippets;
QFile file(fileName);
if (file.exists() && file.open(QIODevice::ReadOnly)) {
QXmlStreamReader xml(&file);
if (xml.readNextStartElement()) {
if (xml.name() == kSnippets) {
while (xml.readNextStartElement()) {
if (xml.name() == kSnippet) {
const QXmlStreamAttributes &atts = xml.attributes();
Snippet snippet(atts.value(kId).toString());
snippet.setTrigger(atts.value(kTrigger).toString());
snippet.setComplement(atts.value(kComplement).toString());
snippet.setGroup(toSnippetGroup(atts.value(kGroup).toString()));
snippet.setIsRemoved(toBool(atts.value(kRemoved).toString()));
snippet.setIsModified(toBool(atts.value(kModified).toString()));
QString content;
while (!xml.atEnd()) {
xml.readNext();
if (xml.isCharacters()) {
content += xml.text();
} else if (xml.isEndElement()) {
snippet.setContent(content);
snippets.append(snippet);
break;
}
}
} else {
xml.skipCurrentElement();
}
}
} else {
xml.skipCurrentElement();
}
}
if (xml.hasError())
qWarning() << fileName << xml.errorString() << xml.lineNumber() << xml.columnNumber();
file.close();
}
return snippets;
}
QSharedPointer<SnippetsCollection> SnippetsManager::snippetsCollection()
QSharedPointer<SnippetsCollection> SnippetsManager::snippetsCollection() const
{
if (!m_collectionLoaded) {
loadSnippetsCollection();
m_collectionLoaded = true;
}
return m_collection;
}
......@@ -30,13 +30,7 @@
#ifndef SNIPPETSMANAGER_H
#define SNIPPETSMANAGER_H
#include "snippet.h"
#include <QtCore/QString>
#include <QtCore/QSharedPointer>
#include <QtCore/QList>
QT_FORWARD_DECLARE_CLASS(QXmlStreamWriter)
namespace TextEditor {
namespace Internal {
......@@ -54,30 +48,10 @@ public:
static SnippetsManager *instance();
void reloadSnippetsCollection();
void persistSnippetsCollection();
QSharedPointer<SnippetsCollection> snippetsCollection();
QSharedPointer<SnippetsCollection> snippetsCollection() const;
private:
void loadSnippetsCollection();
static QList<Snippet> readXML(const QString &fileName);
static void writeSnippetXML(const Snippet &snippet, QXmlStreamWriter *writer);
static const QLatin1String kSnippet;
static const QLatin1String kSnippets;
static const QLatin1String kTrigger;
static const QLatin1String kId;
static const QLatin1String kComplement;
static const QLatin1String kGroup;
static const QLatin1String kRemoved;
static const QLatin1String kModified;
bool m_collectionLoaded;
QSharedPointer<SnippetsCollection> m_collection;
QString m_builtInSnippetsPath;
QString m_userSnippetsPath;
QString m_snippetsFileName;
};
} // Internal
......
......@@ -360,14 +360,16 @@ void SnippetsSettingsPagePrivate::apply()
if (settingsChanged())
writeSettings();
if (m_snippetsCollectionChanged)
SnippetsManager::instance()->persistSnippetsCollection();
if (m_snippetsCollectionChanged) {
SnippetsManager::instance()->snippetsCollection()->synchronize();
m_snippetsCollectionChanged = false;
}
}
void SnippetsSettingsPagePrivate::finish()
{
if (m_snippetsCollectionChanged) {
SnippetsManager::instance()->reloadSnippetsCollection();
SnippetsManager::instance()->snippetsCollection()->reload();
m_snippetsCollectionChanged = false;
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment