Added pseudo autocompletion support for builtin types · mali/kdev-python@51f4309 · GitHub
Skip to content

Commit 51f4309

Browse files
committed
Added pseudo autocompletion support for builtin types
works, but data is missing (i.e. which type has wich attributes)
1 parent 1c47bce commit 51f4309

10 files changed

Lines changed: 136 additions & 40 deletions

‎CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion

‎codecompletion/pythoncodecompletioncontext.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ PythonCodeCompletionContext::PythonCodeCompletionContext(DUContextPointer contex
267267
QStringList expr = currentLine.split(".");
268268
expr.removeAll("");
269269
m_guessTypeOfExpression = expr.join(".");
270-
m_guessTypeOfExpression.replace(QRegExp("\"(.*)\""), "\"STRING\"");
270+
m_guessTypeOfExpression.replace(QRegExp("\"(.*)\""), "\"STRING\"").replace(QRegExp("(.*)\n[\\s]*"), "");
271271
kDebug() << "Guess type of this expression: " << m_guessTypeOfExpression;
272272
m_operation = PythonCodeCompletionContext::MemberAccessCompletion;
273273
return;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# This file denotes the built-in python API. It is imported into every file which is parsed.
2+
class Exception:
3+
pass
4+
5+
class __kdevpythondocumentation_builtin_list():
6+
def append(obj): pass
7+
def extend(obj): return []
8+
9+
class __kdevpythondocumentation_builtin_dict():
10+
def append(obj): pass
11+
12+
class __kdevpythondocumentation_builtin_string():
13+
def replace(before, after): return ""
14+
15+
class __kdevpythondocumentation_builtin_float():
16+
pass
17+
18+
19+
def abs(x): return 0
20+
def all(iterable): return True
21+
def any(iterable): return True
22+
def bin(x): return ""
23+
def bool(x = False): return True
24+
def bytearray(source = None, encoding = None, errors = None): return []
25+
def callable(object): return True
26+
def chr(i): return ""
27+
def classmethod(function): return None
28+
def cmp(x, y): return 0
29+
def compile(source, filename, mode, flags = None, dont_inherit = None): return None
30+
def complex(real = 0, imag = 0): return 1+3j
31+
def delattr(obj, name): return None
32+
def dict(arg = None): return {"string" : None}
33+
def dir(obj = None): return {"string" : None}
34+
def divmod(a, b): return 0
35+
def enumerate(sequence, start = 0): return [(0, 0)]
36+
def eval(expression, glob = None, loc = None): return None
37+
def execfile(filename, glob = None, loc = None): return None
38+
def file(filename, mode = None, bufsize = None): return None
39+
def filter(function, iterable): return []
40+
def float(x = 0): return 0.0
41+
def format(value, format_spec = None): return ""
42+
def frozenset(iterable = None): return ()
43+
def getattr(obj, name, default = None): return None
44+
def globals(): return {}
45+
def hasattr(obj, name): return bool
46+
def hash(obj): return 0
47+
def hex(x): return 0x0
48+
def id(obj): return 0
49+
def input(prompt = None): return None
50+
def isinstance(obj, cls): return True
51+
def issubclass(cls, info): return True
52+
def iter(o, s = None): return __kdevpythondocumentation_builtin_iterator()
53+
def len(s): return 0
54+
def list(i = None): return []
55+
def locals(): return {}
56+
def long(x = None, base = None): return 0L
57+
def map(func, iterab): return []
58+
def max(lst, args = None, key = None): return 0
59+
def memoryview(obj): return None
60+
def min(lst, default = None): return 0
61+
def next(iterator, default = None): return iterator[0]
62+
def object(): return None
63+
def oct(x): return 0o0
64+
def open(filename, mode = None, bufsize = None): return __kdevpythondocumentation_builtin_fileObject()
65+
def ord(c): return 0
66+
def pow(x, y, z = 0): return 0.0
67+
def property(fget = 0, fset = 0, fdel = 0, doc = 0): return 0
68+
def range(start = 0, stop = 0, step = 0): return []
69+
def raw_input(prompt = ""): return ""
70+
def reduce(function, iterable, init = None): return None
71+
def reload(module) : return None
72+
def repr(object): return ""
73+
def reversed(seq): return __kdevpythondocumentation_builtin_iterator()
74+
def round(x, n=0): return 0.0
75+
def set(iterable = None): return ( )
76+
def setattr(obj, name, value): return None
77+
def slice(start = 0, stop = 0, step = 0): return __kdevpythondocumentation_builtin_sliceObject()
78+
def sorted(iterable, cmpre = None, key = None, reverse = False): return []
79+
def staticmethod(function): return function
80+
def str(obj = None): return ""
81+
def sum(iterable): return 0.0
82+
def super(_type, obj = None): return None
83+
def tuple(iterable = None): return ()
84+
def type(object): return __kdevpythondocumentation_builtin_typeObject()
85+
def unichr(i): return ""
86+
def unicode(obj = None, encoding = None, errors = None): return ""
87+
def vars(obj): return None
88+
def xrange(start = 0, stop = 0, step = 0): return []
89+
def zip(iterable = None): return []
90+
def __import__(name, globa = None, loca = None, fromlist = None, level = 0): return None

‎documentation/test.py‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎duchain/contextbuilder.cpp‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "codecompletion/pythoncodecompletioncontext.h"
4646
#include "pythonparsejob.h"
4747
#include "declarationbuilder.h"
48+
#include "parser/parserConfig.h"
4849

4950
using namespace KDevelop;
5051

@@ -193,10 +194,17 @@ void ContextBuilder::visitArguments(ArgumentsAst* node)
193194
}
194195

195196
void ContextBuilder::visitCode(CodeAst* node) {
196-
{
197-
DUChainWriteLocker lock(DUChain::lock());
198-
TopDUContext* internal = DUChain::self()->chainForDocument(KUrl("/home/sven/projects/kde4/python/documentation/test.py"));
197+
IndexedString doc = IndexedString(QString(INSTALL_PATH) + "/builtindocumentation.py");
198+
if ( document() != doc ) {
199+
DUChainReadLocker lock(DUChain::lock());
200+
TopDUContext* internal = DUChain::self()->chainForDocument(doc); // TODO add startup-check and error message, this must exist
201+
lock.unlock();
202+
203+
if ( ! internal ) DUChain::self()->updateContextForUrl(doc, TopDUContext::AllDeclarationsAndContexts);
204+
199205
if ( internal ) {
206+
kDebug() << "Adding builtin function context...";
207+
DUChainWriteLocker wlock(DUChain::lock());
200208
currentContext()->addImportedParentContext(internal);
201209
}
202210
}
@@ -249,8 +257,10 @@ void ContextBuilder::visitImport(ImportAst* node)
249257
moduleChain = DUChain::self()->chainForDocument(doc);
250258
lock.unlock();
251259
if ( ! moduleChain ) {
252-
DUChain::self()->updateContextForUrl(doc, TopDUContext::AllDeclarationsAndContexts);
253-
ReferencedTopDUContext moduleChain = DUChain::self()->waitForUpdate(doc, TopDUContext::AllDeclarationsAndContexts);
260+
// DUChain::self()->updateContextForUrl(doc, TopDUContext::AllDeclarationsAndContexts);
261+
lock.lock();
262+
ReferencedTopDUContext moduleChain = DUChain::self()->chainForDocument(doc);
263+
lock.unlock();
254264
}
255265

256266
contextsForModules.insert(variableDeclarationName, TopDUContextPointer(moduleChain.data()));

‎duchain/expressionvisitor.cpp‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,26 +202,39 @@ void ExpressionVisitor::visitSubscript(SubscriptAst* node)
202202
}
203203
}
204204

205+
AbstractType::Ptr ExpressionVisitor::typeObjectForIntegralType(QString typeDescriptor)
206+
{
207+
DUChainReadLocker lock(DUChain::lock());
208+
QList<Declaration*> decls = m_ctx->topContext()->findDeclarations(QualifiedIdentifier("__kdevpythondocumentation_builtin_" + typeDescriptor));
209+
Declaration* decl = decls.isEmpty() ? 0 : decls.first();
210+
AbstractType::Ptr type = decl ? decl->abstractType() : AbstractType::Ptr(0);
211+
return type;
212+
}
213+
205214
void ExpressionVisitor::visitList(ListAst* node)
206215
{
207216
AstDefaultVisitor::visitList(node);
208-
encounter(AbstractType::Ptr(new IntegralTypeExtended(IntegralTypeExtended::TypeList)));
217+
AbstractType::Ptr type = typeObjectForIntegralType("list");
218+
encounter(type);
209219
}
210220

211221
void ExpressionVisitor::visitDict(DictAst* node)
212222
{
213223
AstDefaultVisitor::visitDict(node);
214-
encounter(AbstractType::Ptr(new IntegralTypeExtended(IntegralTypeExtended::TypeDict)));
224+
AbstractType::Ptr type = typeObjectForIntegralType("dict");
225+
encounter(type);
215226
}
216227

217-
void Python::ExpressionVisitor::visitNumber(Python::NumberAst* )
228+
void ExpressionVisitor::visitNumber(Python::NumberAst* )
218229
{
219-
encounter(AbstractType::Ptr(new IntegralType(IntegralType::TypeFloat)));
230+
AbstractType::Ptr type = typeObjectForIntegralType("float");
231+
encounter(type);
220232
}
221233

222-
void Python::ExpressionVisitor::visitString(Python::StringAst* )
234+
void ExpressionVisitor::visitString(Python::StringAst* )
223235
{
224-
encounter(AbstractType::Ptr(new IntegralType(IntegralType::TypeString)));
236+
AbstractType::Ptr type = typeObjectForIntegralType("string");
237+
encounter(type);
225238
}
226239

227240
RangeInRevision nodeRange(Python::Ast* node)
@@ -230,7 +243,7 @@ RangeInRevision nodeRange(Python::Ast* node)
230243
return RangeInRevision(node->startLine, node->startCol, node->endLine,node->endCol);
231244
}
232245

233-
void Python::ExpressionVisitor::visitName(Python::NameAst* node)
246+
void ExpressionVisitor::visitName(Python::NameAst* node)
234247
{
235248
KDevelop::Identifier id(node->identifier->value);
236249
QHash < KDevelop::Identifier, AbstractType::Ptr >::const_iterator defId = s_defaultTypes.constFind(id);
@@ -255,7 +268,7 @@ void Python::ExpressionVisitor::visitName(Python::NameAst* node)
255268
}
256269
}
257270

258-
void Python::ExpressionVisitor::visitBinaryOperation(Python::BinaryOperationAst* node)
271+
void ExpressionVisitor::visitBinaryOperation(Python::BinaryOperationAst* node)
259272
{
260273
visitNode(node->lhs);
261274
KDevelop::AbstractType::Ptr leftType = m_lastType;
@@ -273,14 +286,14 @@ void Python::ExpressionVisitor::visitBinaryOperation(Python::BinaryOperationAst*
273286
}
274287
}
275288

276-
void Python::ExpressionVisitor::visitUnaryOperation(Python::UnaryOperationAst* node)
289+
void ExpressionVisitor::visitUnaryOperation(Python::UnaryOperationAst* node)
277290
{
278291
visitNode(node->operand);
279292

280293
//FIXME: m_lastValue = m_lastValue;
281294
}
282295

283-
void Python::ExpressionVisitor::visitBooleanOperation(Python::BooleanOperationAst* node)
296+
void ExpressionVisitor::visitBooleanOperation(Python::BooleanOperationAst* node)
284297
{
285298
//
286299
foreach (ExpressionAst* expression, node->values) {

‎duchain/expressionvisitor.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class KDEVPYTHONDUCHAIN_EXPORT ExpressionVisitor : public AstDefaultVisitor
110110
PythonEditorIntegrator* m_editor;
111111

112112
void encounter(KDevelop::AbstractType::Ptr type);
113+
AbstractType::Ptr typeObjectForIntegralType(QString typeDescriptor);
113114

114115
void unknownTypeEncountered();
115116
DeclarationPointer m_lastAccessedNameDeclaration;

‎pythonparsejob.cpp‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ ParseJob::ParseJob(LanguageSupport* parent, const KUrl &url )
7272
{
7373
kDebug();
7474
m_parent = parent;
75-
ParseJob::internalFunctionsFile = new KUrl(QString(INSTALL_PATH) + QString("/test.py"));
7675
ParseJob::m_internalFunctions = 0;
7776
}
7877

@@ -97,13 +96,6 @@ bool ParseJob::wasReadFromDisk() const
9796
return m_readFromDisk;
9897
}
9998

100-
void ParseJob::checkInternalFunctionsParsed()
101-
{
102-
if ( ! ParseJob::m_internalFunctions ) {
103-
DUChain::self()->updateContextForUrl(IndexedString(*internalFunctionsFile), minimumFeatures());
104-
}
105-
}
106-
10799
void ParseJob::run()
108100
{
109101
kDebug();
@@ -113,8 +105,6 @@ void ParseJob::run()
113105
QReadLocker parselock(ilang->parseLock());
114106
UrlParseLock urlLock(document());
115107

116-
if ( m_url != *internalFunctionsFile ) checkInternalFunctionsParsed();
117-
118108
if (abortRequested() || !python() || !python()->language()) {
119109
kWarning() << "Language support is NULL";
120110
return abortJob();

‎pythonparsejob.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ParseJob : public KDevelop::ParseJob
6565

6666
const LanguageSupport* m_parent;
6767
static TopDUContext* m_internalFunctions;
68-
const KUrl* internalFunctionsFile;
68+
static KUrl internalFunctionsFile;
6969

7070
void checkInternalFunctionsParsed();
7171

‎pythonpythonparser.py‎

Lines changed: 4 additions & 0 deletions

0 commit comments

Comments
 (0)