Commit 898bc97c authored by Leandro Melo's avatar Leandro Melo
Browse files

Snippets: Make it flexible for adding builtin snippets

Instead of in one XML embedded as a resource, now builtin
snippets can be specified on any XML distributed under share.
parent f6146632
<?xml version="1.0" encoding="utf-8"?>
<snippets>
<snippet group="C++" trigger="class" id="cpp_genericclass">class $name$
{
public:
$name$() {}
};</snippet>
<snippet group="C++" trigger="class" id="cpp_qobjectclass" complement="derived from QObject">class $name$ : public QObject
{
Q_OBJECT
public:
$name$() {}
virtual ~$name$() {}
};</snippet>
<snippet group="C++" trigger="class" id="cpp_qwidgetclass" complement="derived from QWidget">class $name$ : public QWidget
{
Q_OBJECT
public:
$name$() {}
virtual ~$name$() {}
};</snippet>
<snippet group="C++" trigger="class" id="cpp_classtemplate" complement="template">template &lt;typename $T$&gt;
class $name$
{
public:
$name$() {}
};</snippet>
<snippet group="C++" trigger="do" id="cpp_do">do {
} while ($condition$);</snippet>
<snippet group="C++" trigger="else" id="cpp_else" >else {
}</snippet>
<snippet group="C++" trigger="else" id="cpp_elsewithif" complement="with if">else if ($condition$) {
}</snippet>
<snippet group="C++" trigger="for" id="cpp_for">for (int $var$ = 0; $var$ &lt; $total$; ++$var$) {
}</snippet>
<snippet group="C++" trigger="foreach" id="cpp_foreach">foreach ($var$, $container$) {
}</snippet>
<snippet group="C++" trigger="if" id="cpp_if">if ($condition$) {
}</snippet>
<snippet group="C++" trigger="if" id="cpp_ifandelse" complement="and else">if ($condition$) {
} else {
}</snippet>
<snippet group="C++" trigger="namespace" id="cpp_namespace">namespace $name$ {
}</snippet>
<snippet group="C++" trigger="try" id="cpp_trycatch" complement="and catch">try {
} catch (...) {
}</snippet>
<snippet group="C++" trigger="using" id="cpp_usingnamespace" complement="namespace">using namespace $name$;</snippet>
<snippet group="C++" trigger="while" id="cpp_while">while ($condition$) {
}</snippet>
</snippets>
<?xml version="1.0" encoding="utf-8"?>
<snippets>
<snippet group="QML" trigger="property" id="qml_property">property $type$ $name$: $value$</snippet>
<snippet group="QML" trigger="Item" id="qml_item">Item {
id: $name$
}</snippet>
<snippet group="QML" trigger="BorderImage" id="qml_borderimage">BorderImage {
id: $name$
source: "$file$"
width: $100$; height: $100$
border.left: $5$; border.top: $5$
border.right: $5$; border.bottom: $5$
}</snippet>
<snippet group="QML" trigger="Image" id="qml_image">Image {
id: $name$
source: "$file$"
}</snippet>
<snippet group="QML" trigger="Text" id="qml_text">Text {
id: $name$
text: "$text$"
}</snippet>
<snippet group="QML" trigger="states" id="qml_states">states: [
State {
name: "$name$"
PropertyChanges {
target: $name$
$$
}
}
]</snippet>
<snippet group="QML" trigger="State" id="qml_state">State {
name: "$name$"
PropertyChanges {
target: $name$
$$
}
}</snippet>
<snippet group="QML" trigger="transitions" id="qml_transitions">transitions: [
Transition {
from: "$name$"
to: "$name$"
$$
}
]</snippet>
<snippet group="QML" trigger="Transition" id="qml_transition">Transition {
from: "$name$"
to: "$name$"
$$
}</snippet>
<snippet group="QML" trigger="PropertyChanges" id="qml_propertychanges">PropertyChanges {
target: $name$
$$
}</snippet>
<snippet group="QML" trigger="NumberAnimation" id="qml_numberanimationwithtargets" complement="with targets">NumberAnimation { targets: [$name$]; properties: "$name$"; duration: $200$ }</snippet>
<snippet group="QML" trigger="NumberAnimation" id="qml_numberanimationwithtarget" complement="with target">NumberAnimation { target: $name$; property: "$name$"; to: $value$; duration: $200$ }</snippet>
<snippet group="QML" trigger="PropertyAction" id="qml_propertyactionwithtargets" complement="with targets">PropertyAction { targets: [$name$]; properties: "$name$" }</snippet>
<snippet group="QML" trigger="PropertyAction" id="qml_propertyactionwithtarget" complement="with target">PropertyAction { target: $name$; property: "$name$"; value: $value$ }</snippet>
<snippet group="QML" trigger="PauseAnimation" id="qml_pauseanimation">PauseAnimation { duration: $200$ }</snippet>
<snippet group="QML" trigger="ColorAnimation" id="qml_coloranimation">ColorAnimation { from: $"white"$; to: $"black"$; duration: $200$ }</snippet>
</snippets>
<?xml version="1.0" encoding="utf-8"?>
<snippets>
<snippet group="Text" trigger="global" id="text_global" complement="example">// This is available in all editors.</snippet>
</snippets>
...@@ -25,6 +25,7 @@ isEmpty(vcproj) { ...@@ -25,6 +25,7 @@ isEmpty(vcproj) {
DATA_DIRS = \ DATA_DIRS = \
examplebrowser \ examplebrowser \
snippets \
templates \ templates \
designer \ designer \
schemes \ schemes \
......
<?xml version="1.0" encoding="utf-8"?>
<snippets>
<snippet group="C++" trigger="class" id="genericclass">class $name$
{
public:
$name$() {}
};</snippet>
<snippet group="C++" trigger="class" id="qobjectclass" complement="derived from QObject">class $name$ : public QObject
{
Q_OBJECT
public:
$name$() {}
virtual ~$name$() {}
};</snippet>
<snippet group="C++" trigger="class" id="qwidgetclass" complement="derived from QWidget">class $name$ : public QWidget
{
Q_OBJECT
public:
$name$() {}
virtual ~$name$() {}
};</snippet>
<snippet group="C++" trigger="class" id="classtemplate" complement="template">template &lt;typename $T$&gt;
class $name$
{
public:
$name$() {}
};</snippet>
<snippet group="C++" trigger="do" id="do">do {
} while ($condition$);</snippet>
<snippet group="C++" trigger="else" id="else" >else {
}</snippet>
<snippet group="C++" trigger="else" id="elsewithif" complement="with if">else if ($condition$) {
}</snippet>
<snippet group="C++" trigger="for" id="for">for (int $var$ = 0; $var$ &lt; $total$; ++$var$) {
}</snippet>
<snippet group="C++" trigger="foreach" id="foreach">foreach ($var$, $container$) {
}</snippet>
<snippet group="C++" trigger="if" id="if">if ($condition$) {
}</snippet>
<snippet group="C++" trigger="if" id="ifandelse" complement="and else">if ($condition$) {
} else {
}</snippet>
<snippet group="C++" trigger="namespace" id="namespace">namespace $name$ {
}</snippet>
<snippet group="C++" trigger="try" id="trycatch" complement="and catch">try {
} catch (...) {
}</snippet>
<snippet group="C++" trigger="using" id="usingnamespace" complement="namespace">using namespace $name$;</snippet>
<snippet group="C++" trigger="while" id="while">while ($condition$) {
}</snippet>
<snippet group="QML" trigger="property" id="property">property $type$ $name$: $value$</snippet>
<snippet group="QML" trigger="Item" id="item">Item {
id: $name$
}</snippet>
<snippet group="QML" trigger="BorderImage" id="borderimage">BorderImage {
id: $name$
source: "$file$"
width: $100$; height: $100$
border.left: $5$; border.top: $5$
border.right: $5$; border.bottom: $5$
}</snippet>
<snippet group="QML" trigger="Image" id="image">Image {
id: $name$
source: "$file$"
}</snippet>
<snippet group="QML" trigger="Text" id="text">Text {
id: $name$
text: "$text$"
}</snippet>
<snippet group="QML" trigger="states" id="states">states: [
State {
name: "$name$"
PropertyChanges {
target: $name$
$$
}
}
]</snippet>
<snippet group="QML" trigger="State" id="state">State {
name: "$name$"
PropertyChanges {
target: $name$
$$
}
}</snippet>
<snippet group="QML" trigger="transitions" id="transitions">transitions: [
Transition {
from: "$name$"
to: "$name$"
$$
}
]</snippet>
<snippet group="QML" trigger="Transition" id="transition">Transition {
from: "$name$"
to: "$name$"
$$
}</snippet>
<snippet group="QML" trigger="PropertyChanges" id="propertychanges">PropertyChanges {
target: $name$
$$
}</snippet>
<snippet group="QML" trigger="NumberAnimation" id="numberanimationwithtargets" complement="with targets">NumberAnimation { targets: [$name$]; properties: "$name$"; duration: $200$ }</snippet>
<snippet group="QML" trigger="NumberAnimation" id="numberanimationwithtarget" complement="with target">NumberAnimation { target: $name$; property: "$name$"; to: $value$; duration: $200$ }</snippet>
<snippet group="QML" trigger="PropertyAction" id="propertyactionwithtargets" complement="with targets">PropertyAction { targets: [$name$]; properties: "$name$" }</snippet>
<snippet group="QML" trigger="PropertyAction" id="propertyactionwithtarget" complement="with target">PropertyAction { target: $name$; property: "$name$"; value: $value$ }</snippet>
<snippet group="QML" trigger="PauseAnimation" id="pauseanimation">PauseAnimation { duration: $200$ }</snippet>
<snippet group="QML" trigger="ColorAnimation" id="coloranimation">ColorAnimation { from: $"white"$; to: $"black"$; duration: $200$ }</snippet>
<snippet group="Text" trigger="global" id="global" complement="example">// This is available in all editors.</snippet>
</snippets>
...@@ -100,10 +100,14 @@ int SnippetsCollection::Hint::index() const ...@@ -100,10 +100,14 @@ int SnippetsCollection::Hint::index() const
// SnippetsCollection // SnippetsCollection
SnippetsCollection::SnippetsCollection() : SnippetsCollection::SnippetsCollection() :
m_builtInSnippetsPath(QLatin1String(":/texteditor/snippets/")),
m_userSnippetsPath(Core::ICore::instance()->userResourcePath() + QLatin1String("/snippets/")), m_userSnippetsPath(Core::ICore::instance()->userResourcePath() + QLatin1String("/snippets/")),
m_snippetsFileName(QLatin1String("snippets.xml")) m_userSnippetsFile(QLatin1String("snippets.xml"))
{ {
QDir dir(Core::ICore::instance()->resourcePath() + QLatin1String("/snippets/"));
dir.setNameFilters(QStringList(QLatin1String("*.xml")));
foreach (const QFileInfo &fi, dir.entryInfoList())
m_builtInSnippetsFiles.append(fi.absoluteFilePath());
connect(Core::ICore::instance(), SIGNAL(coreOpened()), this, SLOT(identifyGroups())); connect(Core::ICore::instance(), SIGNAL(coreOpened()), this, SLOT(identifyGroups()));
} }
...@@ -261,10 +265,11 @@ Snippet SnippetsCollection::revertedSnippet(int index, const QString &groupId) c ...@@ -261,10 +265,11 @@ Snippet SnippetsCollection::revertedSnippet(int index, const QString &groupId) c
const Snippet &candidate = snippet(index, groupId); const Snippet &candidate = snippet(index, groupId);
Q_ASSERT(candidate.isBuiltIn()); Q_ASSERT(candidate.isBuiltIn());
const QList<Snippet> &builtIn = foreach (const QString &fileName, m_builtInSnippetsFiles) {
readXML(m_builtInSnippetsPath + m_snippetsFileName, candidate.id()); const QList<Snippet> &builtIn = readXML(fileName, candidate.id());
if (builtIn.size() == 1) if (builtIn.size() == 1)
return builtIn.at(0); return builtIn.at(0);
}
return Snippet(groupId); return Snippet(groupId);
} }
...@@ -272,7 +277,7 @@ void SnippetsCollection::reset(const QString &groupId) ...@@ -272,7 +277,7 @@ void SnippetsCollection::reset(const QString &groupId)
{ {
clearSnippets(groupIndex(groupId)); clearSnippets(groupIndex(groupId));
const QList<Snippet> &builtInSnippets = readXML(m_builtInSnippetsPath + m_snippetsFileName); const QList<Snippet> &builtInSnippets = allBuiltInSnippets();
foreach (const Snippet &snippet, builtInSnippets) foreach (const Snippet &snippet, builtInSnippets)
if (groupId == snippet.groupId()) if (groupId == snippet.groupId())
insertSnippet(snippet); insertSnippet(snippet);
...@@ -282,12 +287,12 @@ void SnippetsCollection::reload() ...@@ -282,12 +287,12 @@ void SnippetsCollection::reload()
{ {
clearSnippets(); clearSnippets();
const QList<Snippet> &builtInSnippets = readXML(m_builtInSnippetsPath + m_snippetsFileName); const QList<Snippet> &builtInSnippets = allBuiltInSnippets();
QHash<QString, Snippet> activeBuiltInSnippets; QHash<QString, Snippet> activeBuiltInSnippets;
foreach (const Snippet &snippet, builtInSnippets) foreach (const Snippet &snippet, builtInSnippets)
activeBuiltInSnippets.insert(snippet.id(), snippet); activeBuiltInSnippets.insert(snippet.id(), snippet);
const QList<Snippet> &userSnippets = readXML(m_userSnippetsPath + m_snippetsFileName); const QList<Snippet> &userSnippets = readXML(m_userSnippetsPath + m_userSnippetsFile);
foreach (const Snippet &snippet, userSnippets) { foreach (const Snippet &snippet, userSnippets) {
if (snippet.isBuiltIn()) if (snippet.isBuiltIn())
// This user snippet overrides the corresponding built-in snippet. // This user snippet overrides the corresponding built-in snippet.
...@@ -302,7 +307,7 @@ void SnippetsCollection::reload() ...@@ -302,7 +307,7 @@ void SnippetsCollection::reload()
void SnippetsCollection::synchronize() void SnippetsCollection::synchronize()
{ {
if (QFile::exists(m_userSnippetsPath) || QDir().mkpath(m_userSnippetsPath)) { if (QFile::exists(m_userSnippetsPath) || QDir().mkpath(m_userSnippetsPath)) {
QFile file(m_userSnippetsPath + m_snippetsFileName); QFile file(m_userSnippetsPath + m_userSnippetsFile);
if (file.open(QFile::WriteOnly | QFile::Truncate)) { if (file.open(QFile::WriteOnly | QFile::Truncate)) {
QXmlStreamWriter writer(&file); QXmlStreamWriter writer(&file);
writer.setAutoFormatting(true); writer.setAutoFormatting(true);
...@@ -389,6 +394,14 @@ QList<Snippet> SnippetsCollection::readXML(const QString &fileName, const QStrin ...@@ -389,6 +394,14 @@ QList<Snippet> SnippetsCollection::readXML(const QString &fileName, const QStrin
return snippets; return snippets;
} }
QList<Snippet> SnippetsCollection::allBuiltInSnippets() const
{
QList<Snippet> builtInSnippets;
foreach (const QString &fileName, m_builtInSnippetsFiles)
builtInSnippets.append(readXML(fileName));
return builtInSnippets;
}
int SnippetsCollection::groupIndex(const QString &groupId) const int SnippetsCollection::groupIndex(const QString &groupId) const
{ {
return m_groupIndexById.value(groupId); return m_groupIndexById.value(groupId);
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
#include "snippet.h" #include "snippet.h"
#include <QtCore/QVector> #include <QtCore/QVector>
#include <QtCore/QList> #include <QtCore/QStringList>
#include <QtCore/QHash> #include <QtCore/QHash>
QT_FORWARD_DECLARE_CLASS(QXmlStreamWriter) QT_FORWARD_DECLARE_CLASS(QXmlStreamWriter)
...@@ -101,6 +101,7 @@ private slots: ...@@ -101,6 +101,7 @@ private slots:
private: private:
int groupIndex(const QString &groupId) const; int groupIndex(const QString &groupId) const;
bool isGroupKnown(const QString &groupId) const;
void clearSnippets(); void clearSnippets();
void clearSnippets(int groupIndex); void clearSnippets(int groupIndex);
...@@ -110,6 +111,8 @@ private: ...@@ -110,6 +111,8 @@ private:
QList<Snippet> readXML(const QString &fileName, const QString &snippetId = QString()) const; QList<Snippet> readXML(const QString &fileName, const QString &snippetId = QString()) const;
void writeSnippetXML(const Snippet &snippet, QXmlStreamWriter *writer) const; void writeSnippetXML(const Snippet &snippet, QXmlStreamWriter *writer) const;
QList<Snippet> allBuiltInSnippets() const;
static const QLatin1String kSnippet; static const QLatin1String kSnippet;
static const QLatin1String kSnippets; static const QLatin1String kSnippets;
static const QLatin1String kTrigger; static const QLatin1String kTrigger;
...@@ -119,14 +122,11 @@ private: ...@@ -119,14 +122,11 @@ private:
static const QLatin1String kRemoved; static const QLatin1String kRemoved;
static const QLatin1String kModified; static const QLatin1String kModified;
bool isGroupKnown(const QString &groupId) const; // Built-in snippets are specified in XMLs distributed in a system's folder. Snippets
// created or modified/removed (if they are built-ins) by the user are stored in another
// Built-in snippets are specified in an XML embedded as a resource. Snippets created/
// modified/removed by the user are stored in another XML created dynamically in the
// user's folder.
QString m_builtInSnippetsPath;
QString m_userSnippetsPath; QString m_userSnippetsPath;
QString m_snippetsFileName; QString m_userSnippetsFile;
QStringList m_builtInSnippetsFiles;
// Snippets for each group are kept in a list. However, not all of them are necessarily // 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 // active. Specifically, removed built-in snippets are kept as the last ones (for each
......
...@@ -5,6 +5,5 @@ ...@@ -5,6 +5,5 @@
<file>TextEditor.mimetypes.xml</file> <file>TextEditor.mimetypes.xml</file>
<file>images/refactormarker.png</file> <file>images/refactormarker.png</file>
<file>images/snippet.png</file> <file>images/snippet.png</file>
<file>snippets/snippets.xml</file>
</qresource> </qresource>
</RCC> </RCC>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment