File indexing completed on 2024-05-05 04:36:48

0001 /* This file is part of KDevelop
0002  *
0003  * Copyright (C) 2012-2015 Miquel Sabaté Solà <mikisabate@gmail.com>
0004  *
0005  * This program is free software: you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation, either version 3 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 
0020 // KDevelop
0021 #include <language/duchain/duchain.h>
0022 #include <language/duchain/duchainlock.h>
0023 #include <language/duchain/types/functiontype.h>
0024 #include <language/codecompletion/codecompletionmodel.h>
0025 
0026 // Ruby
0027 #include <duchain/navigation/navigationwidget.h>
0028 #include <duchain/declarations/moduledeclaration.h>
0029 #include <duchain/declarations/methoddeclaration.h>
0030 #include <completion/items/normalitem.h>
0031 #include <completion/helpers.h>
0032 
0033 
0034 using namespace KDevelop;
0035 
0036 namespace ruby
0037 {
0038 
0039 NormalItem::NormalItem(DeclarationPointer decl,
0040                        CodeCompletionContext::Ptr context,
0041                        int inheritanceDepth)
0042     : NormalDeclarationCompletionItem(decl, context, inheritanceDepth)
0043 {
0044     /* There's nothing to do here */
0045 }
0046 
0047 QVariant NormalItem::data(const QModelIndex &index, int role,
0048                           const CodeCompletionModel *model) const
0049 {
0050     if (!m_declaration) {
0051         return QVariant();
0052     }
0053 
0054     DUChainReadLocker rlock;
0055 
0056     Declaration* dec = const_cast<Declaration*>(m_declaration.data());
0057     switch (role) {
0058     case CodeCompletionModel::ItemSelected:
0059         return QVariant(NavigationWidget::shortDescription(dec));
0060     case Qt::DisplayRole:
0061         switch (index.column()) {
0062         case CodeCompletionModel::Postfix:
0063             return QVariant();
0064         case CodeCompletionModel::Prefix: {
0065             ModuleDeclaration *moDec = dynamic_cast<ModuleDeclaration *>(dec);
0066             if (moDec) {
0067                 if (moDec->isModule()) {
0068                     return i18n("module");
0069                 }
0070                 return i18n("class");
0071             }
0072             return QVariant();
0073         }
0074         case CodeCompletionModel::Arguments:
0075             if (FunctionType::Ptr fType = dec->type<FunctionType>()) {
0076                 return getArgumentList(m_declaration.data(), nullptr);
0077             }
0078             break;
0079         }
0080         break;
0081     }
0082     rlock.unlock();
0083 
0084     return NormalDeclarationCompletionItem::data(index, role, model);
0085 }
0086 
0087 QWidget * NormalItem::createExpandingWidget(const CodeCompletionModel *model) const
0088 {
0089     return new NavigationWidget(m_declaration, model->currentTopContext());
0090 }
0091 
0092 bool NormalItem::createsExpandingWidget() const
0093 {
0094     return true;
0095 }
0096 
0097 }