1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #ifndef SNIPPETSCOLLECTION_H
35 #define SNIPPETSCOLLECTION_H
39 #include <QtCore/QVector>
40 #include <QtCore/QStringList>
41 #include <QtCore/QHash>
42 #include <QtCore/QXmlStreamWriter>
44 namespace TextEditor {
47 // Characteristics of this collection:
48 // - Store snippets by group.
49 // - Keep groups of snippets sorted.
50 // - Allow snippet insertion/replacement based on a hint.
51 // - Allow modification of snippet members that are not sorting keys.
52 // - Track removed/modified built-in snippets.
53 // - Provide fast index access.
56 class SnippetsCollection : public QObject
60 virtual ~SnippetsCollection();
62 static SnippetsCollection *instance();
66 friend class SnippetsCollection;
70 explicit Hint(int index);
71 Hint(int index, QList<Snippet>::iterator it);
73 QList<Snippet>::iterator m_it;
76 void insertSnippet(const Snippet &snippet);
77 void insertSnippet(const Snippet &snippet, const Hint &hint);
78 Hint computeInsertionHint(const Snippet &snippet);
80 // Replace snippets only within the same group.
81 void replaceSnippet(int index, const Snippet &snippet);
82 void replaceSnippet(int index, const Snippet &snippet, const Hint &hint);
83 Hint computeReplacementHint(int index, const Snippet &snippet);
85 void removeSnippet(int index, const QString &groupId);
86 void restoreRemovedSnippets(const QString &groupId);
88 void setSnippetContent(int index, const QString &groupId, const QString &content);
90 const Snippet &snippet(int index, const QString &groupId) const;
91 Snippet revertedSnippet(int index, const QString &groupId) const;
93 void reset(const QString &groupId);
95 int totalActiveSnippets(const QString &groupId) const;
96 int totalSnippets(const QString &groupId) const;
98 QList<QString> groupIds() const;
104 void identifyGroups();
107 SnippetsCollection();
108 Q_DISABLE_COPY(SnippetsCollection)
110 int groupIndex(const QString &groupId) const;
111 bool isGroupKnown(const QString &groupId) const;
113 void clearSnippets();
114 void clearSnippets(int groupIndex);
116 void updateActiveSnippetsEnd(int groupIndex);
118 QList<Snippet> readXML(const QString &fileName, const QString &snippetId = QString()) const;
119 void writeSnippetXML(const Snippet &snippet, QXmlStreamWriter *writer) const;
121 QList<Snippet> allBuiltInSnippets() const;
123 static const QLatin1String kSnippet;
124 static const QLatin1String kSnippets;
125 static const QLatin1String kTrigger;
126 static const QLatin1String kId;
127 static const QLatin1String kComplement;
128 static const QLatin1String kGroup;
129 static const QLatin1String kRemoved;
130 static const QLatin1String kModified;
132 // Built-in snippets are specified in XMLs distributed in a system's folder. Snippets
133 // created or modified/removed (if they are built-ins) by the user are stored in user's
135 QString m_userSnippetsPath;
136 QString m_userSnippetsFile;
137 QStringList m_builtInSnippetsFiles;
139 // Snippets for each group are kept in a list. However, not all of them are necessarily
140 // active. Specifically, removed built-in snippets are kept as the last ones (for each
141 // group there is a iterator that marks the logical end).
142 QVector<QList<Snippet> > m_snippets;
143 QVector<QList<Snippet>::iterator> m_activeSnippetsEnd;
145 QHash<QString, int> m_groupIndexById;
151 #endif // SNIPPETSCOLLECTION_H