2013-07-04 20:11:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2013-07-04 20:11:10 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, 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, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "cppvirtualfunctionassistprovider.h"
|
|
|
|
|
|
2013-12-10 10:47:41 +01:00
|
|
|
#include "cppeditor.h"
|
2013-07-04 20:11:10 +02:00
|
|
|
#include "cppeditorconstants.h"
|
2013-10-19 15:57:12 +02:00
|
|
|
#include "cppvirtualfunctionproposalitem.h"
|
2013-07-04 20:11:10 +02:00
|
|
|
|
|
|
|
|
#include <cplusplus/Icons.h>
|
|
|
|
|
#include <cplusplus/Overview.h>
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
|
|
|
|
#include <coreplugin/actionmanager/command.h>
|
|
|
|
|
|
2013-12-10 11:38:16 +01:00
|
|
|
#include <cpptools/functionutils.h>
|
2013-10-21 13:39:48 +02:00
|
|
|
#include <cpptools/symbolfinder.h>
|
2013-12-10 10:47:41 +01:00
|
|
|
#include <cpptools/typehierarchybuilder.h>
|
2013-10-21 13:39:48 +02:00
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
#include <texteditor/codeassist/genericproposalmodel.h>
|
2013-07-04 20:11:10 +02:00
|
|
|
#include <texteditor/codeassist/genericproposal.h>
|
|
|
|
|
#include <texteditor/codeassist/genericproposalwidget.h>
|
2014-09-04 00:04:18 +02:00
|
|
|
#include <texteditor/codeassist/assistinterface.h>
|
2013-07-04 20:11:10 +02:00
|
|
|
#include <texteditor/codeassist/iassistprocessor.h>
|
|
|
|
|
#include <texteditor/codeassist/iassistproposal.h>
|
2013-12-10 10:47:41 +01:00
|
|
|
#include <texteditor/texteditorconstants.h>
|
2013-07-04 20:11:10 +02:00
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
2014-02-17 19:23:20 +02:00
|
|
|
#include <utils/qtcoverride.h>
|
2013-07-04 20:11:10 +02:00
|
|
|
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
using namespace CppEditor::Internal;
|
2013-12-10 11:38:16 +01:00
|
|
|
using namespace CppTools;
|
2013-07-04 20:11:10 +02:00
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
|
|
|
|
/// Activate current item with the same shortcut that is configured for Follow Symbol Under Cursor.
|
|
|
|
|
/// This is limited to single-key shortcuts without modifiers.
|
|
|
|
|
class VirtualFunctionProposalWidget : public GenericProposalWidget
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
VirtualFunctionProposalWidget(bool openInSplit)
|
|
|
|
|
{
|
|
|
|
|
const char *id = openInSplit
|
|
|
|
|
? TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT
|
|
|
|
|
: TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR;
|
|
|
|
|
if (Core::Command *command = Core::ActionManager::command(id))
|
|
|
|
|
m_sequence = command->keySequence();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
bool eventFilter(QObject *o, QEvent *e)
|
|
|
|
|
{
|
|
|
|
|
if (e->type() == QEvent::ShortcutOverride && m_sequence.count() == 1) {
|
|
|
|
|
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
|
|
|
|
|
const QKeySequence seq(ke->key());
|
|
|
|
|
if (seq == m_sequence) {
|
|
|
|
|
activateCurrentProposalItem();
|
|
|
|
|
e->accept();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return GenericProposalWidget::eventFilter(o, e);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-17 19:23:20 +02:00
|
|
|
void showProposal(const QString &prefix) QTC_OVERRIDE
|
2014-02-16 22:05:23 +02:00
|
|
|
{
|
2014-09-04 00:04:18 +02:00
|
|
|
GenericProposalModel *proposalModel = model();
|
2014-02-16 22:05:23 +02:00
|
|
|
if (proposalModel && proposalModel->size() == 1) {
|
|
|
|
|
emit proposalItemActivated(proposalModel->proposalItem(0));
|
|
|
|
|
deleteLater();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
GenericProposalWidget::showProposal(prefix);
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-04 20:11:10 +02:00
|
|
|
private:
|
|
|
|
|
QKeySequence m_sequence;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class VirtualFunctionProposal : public GenericProposal
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-09-13 02:22:24 +02:00
|
|
|
VirtualFunctionProposal(int cursorPos, const QList<AssistProposalItem *> &items, bool openInSplit)
|
|
|
|
|
: GenericProposal(cursorPos, items)
|
2013-07-04 20:11:10 +02:00
|
|
|
, m_openInSplit(openInSplit)
|
|
|
|
|
{}
|
|
|
|
|
|
2014-02-17 19:23:20 +02:00
|
|
|
bool isFragile() const QTC_OVERRIDE { return true; }
|
2013-07-04 20:11:10 +02:00
|
|
|
|
2014-02-17 19:23:20 +02:00
|
|
|
IAssistProposalWidget *createWidget() const QTC_OVERRIDE
|
2013-07-04 20:11:10 +02:00
|
|
|
{ return new VirtualFunctionProposalWidget(m_openInSplit); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool m_openInSplit;
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-19 17:33:44 +02:00
|
|
|
class VirtualFunctionAssistProcessor : public IAssistProcessor
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2014-09-19 17:33:44 +02:00
|
|
|
VirtualFunctionAssistProcessor(const VirtualFunctionAssistProvider::Parameters ¶ms)
|
2013-10-23 15:14:28 +02:00
|
|
|
: m_params(params)
|
2013-07-04 20:11:10 +02:00
|
|
|
{}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
IAssistProposal *immediateProposal(const TextEditor::AssistInterface *) QTC_OVERRIDE
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
2013-10-23 15:14:28 +02:00
|
|
|
QTC_ASSERT(m_params.function, return 0);
|
2013-07-04 20:11:10 +02:00
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
AssistProposalItem *hintItem = new VirtualFunctionProposalItem(CppEditorWidget::Link());
|
2013-07-04 20:11:10 +02:00
|
|
|
hintItem->setText(QCoreApplication::translate("VirtualFunctionsAssistProcessor",
|
|
|
|
|
"...searching overrides"));
|
|
|
|
|
hintItem->setOrder(-1000);
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
QList<AssistProposalItem *> items;
|
2014-02-14 10:42:36 +02:00
|
|
|
items << itemFromFunction(m_params.function);
|
2013-07-04 20:11:10 +02:00
|
|
|
items << hintItem;
|
2014-09-13 02:22:24 +02:00
|
|
|
return new VirtualFunctionProposal(m_params.cursorPosition, items, m_params.openInNextSplit);
|
2013-07-04 20:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-11 11:40:37 +02:00
|
|
|
IAssistProposal *perform(const AssistInterface *assistInterface) QTC_OVERRIDE
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
2014-09-11 11:40:37 +02:00
|
|
|
delete assistInterface;
|
|
|
|
|
|
2013-10-23 15:14:28 +02:00
|
|
|
QTC_ASSERT(m_params.function, return 0);
|
2013-10-28 15:12:18 +01:00
|
|
|
QTC_ASSERT(m_params.staticClass, return 0);
|
2013-10-23 15:14:28 +02:00
|
|
|
QTC_ASSERT(!m_params.snapshot.isEmpty(), return 0);
|
2013-07-04 20:11:10 +02:00
|
|
|
|
2013-10-25 15:04:51 +02:00
|
|
|
Class *functionsClass = m_finder.findMatchingClassDeclaration(m_params.function,
|
|
|
|
|
m_params.snapshot);
|
|
|
|
|
if (!functionsClass)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2014-02-13 23:42:55 +02:00
|
|
|
const QList<Function *> overrides = FunctionUtils::overrides(
|
2013-10-28 15:12:18 +01:00
|
|
|
m_params.function, functionsClass, m_params.staticClass, m_params.snapshot);
|
2013-10-21 13:39:48 +02:00
|
|
|
if (overrides.isEmpty())
|
|
|
|
|
return 0;
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
QList<AssistProposalItem *> items;
|
2014-02-13 23:42:55 +02:00
|
|
|
foreach (Function *func, overrides)
|
2014-02-14 10:42:36 +02:00
|
|
|
items << itemFromFunction(func);
|
2013-10-21 13:39:48 +02:00
|
|
|
items.first()->setOrder(1000); // Ensure top position for function of static type
|
2013-07-04 20:11:10 +02:00
|
|
|
|
2014-09-13 02:22:24 +02:00
|
|
|
return new VirtualFunctionProposal(m_params.cursorPosition, items, m_params.openInNextSplit);
|
2013-07-04 20:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-21 13:39:48 +02:00
|
|
|
private:
|
2014-02-14 10:42:36 +02:00
|
|
|
Function *maybeDefinitionFor(Function *func) const
|
2013-10-21 13:39:48 +02:00
|
|
|
{
|
2014-02-13 23:42:55 +02:00
|
|
|
if (Function *definition = m_finder.findMatchingDefinition(func, m_params.snapshot))
|
2013-10-21 13:39:48 +02:00
|
|
|
return definition;
|
2014-02-13 23:42:55 +02:00
|
|
|
return func;
|
2013-10-21 13:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
AssistProposalItem *itemFromFunction(Function *func) const
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
2014-07-30 16:30:31 +02:00
|
|
|
const CppEditorWidget::Link link = CppEditorWidget::linkToSymbol(maybeDefinitionFor(func));
|
2014-02-14 10:42:36 +02:00
|
|
|
QString text = m_overview.prettyName(LookupContext::fullyQualifiedName(func));
|
|
|
|
|
if (func->isPureVirtual())
|
|
|
|
|
text += QLatin1String(" = 0");
|
2013-07-04 20:11:10 +02:00
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
AssistProposalItem *item = new VirtualFunctionProposalItem(link, m_params.openInNextSplit);
|
2013-07-04 20:11:10 +02:00
|
|
|
item->setText(text);
|
2014-02-13 23:42:55 +02:00
|
|
|
item->setIcon(m_icons.iconForSymbol(func));
|
2013-07-04 20:11:10 +02:00
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 15:14:28 +02:00
|
|
|
VirtualFunctionAssistProvider::Parameters m_params;
|
2013-07-04 20:11:10 +02:00
|
|
|
Overview m_overview;
|
|
|
|
|
Icons m_icons;
|
2014-02-14 10:42:36 +02:00
|
|
|
mutable CppTools::SymbolFinder m_finder;
|
2013-07-04 20:11:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
VirtualFunctionAssistProvider::VirtualFunctionAssistProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 15:14:28 +02:00
|
|
|
bool VirtualFunctionAssistProvider::configure(const Parameters ¶meters)
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
2013-10-23 15:14:28 +02:00
|
|
|
m_params = parameters;
|
2013-07-04 20:11:10 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool VirtualFunctionAssistProvider::isAsynchronous() const
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-01 11:08:26 +02:00
|
|
|
bool VirtualFunctionAssistProvider::supportsEditor(Core::Id editorId) const
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
2014-08-27 10:01:27 +02:00
|
|
|
return editorId == Constants::CPPEDITOR_ID;
|
2013-07-04 20:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IAssistProcessor *VirtualFunctionAssistProvider::createProcessor() const
|
|
|
|
|
{
|
2014-09-19 17:33:44 +02:00
|
|
|
return new VirtualFunctionAssistProcessor(m_params);
|
2013-07-04 20:11:10 +02:00
|
|
|
}
|