Git commit bcbcb54167ecb32e174d59187795a16abaa2e7e2 by Alexander Dymo. Committed on 26/10/2012 at 14:07. Pushed by dymo into branch 'master'. Update ruby new class generation code to match the new API (changed after merging file templates branch) M +1 -1 CMakeLists.txt M +34 -57 codegen/rubynewclass.cpp M +12 -53 codegen/rubynewclass.h D +0 -91 codegen/rubyrefactoring.cpp D +0 -74 codegen/rubyrefactoring.h M +7 -17 rubylanguagesupport.cpp M +2 -9 rubylanguagesupport.h http://commits.kde.org/kdev-ruby/bcbcb54167ecb32e174d59187795a16abaa2e7e2 diff --git a/CMakeLists.txt b/CMakeLists.txt index 9bff8d1..e70234a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,13 +20,13 @@ add_subdirectory(duchain) add_subdirectory(navigation) add_subdirectory(completion) add_subdirectory(app_templates) +add_subdirectory(file_templates) = set(kdevrubylanguagesupport_PART_SRCS rubyparsejob.cpp rubyhighlighting.cpp rubylanguagesupport.cpp = - codegen/rubyrefactoring.cpp codegen/rubynewclass.cpp ) = diff --git a/codegen/rubynewclass.cpp b/codegen/rubynewclass.cpp index 9c5d532..d2eb2d3 100644 --- a/codegen/rubynewclass.cpp +++ b/codegen/rubynewclass.cpp @@ -18,14 +18,13 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ = +#include = #include #include #include #include -#include -#include -#include + #include = = @@ -33,84 +32,62 @@ namespace Ruby { using namespace KDevelop; = -RubyNewClass::RubyNewClass(KDevelop::ProjectBaseItem *parentItem) - : m_parentItem(parentItem) -{ - /* There's nothing to do here */ -} - -KDevelop::DocumentChangeSet RubyNewClass::generate() -{ - /* TODO */ - KDevelop::DocumentChangeSet changes; - return changes; -} - -KDevelop::StructureType::Ptr RubyNewClass::objectType() const -{ - /* TODO: fix this */ - return KDevelop::StructureType::Ptr(NULL); -} - -const KDevelop::ProjectBaseItem * RubyNewClass::parentItem() const -{ - return m_parentItem; -} - -RubyNewClassAssistant::RubyNewClassAssistant(QWidget *parent, RubyNewClass= *gen, KUrl baseUrl) - : KDevelop::CreateClassAssistant(parent, gen, baseUrl) -{ - setup(); -} - -KDevelop::ClassIdentifierPage* RubyNewClassAssistant::newIdentifierPage() +RubyClassHelper::RubyClassHelper() + :ICreateClassHelper() { - /* TODO: Under construction */ - return new KDevelop::ClassIdentifierPage(this); } = -KDevelop::OverridesPage* RubyNewClassAssistant::newOverridesPage() +RubyClassHelper::~RubyClassHelper() { - return new RubyOverridesPage(generator(), this); } = -RubyOverridesPage::RubyOverridesPage(KDevelop::ClassGenerator *gen, QWidge= t *parent) - : KDevelop::OverridesPage(gen, parent) +TemplateClassGenerator* RubyClassHelper::createGenerator(const KUrl& baseU= rl) { - /* There's nothing to do here */ + return new RubyNewClass(baseUrl); } = -void RubyOverridesPage::populateOverrideTree(const QList & baseList) +QList RubyClassHelper::defaultMethods(const QString& n= ame) const { - /* TODO: read the TODO from the RubyOverridesPage class declaration */ - OverridesPage::populateOverrideTree(baseList); - - // Generate ruby code with some default methods - const QString newClass =3D generator()->name(); KTemporaryFile file; file.setSuffix(".rb"); file.setAutoRemove(false); file.open(); QTextStream stream(&file); - stream << "class " << newClass << "\n" - << " def initialize\n end\n" << " def to_s\n end\nend"; + stream << "class " << name << "\n" + // constructor + << " def initialize\n end\n" + // serializer to string + << " def to_s\n end\nend"; file.close(); = - ReferencedTopDUContext context(DUChain::self()->waitForUpdate(IndexedS= tring(file.fileName()), - KDevelop::= TopDUContext::AllDeclarationsAndContexts)); + ReferencedTopDUContext context(DUChain::self()->waitForUpdate( + IndexedString(file.fileName()), + KDevelop::TopDUContext::AllDeclarationsAndContexts)); DUChainReadLocker lock; + + QList methods; + if (!context || !context->childContexts().size() =3D=3D 1) { kWarning() << "invalid context for generated ruby file with defaul= t methods" << file.fileName(); - file.remove(); - return; + } else { + foreach (Declaration* declaration, context->childContexts().first(= )->localDeclarations()) { + methods << DeclarationPointer(declaration); + } } = - QTreeWidgetItem *items =3D new QTreeWidgetItem(overrideTree(), QString= List() << i18n("Default methods")); - foreach (Declaration *decl, context->childContexts().first()->localDec= larations()) - OverridesPage::addPotentialOverride(items, DeclarationPointer(decl= )); - file.remove(); + return methods; } = + +RubyNewClass::RubyNewClass(KUrl url) + :TemplateClassGenerator(url) +{ } = +KDevelop::DocumentChangeSet RubyNewClass::generate() +{ + return TemplateClassGenerator::generate(); +} + +} diff --git a/codegen/rubynewclass.h b/codegen/rubynewclass.h index 6f274fd..ab2dc4a 100644 --- a/codegen/rubynewclass.h +++ b/codegen/rubynewclass.h @@ -22,73 +22,32 @@ #ifndef RUBY_NEWCLASS_H #define RUBY_NEWCLASS_H = +#include = -#include -#include -#include -#include - - -/* - * TODO: under construction - */ - +#include +#include +#include = = namespace Ruby { = -class RubyNewClass : public KDevelop::ClassGenerator -{ -public: - RubyNewClass(KDevelop::ProjectBaseItem* parentItem); - virtual KDevelop::DocumentChangeSet generate(); - - virtual KDevelop::StructureType::Ptr objectType() const; - - const KDevelop::ProjectBaseItem * parentItem() const; - -private: - KDevelop::ProjectBaseItem *m_parentItem; -}; - -class RubyNewClassAssistant : public KDevelop::CreateClassAssistant -{ +class RubyClassHelper: public KDevelop::ICreateClassHelper { public: - RubyNewClassAssistant(QWidget *parent, RubyNewClass *gen, KUrl baseUrl= =3D KUrl()); + RubyClassHelper(); + virtual ~RubyClassHelper(); = - virtual KDevelop::ClassIdentifierPage * newIdentifierPage(); - virtual KDevelop::OverridesPage * newOverridesPage(); + virtual KDevelop::TemplateClassGenerator* createGenerator(const KUrl& = baseUrl); + virtual QList< KDevelop::DeclarationPointer > defaultMethods(const QSt= ring& name) const; }; = -/* - * TODO: We need to subclass the IdentifierPage so we can implement the - * following ideas: - * -> In ruby multiple inheritance is not allowed. - * -> The user might want to add include's/extend's - */ - -/* - * TODO: The tree should be populated with the methods from the superclass= , the - * included/extended methods and the methods from the Class class. - */ -class RubyOverridesPage : public KDevelop::OverridesPage -{ - Q_OBJECT - +class RubyNewClass : public KDevelop::TemplateClassGenerator { public: - RubyOverridesPage(KDevelop::ClassGenerator *gen, QWidget *parent); + RubyNewClass(KUrl url); + virtual KDevelop::DocumentChangeSet generate(); = - virtual void populateOverrideTree(const QList & baseList); }; = -/* - * TODO: for the other pages: - * - In the license page it seems that there's only licenses with C++= comments. - * - In the output page, it makes no sense to output two files. - */ - - } = = diff --git a/codegen/rubyrefactoring.cpp b/codegen/rubyrefactoring.cpp deleted file mode 100644 index 82b4327..0000000 --- a/codegen/rubyrefactoring.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of KDevelop - * Copyright 2012 Miquel Sabat=C3=A9 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Library General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - - -// Qt + KDE -#include -#include -#include - -// Ruby -#include -#include - - -namespace Ruby -{ - -RubyRefactoring & RubyRefactoring::self() -{ - static RubyRefactoring rr; - return rr; -} - -void RubyRefactoring::doContextMenu(KDevelop::ContextMenuExtension &extens= ion, - KDevelop::Context *context) -{ - if (KDevelop::ProjectItemContext* projectContext =3D dynamic_cast(context)) { - if (projectContext->items().count() =3D=3D 1) { - KDevelop::ProjectBaseItem* item =3D projectContext->items().fi= rst(); - if (item->folder() || item->target()) { - QAction *action =3D new QAction(i18n("Create New Ruby Clas= s"), this); - action->setData(QVariant::fromValue(item)); - connect(action, SIGNAL(triggered(bool)), - this, SLOT(executeNewClassAction())); - extension.addAction(KDevelop::ContextMenuExtension::FileGr= oup, action); - } - } - } -} - -void RubyRefactoring::createNewClass(KDevelop::ProjectBaseItem *item) -{ - /* - * TODO: the code below is not complete. It should detect if this is a= Rails - * application. If it's so, then it has to check if the user clicked - * over a "relevant" directory (models, controllers,...). Otherwise we - * have to retrieve the url just like the c++ plugin does. - * TODO: move this code to the RubyNewClass class. - */ - KUrl u =3D KUrl(); - if (item) { - KDevelop::ProjectBaseItem *ff =3D item->folder(); - if(!ff && item->target()) - ff =3D static_cast(item->parent())= ->folder(); - if(ff) - u =3D ff->url(); - } else { - // TODO: get url from current selection - } - RubyNewClass newClass(item); - RubyNewClassAssistant newClassWizard(qApp->activeWindow(), &newClass, = u); - newClassWizard.exec(); -} - -void RubyRefactoring::executeNewClassAction() -{ - QAction* action =3D qobject_cast(sender()); - if (action) { - KDevelop::ProjectBaseItem* item =3D action->data().value(); - createNewClass(item); - } -} - -} diff --git a/codegen/rubyrefactoring.h b/codegen/rubyrefactoring.h deleted file mode 100644 index 2a79cf8..0000000 --- a/codegen/rubyrefactoring.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of KDevelop - * Copyright 2012 Miquel Sabat=C3=A9 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Library General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - - -#ifndef RUBY_REFACTORING_H -#define RUBY_REFACTORING_H - - -#include -#include - - -namespace Ruby -{ - -/** - * @class RubyRefactoring - * - * The RubyRefactoring class packs all the refactoring functionalities - * that this plugin provides. - */ -class RubyRefactoring : public QObject -{ - Q_OBJECT - -public: - /** - * @return instance of this class. - */ - static RubyRefactoring & self(); - - /** - * It extends the context menu by adding all the available refactoring - * actions. In order to do this, it takes the context menu @p extension - * and the given @param context. - */ - void doContextMenu(KDevelop::ContextMenuExtension &extension, KDevelop= ::Context *context); - - /** - * It makes the 'Create New Ruby Class' dialog to appear. - * - * @param item the KDevelop::ProjectBaseItem that the user may have - * selected. - */ - void createNewClass(KDevelop::ProjectBaseItem *item); - -private slots: - /** - * Executed from the context menu when the user wants to - * create a new class. - */ - void executeNewClassAction(); -}; - -} - -#endif /* RUBY_REFACTORING_H */ diff --git a/rubylanguagesupport.cpp b/rubylanguagesupport.cpp index 3a05221..e71572c 100644 --- a/rubylanguagesupport.cpp +++ b/rubylanguagesupport.cpp @@ -53,11 +53,11 @@ #include #include #include +#include #include #include #include #include -#include //END Includes = = @@ -134,13 +134,6 @@ KDevelop::ICodeHighlighting * LanguageSupport::codeHig= hlighting() const return m_highlighting; } = -ContextMenuExtension LanguageSupport::contextMenuExtension(KDevelop::Conte= xt *ctx) -{ - KDevelop::ContextMenuExtension cm; - RubyRefactoring::self().doContextMenu(cm, ctx); - return cm; -} - bool LanguageSupport::builtinsLoaded() const { return m_builtinsLoaded; @@ -156,11 +149,6 @@ enum ruby_version LanguageSupport::version() const return m_version; } = -void LanguageSupport::createNewClass() -{ - RubyRefactoring::self().createNewClass(0); -} - void LanguageSupport::updateReady(KDevelop::IndexedString url, KDevelop::R= eferencedTopDUContext topContext) { Q_UNUSED(topContext) @@ -312,10 +300,6 @@ void LanguageSupport::createActionsForMainWindow(Subli= me::MainWindow* /*window*/ action->setText(i18n("Run Current Test Function")); action->setShortcut(Qt::META | Qt::SHIFT | Qt::Key_F9); connect(action, SIGNAL(triggered(bool)), this, SLOT(runCurrentTestFunc= tion())); - - action =3D actions.addAction("ruby_new_class"); - action->setText(i18n("Create New Ruby Class")); - connect(action, SIGNAL(triggered(bool)), this, SLOT(createNewClass())); } = void LanguageSupport::setupQuickOpen() @@ -330,6 +314,12 @@ void LanguageSupport::setupQuickOpen() } } = +KDevelop::ICreateClassHelper* LanguageSupport::createClassHelper() const +{ + return new RubyClassHelper; +} + + } // End of namespace Ruby = = diff --git a/rubylanguagesupport.h b/rubylanguagesupport.h index aef5f58..735b072 100644 --- a/rubylanguagesupport.h +++ b/rubylanguagesupport.h @@ -105,12 +105,6 @@ public: virtual KDevelop::ICodeHighlighting * codeHighlighting() const; = /** - * Override from KDevelop::IPlugin so it can @return an extension - * of the context menu. - */ - KDevelop::ContextMenuExtension contextMenuExtension(KDevelop::Context = *ctx); - - /** * @return true if the builtins file is loaded, false otherwise. */ bool builtinsLoaded() const; @@ -130,6 +124,8 @@ public: */ virtual void createActionsForMainWindow(Sublime::MainWindow* window, Q= String& xmlFile, KActionCollection& actions); + + virtual KDevelop::ICreateClassHelper* createClassHelper() const; private: /** * @internal Find or create a launch for a the given @p name. @@ -156,9 +152,6 @@ private: void setupQuickOpen(); = public slots: - /// The slot for the Create New Class dialog. - void createNewClass(); - /// Get notified by background parser when the builtins file is loaded. void updateReady(KDevelop::IndexedString url, KDevelop::ReferencedTopD= UContext topContext); =20