cppvirtualfunctionassistprovider.cpp 7.8 KB
Newer Older
1 2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
** 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"

33
#include "cppeditor.h"
34
#include "cppeditorconstants.h"
35
#include "cppvirtualfunctionproposalitem.h"
36 37 38 39 40 41 42

#include <cplusplus/Icons.h>
#include <cplusplus/Overview.h>

#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>

43
#include <cpptools/functionutils.h>
44
#include <cpptools/symbolfinder.h>
45
#include <cpptools/typehierarchybuilder.h>
46

47 48 49 50 51 52
#include <texteditor/codeassist/basicproposalitemlistmodel.h>
#include <texteditor/codeassist/genericproposal.h>
#include <texteditor/codeassist/genericproposalwidget.h>
#include <texteditor/codeassist/iassistinterface.h>
#include <texteditor/codeassist/iassistprocessor.h>
#include <texteditor/codeassist/iassistproposal.h>
53
#include <texteditor/texteditorconstants.h>
54 55

#include <utils/qtcassert.h>
56
#include <utils/qtcoverride.h>
57 58 59

using namespace CPlusPlus;
using namespace CppEditor::Internal;
60
using namespace CppTools;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
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);
    }

92
    void showProposal(const QString &prefix) QTC_OVERRIDE
93 94 95 96 97 98 99 100 101 102
    {
        IGenericProposalModel *proposalModel = model();
        if (proposalModel && proposalModel->size() == 1) {
            emit proposalItemActivated(proposalModel->proposalItem(0));
            deleteLater();
            return;
        }
        GenericProposalWidget::showProposal(prefix);
    }

103 104 105 106 107 108 109 110 111 112 113 114
private:
    QKeySequence m_sequence;
};

class VirtualFunctionProposal : public GenericProposal
{
public:
    VirtualFunctionProposal(int cursorPos, IGenericProposalModel *model, bool openInSplit)
        : GenericProposal(cursorPos, model)
        , m_openInSplit(openInSplit)
    {}

115
    bool isFragile() const QTC_OVERRIDE { return true; }
116

117
    IAssistProposalWidget *createWidget() const QTC_OVERRIDE
118 119 120 121 122 123 124 125 126
    { return new VirtualFunctionProposalWidget(m_openInSplit); }

private:
    bool m_openInSplit;
};

class VirtualFunctionsAssistProcessor : public IAssistProcessor
{
public:
127 128
    VirtualFunctionsAssistProcessor(const VirtualFunctionAssistProvider::Parameters &params)
        : m_params(params)
129 130
    {}

131
    IAssistProposal *immediateProposal(const TextEditor::IAssistInterface *) QTC_OVERRIDE
132
    {
133
        QTC_ASSERT(m_params.function, return 0);
134 135 136 137 138 139 140

        BasicProposalItem *hintItem = new VirtualFunctionProposalItem(CPPEditorWidget::Link());
        hintItem->setText(QCoreApplication::translate("VirtualFunctionsAssistProcessor",
                                                      "...searching overrides"));
        hintItem->setOrder(-1000);

        QList<BasicProposalItem *> items;
141
        items << itemFromFunction(m_params.function);
142
        items << hintItem;
143
        return new VirtualFunctionProposal(m_params.cursorPosition,
144
                                           new BasicProposalItemListModel(items),
145
                                           m_params.openInNextSplit);
146 147
    }

148
    IAssistProposal *perform(const IAssistInterface *) QTC_OVERRIDE
149
    {
150
        QTC_ASSERT(m_params.function, return 0);
151
        QTC_ASSERT(m_params.staticClass, return 0);
152
        QTC_ASSERT(!m_params.snapshot.isEmpty(), return 0);
153

154 155 156 157 158
        Class *functionsClass = m_finder.findMatchingClassDeclaration(m_params.function,
                                                                      m_params.snapshot);
        if (!functionsClass)
            return 0;

159
        const QList<Function *> overrides = FunctionUtils::overrides(
160
            m_params.function, functionsClass, m_params.staticClass, m_params.snapshot);
161 162 163
        if (overrides.isEmpty())
            return 0;

164
        QList<BasicProposalItem *> items;
165
        foreach (Function *func, overrides)
166
            items << itemFromFunction(func);
167
        items.first()->setOrder(1000); // Ensure top position for function of static type
168

169
        return new VirtualFunctionProposal(m_params.cursorPosition,
170
                                           new BasicProposalItemListModel(items),
171
                                           m_params.openInNextSplit);
172 173
    }

174
private:
175
    Function *maybeDefinitionFor(Function *func) const
176
    {
177
        if (Function *definition = m_finder.findMatchingDefinition(func, m_params.snapshot))
178
            return definition;
179
        return func;
180 181
    }

182
    BasicProposalItem *itemFromFunction(Function *func) const
183
    {
184 185 186 187
        const CPPEditorWidget::Link link = CPPEditorWidget::linkToSymbol(maybeDefinitionFor(func));
        QString text = m_overview.prettyName(LookupContext::fullyQualifiedName(func));
        if (func->isPureVirtual())
            text += QLatin1String(" = 0");
188

189
        BasicProposalItem *item = new VirtualFunctionProposalItem(link, m_params.openInNextSplit);
190
        item->setText(text);
191
        item->setIcon(m_icons.iconForSymbol(func));
192 193 194 195

        return item;
    }

196
    VirtualFunctionAssistProvider::Parameters m_params;
197 198
    Overview m_overview;
    Icons m_icons;
199
    mutable CppTools::SymbolFinder m_finder;
200 201 202 203 204 205
};

VirtualFunctionAssistProvider::VirtualFunctionAssistProvider()
{
}

206
bool VirtualFunctionAssistProvider::configure(const Parameters &parameters)
207
{
208
    m_params = parameters;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    return true;
}

bool VirtualFunctionAssistProvider::isAsynchronous() const
{
    return true;
}

bool VirtualFunctionAssistProvider::supportsEditor(const Core::Id &editorId) const
{
    return editorId == CppEditor::Constants::CPPEDITOR_ID;
}

IAssistProcessor *VirtualFunctionAssistProvider::createProcessor() const
{
224
    return new VirtualFunctionsAssistProcessor(m_params);
225
}