File: context.h

package info (click to toggle)
kdevelop-python 24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,640 kB
  • sloc: python: 183,048; cpp: 18,798; xml: 140; sh: 14; makefile: 9
file content (176 lines) | stat: -rw-r--r-- 5,903 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
    SPDX-FileCopyrightText: 2011-2013 Sven Brauch <svenbrauch@googlemail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#ifndef PYTHONCODECOMPLETIONCONTEXT_H
#define PYTHONCODECOMPLETIONCONTEXT_H

#include <language/codecompletion/codecompletioncontext.h>
#include <language/duchain/duchainpointer.h>
#include <QStack>

#include <QObject>

#include "items/importfile.h"
#include "worker.h"
#include "pythoncompletionexport.h"
#include "helpers.h"
#include "types/unsuretype.h"

using namespace KDevelop;

namespace KDevelop {
    class IProject;
    class ProjectFolderItem;
}

namespace Python {
    
typedef QPair<Declaration*, int> DeclarationDepthPair;

/**
 * @brief Represents a single pair of directory and remaining identifiers, for include completion.
 * 
 * In the directory, the functions in PythonCodeCompletionContext try to resolve the remaining
 * identifiers. If the identifier list is empty, the __init__.py file will be read.
 **/
class IncludeSearchTarget {
public:
    IncludeSearchTarget(QUrl d_, QStringList r_) : directory(d_), remainingIdentifiers(r_) {
        directory.setPath(QDir::cleanPath(directory.path()));
    };
    QUrl directory;
    QStringList remainingIdentifiers;
};

class KDEVPYTHONCOMPLETION_EXPORT PythonCodeCompletionContext : public KDevelop::CodeCompletionContext
{
public:
    enum CompletionContextType {
        ImportFileCompletion,
        MemberAccessCompletion,
        DefaultCompletion,
        ImportSubCompletion,
        NoCompletion,
        NewStatementCompletion,
        DefineCompletion,
        ShebangLineCompletion,
        FunctionCallCompletion,
        InheritanceCompletion,
        RaiseExceptionCompletion,
        GeneratorVariableCompletion,
        StringFormattingCompletion
    };
    
    enum ItemTypeHint {
        NoHint,
        IterableRequested, // < the requested item should be iterable, e.g. a list
        ClassTypeRequested // < the requested item should be a class type
    };
    
    PythonCodeCompletionContext(DUContextPointer context, const QString& text, const QString& followingText,
                                const KDevelop::CursorInRevision& position,
                                int depth, const PythonCodeCompletionWorker* parent);

    CompletionContextType completionContextType();

    ItemTypeHint itemTypeHint();

    /**
     * @brief The "interface method" which returns all the completion items to kdevelop.
     **/
    QList< KDevelop::CompletionTreeItemPointer > completionItems(bool& abort, bool fullCompletion = true) override;

    QList< CompletionTreeElementPointer > ungroupedElements() override;
    
    /**
     * @brief Get all possible items matching the specified dot-separated search string.
     **/
    QList<CompletionTreeItemPointer> includeItemsForSubmodule(QString submodule);
    
    /**
     * @brief Get all include items for the given target list.
     **/
    QList< CompletionTreeItemPointer > findIncludeItems(IncludeSearchTarget);
    /**
     * @brief Get all include items for the given single target.
     **/
    QList< CompletionTreeItemPointer > findIncludeItems(QList< Python::IncludeSearchTarget > items);
    
    /**
     * @brief Get all contexts to list declarations from for the given item.
     **/
    DUContext* internalContextForDeclaration(TopDUContext* topContext, QStringList remainingIdentifiers);
    
    /**
     * @brief Get all items that are attributes of the given type. @param type might be any type.
     **/
    QList<CompletionTreeItemPointer> getCompletionItemsForType(AbstractType::Ptr type);
    /**
     * @brief Get all items that are attributes of the given type. @param type must be non-unsure.
     **/
    QList<CompletionTreeItemPointer> getCompletionItemsForOneType(AbstractType::Ptr type);
    
    /**
     * @brief Convert the given list of declarations to a list of include-items.
     * Convenience function.
     **/
    QList<CompletionTreeItemPointer> declarationListToItemList(const QVector<DeclarationDepthPair>& declarations, int maxDepth = 0);
    QList<CompletionTreeItemPointer> declarationListToItemList(QList<Declaration*> declarations);
    
private:
    /// This constructor is only used for recursive calltips
    PythonCodeCompletionContext(DUContextPointer context, const QString& remainingText,
                                QString calledFunction, int depth, int alreadyGivenParameters, CodeCompletionContext* child);
    void summonParentForEventualCall(TokenList tokens, const QString& text);
    /// Get possible "add import..." items for an expression.
    QList< CompletionTreeItemPointer > getMissingIncludeItems(QString forString);
    void eventuallyAddGroup(QString name, int priority, QList<CompletionTreeItemPointer> items);

private:
    /// Item generating functions
    using ItemList = QList<CompletionTreeItemPointer>;
    ItemList shebangItems();
    ItemList generatorItems();
    ItemList functionCallItems();
    ItemList defineItems();
    ItemList raiseItems();
    ItemList importFileItems();
    ItemList inheritanceItems();
    ItemList memberAccessItems();
    ItemList stringFormattingItems();
    ItemList keywordItems();
    ItemList classMemberInitItems();

private:
    CompletionContextType m_operation;
    ItemTypeHint m_itemTypeHint;
    QStack<ProjectFolderItem*> m_folderStack;
    int m_maxFolderScanDepth;
    QStringList m_searchingForModule;
    QString m_searchImportItemsInModule;
    QUrl m_workingOnDocument;
    
    CodeCompletionContext* m_child;
    
    QString m_guessTypeOfExpression;
    QString m_followingText;
    
    QString m_indent;
    KDevelop::CursorInRevision m_position;
    
    QString m_calledFunction;
    int m_alreadyGivenParametersCount;

    QString m_matchAgainst;

    bool m_fullCompletion;

    QList<CompletionTreeElementPointer> m_storedGroups;
};

}

#endif // PYTHONCODECOMPLETIONCONTEXT_H