2010-10-27 17:38:22 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2010-10-27 17:38:22 +02:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2010-10-27 17:38:22 +02:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
|
|
|
** Please review the following information to ensure the GNU Lesser General
|
|
|
|
|
** Public License version 2.1 requirements will be met:
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2010-10-27 17:38:22 +02:00
|
|
|
**
|
2010-12-17 17:14:20 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 17:14:20 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 17:14:20 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2010-10-27 17:38:22 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef SNIPPETSCOLLECTION_H
|
|
|
|
|
#define SNIPPETSCOLLECTION_H
|
|
|
|
|
|
|
|
|
|
#include "snippet.h"
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QVector>
|
2010-12-03 15:55:51 +01:00
|
|
|
#include <QtCore/QStringList>
|
2010-12-02 17:02:23 +01:00
|
|
|
#include <QtCore/QHash>
|
2010-12-09 11:24:01 +01:00
|
|
|
#include <QtCore/QXmlStreamWriter>
|
2010-11-23 11:31:57 +01:00
|
|
|
|
2010-10-27 17:38:22 +02:00
|
|
|
namespace TextEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
// Characteristics of this collection:
|
|
|
|
|
// - Store snippets by group.
|
|
|
|
|
// - Keep groups of snippets sorted.
|
|
|
|
|
// - Allow snippet insertion/replacement based on a hint.
|
|
|
|
|
// - Allow modification of snippet members that are not sorting keys.
|
|
|
|
|
// - Track removed/modified built-in snippets.
|
|
|
|
|
// - Provide fast index access.
|
|
|
|
|
// - Not thread-safe.
|
|
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
class SnippetsCollection : public QObject
|
2010-10-27 17:38:22 +02:00
|
|
|
{
|
2010-12-02 17:02:23 +01:00
|
|
|
Q_OBJECT
|
2010-10-27 17:38:22 +02:00
|
|
|
public:
|
2010-12-08 16:05:48 +01:00
|
|
|
virtual ~SnippetsCollection();
|
|
|
|
|
|
|
|
|
|
static SnippetsCollection *instance();
|
2010-10-27 17:38:22 +02:00
|
|
|
|
|
|
|
|
class Hint
|
|
|
|
|
{
|
|
|
|
|
friend class SnippetsCollection;
|
|
|
|
|
public:
|
|
|
|
|
int index() const;
|
|
|
|
|
private:
|
2010-12-02 17:02:23 +01:00
|
|
|
explicit Hint(int index);
|
2010-10-27 17:38:22 +02:00
|
|
|
Hint(int index, QList<Snippet>::iterator it);
|
|
|
|
|
int m_index;
|
|
|
|
|
QList<Snippet>::iterator m_it;
|
|
|
|
|
};
|
|
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
void insertSnippet(const Snippet &snippet);
|
|
|
|
|
void insertSnippet(const Snippet &snippet, const Hint &hint);
|
|
|
|
|
Hint computeInsertionHint(const Snippet &snippet);
|
2010-10-27 17:38:22 +02:00
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
// Replace snippets only within the same group.
|
|
|
|
|
void replaceSnippet(int index, const Snippet &snippet);
|
|
|
|
|
void replaceSnippet(int index, const Snippet &snippet, const Hint &hint);
|
|
|
|
|
Hint computeReplacementHint(int index, const Snippet &snippet);
|
2010-10-27 17:38:22 +02:00
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
void removeSnippet(int index, const QString &groupId);
|
|
|
|
|
void restoreRemovedSnippets(const QString &groupId);
|
2010-10-27 17:38:22 +02:00
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
void setSnippetContent(int index, const QString &groupId, const QString &content);
|
2010-10-27 17:38:22 +02:00
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
const Snippet &snippet(int index, const QString &groupId) const;
|
|
|
|
|
Snippet revertedSnippet(int index, const QString &groupId) const;
|
2010-11-24 11:04:24 +01:00
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
void reset(const QString &groupId);
|
2010-10-27 17:38:22 +02:00
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
int totalActiveSnippets(const QString &groupId) const;
|
|
|
|
|
int totalSnippets(const QString &groupId) const;
|
|
|
|
|
|
|
|
|
|
QList<QString> groupIds() const;
|
2010-10-27 17:38:22 +02:00
|
|
|
|
2010-11-23 11:31:57 +01:00
|
|
|
void reload();
|
2011-03-30 15:15:15 +02:00
|
|
|
bool synchronize(QString *errorString);
|
2010-10-27 17:38:22 +02:00
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
private slots:
|
|
|
|
|
void identifyGroups();
|
|
|
|
|
|
2010-10-27 17:38:22 +02:00
|
|
|
private:
|
2010-12-08 16:05:48 +01:00
|
|
|
SnippetsCollection();
|
|
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
int groupIndex(const QString &groupId) const;
|
2010-12-03 15:55:51 +01:00
|
|
|
bool isGroupKnown(const QString &groupId) const;
|
2010-12-02 17:02:23 +01:00
|
|
|
|
|
|
|
|
void clearSnippets();
|
|
|
|
|
void clearSnippets(int groupIndex);
|
|
|
|
|
|
|
|
|
|
void updateActiveSnippetsEnd(int groupIndex);
|
2010-10-27 17:38:22 +02:00
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
QList<Snippet> readXML(const QString &fileName, const QString &snippetId = QString()) const;
|
|
|
|
|
void writeSnippetXML(const Snippet &snippet, QXmlStreamWriter *writer) const;
|
2010-11-23 11:31:57 +01:00
|
|
|
|
2010-12-03 15:55:51 +01:00
|
|
|
QList<Snippet> allBuiltInSnippets() const;
|
|
|
|
|
|
2010-11-23 11:31:57 +01:00
|
|
|
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;
|
|
|
|
|
|
2010-12-03 15:55:51 +01:00
|
|
|
// Built-in snippets are specified in XMLs distributed in a system's folder. Snippets
|
2010-12-08 16:05:48 +01:00
|
|
|
// created or modified/removed (if they are built-ins) by the user are stored in user's
|
|
|
|
|
// folder.
|
2010-11-23 11:31:57 +01:00
|
|
|
QString m_userSnippetsPath;
|
2010-12-03 15:55:51 +01:00
|
|
|
QString m_userSnippetsFile;
|
|
|
|
|
QStringList m_builtInSnippetsFiles;
|
2010-12-02 17:02:23 +01:00
|
|
|
|
|
|
|
|
// Snippets for each group are kept in a list. However, not all of them are necessarily
|
|
|
|
|
// active. Specifically, removed built-in snippets are kept as the last ones (for each
|
|
|
|
|
// group there is a iterator that marks the logical end).
|
|
|
|
|
QVector<QList<Snippet> > m_snippets;
|
|
|
|
|
QVector<QList<Snippet>::iterator> m_activeSnippetsEnd;
|
|
|
|
|
|
|
|
|
|
QHash<QString, int> m_groupIndexById;
|
2010-10-27 17:38:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // Internal
|
|
|
|
|
} // TextEditor
|
|
|
|
|
|
|
|
|
|
#endif // SNIPPETSCOLLECTION_H
|