2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-10-27 17:38:22 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-10-27 17:38:22 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 17:14:20 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-10-27 17:38:22 +02:00
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2010-10-27 17:38:22 +02:00
|
|
|
|
|
|
|
|
#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:
|
2018-09-20 01:16:01 +03:00
|
|
|
~SnippetsCollection() override;
|
2010-12-08 16:05:48 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-12-13 01:18:33 +02:00
|
|
|
private:
|
2010-12-02 17:02:23 +01:00
|
|
|
void identifyGroups();
|
|
|
|
|
|
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
|