File indexing completed on 2021-12-21 13:15:43

0001 /* This file is part of KDevelop
0002  *
0003  * Copyright 2006-2010 Alexander Dymo <adymo@kdevelop.org>
0004  * Copyright (C) 2014-2015 Miquel Sabaté Solà <mikisabate@gmail.com>
0005  *
0006  * This program is free software; you can redistribute it and/or modify
0007  * it under the terms of the GNU Library General Public License as
0008  * published by the Free Software Foundation; either version 2 of the
0009  * License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public
0017  * License along with this program; if not, write to the
0018  * Free Software Foundation, Inc.,
0019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0020  */
0021 
0022 #include <languagesupport.h>
0023 
0024 #include <QProcess>
0025 #include <QStandardPaths>
0026 
0027 #include <KPluginFactory>
0028 
0029 #include <interfaces/contextmenuextension.h>
0030 #include <interfaces/icore.h>
0031 #include <interfaces/ilanguagecontroller.h>
0032 #include <interfaces/iplugincontroller.h>
0033 #include <language/codecompletion/codecompletion.h>
0034 #include <language/interfaces/editorcontext.h>
0035 
0036 #include <completion/model.h>
0037 #include <codegen/refactoring.h>
0038 #include <debug.h>
0039 #include <duchain/helpers.h>
0040 #include <highlighting.h>
0041 #include <launcher.h>
0042 #include <parsejob.h>
0043 #include <rails/helpers.h>
0044 #include <rails/support.h>
0045 
0046 K_PLUGIN_FACTORY_WITH_JSON(KDevRubySupportFactory, "kdevrubylanguagesupport.json", registerPlugin<ruby::LanguageSupport>();)
0047 
0048 using namespace ruby;
0049 using namespace KDevelop;
0050 
0051 LanguageSupport::LanguageSupport(QObject *parent, const QVariantList &)
0052     : IPlugin(QStringLiteral("kdevrubysupport"), parent)
0053     , ILanguageSupport()
0054 {
0055     m_rails = new rails::Support(this);
0056     m_launcher = new Launcher(this);
0057 
0058     setXMLFile("kdevrubysupport.rc");
0059     m_highlighting = new Highlighting(this);
0060     m_refactoring = new Refactoring(this);
0061     CodeCompletionModel *rModel = new CodeCompletionModel(this);
0062     new CodeCompletion(this, rModel, "Ruby");
0063 
0064     /* Retrieving Ruby version */
0065     QString path = QStandardPaths::findExecutable("ruby");
0066     if (!path.isEmpty()) {
0067         QProcess ruby;
0068         ruby.start(path, QStringList{ "--version" });
0069         ruby.waitForFinished(3000);
0070         QList<QByteArray> byteArr = ruby.readAllStandardOutput().split(' ');
0071         if (byteArr.size() > 1) {
0072             const QString &output = byteArr[1];
0073             QStringList version = output.split('.');
0074             if (version.size() > 1) {
0075                 if (version[0] == "1") {
0076                     m_version = (version[1] == "8") ? ruby18 : ruby19;
0077                 } else if (version[1] == "0") {
0078                     m_version = ruby20;
0079                 } else {
0080                     m_version = ruby21;
0081                 }
0082             }
0083         }
0084     } else {
0085         qWarning() << "ruby might not be installed!";
0086     }
0087 }
0088 
0089 LanguageSupport::~LanguageSupport()
0090 {
0091 }
0092 
0093 QString LanguageSupport::name() const
0094 {
0095     return QStringLiteral("Ruby");
0096 }
0097 
0098 KDevelop::ParseJob * LanguageSupport::createParseJob(const IndexedString &url)
0099 {
0100     return new ruby::ParseJob(url, this);
0101 }
0102 
0103 ICodeHighlighting * LanguageSupport::codeHighlighting() const
0104 {
0105     return m_highlighting;
0106 }
0107 
0108 ContextMenuExtension LanguageSupport::contextMenuExtension(Context* context, QWidget* parent)
0109 {
0110     ContextMenuExtension cm;
0111     EditorContext *ed = dynamic_cast<EditorContext *>(context);
0112 
0113     if (ed && ICore::self()->languageController()->languagesForUrl(ed->url()).
0114             contains(this)) {
0115         // It's safe to add our own ContextMenuExtension.
0116         m_refactoring->fillContextMenu(cm, context, parent);
0117     }
0118     return cm;
0119 }
0120 
0121 enum ruby_version LanguageSupport::version() const
0122 {
0123     return m_version;
0124 }
0125 
0126 void LanguageSupport::createActionsForMainWindow(Sublime::MainWindow *,
0127                                                  QString &_xmlFile,
0128                                                  KActionCollection &actions)
0129 {
0130     _xmlFile = xmlFile();
0131 
0132     m_rails->setupActions(actions);
0133     m_launcher->setupActions(actions);
0134 }
0135 
0136 #include "languagesupport.moc"
0137