From bfbf93e64f91630d7fad0bad1c4d38898ed5086b Mon Sep 17 00:00:00 2001 From: Przemyslaw Gorszkowski Date: Tue, 18 Jun 2013 23:05:57 +0200 Subject: C++: fix auto completion for template parameters Fix auto completion for the case when template parameter should be found somewhere of scope of template instantiation declaration. Example: struct A { void foo(); struct B { int b; }; }; template struct Template { T* get() { return 0; } T t; }; void A::foo() { Template templ; templ.get()->//no autocompletion templ.t.//no autocompletion } Task-number: QTCREATORBUG-8852 Task-number: QTCREATORBUG-9169 Change-Id: I56b40776e66740f995ae6fc5d69e3c50139a3af2 Reviewed-by: Nikolai Kosjar --- src/plugins/cpptools/cppcompletion_test.cpp | 208 ++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) (limited to 'src/plugins/cpptools/cppcompletion_test.cpp') diff --git a/src/plugins/cpptools/cppcompletion_test.cpp b/src/plugins/cpptools/cppcompletion_test.cpp index f1229a38d3..45f7dd1dc3 100644 --- a/src/plugins/cpptools/cppcompletion_test.cpp +++ b/src/plugins/cpptools/cppcompletion_test.cpp @@ -3027,3 +3027,211 @@ void CppToolsPlugin::test_completion_local_type_and_member_6() QVERIFY(completions.contains(QLatin1String("OtherType"))); QVERIFY(completions.contains(QLatin1String("otherTypeMember"))); } + +void CppToolsPlugin::test_completion_template_parameter_defined_inside_scope_of_declaration_QTCREATORBUG9169_1() +{ + TestData data; + data.srcText = + "struct A\n" + "{\n" + " void foo();\n" + " struct B\n" + " {\n" + " int b;\n" + " };\n" + "};\n" + "template\n" + "struct Template\n" + "{\n" + " T* get();\n" + "};\n" + "namespace foo\n" + "{\n" + " struct B\n" + " {\n" + " int foo_b;\n" + " };\n" + "}\n" + "using namespace foo;\n" + "void A::foo()\n" + "{\n" + " Template templ;\n" + " @\n" + " // padding so we get the scope right\n" + "}\n" + ; + setup(&data); + + Utils::ChangeSet change; + QString txt = QLatin1String("templ.get()->"); + change.insert(data.pos, txt); + QTextCursor cursor(data.doc); + change.apply(&cursor); + data.pos += txt.length(); + + QStringList completions = getCompletions(data); + + QCOMPARE(completions.size(), 2); + QVERIFY(completions.contains(QLatin1String("B"))); + QVERIFY(completions.contains(QLatin1String("b"))); +} + +void CppToolsPlugin::test_completion_template_parameter_defined_inside_scope_of_declaration_QTCREATORBUG9169_2() +{ + TestData data; + data.srcText = + "struct A\n" + "{\n" + " void foo();\n" + " struct B\n" + " {\n" + " int b;\n" + " };\n" + "};\n" + "template\n" + "struct Template\n" + "{\n" + " T t;\n" + "};\n" + "namespace foo\n" + "{\n" + " struct B\n" + " {\n" + " int foo_b;\n" + " };\n" + "}\n" + "using namespace foo;\n" + "void A::foo()\n" + "{\n" + " Template templ;\n" + " @\n" + " // padding so we get the scope right\n" + "}\n" + ; + setup(&data); + + Utils::ChangeSet change; + QString txt = QLatin1String("templ.t."); + change.insert(data.pos, txt); + QTextCursor cursor(data.doc); + change.apply(&cursor); + data.pos += txt.length(); + + QStringList completions = getCompletions(data); + + QCOMPARE(completions.size(), 2); + QVERIFY(completions.contains(QLatin1String("B"))); + QVERIFY(completions.contains(QLatin1String("b"))); +} + +void CppToolsPlugin::test_completion_template_parameter_defined_inside_scope_of_declaration_QTCREATORBUG8852_1() +{ + TestData data; + data.srcText = + "template \n" + "struct QList\n" + "{\n" + " T at(int i) const;\n" + "};\n" + "namespace ns\n" + "{\n" + " struct Foo { int bar; };\n" + " void foo()\n" + " {\n" + " QList list;\n" + " @\n" + " // padding so we get the scope right\n" + " }\n" + "}\n" + ; + setup(&data); + + Utils::ChangeSet change; + QString txt = QLatin1String("list.at(0)."); + change.insert(data.pos, txt); + QTextCursor cursor(data.doc); + change.apply(&cursor); + data.pos += txt.length(); + + QStringList completions = getCompletions(data); + + QCOMPARE(completions.size(), 2); + QVERIFY(completions.contains(QLatin1String("Foo"))); + QVERIFY(completions.contains(QLatin1String("bar"))); +} + +void CppToolsPlugin::test_completion_template_parameter_defined_inside_scope_of_declaration_QTCREATORBUG8852_2() +{ + TestData data; + data.srcText = + "template \n" + "struct QList\n" + "{\n" + " T at(int i) const;\n" + "};\n" + "namespace ns\n" + "{\n" + " struct Foo { int bar; };\n" + " namespace nested\n" + " {\n" + " void foo()\n" + " {\n" + " QList list;\n" + " @\n" + " // padding so we get the scope right\n" + " }\n" + " }\n" + "}\n" + ; + setup(&data); + + Utils::ChangeSet change; + QString txt = QLatin1String("list.at(0)."); + change.insert(data.pos, txt); + QTextCursor cursor(data.doc); + change.apply(&cursor); + data.pos += txt.length(); + + QStringList completions = getCompletions(data); + + QCOMPARE(completions.size(), 2); + QVERIFY(completions.contains(QLatin1String("Foo"))); + QVERIFY(completions.contains(QLatin1String("bar"))); +} + +void CppToolsPlugin::test_completion_template_parameter_defined_inside_scope_of_declaration_QTCREATORBUG8852_3() +{ + TestData data; + data.srcText = + "template \n" + "struct QList\n" + "{\n" + " T at(int i) const;\n" + "};\n" + "namespace ns\n" + "{\n" + " struct Foo { int bar; };\n" + "}\n" + "void foo()\n" + "{\n" + " using namespace ns;\n" + " QList list;\n" + " @\n" + " // padding so we get the scope right\n" + "}\n" + ; + setup(&data); + + Utils::ChangeSet change; + QString txt = QLatin1String("list.at(0)."); + change.insert(data.pos, txt); + QTextCursor cursor(data.doc); + change.apply(&cursor); + data.pos += txt.length(); + + QStringList completions = getCompletions(data); + + QCOMPARE(completions.size(), 2); + QVERIFY(completions.contains(QLatin1String("Foo"))); + QVERIFY(completions.contains(QLatin1String("bar"))); +} -- cgit v1.2.1