Commit 4c76d857 authored by Sven Brauch's avatar Sven Brauch
Browse files

Correctly build uses for declarations in some corner cases

If the context opened by a node begins after that node does, which is
for example the case for class declarations, then stuff that was being
parsed between the node start and the start of the context was
considered to be in the wrong context. Fixes 309817.
BUG: 309817
parent a51f2383
......@@ -81,6 +81,7 @@ void PyDUChainTest::initShell()
QFile f("/tmp/i.py");
f.open(QIODevice::WriteOnly);
f.write("def checkme(): pass\nlocalvar1 = 3\nlocalvar2 = 5\n");
f.write("class testclass():\n attr = 3\n");
f.close();
DUChain::self()->updateContextForUrl(IndexedString("/tmp/i.py"), KDevelop::TopDUContext::AllDeclarationsContextsAndUses);
......@@ -796,6 +797,7 @@ void PyDUChainTest::testInheritance_data()
QTest::addColumn<QString>("code");
QTest::newRow("simple") << "class A():\n\tattr = 3\n\nclass B(A):\n\tpass\n\ninst=B()\ncheckme = inst.attr";
QTest::newRow("context_import") << "import i\n\nclass B(i.testclass):\n\ti = 4\n\ninst=B()\ncheckme = inst.attr";
}
void PyDUChainTest::testClassContextRanges()
......
......@@ -51,10 +51,24 @@ UseBuilder::UseBuilder (PythonEditorIntegrator* editor) : UseBuilderBase(), m_er
setEditor(editor);
}
DUContext* UseBuilder::contextAtOrCurrent(const CursorInRevision& pos)
{
DUContext* context = 0;
{
DUChainReadLocker lock;
context = topContext()->findContextAt(pos, true);
}
if ( ! context ) {
context = currentContext();
}
return context;
}
void UseBuilder::visitName(NameAst* node)
{
DUContext* context = contextAtOrCurrent(editorFindPositionSafe(node));
Declaration* declaration = Helper::declarationForName(node, identifierForNode(node->identifier),
editorFindRange(node, node), DUContextPointer(currentContext()));
editorFindRange(node, node), DUContextPointer(context));
QStringList keywords;
keywords << "None" << "True" << "False" << "print";
......@@ -99,7 +113,8 @@ void UseBuilder::visitName(NameAst* node)
void UseBuilder::visitAttribute(AttributeAst* node)
{
ExpressionVisitor v(currentContext(), editor());
DUContext* context = contextAtOrCurrent(editorFindPositionSafe(node));
ExpressionVisitor v(context);
kDebug() << "VisitAttribute start";
UseBuilderBase::visitAttribute(node);
kDebug() << "Visit Attribute base end";
......
......@@ -61,6 +61,7 @@ private:
inline void enableErrorReporting() {
m_errorReportingEnabled = true;
};
DUContext* contextAtOrCurrent(const CursorInRevision& pos);
};
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment