From 9121c21230829b3897188c849c0247b8c67b8da5 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Thu, 19 Jul 2012 14:35:43 +0200 Subject: C++: Fix class scope completion for templates. * You now get completion for std::vector::[complete]. * Also added a test. Conflicts: src/plugins/cpptools/cppcompletion_test.cpp src/plugins/cpptools/cpptoolsplugin.h Change-Id: I596ebf6bd18ec9a347113f8d162cc124c8a0d6b4 Reviewed-by: hjk --- src/plugins/cpptools/cppcompletion_test.cpp | 198 +++++++++++++++++++++++++++ src/plugins/cpptools/cppcompletionassist.cpp | 7 + 2 files changed, 205 insertions(+) create mode 100644 src/plugins/cpptools/cppcompletion_test.cpp diff --git a/src/plugins/cpptools/cppcompletion_test.cpp b/src/plugins/cpptools/cppcompletion_test.cpp new file mode 100644 index 00000000000..f02e8640432 --- /dev/null +++ b/src/plugins/cpptools/cppcompletion_test.cpp @@ -0,0 +1,198 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "cpptoolsplugin.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +/*! + Tests for code completion. + */ +using namespace CPlusPlus; +using namespace CppTools; +using namespace CppTools::Internal; +using namespace TextEditor; +using namespace Core; + +struct TestData +{ + QByteArray srcText; + int pos; + Snapshot snapshot; + BaseTextEditorWidget *editor; + QTextDocument *doc; +}; + +static QStringList getCompletions(TestData &data) +{ + QStringList completions; + + CppCompletionAssistInterface *ai = new CppCompletionAssistInterface(data.editor->document(), data.pos, + data.editor->editorDocument(), ExplicitlyInvoked, + data.snapshot, QStringList(), QStringList()); + CppCompletionAssistProcessor processor; + IAssistProposal *proposal = processor.perform(ai); + if (!proposal) + return completions; + IAssistProposalModel *model = proposal->model(); + if (!model) + return completions; + BasicProposalItemListModel *listmodel = dynamic_cast(model); + if (!listmodel) + return completions; + + for (int i = 0; i < listmodel->size(); ++i) + completions << listmodel->text(i); + + return completions; +} + +static void setup(TestData *data) +{ + data->pos = data->srcText.indexOf('@'); + QVERIFY(data->pos != -1); + data->srcText[data->pos] = ' '; + Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h")); + Utils::FileSaver srcSaver(src->fileName()); + srcSaver.write(data->srcText); + srcSaver.finalize(); + src->setUtf8Source(data->srcText); + src->parse(); + src->check(); + + data->snapshot.insert(src); + + data->editor = new PlainTextEditorWidget(0); + QString error; + data->editor->open(&error, src->fileName(), src->fileName()); + + data->doc = data->editor->document(); +} + +void CppToolsPlugin::test_completion_basic_1() +{ + TestData data; + data.srcText = "\n" + "class Foo\n" + "{\n" + " void foo();\n" + " int m;\n" + "};\n" + "\n" + "void func() {\n" + " Foo f;\n" + " @\n" + " // padding so we get the scope right\n" + "}"; + + setup(&data); + + QStringList basicCompletions = getCompletions(data); + + QVERIFY(!basicCompletions.contains("foo")); + QVERIFY(!basicCompletions.contains("m")); + QVERIFY(basicCompletions.contains("Foo")); + QVERIFY(basicCompletions.contains("func")); + QVERIFY(basicCompletions.contains("f")); + + Utils::ChangeSet change; + change.insert(data.pos, "f."); + QTextCursor cursor(data.doc); + change.apply(&cursor); + data.pos += 2; + + QStringList memberCompletions = getCompletions(data); + + QVERIFY(memberCompletions.contains("foo")); + QVERIFY(memberCompletions.contains("m")); + QVERIFY(!memberCompletions.contains("func")); + QVERIFY(!memberCompletions.contains("f")); +} + +void CppToolsPlugin::test_completion_template_1() +{ + TestData data; + data.srcText = "\n" + "template \n" + "class Foo\n" + "{\n" + " typedef T Type;\n" + " T foo();\n" + " T m;\n" + "};\n" + "\n" + "void func() {\n" + " Foo f;\n" + " @\n" + " // padding so we get the scope right\n" + "}"; + + setup(&data); + + Utils::ChangeSet change; + change.insert(data.pos, "Foo::"); + QTextCursor cursor(data.doc); + change.apply(&cursor); + data.pos += 5; + + QStringList completions = getCompletions(data); + + QVERIFY(completions.contains("Type")); + QVERIFY(completions.contains("foo")); + QVERIFY(completions.contains("m")); + QVERIFY(!completions.contains("T")); + QVERIFY(!completions.contains("f")); + QVERIFY(!completions.contains("func")); +} diff --git a/src/plugins/cpptools/cppcompletionassist.cpp b/src/plugins/cpptools/cppcompletionassist.cpp index e7f41b2b733..609c72981e8 100644 --- a/src/plugins/cpptools/cppcompletionassist.cpp +++ b/src/plugins/cpptools/cppcompletionassist.cpp @@ -1432,6 +1432,13 @@ bool CppCompletionAssistProcessor::completeScope(const QListasTemplateType()) { + if (!result.binding()) + continue; + if (ClassOrNamespace *b = result.binding()->lookupType(templ->name())) { + completeClass(b); + break; + } } } -- cgit v1.2.3