Commit 713c1016 authored by Kevin Funk's avatar Kevin Funk

Properly hide non-public methods from completion

Also make sure we don't offer stuff like ctor/dtors, copy ctors,
operator function as completion items. It's (imo) not useful -- and we
didn't do it in oldcpp either.
parent e25f237e
......@@ -272,6 +272,33 @@ int codeCompletionPriorityToMatchQuality(unsigned int completionPriority)
return qBound(0u, completionPriority, 80u) / 8;
}
/**
* @return Whether the declaration represented by identifier @p identifier qualifies as completion result
*
* For example, we don't want to offer SomeClass::operator= as completion item to the user
*/
bool isValidCompletionIdentifier(const QualifiedIdentifier& identifier)
{
const int count = identifier.count();
if (identifier.count() < 2) {
return true;
}
const Identifier scope = identifier.at(count-2);
const Identifier id = identifier.last();
if (scope == id) {
return false; // is constructor
}
const QString idString = id.toString();
if (idString.startsWith("~") && scope.toString() == idString.mid(1)) {
return false; // is destructor
}
if (idString.startsWith("operator")) {
return false; // is operator
}
return true;
}
}
ClangCodeCompletionContext::ClangCodeCompletionContext(const DUContextPointer& context,
......@@ -351,6 +378,12 @@ QList<CompletionTreeItemPointer> ClangCodeCompletionContext::completionItems(boo
for (uint i = 0; i < m_results->NumResults; ++i) {
auto result = m_results->Results[i];
const auto availability = clang_getCompletionAvailability(result.CompletionString);
if (availability == CXAvailability_NotAvailable || availability == CXAvailability_NotAccessible) {
continue;
}
const bool isMacroDefinition = result.CursorKind == CXCursor_MacroDefinition;
if (isMacroDefinition && m_filters & NoMacros) {
continue;
......@@ -450,6 +483,10 @@ QList<CompletionTreeItemPointer> ClangCodeCompletionContext::completionItems(boo
}
qid.push(id);
if (!isValidCompletionIdentifier(qid)) {
continue;
}
Declaration* found = 0;
foreach(Declaration* dec, ctx->findDeclarations(qid, m_position)) {
if (!handled.contains(dec)) {
......
......@@ -124,20 +124,12 @@ void TestCodeCompletion::testClangCodeCompletion_data()
QTest::newRow("dotmemberaccess")
<< "class Foo { public: void foo() {} }; int main() { Foo f; \nf. "
<< CompletionItemsList{{{1, 2}, {
"Foo",
"foo()",
"operator=(Foo &&)",
"operator=(const Foo &)",
"~Foo()",
}}};
QTest::newRow("arrowmemberaccess")
<< "class Foo { public: void foo() {} }; int main() { Foo* f = new Foo; \nf-> }"
<< CompletionItemsList{{{1, 3}, {
"Foo",
"foo()",
"operator=(Foo &&)",
"operator=(const Foo &)",
"~Foo()",
}}};
QTest::newRow("enum-case")
<< "enum Foo { foo, bar }; int main() { Foo f; switch (f) {\ncase "
......@@ -145,6 +137,23 @@ void TestCodeCompletion::testClangCodeCompletion_data()
"bar",
"foo",
}}};
QTest::newRow("only-private")
<< "class SomeStruct { private: void priv() {} };\n"
"int main() { SomeStruct s;\ns. "
<< CompletionItemsList{{{2, 2}, {
}}};
QTest::newRow("private-public")
<< "class SomeStruct { public: void pub() {} private: void priv() {} };\n"
"int main() { SomeStruct s;\ns. "
<< CompletionItemsList{{{2, 2}, {
"pub()",
}}};
QTest::newRow("protected-public")
<< "class SomeStruct { public: void pub() {} protected: void prot() {} };\n"
"int main() { SomeStruct s;\ns. "
<< CompletionItemsList{{{2, 2}, {
"pub()",
}}};
}
void TestCodeCompletion::testVirtualOverride()
......
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