Remove the need to create ISnippetProvider subclasses

Change-Id: I1810aaa945136d9726a66dad41377429a6adc8e1
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2017-04-24 15:52:04 +02:00
parent 46b7701398
commit 9443f7104b
49 changed files with 163 additions and 629 deletions

View File

@@ -1,47 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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
** 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.
**
** 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.
**
****************************************************************************/
#include "plaintextsnippetprovider.h"
#include <texteditor/texteditorconstants.h>
#include <QLatin1String>
#include <QCoreApplication>
using namespace TextEditor;
using namespace Internal;
QString PlainTextSnippetProvider::groupId() const
{
return QLatin1String(Constants::TEXT_SNIPPET_GROUP_ID);
}
QString PlainTextSnippetProvider::displayName() const
{
return QCoreApplication::translate("TextEditor::Internal::PlainTextSnippetProvider", "Text");
}
void PlainTextSnippetProvider::decorateEditor(SnippetEditorWidget *) const
{}

View File

@@ -1,46 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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
** 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.
**
** 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.
**
****************************************************************************/
#pragma once
#include "isnippetprovider.h"
namespace TextEditor {
namespace Internal {
class PlainTextSnippetProvider : public ISnippetProvider
{
Q_OBJECT
public:
~PlainTextSnippetProvider() final = default;
QString groupId() const final;
QString displayName() const final;
void decorateEditor(TextEditor::SnippetEditorWidget *editor) const final;
};
} // Internal
} // TextEditor

View File

@@ -23,7 +23,9 @@
**
****************************************************************************/
#include "isnippetprovider.h"
#include "snippetprovider.h"
#include "texteditorplugin.h"
using namespace TextEditor;
@@ -38,15 +40,14 @@ using namespace TextEditor;
In order to create a new group of snippets two steps are necessary:
\list
\li Implement the TextEditor::ISnippetProvider interface and register it in
the extension system.
\li Register the group with TextEditor::SnippetProvider::registerGroup
\li Create an XML configuration file and place it in the
/share/qtcreator/snippets directory. As an example of the file format
please take a look at the already available ones. The meaning and consistency rules
of the fields are described below:
\list
\li group - This is the group in which the snippet belongs in the user interface.
It must match TextEditor::ISnippetProvider::groupId().
It must match TextEditor::SnippetProvider::groupId().
\li id - A unique string that identifies this snippet among all others available.
The recommended practice is to prefix it with the group so it is easier to have
such control on a file level.
@@ -64,36 +65,57 @@ using namespace TextEditor;
*/
/*!
\class TextEditor::ISnippetProvider
\brief The ISnippetProvider class acts as an interface for providing groups of snippets.
\class TextEditor::SnippetProvider
\brief The SnippetProvider class acts as an interface for providing groups of snippets.
\ingroup Snippets
Known implementors of this interface are the CppSnippetProvider, the QmlJSSnippetProvider,
and the PlainTextSnippetProvider.
*/
ISnippetProvider::ISnippetProvider() : QObject()
{}
ISnippetProvider::~ISnippetProvider()
{}
/*!
\fn QString TextEditor::ISnippetProvider::groupId() const
Returns the unique group id to which this provider is associated.
*/
QString SnippetProvider::groupId() const
{
return m_groupId;
}
/*!
\fn QString TextEditor::ISnippetProvider::displayName() const
Returns the name to be displayed in the user interface for snippets that belong to the group
associated with this provider.
*/
QString SnippetProvider::displayName() const
{
return m_displayName;
}
/*!
\fn void TextEditor::ISnippetProvider::decorateEditor(SnippetEditorWidget *editor) const
This is a hook which allows you to apply customizations such as highlighting or indentation
to the snippet editor.
EditorDecorator is a hook which allows you to apply customizations such as highlighting or
indentation to the snippet editor.
*/
SnippetProvider::EditorDecorator SnippetProvider::editorDecorator() const
{
return m_editorDecorator;
}
/*!
Applies customizations such as highlighting or indentation to the snippet editor.
*/
void SnippetProvider::decorateEditor(TextEditorWidget *editor) const
{
editorDecorator()(editor);
}
static void doNotDecorate(TextEditorWidget *) { }
/*!
Registers a snippet group with \a groupId, \a displayName and \a editorDecorator.
*/
void SnippetProvider::registerGroup(const QString &groupId, const QString &displayName,
EditorDecorator editorDecorator)
{
auto provider = new SnippetProvider;
provider->m_groupId = groupId;
provider->m_displayName = displayName;
provider->m_editorDecorator = editorDecorator ? editorDecorator : EditorDecorator(doNotDecorate);
Internal::TextEditorPlugin::instance()->addAutoReleasedObject(provider);
}

View File

@@ -29,22 +29,33 @@
#include <QObject>
#include <functional>
namespace TextEditor {
class SnippetEditorWidget;
class TextEditorWidget;
class TEXTEDITOR_EXPORT ISnippetProvider : public QObject
class TEXTEDITOR_EXPORT SnippetProvider : public QObject
{
Q_OBJECT
public:
virtual ~ISnippetProvider();
using EditorDecorator = std::function<void(TextEditorWidget *)>;
virtual QString groupId() const = 0;
virtual QString displayName() const = 0;
virtual void decorateEditor(SnippetEditorWidget *editor) const = 0;
static void registerGroup(const QString &groupId, const QString &displayName,
EditorDecorator editorDecorator = EditorDecorator());
protected:
ISnippetProvider();
QString groupId() const;
QString displayName() const;
EditorDecorator editorDecorator() const;
void decorateEditor(TextEditorWidget *editor) const;
private:
SnippetProvider() = default;
QString m_groupId;
QString m_displayName;
EditorDecorator m_editorDecorator;
};
} // TextEditor

View File

@@ -24,7 +24,7 @@
****************************************************************************/
#include "snippetscollection.h"
#include "isnippetprovider.h"
#include "snippetprovider.h"
#include "reuse.h"
#include <coreplugin/icore.h>
@@ -409,9 +409,9 @@ int SnippetsCollection::groupIndex(const QString &groupId) const
void SnippetsCollection::identifyGroups()
{
const QList<ISnippetProvider *> &providers =
ExtensionSystem::PluginManager::getObjects<ISnippetProvider>();
foreach (ISnippetProvider *provider, providers) {
const QList<SnippetProvider *> &providers =
ExtensionSystem::PluginManager::getObjects<SnippetProvider>();
foreach (SnippetProvider *provider, providers) {
const int groupIndex = m_groupIndexById.size();
m_groupIndexById.insert(provider->groupId(), groupIndex);
m_snippets.resize(groupIndex + 1);

View File

@@ -25,7 +25,7 @@
#include "snippetssettingspage.h"
#include "snippeteditor.h"
#include "isnippetprovider.h"
#include "snippetprovider.h"
#include "snippet.h"
#include "snippetscollection.h"
#include "snippetssettings.h"
@@ -328,9 +328,9 @@ void SnippetsSettingsPagePrivate::configureUi(QWidget *w)
{
m_ui.setupUi(w);
const QList<ISnippetProvider *> &providers =
ExtensionSystem::PluginManager::getObjects<ISnippetProvider>();
foreach (ISnippetProvider *provider, providers) {
const QList<SnippetProvider *> &providers =
ExtensionSystem::PluginManager::getObjects<SnippetProvider>();
foreach (SnippetProvider *provider, providers) {
m_ui.groupCombo->addItem(provider->displayName(), provider->groupId());
SnippetEditorWidget *snippetEditor = new SnippetEditorWidget(w);
provider->decorateEditor(snippetEditor);
@@ -537,14 +537,14 @@ void SnippetsSettingsPagePrivate::setSnippetContent()
void SnippetsSettingsPagePrivate::decorateEditors(const TextEditor::FontSettings &fontSettings)
{
const QList<ISnippetProvider *> &providers =
ExtensionSystem::PluginManager::getObjects<ISnippetProvider>();
const QList<SnippetProvider *> &providers =
ExtensionSystem::PluginManager::getObjects<SnippetProvider>();
for (int i = 0; i < m_ui.groupCombo->count(); ++i) {
SnippetEditorWidget *snippetEditor = editorAt(i);
snippetEditor->textDocument()->setFontSettings(fontSettings);
const QString &id = m_ui.groupCombo->itemData(i).toString();
// This list should be quite short... Re-iterating over it is ok.
foreach (const ISnippetProvider *provider, providers) {
foreach (const SnippetProvider *provider, providers) {
if (provider->groupId() == id)
provider->decorateEditor(snippetEditor);
}