OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / snippets / snippetscollection.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #ifndef SNIPPETSCOLLECTION_H
35 #define SNIPPETSCOLLECTION_H
36
37 #include "snippet.h"
38
39 #include <QtCore/QVector>
40 #include <QtCore/QStringList>
41 #include <QtCore/QHash>
42 #include <QtCore/QXmlStreamWriter>
43
44 namespace TextEditor {
45 namespace Internal {
46
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.
54 // - Not thread-safe.
55
56 class SnippetsCollection : public QObject
57 {
58     Q_OBJECT
59 public:
60     virtual ~SnippetsCollection();
61
62     static SnippetsCollection *instance();
63
64     class Hint
65     {
66         friend class SnippetsCollection;
67     public:
68         int index() const;
69     private:
70         explicit Hint(int index);
71         Hint(int index, QList<Snippet>::iterator it);
72         int m_index;
73         QList<Snippet>::iterator m_it;
74     };
75
76     void insertSnippet(const Snippet &snippet);
77     void insertSnippet(const Snippet &snippet, const Hint &hint);
78     Hint computeInsertionHint(const Snippet &snippet);
79
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);
84
85     void removeSnippet(int index, const QString &groupId);
86     void restoreRemovedSnippets(const QString &groupId);
87
88     void setSnippetContent(int index, const QString &groupId, const QString &content);
89
90     const Snippet &snippet(int index, const QString &groupId) const;
91     Snippet revertedSnippet(int index, const QString &groupId) const;
92
93     void reset(const QString &groupId);
94
95     int totalActiveSnippets(const QString &groupId) const;
96     int totalSnippets(const QString &groupId) const;
97
98     QList<QString> groupIds() const;
99
100     void reload();
101     void synchronize();
102
103 private slots:
104     void identifyGroups();
105
106 private:
107     SnippetsCollection();
108     Q_DISABLE_COPY(SnippetsCollection)
109
110     int groupIndex(const QString &groupId) const;
111     bool isGroupKnown(const QString &groupId) const;
112
113     void clearSnippets();
114     void clearSnippets(int groupIndex);
115
116     void updateActiveSnippetsEnd(int groupIndex);
117
118     QList<Snippet> readXML(const QString &fileName, const QString &snippetId = QString()) const;
119     void writeSnippetXML(const Snippet &snippet, QXmlStreamWriter *writer) const;
120
121     QList<Snippet> allBuiltInSnippets() const;
122
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;
131
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
134     // folder.
135     QString m_userSnippetsPath;
136     QString m_userSnippetsFile;
137     QStringList m_builtInSnippetsFiles;
138
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;
144
145     QHash<QString, int> m_groupIndexById;
146 };
147
148 } // Internal
149 } // TextEditor
150
151 #endif // SNIPPETSCOLLECTION_H