1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #ifndef SNIPPETSCOLLECTION_H
34 #define SNIPPETSCOLLECTION_H
38 #include <QtCore/QVector>
39 #include <QtCore/QStringList>
40 #include <QtCore/QHash>
41 #include <QtCore/QXmlStreamWriter>
43 namespace TextEditor {
46 // Characteristics of this collection:
47 // - Store snippets by group.
48 // - Keep groups of snippets sorted.
49 // - Allow snippet insertion/replacement based on a hint.
50 // - Allow modification of snippet members that are not sorting keys.
51 // - Track removed/modified built-in snippets.
52 // - Provide fast index access.
55 class SnippetsCollection : public QObject
59 virtual ~SnippetsCollection();
61 static SnippetsCollection *instance();
65 friend class SnippetsCollection;
69 explicit Hint(int index);
70 Hint(int index, QList<Snippet>::iterator it);
72 QList<Snippet>::iterator m_it;
75 void insertSnippet(const Snippet &snippet);
76 void insertSnippet(const Snippet &snippet, const Hint &hint);
77 Hint computeInsertionHint(const Snippet &snippet);
79 // Replace snippets only within the same group.
80 void replaceSnippet(int index, const Snippet &snippet);
81 void replaceSnippet(int index, const Snippet &snippet, const Hint &hint);
82 Hint computeReplacementHint(int index, const Snippet &snippet);
84 void removeSnippet(int index, const QString &groupId);
85 void restoreRemovedSnippets(const QString &groupId);
87 void setSnippetContent(int index, const QString &groupId, const QString &content);
89 const Snippet &snippet(int index, const QString &groupId) const;
90 Snippet revertedSnippet(int index, const QString &groupId) const;
92 void reset(const QString &groupId);
94 int totalActiveSnippets(const QString &groupId) const;
95 int totalSnippets(const QString &groupId) const;
97 QList<QString> groupIds() const;
103 void identifyGroups();
106 SnippetsCollection();
107 Q_DISABLE_COPY(SnippetsCollection)
109 int groupIndex(const QString &groupId) const;
110 bool isGroupKnown(const QString &groupId) const;
112 void clearSnippets();
113 void clearSnippets(int groupIndex);
115 void updateActiveSnippetsEnd(int groupIndex);
117 QList<Snippet> readXML(const QString &fileName, const QString &snippetId = QString()) const;
118 void writeSnippetXML(const Snippet &snippet, QXmlStreamWriter *writer) const;
120 QList<Snippet> allBuiltInSnippets() const;
122 static const QLatin1String kSnippet;
123 static const QLatin1String kSnippets;
124 static const QLatin1String kTrigger;
125 static const QLatin1String kId;
126 static const QLatin1String kComplement;
127 static const QLatin1String kGroup;
128 static const QLatin1String kRemoved;
129 static const QLatin1String kModified;
131 // Built-in snippets are specified in XMLs distributed in a system's folder. Snippets
132 // created or modified/removed (if they are built-ins) by the user are stored in user's
134 QString m_userSnippetsPath;
135 QString m_userSnippetsFile;
136 QStringList m_builtInSnippetsFiles;
138 // Snippets for each group are kept in a list. However, not all of them are necessarily
139 // active. Specifically, removed built-in snippets are kept as the last ones (for each
140 // group there is a iterator that marks the logical end).
141 QVector<QList<Snippet> > m_snippets;
142 QVector<QList<Snippet>::iterator> m_activeSnippetsEnd;
144 QHash<QString, int> m_groupIndexById;
150 #endif // SNIPPETSCOLLECTION_H