Commit 7528c6d6 authored by Leandro Melo's avatar Leandro Melo

Snippets: Feature enhancement start...

Provide an interface so users can create/edit/remove snippets.
parent 27bab4e8
<?xml version="1.0" encoding="utf-8"?>
<snippets>
<snippet>class <tab>name</tab>
{
public:
<tab>name</tab>() {}
};
</snippet>
<snippet description="derived from QObject">class <tab>name</tab> : public QObject
{
Q_OBJECT
public:
<tab>name</tab>() {}
virtual ~<tab>name</tab>() {}
};
</snippet>
<snippet description="derived from QWidget">class <tab>name</tab> : public QWidget
{
Q_OBJECT
public:
<tab>name</tab>() {}
virtual ~<tab>name</tab>() {}
};
</snippet>
<snippet>do {
} while (<tab>condition</tab>);
</snippet>
<snippet>else {
}
</snippet>
<snippet description="with if">else if (<tab>condition</tab>) {
}
</snippet>
<snippet>for (int <tab>var</tab> = 0; <tab>var</tab> &lt; <tab>total</tab>; ++<tab>var</tab>) {
}
</snippet>
<snippet>foreach (<tab>var</tab>, <tab>container</tab>) {
}
</snippet>
<snippet>if (<tab>condition</tab>) {
}
</snippet>
<snippet description="and else">if (<tab>condition</tab>) {
} else {
}
</snippet>
<snippet>namespace <tab>name</tab> {
}
</snippet>
<snippet>template &lt;typename <tab>T</tab>&gt;
class <tab>name</tab>
{
public:
<tab>name</tab>() {}
};
</snippet>
<snippet>try {
} catch (...) {
}
</snippet>
<snippet>using namespace <tab>name</tab>;
</snippet>
<snippet>while (<tab>condition</tab>) {
}
</snippet>
</snippets>
<?xml version="1.0" encoding="utf-8"?>
<snippets>
<snippet>property <tab>type</tab> <tab>name</tab>: <tab>value</tab>
</snippet>
<snippet>Item {
id: <tab>name</tab>
}
</snippet>
<snippet>BorderImage {
id: <tab>name</tab>
source: "<tab>file</tab>"
width: <tab>100</tab>; height: <tab>100</tab>
border.left: <tab>5</tab>; border.top: <tab>5</tab>
border.right: <tab>5</tab>; border.bottom: <tab>5</tab>
}
</snippet>
<snippet>Image {
id: <tab>name</tab>
source: "<tab>file</tab>"
}
</snippet>
<snippet>Text {
id: <tab>name</tab>
text: "<tab>text</tab>"
}
</snippet>
<snippet>states: [
State {
name: "<tab>name</tab>"
PropertyChanges {
target: <tab>name</tab>
<tab/>
}
}
]
</snippet>
<snippet>State {
name: "<tab>name</tab>"
PropertyChanges {
target: <tab>name</tab>
<tab/>
}
}
</snippet>
<snippet>transitions: [
Transition {
from: "<tab>name</tab>"
to: "<tab>name</tab>"
<tab/>
}
]
</snippet>
<snippet>Transition {
from: "<tab>name</tab>"
to: "<tab>name</tab>"
<tab/>
}
</snippet>
<snippet>PropertyChanges {
target: <tab>name</tab>
<tab/>
}
</snippet>
<snippet description="with targets">NumberAnimation { targets: [<tab>name</tab>]; properties: "<tab>name</tab>"; duration: <tab>200</tab> }
</snippet>
<snippet description="with target">NumberAnimation { target: <tab>name</tab>; property: "<tab>name</tab>"; to: <tab>value</tab>; duration: <tab>200</tab> }
</snippet>
<snippet description="with targets">PropertyAction { targets: [<tab>name</tab>]; properties: "<tab>name</tab>" }
</snippet>
<snippet description="with target">PropertyAction { target: <tab>name</tab>; property: "<tab>name</tab>"; value: <tab>value</tab> }
</snippet>
<snippet>PauseAnimation { duration: <tab>200</tab> }
</snippet>
<snippet>ColorAnimation { from: <tab>"white"</tab>; to: <tab>"black"</tab>; duration: <tab>200</tab> }
</snippet>
</snippets>
......@@ -25,7 +25,6 @@ isEmpty(vcproj) {
DATA_DIRS = \
examplebrowser \
snippets \
templates \
designer \
schemes \
......
......@@ -1735,22 +1735,7 @@ void CPPEditor::setFontSettings(const TextEditor::FontSettings &fs)
if (!highlighter)
return;
static QVector<QString> categories;
if (categories.isEmpty()) {
categories << QLatin1String(TextEditor::Constants::C_NUMBER)
<< QLatin1String(TextEditor::Constants::C_STRING)
<< QLatin1String(TextEditor::Constants::C_TYPE)
<< QLatin1String(TextEditor::Constants::C_KEYWORD)
<< QLatin1String(TextEditor::Constants::C_OPERATOR)
<< QLatin1String(TextEditor::Constants::C_PREPROCESSOR)
<< QLatin1String(TextEditor::Constants::C_LABEL)
<< QLatin1String(TextEditor::Constants::C_COMMENT)
<< QLatin1String(TextEditor::Constants::C_DOXYGEN_COMMENT)
<< QLatin1String(TextEditor::Constants::C_DOXYGEN_TAG)
<< QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE);
}
const QVector<QTextCharFormat> formats = fs.toTextCharFormats(categories);
const QVector<QTextCharFormat> formats = fs.toTextCharFormats(highlighterFormatCategories());
highlighter->setFormats(formats.constBegin(), formats.constEnd());
m_occurrencesFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_OCCURRENCES));
......@@ -2175,4 +2160,23 @@ QModelIndex CPPEditor::indexForPosition(int line, int column, const QModelIndex
return lastIndex;
}
QVector<QString> CPPEditor::highlighterFormatCategories()
{
static QVector<QString> categories;
if (categories.isEmpty()) {
categories << QLatin1String(TextEditor::Constants::C_NUMBER)
<< QLatin1String(TextEditor::Constants::C_STRING)
<< QLatin1String(TextEditor::Constants::C_TYPE)
<< QLatin1String(TextEditor::Constants::C_KEYWORD)
<< QLatin1String(TextEditor::Constants::C_OPERATOR)
<< QLatin1String(TextEditor::Constants::C_PREPROCESSOR)
<< QLatin1String(TextEditor::Constants::C_LABEL)
<< QLatin1String(TextEditor::Constants::C_COMMENT)
<< QLatin1String(TextEditor::Constants::C_DOXYGEN_COMMENT)
<< QLatin1String(TextEditor::Constants::C_DOXYGEN_TAG)
<< QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE);
}
return categories;
}
#include "cppeditor.moc"
......@@ -43,6 +43,7 @@
#include <QtCore/QWaitCondition>
#include <QtCore/QFutureWatcher>
#include <QtCore/QModelIndex>
#include <QtCore/QVector>
QT_BEGIN_NAMESPACE
class QComboBox;
......@@ -184,6 +185,8 @@ public:
static Link linkToSymbol(CPlusPlus::Symbol *symbol);
static QVector<QString> highlighterFormatCategories();
Q_SIGNALS:
void outlineModelIndexChanged(const QModelIndex &index);
......
......@@ -25,7 +25,9 @@ HEADERS += cppplugin.h \
cppquickfixcollector.h \
cppqtstyleindenter.h \
cppautocompleter.h \
cppcompleteswitch.h
cppcompleteswitch.h \
cppsnippeteditordecorator.h
SOURCES += cppplugin.cpp \
cppeditor.cpp \
cpphighlighter.cpp \
......@@ -44,6 +46,8 @@ SOURCES += cppplugin.cpp \
cppquickfixcollector.cpp \
cppqtstyleindenter.cpp \
cppautocompleter.cpp \
cppcompleteswitch.cpp
cppcompleteswitch.cpp \
cppsnippeteditordecorator.cpp
RESOURCES += cppeditor.qrc
OTHER_FILES += CppEditor.mimetypes.xml
......@@ -38,6 +38,7 @@
#include "cppoutline.h"
#include "cppquickfixcollector.h"
#include "cpptypehierarchy.h"
#include "cppsnippeteditordecorator.h"
#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
......@@ -211,6 +212,7 @@ bool CppPlugin::initialize(const QStringList & /*arguments*/, QString *errorMess
addAutoReleasedObject(new CppHoverHandler);
addAutoReleasedObject(new CppOutlineWidgetFactory);
addAutoReleasedObject(new CppTypeHierarchyFactory);
addAutoReleasedObject(new CppSnippetEditorDecorator);
m_quickFixCollector = new CppQuickFixCollector;
addAutoReleasedObject(m_quickFixCollector);
......
/**************************************************************************
**
** 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 "cppsnippeteditordecorator.h"
#include "cpphighlighter.h"
#include "cppeditor.h"
#include <texteditor/texteditorsettings.h>
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/snippets/snippeteditor.h>
using namespace CppEditor;
using namespace Internal;
CppSnippetEditorDecorator::CppSnippetEditorDecorator() :
TextEditor::ISnippetEditorDecorator()
{}
CppSnippetEditorDecorator::~CppSnippetEditorDecorator()
{}
bool CppSnippetEditorDecorator::supports(TextEditor::Snippet::Group group) const
{
if (group == TextEditor::Snippet::Cpp)
return true;
return false;
}
void CppSnippetEditorDecorator::apply(TextEditor::SnippetEditor *editor) const
{
CppHighlighter *highlighter = new CppHighlighter;
const TextEditor::FontSettings &fs = TextEditor::TextEditorSettings::instance()->fontSettings();
const QVector<QTextCharFormat> &formats =
fs.toTextCharFormats(CPPEditor::highlighterFormatCategories());
highlighter->setFormats(formats.constBegin(), formats.constEnd());
editor->installSyntaxHighlighter(highlighter);
}
/**************************************************************************
**
** 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 CPPSNIPPETEDITORDECORATOR_H
#define CPPSNIPPETEDITORDECORATOR_H
#include <texteditor/snippets/isnippeteditordecorator.h>
namespace CppEditor {
namespace Internal {
class CppSnippetEditorDecorator : public TextEditor::ISnippetEditorDecorator
{
public:
CppSnippetEditorDecorator();
virtual ~CppSnippetEditorDecorator();
public:
virtual bool supports(TextEditor::Snippet::Group group) const;
virtual void apply(TextEditor::SnippetEditor *editor) const;
};
} // Internal
} // CppEditor
#endif // CPPSNIPPETEDITORDECORATOR_H
......@@ -59,6 +59,7 @@
#include <texteditor/itexteditor.h>
#include <texteditor/itexteditable.h>
#include <texteditor/basetexteditor.h>
#include <texteditor/snippets/snippet.h>
#include <projectexplorer/projectexplorer.h>
#include <utils/faketooltip.h>
......@@ -462,7 +463,8 @@ CppCodeCompletion::CppCodeCompletion(CppModelManager *manager)
m_automaticCompletion(false),
m_completionOperator(T_EOF_SYMBOL),
m_objcEnabled(true),
m_snippetsParser(Core::ICore::instance()->resourcePath() + QLatin1String("/snippets/cpp.xml"))
m_snippetProvider(TextEditor::Snippet::Cpp,
QIcon(QLatin1String(":/texteditor/images/snippet.png")))
{
}
......@@ -751,12 +753,12 @@ void CppCodeCompletion::completeObjCMsgSend(ClassOrNamespace *binding,
Symbol *arg = method->argumentAt(i);
text += selectorName->nameAt(i)->identifier()->chars();
text += QLatin1Char(':');
text += QChar::ObjectReplacementCharacter;
text += TextEditor::Snippet::kVariableDelimiter;
text += QLatin1Char('(');
text += oo(arg->type());
text += QLatin1Char(')');
text += oo(arg->name());
text += QChar::ObjectReplacementCharacter;
text += TextEditor::Snippet::kVariableDelimiter;
}
} else {
text = selectorName->identifier()->chars();
......@@ -2058,8 +2060,7 @@ bool CppCodeCompletion::objcKeywordsWanted() const
void CppCodeCompletion::addSnippets()
{
static const QIcon icon(QLatin1String(":/texteditor/images/snippet.png"));
m_completions.append(m_snippetsParser.execute(this, icon));
m_completions.append(m_snippetProvider.getSnippets(this));
}
#include "cppcodecompletion.moc"
......@@ -37,7 +37,7 @@
#include <cplusplus/TypeOfExpression.h>
#include <texteditor/icompletioncollector.h>
#include <texteditor/snippetsparser.h>
#include <texteditor/snippets/snippetprovider.h>
#include <QtCore/QObject>
#include <QtCore/QPointer>
......@@ -153,7 +153,7 @@ private:
unsigned m_completionOperator;
bool m_objcEnabled;
TextEditor::SnippetsParser m_snippetsParser;
TextEditor::SnippetProvider m_snippetProvider;
CPlusPlus::Icons m_icons;
CPlusPlus::Overview overview;
......
......@@ -487,7 +487,7 @@ CodeCompletion::CodeCompletion(ModelManagerInterface *modelManager, QObject *par
m_editor(0),
m_startPosition(0),
m_restartCompletion(false),
m_snippetsParser(Core::ICore::instance()->resourcePath() + QLatin1String("/snippets/qml.xml"))
m_snippetProvider(TextEditor::Snippet::Qml, iconForColor(Qt::red), SnippetOrder)
{
Q_ASSERT(modelManager);
}
......@@ -952,7 +952,7 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
}
if (isQmlFile && (completionOperator.isNull() || completionOperator.isSpace() || isDelimiter(completionOperator))) {
m_completions.append(m_snippetsParser.execute(this, iconForColor(Qt::red), SnippetOrder));
m_completions.append(m_snippetProvider.getSnippets(this));
}
if (! m_completions.isEmpty())
......
......@@ -32,7 +32,7 @@
#include <qmljs/qmljsdocument.h>
#include <texteditor/icompletioncollector.h>
#include <texteditor/snippetsparser.h>
#include <texteditor/snippets/snippetprovider.h>
#include <QtCore/QDateTime>
#include <QtCore/QPointer>
......@@ -97,7 +97,7 @@ private:
TextEditor::ITextEditable *m_editor;
int m_startPosition;
bool m_restartCompletion;
TextEditor::SnippetsParser m_snippetsParser;
TextEditor::SnippetProvider m_snippetProvider;
QList<TextEditor::CompletionItem> m_completions;
QPointer<FunctionArgumentWidget> m_functionArgumentWidget;
};
......
......@@ -1187,27 +1187,7 @@ void QmlJSTextEditor::setFontSettings(const TextEditor::FontSettings &fs)
if (!highlighter)
return;
/*
NumberFormat,
StringFormat,
TypeFormat,
KeywordFormat,
LabelFormat,
CommentFormat,
VisualWhitespace,
*/
static QVector<QString> categories;
if (categories.isEmpty()) {
categories << QLatin1String(TextEditor::Constants::C_NUMBER)
<< QLatin1String(TextEditor::Constants::C_STRING)
<< QLatin1String(TextEditor::Constants::C_TYPE)
<< QLatin1String(TextEditor::Constants::C_KEYWORD)
<< QLatin1String(TextEditor::Constants::C_FIELD)
<< QLatin1String(TextEditor::Constants::C_COMMENT)
<< QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE);
}
highlighter->setFormats(fs.toTextCharFormats(categories));
highlighter->setFormats(fs.toTextCharFormats(highlighterFormatCategories()));
highlighter->rehighlight();
m_occurrencesFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_OCCURRENCES));
......@@ -1571,6 +1551,30 @@ bool QmlJSTextEditor::hideContextPane()
return b;
}
QVector<QString> QmlJSTextEditor::highlighterFormatCategories()
{
/*
NumberFormat,
StringFormat,
TypeFormat,
KeywordFormat,
LabelFormat,
CommentFormat,
VisualWhitespace,
*/
static QVector<QString> categories;
if (categories.isEmpty()) {
categories << QLatin1String(TextEditor::Constants::C_NUMBER)
<< QLatin1String(TextEditor::Constants::C_STRING)
<< QLatin1String(TextEditor::Constants::C_TYPE)
<< QLatin1String(TextEditor::Constants::C_KEYWORD)
<< QLatin1String(TextEditor::Constants::C_FIELD)
<< QLatin1String(TextEditor::Constants::C_COMMENT)
<< QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE);
}
return categories;
}
SemanticHighlighterSource QmlJSTextEditor::currentSource(bool force)
{
int line = 0, column = 0;
......
......@@ -155,6 +155,8 @@ public:
void renameId(const QString &oldId, const QString &newId);
static QVector<QString> highlighterFormatCategories();
public slots:
void followSymbolUnderCursor();
void findUsages();
......
......@@ -34,7 +34,8 @@ HEADERS += \
qmljssemantichighlighter.h \
qmljsindenter.h \
qmljsautocompleter.h \
jsfilewizard.h
jsfilewizard.h \
qmljssnippeteditordecorator.h
SOURCES += \
qmljscodecompletion.cpp \
......@@ -62,7 +63,8 @@ SOURCES += \
qmljssemantichighlighter.cpp \
qmljsindenter.cpp \
qmljsautocompleter.cpp \
jsfilewizard.cpp
jsfilewizard.cpp \
qmljssnippeteditordecorator.cpp
RESOURCES += qmljseditor.qrc
OTHER_FILES += QmlJSEditor.mimetypes.xml
......
......@@ -39,6 +39,7 @@
#include "qmljsoutline.h"
#include "qmljspreviewrunner.h"
#include "qmljsquickfix.h"
#include "qmljssnippeteditordecorator.h"
#include "qmltaskmanager.h"
#include "quicktoolbar.h"
#include "quicktoolbarsettingspage.h"
......@@ -128,6 +129,7 @@ bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *e
return false;
m_modelManager = QmlJS::ModelManagerInterface::instance();
addAutoReleasedObject(new QmlJSSnippetEditorDecorator);
Core::Context context(QmlJSEditor::Constants::C_QMLJSEDITOR_ID);
......
/**************************************************************************
**
** 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 "qmljssnippeteditordecorator.h"
#include "qmljshighlighter.h"
#include "qmljseditor.h"
#include <texteditor/texteditorsettings.h>
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/snippets/snippeteditor.h>
using namespace QmlJSEditor;
using namespace Internal;
QmlJSSnippetEditorDecorator::QmlJSSnippetEditorDecorator() :
TextEditor::ISnippetEditorDecorator()
{}
QmlJSSnippetEditorDecorator::~QmlJSSnippetEditorDecorator()
{}
bool QmlJSSnippetEditorDecorator::supports(TextEditor::Snippet::Group group) const
{
if (group == TextEditor::Snippet::Qml)
return true;
return false;
}
void QmlJSSnippetEditorDecorator::apply(TextEditor::SnippetEditor *editor) const
{
Highlighter *highlighter = new Highlighter;
const TextEditor::FontSettings &fs = TextEditor::TextEditorSettings::instance()->fontSettings();
highlighter->setFormats(fs.toTextCharFormats(QmlJSTextEditor::highlighterFormatCategories()));
editor->installSyntaxHighlighter(highlighter);
}
/**************************************************************************
**
** This file is part of Qt Creator
**