Commit 1175705b authored by Leandro Melo's avatar Leandro Melo

Snippets: Make it easier for plugins to add snippets groups

Groups are no longer enum values but identified from snippet providers.
parent fdbb34ad
......@@ -26,7 +26,7 @@ HEADERS += cppplugin.h \
cppqtstyleindenter.h \
cppautocompleter.h \
cppcompleteswitch.h \
cppsnippeteditordecorator.h
cppsnippetprovider.h
SOURCES += cppplugin.cpp \
cppeditor.cpp \
......@@ -47,7 +47,7 @@ SOURCES += cppplugin.cpp \
cppqtstyleindenter.cpp \
cppautocompleter.cpp \
cppcompleteswitch.cpp \
cppsnippeteditordecorator.cpp
cppsnippetprovider.cpp
RESOURCES += cppeditor.qrc
OTHER_FILES += CppEditor.mimetypes.xml
......@@ -68,6 +68,8 @@ const char * const CPP_HEADER_MIMETYPE = "text/x-c++hdr";
const char * const WIZARD_CATEGORY = "O.C++";
const char * const WIZARD_TR_CATEGORY = QT_TRANSLATE_NOOP("CppEditor", "C++");
const char * const CPP_SNIPPETS_GROUP_ID = "C++";
} // namespace Constants
} // namespace CppEditor
......
......@@ -38,7 +38,7 @@
#include "cppoutline.h"
#include "cppquickfixcollector.h"
#include "cpptypehierarchy.h"
#include "cppsnippeteditordecorator.h"
#include "cppsnippetprovider.h"
#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
......@@ -212,7 +212,7 @@ bool CppPlugin::initialize(const QStringList & /*arguments*/, QString *errorMess
addAutoReleasedObject(new CppHoverHandler);
addAutoReleasedObject(new CppOutlineWidgetFactory);
addAutoReleasedObject(new CppTypeHierarchyFactory);
addAutoReleasedObject(new CppSnippetEditorDecorator);
addAutoReleasedObject(new CppSnippetProvider);
m_quickFixCollector = new CppQuickFixCollector;
addAutoReleasedObject(m_quickFixCollector);
......
......@@ -27,35 +27,41 @@
**
**************************************************************************/
#include "cppsnippeteditordecorator.h"
#include "cppsnippetprovider.h"
#include "cpphighlighter.h"
#include "cppeditor.h"
#include "cppqtstyleindenter.h"
#include "cppautocompleter.h"
#include "cppeditorconstants.h"
#include <texteditor/texteditorsettings.h>
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/snippets/snippeteditor.h>
#include <QtCore/QLatin1String>
using namespace CppEditor;
using namespace Internal;
CppSnippetEditorDecorator::CppSnippetEditorDecorator() :
TextEditor::ISnippetEditorDecorator()
CppSnippetProvider::CppSnippetProvider() :
TextEditor::ISnippetProvider()
{}
CppSnippetEditorDecorator::~CppSnippetEditorDecorator()
CppSnippetProvider::~CppSnippetProvider()
{}
bool CppSnippetEditorDecorator::supports(TextEditor::Snippet::Group group) const
QString CppSnippetProvider::groupId() const
{
return QLatin1String(Constants::CPP_SNIPPETS_GROUP_ID);
}
QString CppSnippetProvider::displayName() const
{
if (group == TextEditor::Snippet::Cpp)
return true;
return false;
return tr("C++");
}
void CppSnippetEditorDecorator::apply(TextEditor::SnippetEditor *editor) const
void CppSnippetProvider::decorateEditor(TextEditor::SnippetEditor *editor) const
{
CppHighlighter *highlighter = new CppHighlighter;
const TextEditor::FontSettings &fs = TextEditor::TextEditorSettings::instance()->fontSettings();
......
......@@ -30,20 +30,21 @@
#ifndef CPPSNIPPETEDITORDECORATOR_H
#define CPPSNIPPETEDITORDECORATOR_H
#include <texteditor/snippets/isnippeteditordecorator.h>
#include <texteditor/snippets/isnippetprovider.h>
namespace CppEditor {
namespace Internal {
class CppSnippetEditorDecorator : public TextEditor::ISnippetEditorDecorator
class CppSnippetProvider : public TextEditor::ISnippetProvider
{
public:
CppSnippetEditorDecorator();
virtual ~CppSnippetEditorDecorator();
CppSnippetProvider();
virtual ~CppSnippetProvider();
public:
virtual bool supports(TextEditor::Snippet::Group group) const;
virtual void apply(TextEditor::SnippetEditor *editor) const;
virtual QString groupId() const;
virtual QString displayName() const;
virtual void decorateEditor(TextEditor::SnippetEditor *editor) const;
};
} // Internal
......
......@@ -52,6 +52,8 @@
#include <cplusplus/BackwardsScanner.h>
#include <cplusplus/LookupContext.h>
#include <cppeditor/cppeditorconstants.h>
#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>
#include <coreplugin/editormanager/editormanager.h>
......@@ -463,7 +465,7 @@ CppCodeCompletion::CppCodeCompletion(CppModelManager *manager)
m_automaticCompletion(false),
m_completionOperator(T_EOF_SYMBOL),
m_objcEnabled(true),
m_snippetProvider(TextEditor::Snippet::Cpp,
m_snippetProvider(CppEditor::Constants::CPP_SNIPPETS_GROUP_ID,
QIcon(QLatin1String(":/texteditor/images/snippet.png")))
{
}
......
......@@ -37,7 +37,7 @@
#include <cplusplus/TypeOfExpression.h>
#include <texteditor/icompletioncollector.h>
#include <texteditor/snippets/snippetprovider.h>
#include <texteditor/snippets/snippetcollector.h>
#include <QtCore/QObject>
#include <QtCore/QPointer>
......@@ -153,7 +153,7 @@ private:
unsigned m_completionOperator;
bool m_objcEnabled;
TextEditor::SnippetProvider m_snippetProvider;
TextEditor::SnippetCollector m_snippetProvider;
CPlusPlus::Icons m_icons;
CPlusPlus::Overview overview;
......
......@@ -30,6 +30,7 @@
#include "qmljscodecompletion.h"
#include "qmlexpressionundercursor.h"
#include "qmljseditor.h"
#include "qmljseditorconstants.h"
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <qmljs/parser/qmljsast_p.h>
......@@ -487,7 +488,7 @@ CodeCompletion::CodeCompletion(ModelManagerInterface *modelManager, QObject *par
m_editor(0),
m_startPosition(0),
m_restartCompletion(false),
m_snippetProvider(TextEditor::Snippet::Qml, iconForColor(Qt::red), SnippetOrder)
m_snippetProvider(Constants::QML_SNIPPETS_GROUP_ID, iconForColor(Qt::red), SnippetOrder)
{
Q_ASSERT(modelManager);
}
......
......@@ -32,7 +32,7 @@
#include <qmljs/qmljsdocument.h>
#include <texteditor/icompletioncollector.h>
#include <texteditor/snippets/snippetprovider.h>
#include <texteditor/snippets/snippetcollector.h>
#include <QtCore/QDateTime>
#include <QtCore/QPointer>
......@@ -97,7 +97,7 @@ private:
TextEditor::ITextEditable *m_editor;
int m_startPosition;
bool m_restartCompletion;
TextEditor::SnippetProvider m_snippetProvider;
TextEditor::SnippetCollector m_snippetProvider;
QList<TextEditor::CompletionItem> m_completions;
QPointer<FunctionArgumentWidget> m_functionArgumentWidget;
};
......
......@@ -35,7 +35,7 @@ HEADERS += \
qmljsindenter.h \
qmljsautocompleter.h \
jsfilewizard.h \
qmljssnippeteditordecorator.h
qmljssnippetprovider.h
SOURCES += \
qmljscodecompletion.cpp \
......@@ -64,7 +64,7 @@ SOURCES += \
qmljsindenter.cpp \
qmljsautocompleter.cpp \
jsfilewizard.cpp \
qmljssnippeteditordecorator.cpp
qmljssnippetprovider.cpp
RESOURCES += qmljseditor.qrc
OTHER_FILES += QmlJSEditor.mimetypes.xml
......
......@@ -60,6 +60,7 @@ const char *const TASK_CATEGORY_QML = "Task.Category.Qml";
const char * const WIZARD_CATEGORY_QML = "S.Qml";
const char * const WIZARD_TR_CATEGORY_QML = QT_TRANSLATE_NOOP("QmlJsEditor", "QML");
const char * const QML_SNIPPETS_GROUP_ID = "QML";
} // namespace Constants
} // namespace QmlJSEditor
......
......@@ -39,7 +39,7 @@
#include "qmljsoutline.h"
#include "qmljspreviewrunner.h"
#include "qmljsquickfix.h"
#include "qmljssnippeteditordecorator.h"
#include "qmljssnippetprovider.h"
#include "qmltaskmanager.h"
#include "quicktoolbar.h"
#include "quicktoolbarsettingspage.h"
......@@ -129,7 +129,7 @@ bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *e
return false;
m_modelManager = QmlJS::ModelManagerInterface::instance();
addAutoReleasedObject(new QmlJSSnippetEditorDecorator);
addAutoReleasedObject(new QmlJSSnippetProvider);
Core::Context context(QmlJSEditor::Constants::C_QMLJSEDITOR_ID);
......
......@@ -27,35 +27,41 @@
**
**************************************************************************/
#include "qmljssnippeteditordecorator.h"
#include "qmljssnippetprovider.h"
#include "qmljshighlighter.h"
#include "qmljseditor.h"
#include "qmljsindenter.h"
#include "qmljsautocompleter.h"
#include "qmljseditorconstants.h"
#include <texteditor/texteditorsettings.h>
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/snippets/snippeteditor.h>
#include <QtCore/QLatin1String>
using namespace QmlJSEditor;
using namespace Internal;
QmlJSSnippetEditorDecorator::QmlJSSnippetEditorDecorator() :
TextEditor::ISnippetEditorDecorator()
QmlJSSnippetProvider::QmlJSSnippetProvider() :
TextEditor::ISnippetProvider()
{}
QmlJSSnippetEditorDecorator::~QmlJSSnippetEditorDecorator()
QmlJSSnippetProvider::~QmlJSSnippetProvider()
{}
bool QmlJSSnippetEditorDecorator::supports(TextEditor::Snippet::Group group) const
QString QmlJSSnippetProvider::groupId() const
{
return QLatin1String(Constants::QML_SNIPPETS_GROUP_ID);
}
QString QmlJSSnippetProvider::displayName() const
{
if (group == TextEditor::Snippet::Qml)
return true;
return false;
return tr("QML");
}
void QmlJSSnippetEditorDecorator::apply(TextEditor::SnippetEditor *editor) const
void QmlJSSnippetProvider::decorateEditor(TextEditor::SnippetEditor *editor) const
{
Highlighter *highlighter = new Highlighter;
const TextEditor::FontSettings &fs = TextEditor::TextEditorSettings::instance()->fontSettings();
......
......@@ -30,20 +30,21 @@
#ifndef QMLJSSNIPPETEDITORDECORATOR_H
#define QMLJSSNIPPETEDITORDECORATOR_H
#include <texteditor/snippets/isnippeteditordecorator.h>
#include <texteditor/snippets/isnippetprovider.h>
namespace QmlJSEditor {
namespace Internal {
class QmlJSSnippetEditorDecorator : public TextEditor::ISnippetEditorDecorator
class QmlJSSnippetProvider : public TextEditor::ISnippetProvider
{
public:
QmlJSSnippetEditorDecorator();
virtual ~QmlJSSnippetEditorDecorator();
QmlJSSnippetProvider();
virtual ~QmlJSSnippetProvider();
public:
virtual bool supports(TextEditor::Snippet::Group group) const;
virtual void apply(TextEditor::SnippetEditor *editor) const;
virtual QString groupId() const;
virtual QString displayName() const;
virtual void decorateEditor(TextEditor::SnippetEditor *editor) const;
};
} // Internal
......
......@@ -27,12 +27,12 @@
**
**************************************************************************/
#include "isnippeteditordecorator.h"
#include "isnippetprovider.h"
using namespace TextEditor;
ISnippetEditorDecorator::ISnippetEditorDecorator() : QObject()
ISnippetProvider::ISnippetProvider() : QObject()
{}
ISnippetEditorDecorator::~ISnippetEditorDecorator()
ISnippetProvider::~ISnippetProvider()
{}
......@@ -40,17 +40,18 @@ namespace TextEditor {
class SnippetEditor;
class TEXTEDITOR_EXPORT ISnippetEditorDecorator : public QObject
class TEXTEDITOR_EXPORT ISnippetProvider : public QObject
{
Q_OBJECT
public:
virtual ~ISnippetEditorDecorator();
virtual ~ISnippetProvider();
virtual bool supports(Snippet::Group group) const = 0;
virtual void apply(SnippetEditor *editor) const = 0;
virtual QString groupId() const = 0;
virtual QString displayName() const = 0;
virtual void decorateEditor(SnippetEditor *editor) const = 0;
protected:
ISnippetEditorDecorator();
ISnippetProvider();
};
} // TextEditor
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "plaintextsnippetprovider.h"
#include <texteditor/texteditorconstants.h>
#include <QtCore/QLatin1String>
using namespace TextEditor;
using namespace Internal;
PlainTextSnippetProvider::PlainTextSnippetProvider()
{}
PlainTextSnippetProvider::~PlainTextSnippetProvider()
{}
QString PlainTextSnippetProvider::groupId() const
{
return QLatin1String(Constants::TEXT_SNIPPET_GROUP_ID);
}
QString PlainTextSnippetProvider::displayName() const
{
return tr("Text");
}
void PlainTextSnippetProvider::decorateEditor(TextEditor::SnippetEditor *) const
{}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef PLAINTEXTSNIPPETPROVIDER_H
#define PLAINTEXTSNIPPETPROVIDER_H
#include "isnippetprovider.h"
namespace TextEditor {
namespace Internal {
class PlainTextSnippetProvider : public ISnippetProvider
{
public:
PlainTextSnippetProvider();
virtual ~PlainTextSnippetProvider();
public:
virtual QString groupId() const;
virtual QString displayName() const;
virtual void decorateEditor(TextEditor::SnippetEditor *editor) const;
};
} // Internal
} // TextEditor
#endif // PLAINTEXTSNIPPETPROVIDER_H
......@@ -30,41 +30,15 @@
#ifndef REUSE_H
#define REUSE_H
#include "snippet.h"
#include <QtCore/QString>
#include <QtCore/QLatin1String>
namespace TextEditor {
namespace Internal {
const QLatin1String kCpp("C++");
const QLatin1String kQml("QML");
const QLatin1String kText("Text");
const QLatin1String kTrue("true");
const QLatin1String kFalse("false");
inline Snippet::Group toSnippetGroup(const QString &s)
{
const QString &upper = s.toUpper();
if (upper == kCpp)
return Snippet::Cpp;
else if (upper == kQml)
return Snippet::Qml;
else
return Snippet::PlainText;
}
inline QString fromSnippetGroup(Snippet::Group group)
{
if (group == Snippet::Cpp)
return kCpp;
else if (group == Snippet::Qml)
return kQml;
else
return kText;
}
inline bool toBool(const QString &s)
{
if (s == kTrue)
......
......@@ -37,7 +37,8 @@ using namespace TextEditor;
const QChar Snippet::kVariableDelimiter(QLatin1Char('$'));
Snippet::Snippet(const QString &id) : m_isRemoved(false), m_isModified(false), m_id(id)
Snippet::Snippet(const QString &groupId, const QString &id) :
m_isRemoved(false), m_isModified(false), m_groupId(groupId), m_id(id)
{}
Snippet::~Snippet()
......@@ -48,6 +49,11 @@ const QString &Snippet::id() const
return m_id;
}
const QString &Snippet::groupId() const
{
return m_groupId;
}
bool Snippet::isBuiltIn() const
{
return !m_id.isEmpty();
......@@ -103,16 +109,6 @@ bool Snippet::isModified() const
return m_isModified;
}
void Snippet::setGroup(Group group)
{
m_group = group;
}
Snippet::Group Snippet::group() const
{
return m_group;
}
QString Snippet::generateTip() const
{
static const QLatin1Char kNewLine('\n');
......
......@@ -40,18 +40,11 @@ namespace TextEditor {
class TEXTEDITOR_EXPORT Snippet
{
public:
Snippet(const QString &id = QString());
Snippet(const QString &groupId = QString(), const QString &id = QString());
~Snippet();
// Values from this enumeration need to be contiguous (they are used as indexes).
enum Group {
Cpp = 0,
Qml,
PlainText,
GroupSize
};
const QString &id() const;
const QString &groupId() const;
bool isBuiltIn() const;
......@@ -70,9 +63,6 @@ public:
void setIsModified(bool modified);
bool isModified() const;
void setGroup(Group group);
Group group() const;
QString generateTip() const;
static const QChar kVariableDelimiter;
......@@ -80,19 +70,13 @@ public:
private:
bool m_isRemoved;
bool m_isModified;
QString m_groupId;
QString m_id; // Only built-in snippets have an id.
QString m_trigger;
QString m_content;
QString m_complement;
Group m_group;
};
inline Snippet::Group& operator++(Snippet::Group& group)
{
group = Snippet::Group(group + 1);
return group;
}
} // TextEditor
#endif // SNIPPET_H
......@@ -27,10 +27,12 @@
**
**************************************************************************/
#include "snippetprovider.h"
#include "snippetcollector.h"
#include "snippetsmanager.h"
#include "snippetscollection.h"
#include <texteditor/texteditorconstants.h>
using namespace TextEditor;
using namespace Internal;
......@@ -38,15 +40,15 @@ namespace {
void appendSnippets(ICompletionCollector *collector,
QList<CompletionItem> *completionItems,
Snippet::Group group,
const QString &groupId,
const QIcon &icon,
int order)
{
QSharedPointer<SnippetsCollection> collection =
SnippetsManager::instance()->snippetsCollection();
const int size = collection->totalActiveSnippets(group);
const int size = collection->totalActiveSnippets(groupId);
for (int i = 0; i < size; ++i) {
const Snippet &snippet = collection->snippet(i, group);
const Snippet &snippet = collection->snippet(i, groupId);
CompletionItem item(collector);
item.text = snippet.trigger() + QLatin1Char(' ') + snippet.complement();
item.data = snippet.content();
......@@ -60,17 +62,17 @@ void appendSnippets(ICompletionCollector *collector,
} // anonymous
SnippetProvider::SnippetProvider(Snippet::Group group, const QIcon &icon, int order) :
m_group(group), m_icon(icon), m_order(order)
SnippetCollector::SnippetCollector(const QString &groupId, const QIcon &icon, int order) :
m_groupId(groupId), m_icon(icon), m_order(order)
{}
SnippetProvider::~SnippetProvider()
SnippetCollector::~SnippetCollector()
{}
QList<CompletionItem> SnippetProvider::getSnippets(ICompletionCollector *collector) const
QList<CompletionItem> SnippetCollector::getSnippets(ICompletionCollector *collector) const
{
QList<CompletionItem> completionItems;
appendSnippets(collector, &completionItems, m_group, m_icon, m_order);
appendSnippets(collector, &completionItems, Snippet::PlainText, m_icon, m_order);
appendSnippets(collector, &completionItems, m_groupId, m_icon, m_order);
appendSnippets(collector, &completionItems, Constants::TEXT_SNIPPET_GROUP_ID, m_icon, m_order);
return completionItems;
}
......@@ -30,26 +30,25 @@
#ifndef SNIPPETPROVIDER_H
#define SNIPPETPROVIDER_H
#include "snippet.h"
#include <texteditor/texteditor_global.h>
#include <texteditor/icompletioncollector.h>
#include <QtCore/QString>
#include <QtCore/QList>
#include <QtGui/QIcon>
namespace TextEditor {
class TEXTEDITOR_EXPORT SnippetProvider
class TEXTEDITOR_EXPORT SnippetCollector
{
public:
SnippetProvider(Snippet::Group group, const QIcon &icon, int order = 0);
~SnippetProvider();
SnippetCollector(const QString &groupId, const QIcon &icon, int order = 0);
~SnippetCollector();
QList<CompletionItem> getSnippets(ICompletionCollector *collector) const;
private:
Snippet::Group m_group;
QString m_groupId;
QIcon m_icon;
int m_order;
};