-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.cpp
More file actions
1311 lines (1191 loc) · 57.2 KB
/
Copy pathcontext.cpp
File metadata and controls
1311 lines (1191 loc) · 57.2 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* Copyright (c) 2011-2012 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 "context.h"
#include "items/keyword.h"
#include "items/importfile.h"
#include "items/functiondeclaration.h"
#include "items/implementfunction.h"
#include "items/missingincludeitem.h"
#include "items/replacementvariable.h"
#include "worker.h"
#include "helpers.h"
#include "duchain/pythoneditorintegrator.h"
#include "duchain/expressionvisitor.h"
#include "duchain/declarationbuilder.h"
#include "duchain/helpers.h"
#include "duchain/types/unsuretype.h"
#include "duchain/navigation/navigationwidget.h"
#include "parser/astbuilder.h"
#include <language/duchain/functiondeclaration.h>
#include <language/duchain/classdeclaration.h>
#include <language/duchain/aliasdeclaration.h>
#include <language/duchain/duchainutils.h>
#include <language/util/includeitem.h>
#include <language/codecompletion/normaldeclarationcompletionitem.h>
#include <language/codecompletion/codecompletionitem.h>
#include <language/codecompletion/codecompletionitemgrouper.h>
#include <interfaces/icore.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iproject.h>
#include <interfaces/idocumentcontroller.h>
#include <project/projectmodel.h>
#include <QProcess>
#include <QRegExp>
#include <KTextEditor/View>
#include <memory>
#include <QDebug>
#include "codecompletiondebug.h"
using namespace KTextEditor;
using namespace KDevelop;
namespace Python {
PythonCodeCompletionContext::ItemTypeHint PythonCodeCompletionContext::itemTypeHint()
{
return m_itemTypeHint;
}
PythonCodeCompletionContext::CompletionContextType PythonCodeCompletionContext::completionContextType()
{
return m_operation;
}
std::unique_ptr<ExpressionVisitor> visitorForString(QString str, DUContext* context,
CursorInRevision scanUntil = CursorInRevision::invalid())
{
ENSURE_CHAIN_NOT_LOCKED
AstBuilder builder;
CodeAst::Ptr tmpAst = builder.parse({}, str);
if ( ! tmpAst ) {
return std::unique_ptr<ExpressionVisitor>(nullptr);
}
ExpressionVisitor* v = new ExpressionVisitor(context);
v->enableGlobalSearching();
if ( scanUntil.isValid() ) {
v->scanUntil(scanUntil);
v->enableUnknownNameReporting();
}
v->visitCode(tmpAst.data());
return std::unique_ptr<ExpressionVisitor>(v);
}
void PythonCodeCompletionContext::eventuallyAddGroup(QString name, int priority,
QList<CompletionTreeItemPointer> items)
{
if ( items.isEmpty() ) {
return;
}
KDevelop::CompletionCustomGroupNode* node = new KDevelop::CompletionCustomGroupNode(name, priority);
node->appendChildren(items);
m_storedGroups << CompletionTreeElementPointer(node);
}
QList< CompletionTreeElementPointer > PythonCodeCompletionContext::ungroupedElements()
{
return m_storedGroups;
}
static QList<CompletionTreeItemPointer> setOmitParentheses(QList<CompletionTreeItemPointer> items) {
for ( auto current: items ) {
if ( auto func = dynamic_cast<FunctionDeclarationCompletionItem*>(current.data()) ) {
func->setDoNotCall(true);
}
}
return items;
};
PythonCodeCompletionContext::ItemList PythonCodeCompletionContext::shebangItems()
{
KeywordItem::Flags f = (KeywordItem::Flags) ( KeywordItem::ForceLineBeginning | KeywordItem::ImportantItem );
QList<CompletionTreeItemPointer> shebangGroup;
if ( m_position.line == 0 && ( m_text.startsWith('#') || m_text.isEmpty() ) ) {
QString i18ndescr = i18n("insert Shebang line");
shebangGroup << CompletionTreeItemPointer(new KeywordItem(KDevelop::CodeCompletionContext::Ptr(this),
"#!/usr/bin/env python\n", i18ndescr, f));
shebangGroup << CompletionTreeItemPointer(new KeywordItem(KDevelop::CodeCompletionContext::Ptr(this),
"#!/usr/bin/env python2.7\n", i18ndescr, f));
shebangGroup << CompletionTreeItemPointer(new KeywordItem(KDevelop::CodeCompletionContext::Ptr(this),
"#!/usr/bin/env python3\n", i18ndescr, f));
}
else if ( m_position.line <= 1 && m_text.endsWith('#') ) {
shebangGroup << CompletionTreeItemPointer(new KeywordItem(KDevelop::CodeCompletionContext::Ptr(this),
"# -*- coding:utf-8 -*-\n\n", i18n("specify document encoding"), f));
}
eventuallyAddGroup(i18n("Add file header"), 1000, shebangGroup);
return ItemList();
}
PythonCodeCompletionContext::ItemList PythonCodeCompletionContext::functionCallItems()
{
ItemList resultingItems;
// gather additional items to show above the real ones (for parameters, and stuff)
FunctionDeclaration* functionCalled = 0;
auto v = visitorForString(m_guessTypeOfExpression, m_duContext.data());
DUChainReadLocker lock;
if ( ! v || ! v->lastDeclaration() ) {
qCWarning(KDEV_PYTHON_CODECOMPLETION) << "Did not receive a function declaration from expression visitor! Not offering call tips.";
qCWarning(KDEV_PYTHON_CODECOMPLETION) << "Tried: " << m_guessTypeOfExpression;
return resultingItems;
}
functionCalled = Helper::functionDeclarationForCalledDeclaration(v->lastDeclaration()).first.data();
auto current = Helper::resolveAliasDeclaration(functionCalled);
QList<Declaration*> calltips;
if ( current && current->isFunctionDeclaration() ) {
calltips << current;
}
auto calltipItems = declarationListToItemList(calltips);
foreach ( CompletionTreeItemPointer current, calltipItems ) {
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Adding calltip item, at argument:" << m_alreadyGivenParametersCount+1;
FunctionDeclarationCompletionItem* item = static_cast<FunctionDeclarationCompletionItem*>(current.data());
item->setAtArgument(m_alreadyGivenParametersCount + 1);
item->setDepth(depth());
}
resultingItems.append(calltipItems);
// If this is the top-level calltip, add additional items for the default-parameters of the function,
// but only if all non-default arguments (the mandatory ones) already have been provided.
// TODO fancy feature: Filter out already provided default-parameters
if ( depth() != 1 || ! functionCalled ) {
return resultingItems;
}
if ( DUContext* args = DUChainUtils::getArgumentContext(functionCalled) ) {
int normalParameters = args->localDeclarations().count() - functionCalled->defaultParametersSize();
if ( normalParameters > m_alreadyGivenParametersCount ) {
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Not at default arguments yet";
return resultingItems;
}
for ( unsigned int i = 0; i < functionCalled->defaultParametersSize(); i++ ) {
QString paramName = args->localDeclarations().at(normalParameters + i)->identifier().toString();
resultingItems << CompletionTreeItemPointer(new KeywordItem(CodeCompletionContext::Ptr(m_child),
paramName + "=", i18n("specify default parameter"),
KeywordItem::ImportantItem));
}
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "adding " << functionCalled->defaultParametersSize() << "default args";
}
return resultingItems;
}
PythonCodeCompletionContext::ItemList PythonCodeCompletionContext::defineItems()
{
DUChainReadLocker lock;
ItemList resultingItems;
// Find all base classes of the current class context
if ( m_duContext->type() != DUContext::Class ) {
qCWarning(KDEV_PYTHON_CODECOMPLETION) << "current context is not a class context, not offering define completion";
return resultingItems;
}
ClassDeclaration* klass = dynamic_cast<ClassDeclaration*>(m_duContext->owner());
if ( ! klass ) {
return resultingItems;
}
QList<DUContext*> baseClassContexts = Helper::internalContextsForClass(
klass->type<StructureType>(), m_duContext->topContext()
);
// This class' context is put first in the list, so all functions existing here
// can be skipped.
baseClassContexts.removeAll(m_duContext.data());
baseClassContexts.prepend(m_duContext.data());
Q_ASSERT(baseClassContexts.size() >= 1);
QList<IndexedString> existingIdentifiers;
bool isOwnContext = true;
foreach ( DUContext* c, baseClassContexts ) {
QList<DeclarationDepthPair> declarations = c->allDeclarations(
CursorInRevision::invalid(), m_duContext->topContext(), false
);
foreach ( const DeclarationDepthPair& d, declarations ) {
if ( FunctionDeclaration* funcDecl = dynamic_cast<FunctionDeclaration*>(d.first) ) {
// python does not have overloads or similar, so comparing the function names is enough.
const IndexedString identifier = funcDecl->identifier().identifier();
if ( isOwnContext ) {
existingIdentifiers << identifier;
}
if ( existingIdentifiers.contains(identifier) ) {
continue;
}
existingIdentifiers << identifier;
QStringList argumentNames;
DUContext* argumentsContext = DUChainUtils::getArgumentContext(funcDecl);
if ( argumentsContext ) {
foreach ( Declaration* argument, argumentsContext->localDeclarations() ) {
argumentNames << argument->identifier().toString();
}
resultingItems << CompletionTreeItemPointer(new ImplementFunctionCompletionItem(
funcDecl->identifier().toString(), argumentNames, m_indent)
);
}
}
}
isOwnContext = false;
}
return resultingItems;
}
PythonCodeCompletionContext::ItemList PythonCodeCompletionContext::raiseItems()
{
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Finding items for raise statement";
DUChainReadLocker lock;
ItemList resultingItems;
ReferencedTopDUContext ctx = Helper::getDocumentationFileContext();
QList< Declaration* > declarations = ctx->findDeclarations(QualifiedIdentifier("BaseException"));
if ( declarations.isEmpty() || ! declarations.first()->abstractType() ) {
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "No valid exception classes found, aborting";
return resultingItems;
}
Declaration* base = declarations.first();
IndexedType baseType = base->abstractType()->indexed();
QList<DeclarationDepthPair> validDeclarations;
ClassDeclaration* current = 0;
StructureType::Ptr type;
auto decls = m_duContext->topContext()->allDeclarations(CursorInRevision::invalid(), m_duContext->topContext());
foreach ( const DeclarationDepthPair d, decls ) {
current = dynamic_cast<ClassDeclaration*>(d.first);
if ( ! current || ! current->baseClassesSize() ) {
continue;
}
FOREACH_FUNCTION( const BaseClassInstance& base, current->baseClasses ) {
if ( base.baseClass == baseType ) {
validDeclarations << d;
}
}
}
auto items = declarationListToItemList(validDeclarations);
if ( m_itemTypeHint == ClassTypeRequested ) {
// used for except <cursor>, we don't want the parentheses there
items = setOmitParentheses(items);
}
resultingItems.append(items);
return resultingItems;
}
PythonCodeCompletionContext::ItemList PythonCodeCompletionContext::importFileItems()
{
DUChainReadLocker lock;
ItemList resultingItems;
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Preparing to do autocompletion for import...";
m_maxFolderScanDepth = 1;
resultingItems << includeItemsForSubmodule("");
return resultingItems;
}
PythonCodeCompletionContext::ItemList PythonCodeCompletionContext::inheritanceItems()
{
ItemList resultingItems;
DUChainReadLocker lock;
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "InheritanceCompletion";
QList<DeclarationDepthPair> declarations;
if ( ! m_guessTypeOfExpression.isEmpty() ) {
// The class completion is a member access
lock.unlock();
auto v = visitorForString(m_guessTypeOfExpression, m_duContext.data());
lock.lock();
if ( v ) {
TypePtr<StructureType> cls = StructureType::Ptr::dynamicCast(v->lastType());
if ( cls && cls->declaration(m_duContext->topContext()) ) {
if ( DUContext* internal = cls->declaration(m_duContext->topContext())->internalContext() ) {
declarations = internal->allDeclarations(m_position, m_duContext->topContext(), false);
}
}
}
}
else {
declarations = m_duContext->allDeclarations(m_position, m_duContext->topContext());
}
QList<DeclarationDepthPair> remainingDeclarations;
foreach ( const DeclarationDepthPair& d, declarations ) {
Declaration* r = Helper::resolveAliasDeclaration(d.first);
if ( r && r->topContext() == Helper::getDocumentationFileContext() ) {
continue;
}
if ( r && dynamic_cast<ClassDeclaration*>(r) ) {
remainingDeclarations << d;
}
}
resultingItems.append(setOmitParentheses(declarationListToItemList(remainingDeclarations)));
return resultingItems;
}
PythonCodeCompletionContext::ItemList PythonCodeCompletionContext::memberAccessItems()
{
ItemList resultingItems;
auto v = visitorForString(m_guessTypeOfExpression, m_duContext.data());
DUChainReadLocker lock;
if ( v ) {
if ( v->lastType() ) {
qCDebug(KDEV_PYTHON_CODECOMPLETION) << v->lastType()->toString();
resultingItems << getCompletionItemsForType(v->lastType());
}
else {
qCWarning(KDEV_PYTHON_CODECOMPLETION) << "Did not receive a type from expression visitor! Not offering autocompletion.";
}
}
else {
qCWarning(KDEV_PYTHON_CODECOMPLETION) << "Completion requested for syntactically invalid expression, not offering anything";
}
// append eventually stripped postfix, for e.g. os.chdir|
bool needDot = true;
foreach ( const QChar& c, m_followingText ) {
if ( needDot ) {
m_guessTypeOfExpression.append('.');
needDot = false;
}
if ( c.isLetterOrNumber() || c == '_' ) {
m_guessTypeOfExpression.append(c);
}
}
if ( resultingItems.isEmpty() && m_fullCompletion ) {
resultingItems << getMissingIncludeItems(m_guessTypeOfExpression);
}
return resultingItems;
}
PythonCodeCompletionContext::ItemList PythonCodeCompletionContext::stringFormattingItems()
{
if ( ! m_fullCompletion ) {
return ItemList();
}
DUChainReadLocker lock;
ItemList resultingItems;
int cursorPosition;
StringFormatter stringFormatter(CodeHelpers::extractStringUnderCursor(m_text,
m_duContext->range().castToSimpleRange(),
m_position.castToSimpleCursor(),
&cursorPosition));
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Next identifier id: " << stringFormatter.nextIdentifierId();
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Cursor position in string: " << cursorPosition;
bool insideReplacementVariable = stringFormatter.isInsideReplacementVariable(cursorPosition);
RangeInString variablePosition = stringFormatter.getVariablePosition(cursorPosition);
bool onVariableBoundary = (cursorPosition == variablePosition.beginIndex || cursorPosition == variablePosition.endIndex);
if ( ! insideReplacementVariable || onVariableBoundary ) {
resultingItems << CompletionTreeItemPointer(new ReplacementVariableItem(
ReplacementVariable(QString::number(stringFormatter.nextIdentifierId())),
i18n("Insert next positional variable"), false)
);
resultingItems << CompletionTreeItemPointer(new ReplacementVariableItem(
ReplacementVariable("${argument}"),
i18n("Insert named variable"), true)
);
}
if ( ! insideReplacementVariable ) {
return resultingItems;
}
const ReplacementVariable *variable = stringFormatter.getReplacementVariable(cursorPosition);
// Convert the range relative to the beginning of the string to the absolute position
// in the document. We can safely assume that the replacement variable is on one line,
// because the regex does not allow newlines inside replacement variables.
KTextEditor::Range range;
range.setStart({m_position.line, m_position.column - (cursorPosition - variablePosition.beginIndex)});
range.setEnd({m_position.line, m_position.column + (variablePosition.endIndex - cursorPosition)});
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Variable under cursor: " << variable->toString();
bool hasNumericOnlyOption = variable->hasPrecision()
|| (variable->hasType() && variable->type() != 's')
|| variable->align() == '=';
auto makeFormattingItem = [&variable, &range](const QChar& conversion, const QString& spec,
const QString& description, bool useTemplateEngine)
{
return CompletionTreeItemPointer(
new ReplacementVariableItem(ReplacementVariable(variable->identifier(), conversion, spec),
description, useTemplateEngine, range)
);
};
if ( ! variable->hasConversion() && ! hasNumericOnlyOption ) {
auto addConversionItem = [&](const QChar& conversion, const QString& title) {
resultingItems.append(makeFormattingItem(conversion, variable->formatSpec(), title, false));
};
addConversionItem('s', i18n("Format using str()"));
addConversionItem('r', i18n("Format using repr()"));
}
if ( ! variable->hasFormatSpec() ) {
auto addFormatSpec = [&](const QString& format, const QString& title, bool useTemplateEngine)
{
resultingItems.append(makeFormattingItem(variable->conversion(), format, title, useTemplateEngine));
};
addFormatSpec("<${width}", i18n("Format as left-aligned"), true);
addFormatSpec(">${width}", i18n("Format as right-aligned"), true);
addFormatSpec("^${width}", i18n("Format as centered"), true);
// These options don't make sense if we've set conversion using str() or repr()
if ( ! variable->hasConversion() ) {
addFormatSpec(".${precision}", i18n("Specify precision"), true);
addFormatSpec("%", i18n("Format as percentage"), false);
addFormatSpec("c", i18n("Format as character"), false);
addFormatSpec("b", i18n("Format as binary number"), false);
addFormatSpec("o", i18n("Format as octal number"), false);
addFormatSpec("x", i18n("Format as hexadecimal number"), false);
addFormatSpec("e", i18n("Format in scientific (exponent) notation"), false);
addFormatSpec("f", i18n("Format as fixed point number"), false);
}
}
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Resulting items size: " << resultingItems.size();
return resultingItems;
}
PythonCodeCompletionContext::ItemList PythonCodeCompletionContext::keywordItems()
{
ItemList resultingItems;
QStringList keywordItems;
keywordItems << "def" << "class" << "lambda" << "global" << "import"
<< "from" << "while" << "for" << "yield" << "return";
foreach ( const QString& current, keywordItems ) {
KeywordItem* k = new KeywordItem(KDevelop::CodeCompletionContext::Ptr(this), current + " ", "");
resultingItems << CompletionTreeItemPointer(k);
}
return resultingItems;
}
PythonCodeCompletionContext::ItemList PythonCodeCompletionContext::classMemberInitItems()
{
DUChainReadLocker lock;
ItemList resultingItems;
Declaration* decl = duContext()->owner();
if ( ! decl ) {
return resultingItems;
}
DUContext* args = DUChainUtils::getArgumentContext(duContext()->owner());
if ( ! args ) {
return resultingItems;
}
if ( ! decl->isFunctionDeclaration() || decl->identifier() != KDevelop::Identifier("__init__") ) {
return resultingItems;
}
// the current context actually belongs to a constructor
foreach ( const Declaration* argument, args->localDeclarations() ) {
const QString argName = argument->identifier().toString();
// Do not suggest "self.self = self"
if ( argName == "self" ) {
continue;
}
bool usedAlready = false;
// Do not suggest arguments which already have a use in the context
// This is uesful because you can then do { Ctrl+Space Enter Enter } while ( 1 )
// to initialize all available class variables, without using arrow keys.
for ( int i = 0; i < duContext()->usesCount(); i++ ) {
if ( duContext()->uses()[i].usedDeclaration(duContext()->topContext()) == argument ) {
usedAlready = true;
break;
}
}
if ( usedAlready ) {
continue;
}
const QString value = "self." + argName + " = " + argName;
KeywordItem* item = new KeywordItem(KDevelop::CodeCompletionContext::Ptr(this),
value, i18n("Initialize property"),
KeywordItem::ImportantItem);
resultingItems.append(CompletionTreeItemPointer(item));
}
return resultingItems;
}
PythonCodeCompletionContext::ItemList PythonCodeCompletionContext::generatorItems()
{
ItemList resultingItems;
QList<KeywordItem*> items;
auto v = visitorForString(m_guessTypeOfExpression, m_duContext.data(), m_position);
DUChainReadLocker lock;
if ( ! v || v->unknownNames().isEmpty() ) {
return resultingItems;
}
if ( v->unknownNames().size() >= 2 ) {
// we only take the first two, and only two. It gets too much items otherwise.
QStringList combinations;
auto names = v->unknownNames().toList();
combinations << names.at(0) + ", " + names.at(1);
combinations << names.at(1) + ", " + names.at(0);
foreach ( const QString& c, combinations ) {
items << new KeywordItem(KDevelop::CodeCompletionContext::Ptr(this), "" + c + " in ", "");
}
}
foreach ( const QString& n, v->unknownNames() ) {
items << new KeywordItem(KDevelop::CodeCompletionContext::Ptr(this), "" + n + " in ", "");
}
foreach ( KeywordItem* item, items ) {
resultingItems << CompletionTreeItemPointer(item);
}
return resultingItems;
}
QList<CompletionTreeItemPointer> PythonCodeCompletionContext::completionItems(bool& abort, bool fullCompletion)
{
m_fullCompletion = fullCompletion;
ItemList resultingItems;
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Line: " << m_position.line;
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Completion type:" << m_operation;
if ( m_operation != FunctionCallCompletion ) {
resultingItems.append(shebangItems());
}
// Find all calltips recursively
if ( parentContext() ) {
resultingItems.append(parentContext()->completionItems(abort, fullCompletion));
}
if ( m_operation == PythonCodeCompletionContext::NoCompletion ) {
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "no code completion";
}
else if ( m_operation == PythonCodeCompletionContext::GeneratorVariableCompletion ) {
resultingItems.append(generatorItems());
}
else if ( m_operation == PythonCodeCompletionContext::FunctionCallCompletion ) {
resultingItems.append(functionCallItems());
}
else if ( m_operation == PythonCodeCompletionContext::DefineCompletion ) {
resultingItems.append(defineItems());
}
else if ( m_operation == PythonCodeCompletionContext::RaiseExceptionCompletion ) {
resultingItems.append(raiseItems());
}
else if ( m_operation == PythonCodeCompletionContext::ImportFileCompletion ) {
resultingItems.append(importFileItems());
}
else if ( m_operation == PythonCodeCompletionContext::ImportSubCompletion ) {
DUChainReadLocker lock;
resultingItems.append(includeItemsForSubmodule(m_searchImportItemsInModule));
}
else if ( m_operation == PythonCodeCompletionContext::InheritanceCompletion ) {
resultingItems.append(inheritanceItems());
}
else if ( m_operation == PythonCodeCompletionContext::MemberAccessCompletion ) {
resultingItems.append(memberAccessItems());
}
else if ( m_operation == PythonCodeCompletionContext::StringFormattingCompletion ) {
resultingItems.append(stringFormattingItems());
}
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 ) {
resultingItems.append(keywordItems());
}
if ( m_operation == PythonCodeCompletionContext::NewStatementCompletion ) {
// Eventually suggest initializing class members from constructor arguments
resultingItems.append(classMemberInitItems());
}
if ( abort ) {
return ItemList();
}
DUChainReadLocker lock;
QList<DeclarationDepthPair> declarations = m_duContext->allDeclarations(m_position, m_duContext->topContext());
foreach ( const DeclarationDepthPair& d, declarations ) {
if ( d.first && d.first->context()->type() == DUContext::Class ) {
declarations.removeAll(d);
}
}
resultingItems.append(declarationListToItemList(declarations));
}
m_searchingForModule.clear();
m_searchImportItemsInModule.clear();
return resultingItems;
}
QList<CompletionTreeItemPointer> PythonCodeCompletionContext::getMissingIncludeItems(QString forString)
{
QList<CompletionTreeItemPointer> items;
// Find all the non-empty name components (mainly, remove the last empty one for "sys." or similar)
QStringList components = forString.split('.');
components.removeAll(QString());
// Check all components are alphanumeric
QRegExp alnum("\\w*");
foreach ( const QString& component, components ) {
if ( ! alnum.exactMatch(component) ) return items;
}
if ( components.isEmpty() ) {
return items;
}
Declaration* existing = Helper::declarationForName(QualifiedIdentifier(components.first()),
RangeInRevision(m_position, m_position),
DUChainPointer<const DUContext>(m_duContext.data()));
if ( existing ) {
// There's already a declaration for the first component; no need to suggest it
return items;
}
// See if there's a module called like that.
auto found = ContextBuilder::findModulePath(components.join("."), m_workingOnDocument);
// Check if anything was found
if ( found.first.isValid() ) {
// Add items for the "from" and the plain import
if ( components.size() > 1 && found.second.isEmpty() ) {
// There's something left for X in "from foo import X",
// and it's not a declaration inside the module so offer that
const QString module = QStringList(components.mid(0, components.size() - 1)).join(".");
const QString text = QString("from %1 import %2").arg(module, components.last());
MissingIncludeItem* item = new MissingIncludeItem(text, components.last(), forString);
items << CompletionTreeItemPointer(item);
}
const QString module = QStringList(components.mid(0, components.size() - found.second.size())).join(".");
const QString text = QString("import %1").arg(module);
MissingIncludeItem* item = new MissingIncludeItem(text, components.last());
items << CompletionTreeItemPointer(item);
}
return items;
}
QList<CompletionTreeItemPointer> PythonCodeCompletionContext::declarationListToItemList(QList<DeclarationDepthPair> declarations, int maxDepth)
{
QList<CompletionTreeItemPointer> items;
DeclarationPointer currentDeclaration;
Declaration* checkDeclaration = 0;
int count = declarations.length();
for ( int i = 0; i < count; i++ ) {
if ( maxDepth && maxDepth > declarations.at(i).second ) {
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Skipped completion item because of its depth";
continue;
}
currentDeclaration = DeclarationPointer(declarations.at(i).first);
PythonDeclarationCompletionItem* item = 0;
checkDeclaration = Helper::resolveAliasDeclaration(currentDeclaration.data());
if ( ! checkDeclaration ) {
continue;
}
if ( checkDeclaration->isFunctionDeclaration()
|| (checkDeclaration->internalContext() && checkDeclaration->internalContext()->type() == DUContext::Class) ) {
item = new FunctionDeclarationCompletionItem(currentDeclaration, KDevelop::CodeCompletionContext::Ptr(this));
}
else {
item = new PythonDeclarationCompletionItem(currentDeclaration, KDevelop::CodeCompletionContext::Ptr(this));
}
if ( ! m_matchAgainst.isEmpty() ) {
item->addMatchQuality(identifierMatchQuality(m_matchAgainst, checkDeclaration->identifier().toString()));
}
items << CompletionTreeItemPointer(item);
}
return items;
}
QList< CompletionTreeItemPointer > PythonCodeCompletionContext::declarationListToItemList(QList< Declaration* > declarations)
{
QList<DeclarationDepthPair> fakeItems;
foreach ( Declaration* d, declarations ) {
fakeItems << DeclarationDepthPair(d, 0);
}
return declarationListToItemList(fakeItems);
}
QList< CompletionTreeItemPointer > PythonCodeCompletionContext::getCompletionItemsForType(AbstractType::Ptr type)
{
type = Helper::resolveAliasType(type);
if ( type->whichType() != AbstractType::TypeUnsure ) {
return getCompletionItemsForOneType(type);
}
QList<CompletionTreeItemPointer> result;
UnsureType::Ptr unsure = type.cast<UnsureType>();
int count = unsure->typesSize();
for ( int i = 0; i < count; i++ ) {
result.append(getCompletionItemsForOneType(unsure->types()[i].abstractType()));
}
// Do some weighting: the more often an entry appears, the better the entry.
// That way, entries which are in all of the types this object could have will
// be sorted higher up.
QStringList itemTitles;
QList<CompletionTreeItemPointer> remove;
for ( int i = 0; i < result.size(); i++ ) {
DeclarationPointer decl = result.at(i)->declaration();
if ( ! decl ) {
itemTitles.append(QString());
continue;
}
const QString& title = decl->identifier().toString();
if ( itemTitles.contains(title) ) {
// there's already an item with that title, increase match quality
int item = itemTitles.indexOf(title);
PythonDeclarationCompletionItem* declItem = dynamic_cast<PythonDeclarationCompletionItem*>(result[item].data());
if ( ! m_fullCompletion ) {
remove.append(result.at(i));
}
if ( declItem ) {
// Add 1 to the match quality of the first item in the list.
declItem->addMatchQuality(1);
}
}
itemTitles.append(title);
}
foreach ( const CompletionTreeItemPointer& ptr, remove ) {
result.removeOne(ptr);
}
return result;
}
QList<CompletionTreeItemPointer> PythonCodeCompletionContext::getCompletionItemsForOneType(AbstractType::Ptr type)
{
type = Helper::resolveAliasType(type);
ReferencedTopDUContext builtinTopContext = Helper::getDocumentationFileContext();
if ( type->whichType() != AbstractType::TypeStructure ) {
return ItemList();
}
// find properties of class declaration
TypePtr<StructureType> cls = StructureType::Ptr::dynamicCast(type);
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Finding completion items for class type";
if ( ! cls || ! cls->internalContext(m_duContext->topContext()) ) {
qCWarning(KDEV_PYTHON_CODECOMPLETION) << "No class type available, no completion offered";
return QList<CompletionTreeItemPointer>();
}
// the PublicOnly will filter out non-explictly defined __get__ etc. functions inherited from object
QList<DUContext*> searchContexts = Helper::internalContextsForClass(cls, m_duContext->topContext(), Helper::PublicOnly);
QList<DeclarationDepthPair> keepDeclarations;
foreach ( const DUContext* currentlySearchedContext, searchContexts ) {
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "searching context " << currentlySearchedContext->scopeIdentifier() << "for autocompletion items";
QList<DeclarationDepthPair> declarations = currentlySearchedContext->allDeclarations(CursorInRevision::invalid(),
m_duContext->topContext(),
false);
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "found" << declarations.length() << "declarations";
// filter out those which are builtin functions, and those which were imported; we don't want those here
// also, discard all magic functions from autocompletion
// TODO rework this, it's maybe not the most elegant solution possible
// TODO rework the magic functions thing, I want them sorted at the end of the list but KTE doesn't seem to allow that
foreach ( const DeclarationDepthPair& current, declarations ) {
if ( current.first->context() != builtinTopContext && ! current.first->identifier().identifier().str().startsWith("__") ) {
keepDeclarations.append(current);
}
else {
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Discarding declaration " << current.first->toString();
}
}
}
return declarationListToItemList(keepDeclarations);
}
QList<CompletionTreeItemPointer> PythonCodeCompletionContext::findIncludeItems(IncludeSearchTarget item)
{
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "TARGET:" << item.directory.path() << item.remainingIdentifiers;
QDir currentDirectory(item.directory.path());
QFileInfoList contents = currentDirectory.entryInfoList(QStringList(), QDir::Files | QDir::Dirs);
bool atBottom = item.remainingIdentifiers.isEmpty();
QList<CompletionTreeItemPointer> items;
QString sourceFile;
if ( item.remainingIdentifiers.isEmpty() ) {
// check for the __init__ file
QFileInfo initFile(item.directory.path(), "__init__.py");
if ( initFile.exists() ) {
IncludeItem init;
init.basePath = item.directory;
init.isDirectory = true;
init.name = "";
if ( ! item.directory.fileName().contains('-') ) {
// Do not include items which contain "-", those are not valid
// modules but instead often e.g. .egg directories
ImportFileItem* importfile = new ImportFileItem(init);
importfile->moduleName = item.directory.fileName();
items << CompletionTreeItemPointer(importfile);
sourceFile = initFile.filePath();
}
}
}
else {
QFileInfo file(item.directory.path(), item.remainingIdentifiers.first() + ".py");
item.remainingIdentifiers.removeFirst();
qCDebug(KDEV_PYTHON_CODECOMPLETION) << " CHECK:" << file.absoluteFilePath();
if ( file.exists() ) {
sourceFile = file.absoluteFilePath();
}
}
if ( ! sourceFile.isEmpty() ) {
IndexedString filename(sourceFile);
TopDUContext* top = DUChain::self()->chainForDocument(filename);
qCDebug(KDEV_PYTHON_CODECOMPLETION) << top;
DUContext* c = internalContextForDeclaration(top, item.remainingIdentifiers);
qCDebug(KDEV_PYTHON_CODECOMPLETION) << " GOT:" << c;
if ( c ) {
// tell function declaration items not to add brackets
items << setOmitParentheses(declarationListToItemList(c->localDeclarations().toList()));
}
else {
// do better next time
DUChain::self()->updateContextForUrl(filename, TopDUContext::AllDeclarationsAndContexts);
}
}
if ( atBottom ) {
// append all python files in the directory
foreach ( QFileInfo file, contents ) {
// TODO windows
if ( file.fileName().startsWith('.') ) {
continue;
}
qCDebug(KDEV_PYTHON_CODECOMPLETION) << " > CONTENT:" << file.absolutePath() << file.fileName();
if ( file.isFile() ) {
if ( file.fileName().endsWith(".py") || file.fileName().endsWith(".so") ) {
IncludeItem fileInclude;
fileInclude.basePath = item.directory;
fileInclude.isDirectory = false;
fileInclude.name = file.fileName().mid(0, file.fileName().length() - 3); // remove ".py"
ImportFileItem* import = new ImportFileItem(fileInclude);
import->moduleName = fileInclude.name;
items << CompletionTreeItemPointer(import);
}
}
else if ( ! file.fileName().contains('-') ) {
IncludeItem dirInclude;
dirInclude.basePath = item.directory;
dirInclude.isDirectory = true;
dirInclude.name = file.fileName();
ImportFileItem* import = new ImportFileItem(dirInclude);
import->moduleName = dirInclude.name;
items << CompletionTreeItemPointer(import);
}
}
}
return items;
}
QList<CompletionTreeItemPointer> PythonCodeCompletionContext::findIncludeItems(QList< Python::IncludeSearchTarget > items)
{
QList<CompletionTreeItemPointer> results;
foreach ( const IncludeSearchTarget& item, items ) {
results << findIncludeItems(item);
}
return results;
}
DUContext* PythonCodeCompletionContext::internalContextForDeclaration(TopDUContext* topContext, QStringList remainingIdentifiers)
{
Declaration* d = 0;
DUContext* c = topContext;
if ( ! topContext ) {
return 0;
}
if ( remainingIdentifiers.isEmpty() ) {
return topContext;
}
do {
QList< Declaration* > decls = c->findDeclarations(QualifiedIdentifier(remainingIdentifiers.first()));
remainingIdentifiers.removeFirst();
if ( decls.isEmpty() ) {
return 0;
}
d = decls.first();
if ( (c = d->internalContext()) ) {
if ( remainingIdentifiers.isEmpty() ) {
return c;
}
}
else return 0;
} while ( d && ! remainingIdentifiers.isEmpty() );
return 0;
}
QList<CompletionTreeItemPointer> PythonCodeCompletionContext::includeItemsForSubmodule(QString submodule)
{
QList<QUrl> searchPaths = Helper::getSearchPaths(m_workingOnDocument);
QStringList subdirs;
if ( ! submodule.isEmpty() ) {
subdirs = submodule.split(".");
}
Q_ASSERT(! subdirs.contains(""));
QList<IncludeSearchTarget> foundPaths;
// this is a bit tricky. We need to find every path formed like /.../foo/bar for
// a query string ("submodule" variable) like foo.bar
// we also need paths like /foo.py, because then bar is probably a module in that file.
// Thus, we first generate a list of possible paths, then match them against those which actually exist
// and then gather all the items in those paths.
foreach ( QUrl currentPath, searchPaths ) {
auto d = QDir(currentPath.path());
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Searching: " << currentPath << subdirs;
int identifiersUsed = 0;
foreach ( const QString& subdir, subdirs ) {
qDebug() << "changing into subdir" << subdir;
if ( ! d.cd(subdir) ) {
break;
}
qCDebug(KDEV_PYTHON_CODECOMPLETION) << d.absolutePath() << d.exists();
identifiersUsed++;
}
QStringList remainingIdentifiers = subdirs.mid(identifiersUsed, -1);
foundPaths.append(IncludeSearchTarget(d.absolutePath(), remainingIdentifiers));
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "Found path:" << d.absolutePath() << remainingIdentifiers << subdirs;
}
return findIncludeItems(foundPaths);
}
PythonCodeCompletionContext::PythonCodeCompletionContext(DUContextPointer context, const QString& remainingText,
QString calledFunction,
int depth, int alreadyGivenParameters,
CodeCompletionContext* child)
: CodeCompletionContext(context, remainingText, CursorInRevision::invalid(), depth)
, m_operation(FunctionCallCompletion)
, m_itemTypeHint(NoHint)
, m_child(child)
, m_guessTypeOfExpression(calledFunction)
, m_alreadyGivenParametersCount(alreadyGivenParameters)
, m_fullCompletion(false)
{
ExpressionParser p(remainingText);
summonParentForEventualCall(p.popAll(), remainingText);
}
void PythonCodeCompletionContext::summonParentForEventualCall(TokenList allExpressions, const QString& text)
{
DUChainReadLocker lock;
int offset = 0;
while ( true ) {
QPair<int, int> nextCall = allExpressions.nextIndexOfStatus(ExpressionParser::EventualCallFound, offset);
qCDebug(KDEV_PYTHON_CODECOMPLETION) << "next call:" << nextCall;
qCDebug(KDEV_PYTHON_CODECOMPLETION) << allExpressions.toString();
if ( nextCall.first == -1 ) {
// no more eventual calls
break;
}
offset = nextCall.first;
allExpressions.reset(offset);
TokenListEntry eventualFunction = allExpressions.weakPop();
qCDebug(KDEV_PYTHON_CODECOMPLETION) << eventualFunction.expression << eventualFunction.status;
// it's only a call if a "(" bracket is followed (<- direction) by an expression.
if ( eventualFunction.status != ExpressionParser::ExpressionFound ) {