Commit cc780e7b authored by Sergey Kalinichev's avatar Sergey Kalinichev

Fix code-completion scope issue.

Prefer declarations from local scope.
parent 4f349dc9
......@@ -449,6 +449,9 @@ Declaration* findDeclaration(const QualifiedIdentifier& qid, const DUContextPoin
for (auto it = decl.iterator(); it; ++it) {
auto declaration = it->declaration();
if (declaration->kind() == Declaration::Instance) {
break;
}
if (!handled.contains(declaration)) {
handled.insert(declaration);
return declaration;
......
......@@ -803,3 +803,29 @@ void TestCodeCompletion::testCompletionPriority_data()
"void Derived::f(){\n }"
<< CompletionPriorityItems{{1,0}, {{"m_protected", 0, 37}}};
}
void TestCodeCompletion::testVariableScope()
{
TestFile file("int var; \nvoid test(int var) {int tmp =\n }", "cpp");
QVERIFY(file.parseAndWait(TopDUContext::AllDeclarationsContextsUsesAndAST));
DUChainReadLocker lock;
auto top = file.topContext();
QVERIFY(top);
const ParseSessionData::Ptr sessionData(dynamic_cast<ParseSessionData*>(top->ast().data()));
QVERIFY(sessionData);
DUContextPointer topPtr(top);
lock.unlock();
const auto context = new ClangCodeCompletionContext(topPtr, sessionData, file.url().toUrl(), {2, 0}, QString());
context->setFilters(ClangCodeCompletionContext::ContextFilters(
ClangCodeCompletionContext::NoBuiltins |
ClangCodeCompletionContext::NoMacros));
lock.lock();
const auto tester = ClangCodeCompletionItemTester(QExplicitlySharedDataPointer<ClangCodeCompletionContext>(context));
QCOMPARE(tester.items.size(), 4);
auto item = tester.findItem(QStringLiteral("var"));
VERIFY(item);
QCOMPARE(item->declaration()->range().start, CursorInRevision(1, 14));
}
......@@ -48,6 +48,7 @@ private slots:
void testCompletionPriority_data();
void testOverloadedFunctions();
void testVariableScope();
};
#endif // TESTCODECOMPLETION_H
Markdown is supported
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