-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpythoncodecompletioncontext.cpp
More file actions
284 lines (245 loc) · 12.1 KB
/
Copy pathpythoncodecompletioncontext.cpp
File metadata and controls
284 lines (245 loc) · 12.1 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* This file is part of KDevelop
* Copyright 2010 Sven Brauch <svenbrauch@googlemail.com>
* Licensed under the GNU GPL
* */
#include "pythoncodecompletioncontext.h"
#include <language/codecompletion/normaldeclarationcompletionitem.h>
#include <language/codecompletion/abstractincludefilecompletionitem.h>
#include <language/codecompletion/codecompletionitem.h>
#include <language/util/includeitem.h>
#include <language/duchain/duchainpointer.h>
#include <language/duchain/declaration.h>
#include "navigation/navigationwidget.h"
#include "importfileitem.h"
#include "functiondeclarationcompletionitem.h"
#include <qprocess.h>
#include <interfaces/icore.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iproject.h>
#include <project/projectmodel.h>
#include <QtCore/QRegExp>
#include <language/duchain/functiondeclaration.h>
#include <language/codecompletion/codecompletionitem.h>
#include "keyworditem.h"
using namespace KDevelop;
typedef QPair<Declaration*, int> DeclarationDepthPair;
namespace Python {
QList<CompletionTreeItemPointer> PythonCodeCompletionContext::completionItems(bool& abort, bool fullCompletion)
{
if ( abort )
return QList<CompletionTreeItemPointer>();
QList<CompletionTreeItemPointer> items;
DUChainReadLocker lock(DUChain::lock());
if ( m_operation == PythonCodeCompletionContext::NoCompletion ) {
}
else if ( m_operation == PythonCodeCompletionContext::ImportFileCompletion ) {
m_maxFolderScanDepth = 1;
foreach ( ImportFileItem* item, includeFileItems() ) {
item->includeItem.name = QString(item->moduleName + " (from " + KUrl::relativeUrl(item->fromProject->folder(), item->includeItem.basePath) + ")");
items << CompletionTreeItemPointer( item );
}
}
else if ( m_operation == PythonCodeCompletionContext::ImportSubCompletion ) {
kDebug() << "Stuff found for completion: " << findFilesForName(m_subForModule);
foreach ( ImportFileItem* item, findFilesForName(m_subForModule) ) {
item->includeItem.name = QString(item->moduleName + " (from " + KUrl::relativeUrl(item->fromProject->folder(), item->includeItem.basePath) + ")");
items << CompletionTreeItemPointer( item );
}
}
else if ( m_operation == PythonCodeCompletionContext::MemberAccessCompletion ) {
// we don't have type support, so we cannot support completing mebers yet. But we can at least prevent kdevelop from opening a pointless
// popup with completion items you don't want
}
else {
// it's stupid to display a 3-letter completion item on manually invoked code completion and makes everything look crowded
if ( m_operation == PythonCodeCompletionContext::NewStatementCompletion && ! fullCompletion ) {
QStringList keywordItems;
keywordItems << "def" << "class" << "lambda" << "global" << "print";
foreach ( const QString& current, keywordItems ) {
items << CompletionTreeItemPointer(new KeywordItem(KDevelop::CodeCompletionContext::Ptr(this), current));
}
}
if ( abort ) {
return QList<CompletionTreeItemPointer>();
}
QList<DeclarationDepthPair> declarations = m_duContext->allDeclarations(m_position, m_duContext->topContext());
DeclarationPointer currentDeclaration;
int count = declarations.length();
for ( int i = 0; i < count; i++ ) {
if ( abort ) {
return items;
}
currentDeclaration = DeclarationPointer(declarations.at(i).first);
kDebug() << "Adding item: " << currentDeclaration.data()->identifier().identifier().str();
NormalDeclarationCompletionItem* item;
if ( currentDeclaration.data()->abstractType() && currentDeclaration.data()->abstractType().constData()->whichType() == AbstractType::TypeFunction ) {
kDebug() << "Adding function declaration item";
item = new FunctionDeclarationCompletionItem(currentDeclaration);
}
else {
item = new NormalDeclarationCompletionItem(currentDeclaration, KDevelop::CodeCompletionContext::Ptr(this));
}
kDebug() << item->declaration().data()->identifier().identifier().str();
items << CompletionTreeItemPointer(item);
}
}
m_searchingForModule.clear();
m_subForModule.clear();
return items;
}
QList< ImportFileItem* > PythonCodeCompletionContext::findFilesForName(const QString& name)
{
kDebug() << "Name: " << name;
QStringList resolvedName = name.split(".");
m_maxFolderScanDepth = resolvedName.length() + 1;
m_searchingForModule = resolvedName;
return includeFileItems();
}
QList<ImportFileItem*> PythonCodeCompletionContext::includeFileItems() {
QList<ImportFileItem*> items;
foreach (IProject* project, ICore::self()->projectController()->projects() ) {
foreach ( KDevelop::ProjectFolderItem* folder, project->foldersForUrl( KUrl(project->folder().url()) ) ) {
m_folderStack.push(folder);
items << fileItemsForFolder(folder, project);
m_folderStack.pop();
}
}
return items;
}
QList<ImportFileItem*> PythonCodeCompletionContext::fileItemsForFolder(KDevelop::ProjectFolderItem* folder, IProject* project)
{
kDebug() << " +++++ Processing folder: " << folder->folderName();
kDebug() << "current folder stack count " << m_folderStack.count();
if ( ! folder ) {
m_dontAddMe = true;
return QList<ImportFileItem*>();
}
bool continue_recursion = true;
bool do_recursion = true;
kDebug() << m_maxFolderScanDepth << m_folderStack.count() << m_searchingForModule;
if ( m_maxFolderScanDepth - 1 < m_folderStack.count() ) continue_recursion = false; // we dont offer foo.bar.baz.bang.bar if there's only one dot in the address by now
if ( m_searchingForModule.length() > 0 && folder->url() != project->folder().url() ) {
if ( m_searchingForModule.length() >= m_folderStack.count() && m_searchingForModule.at(m_folderStack.count() - 2) != folder->folderName() ) {
kDebug() << "Skip: " << m_searchingForModule.at(m_folderStack.count() - 2) << m_searchingForModule << m_folderStack.count() - 2 << folder->folderName();
m_dontAddMe = true;
return QList<ImportFileItem*>();
}
else if ( m_searchingForModule.at(m_folderStack.count() - 2) != folder->folderName() ) {
m_dontAddMe = true;
return QList<ImportFileItem*>();
}
kDebug() << "USE: " << m_searchingForModule.at(m_folderStack.count() - 2) << m_searchingForModule << m_folderStack << folder->folderName();
}
kDebug() << " >>>>> For directory " << folder->folderName() << " : " << "doing recursion: " << do_recursion << "; continuing downwards: " << continue_recursion;
QList<ImportFileItem*> items;
foreach ( KDevelop::ProjectFolderItem* folder, folder->folderList() ) {
if ( ! folder ) continue;
m_folderStack.push(folder);
if ( continue_recursion ) {
kDebug() << "Scanning for include items: " << folder->folderName();
items << fileItemsForFolder(folder, project);
if ( m_dontAddMe ) {
m_dontAddMe = false;
m_folderStack.pop();
continue;
}
}
// only add items when at right level
if ( m_searchingForModule.length() != 0 && m_maxFolderScanDepth != m_folderStack.count() ) {
kDebug() << "CONTINUE: " << m_maxFolderScanDepth << m_folderStack.count();
if ( m_searchingForModule.length() < m_folderStack.count() ) do_recursion = false; // don't add items from here, we're too deep
else {
// we're not yet deep enough, so don't even add the folder
m_folderStack.pop();
continue;
}
}
else {
kDebug() << "ADD: " << m_maxFolderScanDepth << m_folderStack.count();
kDebug() << "adding files and folders from directory " << folder->folderName();
}
// Add the folder
IncludeItem* folderItem = new IncludeItem();
folderItem->basePath = folder->url();
folderItem->isDirectory = true;
ImportFileItem* importFolderItem = new ImportFileItem(*folderItem);
importFolderItem->fromProject = project;
importFolderItem->moduleName = folder->folderName();
items << importFolderItem;
if ( continue_recursion && do_recursion ) {
// Add all sub-items and folders
foreach ( ProjectFileItem* file, folder->fileList() ) {
if ( ! file->fileName().endsWith(".py") || file->fileName() == "__init__.py" ) continue;
IncludeItem* item = new IncludeItem();
item->basePath = folder->url();
ImportFileItem* importItem = new ImportFileItem(*item);
importItem->moduleName = file->fileName().replace(".py", "");
importItem->fromProject = project;
items << importItem;
}
}
m_folderStack.pop();
}
return items;
}
PythonCodeCompletionContext::PythonCodeCompletionContext(DUContextPointer context, const QString& text, const KDevelop::CursorInRevision& position,
int depth): CodeCompletionContext(context, text, position, depth)
{
QString currentLine = "\n" + text.split("\n").last(); // we'll only look at the last line, as 99% of python statements are limited to one line
kDebug() << "Doing auto-completion context scan for: " << currentLine;
QRegExp importsub("(.*)\n[\\s]*from(.*)import[\\s]*$");
importsub.setMinimal(true);
bool is_importSub = importsub.exactMatch(currentLine);
QRegExp importsub2("(.*)\n[\\s]*(from(.*)|import(.*))\\.$");
importsub2.setMinimal(true);
bool is_importSub2 = importsub2.exactMatch(currentLine);
if ( is_importSub || is_importSub2 ) {
QStringList for_module_match;
if ( is_importSub ) for_module_match = importsub.capturedTexts();
else for_module_match = importsub2.capturedTexts();
kDebug() << for_module_match;
QString for_module;
if ( is_importSub ) for_module = for_module_match.last().replace(" ", "");
else for_module = for_module_match[3].replace(" ", "");
kDebug() << "Matching against module name: " << for_module_match;
m_operation = PythonCodeCompletionContext::ImportSubCompletion;
m_subForModule = for_module;
return;
}
QRegExp newStatementCompletion("(.*)\n[\\s]*$");
newStatementCompletion.setMinimal(true);
bool isNewStatementCompletion = newStatementCompletion.exactMatch(currentLine);
if ( isNewStatementCompletion ) {
m_operation = PythonCodeCompletionContext::NewStatementCompletion;
return;
}
QRegExp importfile("(.*)\n[\\s]*import[\\s]*$");
importfile.setMinimal(true);
bool is_importfile = importfile.exactMatch(currentLine);
QRegExp fromimport("(.*)\n[\\s]*from[\\s]*$");
fromimport.setMinimal(true);
bool is_fromimport = fromimport.exactMatch(currentLine);
if ( is_importfile || is_fromimport ) {
m_operation = PythonCodeCompletionContext::ImportFileCompletion;
return;
}
QRegExp attributeAccess("(.*)\n[\\s]*(.*)\\.$");
attributeAccess.setMinimal(true);
bool is_attributeAccess = attributeAccess.exactMatch(currentLine);
if ( is_attributeAccess ) {
m_operation = PythonCodeCompletionContext::MemberAccessCompletion;
return;
}
QRegExp noCompletionPossible("(.*)\n[\\s]*(class|def)[\\s]*$");
noCompletionPossible.setMinimal(true);
bool is_noCompletionPossible = noCompletionPossible.exactMatch(currentLine);
if ( is_noCompletionPossible ) {
m_operation = PythonCodeCompletionContext::NoCompletion;
return;
}
QRegExp memberaccess("");
kDebug() << "Is import file: " << is_importfile;
// Q_ASSERT(false);
}
}