Initial import

This commit is contained in:
con
2008-12-02 12:01:29 +01:00
commit 05c35356ab
1675 changed files with 229938 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
<plugin name="Snippets" version="0.9.1" compatVersion="0.9.1">
<vendor>Nokia Corporation</vendor>
<copyright>(C) 2008 Nokia Corporation</copyright>
<license>Nokia Technology Preview License Agreement</license>
<description>Code snippet plugin.</description>
<url>http://www.trolltech.com/</url>
<dependencyList>
<dependency name="Core" version="0.9.1"/>
<dependency name="TextEditor" version="0.9.1"/>
<dependency name="ProjectExplorer" version="0.9.1"/>
</dependencyList>
</plugin>

Binary file not shown.

After

Width:  |  Height:  |  Size: 862 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 931 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 452 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 908 B

View File

@@ -0,0 +1,134 @@
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
#include <QtCore/QDebug>
#include <QtGui/QApplication>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QHBoxLayout>
#include <QtGui/QBitmap>
#include <QtGui/QPainter>
#include <QtGui/QResizeEvent>
#include "inputwidget.h"
using namespace Snippets::Internal;
InputWidget::InputWidget(const QString &text, const QString &value)
: QFrame(0, Qt::Popup)
{
setAttribute(Qt::WA_DeleteOnClose);
m_label = new QLabel();
m_label->setTextFormat(Qt::RichText);
m_label->setText(text);
m_lineEdit = new QLineEdit();
m_lineEdit->setText(value);
m_lineEdit->setSelection(0, value.length());
qApp->installEventFilter(this);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(m_label);
layout->addWidget(m_lineEdit);
layout->setMargin(3);
setLayout(layout);
ensurePolished();
setAutoFillBackground(false);
}
void InputWidget::resizeEvent(QResizeEvent *event)
{
int height = event->size().height();
int width = event->size().width();
qDebug() << event->size();
QPalette pal = palette();
QLinearGradient bg(0,0,0,height);
bg.setColorAt(0, QColor(195,195,255));
bg.setColorAt(1, QColor(230,230,255));
pal.setBrush(QPalette::Background, QBrush(bg));
setPalette(pal);
QBitmap bm(width, height);
bm.fill(Qt::color0);
QPainter p(&bm);
p.setBrush(QBrush(Qt::color1, Qt::SolidPattern));
p.setPen(Qt::color1);
int rw = (25 * height) / width;
p.drawRoundRect(0,0,width,height, rw, 25);
setMask(bm);
}
void InputWidget::showInputWidget(const QPoint &position)
{
move(position);
show();
m_lineEdit->setFocus();
}
bool InputWidget::eventFilter(QObject *o, QEvent *e)
{
if (o != m_lineEdit) {
switch (e->type()) {
case QEvent::MouseButtonPress:
case QEvent::MouseButtonDblClick:
closeInputWidget(true);
default:
break;
}
} else if (e->type() == QEvent::KeyPress) {
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
switch (ke->key()) {
case Qt::Key_Escape:
qDebug() << "Escape";
closeInputWidget(true);
break;
case Qt::Key_Enter:
case Qt::Key_Return:
qDebug() << "Enter";
closeInputWidget(false);
return true;
}
}
return false;
}
void InputWidget::closeInputWidget(bool cancel)
{
emit finished(cancel, m_lineEdit->text());
close();
}

View File

@@ -0,0 +1,70 @@
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
#ifndef INPUTWIDGET_H
#define INPUTWIDGET_H
#include <QtGui/QFrame>
QT_BEGIN_NAMESPACE
class QLabel;
class QLineEdit;
QT_END_NAMESPACE
namespace Snippets {
namespace Internal {
class InputWidget : public QFrame
{
Q_OBJECT
public:
InputWidget(const QString &text, const QString &value);
void showInputWidget(const QPoint &position);
signals:
void finished(bool canceled, const QString &value);
protected:
bool eventFilter(QObject *, QEvent *);
void resizeEvent(QResizeEvent *event);
private:
void closeInputWidget(bool cancel);
QLabel *m_label;
QLineEdit *m_lineEdit;
};
} //namespace Internal
} //namespace Snippets
#endif // INPUTWIDGET_H

View File

@@ -0,0 +1,24 @@
TEMPLATE = lib
TARGET = Snippets
QT += xml
include(../../qworkbenchplugin.pri)
include(../../plugins/projectexplorer/projectexplorer.pri)
include(../../plugins/coreplugin/coreplugin.pri)
include(../../plugins/texteditor/texteditor.pri)
INCLUDEPATH += ../projectexplorer
HEADERS += snippetsplugin.h \
snippetswindow.h \
snippetspec.h \
snippetscompletion.h \
inputwidget.h
SOURCES += snippetsplugin.cpp \
snippetswindow.cpp \
snippetspec.cpp \
snippetscompletion.cpp \
inputwidget.cpp
RESOURCES += snippets.qrc

View File

@@ -0,0 +1,8 @@
<RCC>
<qresource prefix="/snippets" >
<file>images/file.png</file>
<file>images/dir.png</file>
<file>images/diropen.png</file>
<file>images/snippets.png</file>
</qresource>
</RCC>

View File

@@ -0,0 +1,159 @@
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
#include "snippetscompletion.h"
#include "snippetswindow.h"
#include "snippetspec.h"
#include "snippetsplugin.h"
#include <texteditor/itexteditable.h>
#include <QtCore/QDebug>
#include <QtCore/QMap>
#include <QtGui/QAction>
#include <QtGui/QKeyEvent>
using namespace Snippets::Internal;
const QIcon SnippetsCompletion::m_fileIcon = QIcon(":/snippets/images/file.png");
SnippetsCompletion::SnippetsCompletion(QObject *parent)
: ICompletionCollector(parent)
{
m_core = SnippetsPlugin::core();
m_snippetsWnd = SnippetsPlugin::snippetsWindow();
updateCompletions();
}
SnippetsCompletion::~SnippetsCompletion()
{
qDeleteAll(m_autoCompletions.values());
m_autoCompletions.clear();
}
void SnippetsCompletion::updateCompletions()
{
qDeleteAll(m_autoCompletions.values());
m_autoCompletions.clear();
int index = 0;
foreach (SnippetSpec *spec, m_snippetsWnd->snippets()) {
if (!spec->completionShortcut().isEmpty()) {
TextEditor::CompletionItem *item = new TextEditor::CompletionItem;
item->m_key = spec->name();
item->m_collector = this;
item->m_index = index;
item->m_relevance = 0;
m_autoCompletions.insert(spec->completionShortcut(), item);
++index;
}
}
}
bool SnippetsCompletion::triggersCompletion(TextEditor::ITextEditable *editor)
{
QString currentWord = editor->textAt(editor->position() - 3, 3);
currentWord = currentWord.trimmed();
return currentWord.length() == 2 && m_autoCompletions.contains(currentWord) &&
!editor->characterAt(editor->position() - 1).isSpace();
}
int SnippetsCompletion::startCompletion(TextEditor::ITextEditable *editor)
{
m_editor = editor;
m_startPosition = findStartOfName(m_editor);
return m_startPosition;
}
void SnippetsCompletion::completions(QList<TextEditor::CompletionItem *> *completions)
{
const int length = m_editor->position() - m_startPosition;
if (length >= 2) {
QString key = m_editor->textAt(m_startPosition, length);
foreach (TextEditor::CompletionItem* item, m_autoCompletions.values()) {
if (item->m_key.startsWith(key, Qt::CaseInsensitive)) {
(*completions) << item;
}
}
}
}
QString SnippetsCompletion::text(TextEditor::CompletionItem *item) const
{
const SnippetSpec *spec = m_snippetsWnd->snippets().at(item->m_index);
return spec->name();
}
QString SnippetsCompletion::details(TextEditor::CompletionItem *item) const
{
const SnippetSpec *spec = m_snippetsWnd->snippets().at(item->m_index);
return spec->description();
}
QIcon SnippetsCompletion::icon(TextEditor::CompletionItem *) const
{
return m_fileIcon;
}
void SnippetsCompletion::complete(TextEditor::CompletionItem *item)
{
SnippetSpec *spec = m_snippetsWnd->snippets().at(item->m_index);
int length = m_editor->position() - m_startPosition;
m_editor->setCurPos(m_startPosition);
m_editor->remove(length);
m_snippetsWnd->insertSnippet(m_editor, spec);
}
bool SnippetsCompletion::partiallyComplete()
{
return false;
}
void SnippetsCompletion::cleanup()
{
}
int SnippetsCompletion::findStartOfName(const TextEditor::ITextEditor *editor)
{
int pos = editor->position() - 1;
QChar chr = editor->characterAt(pos);
// Skip to the start of a name
while (!chr.isSpace() && !chr.isNull())
chr = editor->characterAt(--pos);
return pos + 1;
}

View File

@@ -0,0 +1,99 @@
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
#ifndef SNIPPETSCOMPLETION_H
#define SNIPPETSCOMPLETION_H
#include <QtCore/QObject>
#include <QtCore/QMap>
#include <QtCore/QDir>
#include <QtGui/QIcon>
#include <texteditor/icompletioncollector.h>
namespace Core {
class ICore;
}
namespace TextEditor {
class ITextEditable;
class ITextEditor;
}
namespace Snippets {
namespace Internal {
class SnippetsWindow;
class SnippetSpec;
class SnippetsCompletion : public TextEditor::ICompletionCollector
{
Q_OBJECT
public:
SnippetsCompletion(QObject *parent);
~SnippetsCompletion();
// ICompletionCollector
bool triggersCompletion(TextEditor::ITextEditable *editor);
int startCompletion(TextEditor::ITextEditable *editor);
void completions(QList<TextEditor::CompletionItem *> *completions);
QString text(TextEditor::CompletionItem *item) const;
QString details(TextEditor::CompletionItem *item) const;
QIcon icon(TextEditor::CompletionItem *item) const;
void complete(TextEditor::CompletionItem *item);
bool partiallyComplete();
void cleanup();
private slots:
void updateCompletions();
private:
static int findStartOfName(const TextEditor::ITextEditor *editor);
TextEditor::ITextEditable *m_editor;
int m_startPosition; // Position of the cursor from which completion started
SnippetsWindow *m_snippetsWnd;
Core::ICore *m_core;
QMultiMap<QString, TextEditor::CompletionItem *> m_autoCompletions;
static const QIcon m_fileIcon;
};
} // namespace Internal
} // namespace Snippets
#endif // SNIPPETSCOMPLETION_H

View File

@@ -0,0 +1,100 @@
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
#include "snippetspec.h"
#include "persistentsettings.h"
using namespace Snippets::Internal;
using ProjectExplorer::PersistentSettingsReader;
bool SnippetSpec::load(const QString &fileName)
{
PersistentSettingsReader reader;
if (!reader.load(fileName))
return false;
m_contents = reader.restoreValue(QLatin1String("Contents")).toString();
m_name = reader.restoreValue(QLatin1String("Name")).toString();
m_description = reader.restoreValue(QLatin1String("Description")).toString();
m_category = reader.restoreValue(QLatin1String("Category")).toString();
m_completionShortcut = reader.restoreValue(QLatin1String("Shortcut")).toString();
QMap<QString, QVariant> temp = reader.restoreValue(QLatin1String("Arguments")).toMap();
QMap<QString, QVariant>::const_iterator it, end;
end = temp.constEnd();
for (it = temp.constBegin(); it != end; ++it) {
m_argumentDescription.insert( it.key().toInt(), it.value().toString());
}
temp = reader.restoreValue(QLatin1String("ArgumentDefaults")).toMap();
end = temp.constEnd();
for (it = temp.constBegin(); it != end; ++it) {
m_argumentDefault.insert(it.key().toInt(), it.value().toString());
}
return true;
}
QString SnippetSpec::contents() const
{
return m_contents;
}
QString SnippetSpec::name() const
{
return m_name;
}
QString SnippetSpec::description() const
{
return m_description;
}
QString SnippetSpec::category() const
{
return m_category;
}
QString SnippetSpec::completionShortcut() const
{
return m_completionShortcut;
}
QString SnippetSpec::argumentDescription(int id) const
{
return m_argumentDescription.value(id);
}
QString SnippetSpec::argumentDefault(int id) const
{
return m_argumentDefault.value(id);
}

View File

@@ -0,0 +1,68 @@
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
#ifndef SNIPPETSPEC_H
#define SNIPPETSPEC_H
#include <QtCore/QMap>
#include <QtCore/QString>
namespace Snippets {
namespace Internal {
class SnippetSpec
{
public:
bool load(const QString &fileName);
QString contents() const;
QString name() const;
QString description() const;
QString category() const;
QString completionShortcut() const;
QString argumentDescription(int id) const;
QString argumentDefault(int id) const;
private:
QString m_contents;
QString m_name;
QString m_description;
QString m_category;
QString m_completionShortcut;
QMap<int, QString> m_argumentDescription;
QMap<int, QString> m_argumentDefault;
};
} //namespace Internal
} //namespace Snippets
#endif // SNIPPETSPEC_H

View File

@@ -0,0 +1,118 @@
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
#include "snippetswindow.h"
#include "snippetscompletion.h"
#include "snippetsplugin.h"
#include "snippetspec.h"
#include <QtCore/qplugin.h>
#include <QtCore/QDebug>
#include <QtGui/QShortcut>
#include <QtGui/QApplication>
#include <extensionsystem/pluginmanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/uniqueidmanager.h>
#include <coreplugin/actionmanager/actionmanagerinterface.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/CoreTools>
#include <texteditor/itexteditable.h>
#include <texteditor/texteditorconstants.h>
using namespace Snippets::Internal;
SnippetsPlugin *SnippetsPlugin::m_instance = 0;
SnippetsPlugin::SnippetsPlugin()
{
m_instance = this;
}
SnippetsPlugin::~SnippetsPlugin()
{
removeObject(m_snippetsCompletion);
delete m_snippetsCompletion;
}
void SnippetsPlugin::extensionsInitialized()
{
}
bool SnippetsPlugin::initialize(const QStringList & /*arguments*/, QString *)
{
m_core = ExtensionSystem::PluginManager::instance()->getObject<Core::ICore>();
Core::ActionManagerInterface *am = m_core->actionManager();
QList<int> context;
context << m_core->uniqueIDManager()->uniqueIdentifier(TextEditor::Constants::C_TEXTEDITOR);
m_snippetWnd = new SnippetsWindow();
addAutoReleasedObject(new Core::BaseView("Snippets.SnippetsTree",
m_snippetWnd,
QList<int>() << m_core->uniqueIDManager()->uniqueIdentifier(QLatin1String("Snippets Window"))
<< m_core->uniqueIDManager()->uniqueIdentifier(TextEditor::Constants::C_TEXTEDITOR),
Qt::RightDockWidgetArea));
m_snippetsCompletion = new SnippetsCompletion(this);
addObject(m_snippetsCompletion);
foreach (SnippetSpec *snippet, m_snippetWnd->snippets()) {
QShortcut *sc = new QShortcut(m_snippetWnd);
Core::ICommand *cmd = am->registerShortcut(sc, simplifySnippetName(snippet), context);
cmd->setCategory(tr("Snippets"));
connect(sc, SIGNAL(activated()), this, SLOT(snippetActivated()));
m_shortcuts.insert(sc, snippet);
}
return true;
}
QString SnippetsPlugin::simplifySnippetName(SnippetSpec *snippet) const
{
return QLatin1String("Snippets.")
+ snippet->category().simplified().replace(QLatin1String(" "), QLatin1String(""))
+ QLatin1Char('.')
+ snippet->name().simplified().replace(QLatin1String(" "), QLatin1String(""));
}
void SnippetsPlugin::snippetActivated()
{
SnippetSpec *snippet = m_shortcuts.value(sender());
if (snippet && m_core->editorManager()->currentEditor()) {
TextEditor::ITextEditable *te =
qobject_cast<TextEditor::ITextEditable *>(
m_core->editorManager()->currentEditor());
m_snippetWnd->insertSnippet(te, snippet);
}
}
Q_EXPORT_PLUGIN(SnippetsPlugin)

View File

@@ -0,0 +1,90 @@
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
#ifndef SNIPPETS_H
#define SNIPPETS_H
#include <QtCore/QMap>
#include <QtCore/QObject>
#include <QtGui/QShortcut>
#include <extensionsystem/iplugin.h>
namespace Core {
class ICore;
struct Application;
}
namespace Snippets {
namespace Internal {
class SnippetsWindow;
class SnippetSpec;
class SnippetsCompletion;
class SnippetsPlugin : public ExtensionSystem::IPlugin
{
Q_OBJECT
public:
SnippetsPlugin();
virtual ~SnippetsPlugin();
static SnippetsPlugin *instance() { return m_instance; }
static SnippetsWindow *snippetsWindow() { return m_instance->m_snippetWnd; }
static Core::ICore *core() { return m_instance->m_core; }
bool initialize(const QStringList &arguments, QString *error_message);
void extensionsInitialized();
private slots:
void snippetActivated();
private:
static SnippetsPlugin *m_instance;
QString simplifySnippetName(SnippetSpec *snippet) const;
Core::ICore *m_core;
SnippetsCompletion *m_snippetsCompletion;
SnippetsWindow *m_snippetWnd;
int m_textContext;
int m_snippetsMode;
QShortcut *m_exitShortcut;
QShortcut *m_modeShortcut;
QMap<QObject*, SnippetSpec*> m_shortcuts;
};
} // namespace Internal
} // namespace Snippets
#endif // SNIPPETS_H

View File

@@ -0,0 +1,434 @@
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
#include "snippetswindow.h"
#include "snippetspec.h"
#include "inputwidget.h"
#include "snippetsplugin.h"
#include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h>
#include <texteditor/itexteditable.h>
#include <texteditor/itexteditor.h>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtGui/QDragEnterEvent>
#include <QtGui/QApplication>
#include <QtGui/QLabel>
#include <QtCore/QMimeData>
#include <QtGui/QHeaderView>
using namespace Snippets::Internal;
const QIcon SnippetsWindow::m_fileIcon = QIcon(":/snippets/images/file.png");
const QIcon SnippetsWindow::m_dirIcon = QIcon(":/snippets/images/dir.png");
const QIcon SnippetsWindow::m_dirOpenIcon = QIcon(":/snippets/images/diropen.png");
Q_DECLARE_METATYPE(Snippets::Internal::SnippetSpec *)
SnippetsWindow::SnippetsWindow()
{
m_core = SnippetsPlugin::core();
setWindowTitle(tr("Snippets"));
setWindowIcon(QIcon(":/snippets/images/snippets.png"));
setOrientation(Qt::Vertical);
m_snippetsTree = new SnippetsTree(this);
addWidget(m_snippetsTree);
m_descLabel = new QLabel(this);
m_descLabel->setAlignment(Qt::AlignTop|Qt::AlignLeft);
m_descLabel->setFrameShape(QFrame::Panel);
m_descLabel->setFrameShadow(QFrame::Raised);
m_descLabel->setWordWrap(true);
addWidget(m_descLabel);
m_snippetsDir = QDir::home();
if (!initSnippetsDir())
setDisabled(true);
else {
QDir defaultDir(m_core->resourcePath() + QLatin1String("/snippets"));
if (defaultDir.exists())
initSnippets(defaultDir);
initSnippets(m_snippetsDir);
}
connect(m_snippetsTree, SIGNAL(itemCollapsed(QTreeWidgetItem *)),
this, SLOT(setClosedIcon(QTreeWidgetItem *)));
connect(m_snippetsTree, SIGNAL(itemExpanded(QTreeWidgetItem *)),
this, SLOT(setOpenIcon(QTreeWidgetItem *)));
connect(m_snippetsTree, SIGNAL(itemActivated(QTreeWidgetItem *, int)),
this, SLOT(activateSnippet(QTreeWidgetItem *, int)));
connect(m_snippetsTree, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
this, SLOT(updateDescription(QTreeWidgetItem *)));
}
SnippetsWindow::~SnippetsWindow()
{
qDeleteAll(m_snippets);
}
void SnippetsWindow::activateSnippet(QTreeWidgetItem *item, int column)
{
if (!item->parent())
return;
TextEditor::ITextEditable *editor = 0;
if (m_core->editorManager()->currentEditor())
editor = qobject_cast<TextEditor::ITextEditable *>(
m_core->editorManager()->currentEditor());
if (editor) {
SnippetSpec* spec = qVariantValue<SnippetSpec*>(item->data(0, Qt::UserRole));
insertSnippet(editor, spec);
}
Q_UNUSED(column);
}
const QList<SnippetSpec *> &SnippetsWindow::snippets() const
{
return m_snippets;
}
void SnippetsWindow::initSnippets(const QDir &dir)
{
QString name;
QString category;
QMap<QString, QTreeWidgetItem *> categories;
for (int i = 0; i < m_snippetsTree->topLevelItemCount(); ++i) {
categories.insert(m_snippetsTree->topLevelItem(i)->text(0),
m_snippetsTree->topLevelItem(i));
}
foreach (const QString &snippet, dir.entryList(QStringList("*.snp"))) {
SnippetSpec *spec = new SnippetSpec();
if (spec->load(dir.filePath(snippet))) {
if (!categories.contains(spec->category())) {
QTreeWidgetItem *citem = new QTreeWidgetItem(m_snippetsTree);
citem->setText(0, spec->category());
citem->setIcon(0, m_dirIcon);
categories.insert(spec->category(), citem);
}
QTreeWidgetItem *item = new QTreeWidgetItem(
categories.value(spec->category()));
item->setText(0, spec->name());
item->setIcon(0, m_fileIcon);
QVariant v;
qVariantSetValue<SnippetSpec *>(v, spec);
item->setData(0, Qt::UserRole, v);
m_snippets.append(spec);
}
}
}
QString SnippetsWindow::createUniqueFileName()
{
int fileNumber = 0;
QString baseName = "snippet";
while (m_snippetsDir.exists(baseName + QString::number(fileNumber) + ".snp")) {
++fileNumber;
}
return baseName + QString::number(fileNumber) + ".snp";
}
void SnippetsWindow::writeSnippet(const QMimeData *)
{
}
bool SnippetsWindow::initSnippetsDir()
{
if (!m_snippetsDir.exists(".qworkbench"))
m_snippetsDir.mkdir(".qworkbench");
if (!m_snippetsDir.cd(".qworkbench"))
return false;
if (!m_snippetsDir.exists("snippets"))
m_snippetsDir.mkdir("snippets");
return m_snippetsDir.cd("snippets");
}
void SnippetsWindow::getArguments()
{
QString contents = m_currentSnippet->contents();
int index = 0;
bool pc = false;
QString nrstr;
QSet<int> requiredArgs;
m_requiredArgs.clear();
m_args.clear();
while (index < contents.length()) {
QChar c = contents.at(index);
if (c == QLatin1Char('%')) {
pc = !pc;
} else if (pc) {
if (c.isNumber()) {
nrstr += c;
} else {
pc = false;
}
}
if (!pc && !nrstr.isEmpty()) {
requiredArgs << nrstr.toInt();
nrstr.clear();
}
++index;
}
m_requiredArgs = requiredArgs.toList();
m_requiredArgs.prepend(-1);
showInputWidget(false, QString());
}
void SnippetsWindow::showInputWidget(bool canceled, const QString &value)
{
if (canceled)
return;
TextEditor::ITextEditor *te = 0;
if (m_core->editorManager()->currentEditor())
te = qobject_cast<TextEditor::ITextEditor*>(
m_core->editorManager()->currentEditor());
int arg = m_requiredArgs.takeFirst();
if (arg != -1)
m_args << value;
if (!te || m_requiredArgs.isEmpty()) {
qDebug("replaceAndInsert");
replaceAndInsert();
} else {
QString desc = m_currentSnippet->argumentDescription(m_requiredArgs.first());
QString def = m_currentSnippet->argumentDefault(m_requiredArgs.first());
foreach(QString arg, m_args) {
desc = desc.arg(arg);
def = def.arg(arg);
}
InputWidget *iw = new InputWidget(desc, def);
connect(iw, SIGNAL(finished(bool, const QString &)),
this, SLOT(showInputWidget(bool, const QString &)));
iw->showInputWidget(te->cursorRect().bottomRight());
}
}
void SnippetsWindow::replaceAndInsert()
{
QString result;
QString keyWord;
int setAnchor = -1;
int setCursor = -1;
int selLength = 0;
//clean up selection
int startPos = m_currentEditor->position(TextEditor::ITextEditable::Anchor);
int endPos = m_currentEditor->position();
if (startPos < 0) {
startPos = endPos;
} else {
if (startPos > endPos) {
int tmp = startPos;
startPos = endPos;
endPos = tmp;
}
selLength = endPos - startPos;
}
//parse the contents
m_currentEditor->setCurPos(startPos);
QString editorIndent = getCurrentIndent(m_currentEditor);
QString content = m_currentSnippet->contents();
foreach (const QString &arg, m_args) {
content = content.arg(arg);
}
int startOfKey = -1;
for (int i = 0; i<content.length(); ++i) {
//handle windows,mac and linux new lines...
if (content.at(i) == QLatin1Char('\n')) {
if ((i <= 0) || content.at(i-1) != QLatin1Char('\r'))
result += QLatin1Char('\n') + editorIndent;
continue;
} else if (content.at(i) == QLatin1Char('\r')) {
result += QLatin1Char('\n') + editorIndent;
continue;
}
if (content.at(i) == QChar('$')) {
if (startOfKey != -1) {
m_currentEditor->insert(result);
if (keyWord == QLatin1String("selection")) {
const QString &indent = indentOfString(content, i);
int selStartPos = m_currentEditor->position();
m_currentEditor->setCurPos(selStartPos + selLength);
insertIdents(m_currentEditor, indent, selStartPos, m_currentEditor->position());
} else if (keyWord == QLatin1String("anchor")) {
setAnchor = m_currentEditor->position();
} else if (keyWord == QLatin1String("cursor")) {
setCursor = m_currentEditor->position();
}
result.clear();
keyWord.clear();
startOfKey = -1;
} else {
startOfKey = i;
}
} else {
if (startOfKey != -1)
keyWord += content.at(i).toLower();
else
result += content.at(i);
}
}
m_currentEditor->insert(result);
if (setAnchor != -1) {
m_currentEditor->setCurPos(setAnchor);
m_currentEditor->select(setCursor);
} else if (setCursor != -1) {
m_currentEditor->setCurPos(setCursor);
}
}
void SnippetsWindow::insertSnippet(TextEditor::ITextEditable *editor, SnippetSpec *snippet)
{
m_currentEditor = editor;
m_currentSnippet = snippet;
getArguments();
}
QString SnippetsWindow::getCurrentIndent(TextEditor::ITextEditor *editor)
{
const int startPos = editor->position(TextEditor::ITextEditor::StartOfLine);
const int endPos = editor->position(TextEditor::ITextEditor::EndOfLine);
if (startPos < endPos)
return indentOfString(editor->textAt(startPos, endPos - startPos));
return QString();
}
void SnippetsWindow::insertIdents(TextEditor::ITextEditable *editor,
const QString &indent, int fromPos, int toPos)
{
int offset = 0;
const int startPos = editor->position();
editor->setCurPos(toPos);
int currentLinePos = editor->position(TextEditor::ITextEditor::StartOfLine);
while (currentLinePos > fromPos) {
editor->setCurPos(currentLinePos);
editor->insert(indent);
offset += indent.length();
editor->setCurPos(currentLinePos-1);
currentLinePos = editor->position(TextEditor::ITextEditor::StartOfLine);
}
editor->setCurPos(startPos + offset);
}
QString SnippetsWindow::indentOfString(const QString &str, int at)
{
QString result;
int startAt = at;
if (startAt < 0)
startAt = str.length() - 1;
// find start position
while (startAt >= 0 && str.at(startAt) != QChar('\n')
&& str.at(startAt) != QChar('\r')) --startAt;
for (int i = (startAt + 1); i < str.length(); ++i) {
if (str.at(i) == QChar(' ') || str.at(i) == QChar('\t'))
result += str.at(i);
else
break;
}
return result;
}
void SnippetsWindow::setOpenIcon(QTreeWidgetItem *item)
{
item->setIcon(0, m_dirOpenIcon);
}
void SnippetsWindow::setClosedIcon(QTreeWidgetItem *item)
{
item->setIcon(0, m_dirIcon);
}
void SnippetsWindow::updateDescription(QTreeWidgetItem *item)
{
const SnippetSpec* spec = qVariantValue<SnippetSpec*>(item->data(0, Qt::UserRole));
if (spec) {
m_descLabel->setText(QLatin1String("<b>") + spec->name() + QLatin1String("</b><br>")
+ spec->description());
} else {
m_descLabel->setText(QLatin1String("<b>") + item->text(0) + QLatin1String("</b><br>"));
}
}
SnippetsTree::SnippetsTree(QWidget *parent)
: QTreeWidget(parent)
{
setColumnCount(1);
header()->setVisible(false);
setAlternatingRowColors(true);
setAcceptDrops(true);
}
void SnippetsTree::dropEvent(QDropEvent *)
{
//writeSnippet(event->mimeData());
}
void SnippetsTree::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasText())
event->acceptProposedAction();
}
void SnippetsTree::dragMoveEvent(QDragMoveEvent *)
{
}

View File

@@ -0,0 +1,128 @@
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
#ifndef SNIPPETSWINDOW_H
#define SNIPPETSWINDOW_H
#include <QtCore/QDir>
#include <QtGui/QTreeWidget>
#include <QtGui/QSplitter>
#include <QtGui/QIcon>
QT_BEGIN_NAMESPACE
class QDir;
class QLabel;
QT_END_NAMESPACE
namespace Core {
class ICore;
}
namespace TextEditor {
class ITextEditable;
class ITextEditor;
}
namespace Snippets {
namespace Internal {
class SnippetSpec;
class SnippetsTree;
class InputWidget;
class SnippetsWindow : public QSplitter
{
Q_OBJECT
public:
SnippetsWindow();
~SnippetsWindow();
const QList<SnippetSpec *> &snippets() const;
void insertSnippet(TextEditor::ITextEditable *editor, SnippetSpec *snippet);
private slots:
void updateDescription(QTreeWidgetItem *item);
void activateSnippet(QTreeWidgetItem *item, int column);
void setOpenIcon(QTreeWidgetItem *item);
void setClosedIcon(QTreeWidgetItem *item);
void showInputWidget(bool canceled, const QString &value);
private:
void getArguments();
void replaceAndInsert();
QString indentOfString(const QString &str, int at = -1);
void insertIdents(TextEditor::ITextEditable *editor,
const QString &indent, int fromPos, int toPos);
QString getCurrentIndent(TextEditor::ITextEditor *editor);
QList<SnippetSpec *> m_snippets;
QString createUniqueFileName();
void writeSnippet(const QMimeData *mData);
bool initSnippetsDir();
void initSnippets(const QDir &dir);
QList<int> m_requiredArgs;
QStringList m_args;
SnippetSpec *m_currentSnippet;
TextEditor::ITextEditable *m_currentEditor;
Core::ICore *m_core;
QDir m_snippetsDir;
SnippetsTree *m_snippetsTree;
QLabel *m_descLabel;
static const QIcon m_fileIcon;
static const QIcon m_dirIcon;
static const QIcon m_dirOpenIcon;
};
class SnippetsTree : public QTreeWidget
{
Q_OBJECT
public:
SnippetsTree(QWidget *parent);
protected:
void dragMoveEvent(QDragMoveEvent * event);
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
};
} // namespace Internal
} // namespace Snippets
#endif // SNIPPETSWINDOW_H