File: declaration.cpp

package info (click to toggle)
kdevelop-python 5.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,480 kB
  • sloc: python: 183,325; cpp: 17,155; xml: 1,137; sh: 14; makefile: 13
file content (116 lines) | stat: -rw-r--r-- 4,931 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*****************************************************************************
 * Copyright (c) 2011-2016 Sven Brauch <svenbrauch@googlemail.com>           *
 *                                                                           *
 * This program is free software; you can redistribute it and/or             *
 * modify it under the terms of the GNU 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, see <http://www.gnu.org/licenses/>.     *
 *****************************************************************************
 */

#include "declaration.h"
#include "duchain/helpers.h"

#include <language/codecompletion/codecompletionmodel.h>
#include <language/codecompletion/codecompletioncontext.h>
#include <language/duchain/types/containertypes.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/duchain.h>
#include <language/duchain/types/typepointer.h>
#include <language/duchain/classfunctiondeclaration.h>
#include <language/duchain/types/functiontype.h>
#include <language/duchain/types/enumeratortype.h>
#include <language/duchain/duchainutils.h>
#include <language/duchain/aliasdeclaration.h>

using namespace KDevelop;

namespace Python {

PythonDeclarationCompletionItem::PythonDeclarationCompletionItem(DeclarationPointer decl, 
                                                                 QExplicitlySharedDataPointer<CodeCompletionContext> context,
                                                                 int inheritanceDepth)
                               : NormalDeclarationCompletionItem(decl, context, inheritanceDepth)
                               , m_typeHint(PythonCodeCompletionContext::NoHint)
                               , m_addMatchQuality(0)
{
    Q_ASSERT(decl->alwaysForceDirect());
    if ( context ) {
        setTypeHint(static_cast<PythonCodeCompletionContext*>(context.data())->itemTypeHint());
    }
}

void PythonDeclarationCompletionItem::addMatchQuality(int add)
{
    m_addMatchQuality += add;
}

void PythonDeclarationCompletionItem::setTypeHint(PythonCodeCompletionContext::ItemTypeHint type)
{
    m_typeHint = type;
}

QString PythonDeclarationCompletionItem::shortenedTypeString(const KDevelop::DeclarationPointer& decl, int desiredTypeLength) const {
    if ( !decl ) {
        return {};
    }
    auto type = decl->abstractType()->toString();
    if ( type.length() > desiredTypeLength ) {
        return type.left(desiredTypeLength) + "...";
    }
    return type;
}

QVariant PythonDeclarationCompletionItem::data(const QModelIndex& index, int role, const KDevelop::CodeCompletionModel* model) const
{
    switch ( role ) {
        case KDevelop::CodeCompletionModel::MatchQuality: {
            if ( ! declaration() ) return 0;
            if ( ! model->completionContext()->duContext() ) return 0;
            if ( declaration()->identifier().identifier().str().startsWith('_') ) {
                return 0;
            }
            if ( declaration()->context()->topContext() == Helper::getDocumentationFileContext() ) {
                return 0;
            }
            if (   m_typeHint == PythonCodeCompletionContext::IterableRequested 
                && dynamic_cast<ListType*>(declaration()->abstractType().data()) )
            {
                return 10;
            }
            if ( model->completionContext()->duContext() == declaration()->context() ) {
                return 5 + m_addMatchQuality;
            }
            if ( model->completionContext()->duContext()->topContext() == declaration()->context()->topContext() ) {
                return 3 + m_addMatchQuality;
            }
            return m_addMatchQuality;
        }
        case KDevelop::CodeCompletionModel::BestMatchesCount: {
            return 5;
        }
    }

    QVariant data = KDevelop::NormalDeclarationCompletionItem::data(index, role, model);
    if ( data.canConvert<QString>() ) {
        QString s = data.toString();
        s.replace("<unknown>", "?");
        return QVariant(s);
    }
    else if ( data.canConvert<QStringList>() ) {
        QStringList s = data.toStringList();
        s.replaceInStrings("<unknown>", "?");
        return QVariant(s);
    }
    else return data;
}

}