File indexing completed on 2024-05-19 15:41:37

0001 /*
0002     SPDX-FileCopyrightText: 2011-2016 Sven Brauch <svenbrauch@googlemail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "declaration.h"
0008 #include "duchain/helpers.h"
0009 
0010 #include <language/codecompletion/codecompletionmodel.h>
0011 #include <language/codecompletion/codecompletioncontext.h>
0012 #include <language/duchain/types/containertypes.h>
0013 #include <language/duchain/duchainlock.h>
0014 #include <language/duchain/duchain.h>
0015 #include <language/duchain/types/typepointer.h>
0016 #include <language/duchain/classfunctiondeclaration.h>
0017 #include <language/duchain/types/functiontype.h>
0018 #include <language/duchain/types/enumeratortype.h>
0019 #include <language/duchain/duchainutils.h>
0020 #include <language/duchain/aliasdeclaration.h>
0021 
0022 using namespace KDevelop;
0023 
0024 namespace Python {
0025 
0026 PythonDeclarationCompletionItem::PythonDeclarationCompletionItem(DeclarationPointer decl, 
0027                                                                  QExplicitlySharedDataPointer<CodeCompletionContext> context,
0028                                                                  int inheritanceDepth)
0029                                : NormalDeclarationCompletionItem(decl, context, inheritanceDepth)
0030                                , m_typeHint(PythonCodeCompletionContext::NoHint)
0031                                , m_addMatchQuality(0)
0032 {
0033     Q_ASSERT(decl->alwaysForceDirect());
0034     if ( context ) {
0035         setTypeHint(static_cast<PythonCodeCompletionContext*>(context.data())->itemTypeHint());
0036     }
0037 }
0038 
0039 void PythonDeclarationCompletionItem::addMatchQuality(int add)
0040 {
0041     m_addMatchQuality += add;
0042 }
0043 
0044 void PythonDeclarationCompletionItem::setTypeHint(PythonCodeCompletionContext::ItemTypeHint type)
0045 {
0046     m_typeHint = type;
0047 }
0048 
0049 QString PythonDeclarationCompletionItem::shortenedTypeString(const KDevelop::DeclarationPointer& decl, int desiredTypeLength) const {
0050     if ( !decl ) {
0051         return {};
0052     }
0053     auto type = decl->abstractType()->toString();
0054     if ( type.length() > desiredTypeLength ) {
0055         return type.left(desiredTypeLength) + "...";
0056     }
0057     return type;
0058 }
0059 
0060 QVariant PythonDeclarationCompletionItem::data(const QModelIndex& index, int role, const KDevelop::CodeCompletionModel* model) const
0061 {
0062     switch ( role ) {
0063         case KDevelop::CodeCompletionModel::MatchQuality: {
0064             if ( ! declaration() ) return 0;
0065             if ( ! model->completionContext()->duContext() ) return 0;
0066             if ( declaration()->identifier().identifier().str().startsWith('_') ) {
0067                 return 0;
0068             }
0069             if ( declaration()->context()->topContext() == Helper::getDocumentationFileContext() ) {
0070                 return 0;
0071             }
0072             if (   m_typeHint == PythonCodeCompletionContext::IterableRequested 
0073                 && dynamic_cast<ListType*>(declaration()->abstractType().data()) )
0074             {
0075                 return 10;
0076             }
0077             if ( model->completionContext()->duContext() == declaration()->context() ) {
0078                 return 5 + m_addMatchQuality;
0079             }
0080             if ( model->completionContext()->duContext()->topContext() == declaration()->context()->topContext() ) {
0081                 return 3 + m_addMatchQuality;
0082             }
0083             return m_addMatchQuality;
0084         }
0085         case KDevelop::CodeCompletionModel::BestMatchesCount: {
0086             return 5;
0087         }
0088     }
0089 
0090     QVariant data = KDevelop::NormalDeclarationCompletionItem::data(index, role, model);
0091     if ( data.canConvert<QString>() ) {
0092         QString s = data.toString();
0093         s.replace("<unknown>", "?");
0094         return QVariant(s);
0095     }
0096     else if ( data.canConvert<QStringList>() ) {
0097         QStringList s = data.toStringList();
0098         s.replaceInStrings("<unknown>", "?");
0099         return QVariant(s);
0100     }
0101     else return data;
0102 }
0103 
0104 }