2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-10-27 17:38:22 +02:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2010-10-27 17:38:22 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-10-27 17:38:22 +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
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2010-10-27 17:38:22 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 17:14:20 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-10-27 17:38:22 +02:00
|
|
|
|
|
|
|
|
#ifndef SNIPPETSCOLLECTION_H
|
|
|
|
|
#define SNIPPETSCOLLECTION_H
|
|
|
|
|
|
|
|
|
|
#include "snippet.h"
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QVector>
|
|
|
|
|
#include <QStringList>
|
|
|
|
|
#include <QHash>
|
|
|
|
|
#include <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
|