cppvirtualfunctionassistprovider.cpp 7.3 KB
Newer Older
1 2 3 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
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** 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 56 57 58

#include <utils/qtcassert.h>

using namespace CPlusPlus;
using namespace CppEditor::Internal;
59
using namespace CppTools;
60 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
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);
    }

private:
    QKeySequence m_sequence;
};

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

    bool isFragile() const
    { return true; }

    IAssistProposalWidget *createWidget() const
    { return new VirtualFunctionProposalWidget(m_openInSplit); }

private:
    bool m_openInSplit;
};

class VirtualFunctionsAssistProcessor : public IAssistProcessor
{
public:
116 117
    VirtualFunctionsAssistProcessor(const VirtualFunctionAssistProvider::Parameters &params)
        : m_params(params)
118 119
    {}

120
    IAssistProposal *immediateProposal(const TextEditor::IAssistInterface *)
121
    {
122
        QTC_ASSERT(m_params.function, return 0);
123 124 125 126 127 128 129

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

        QList<BasicProposalItem *> items;
130
        items << itemFromSymbol(maybeDefinitionFor(m_params.function));
131
        items << hintItem;
132
        return new VirtualFunctionProposal(m_params.cursorPosition,
133
                                           new BasicProposalItemListModel(items),
134
                                           m_params.openInNextSplit);
135 136
    }

137
    IAssistProposal *perform(const IAssistInterface *)
138
    {
139
        QTC_ASSERT(m_params.function, return 0);
140
        QTC_ASSERT(m_params.staticClass, return 0);
141
        QTC_ASSERT(!m_params.snapshot.isEmpty(), return 0);
142

143 144 145 146 147
        Class *functionsClass = m_finder.findMatchingClassDeclaration(m_params.function,
                                                                      m_params.snapshot);
        if (!functionsClass)
            return 0;

148
        const QList<Symbol *> overrides = FunctionUtils::overrides(
149
            m_params.function, functionsClass, m_params.staticClass, m_params.snapshot);
150 151 152
        if (overrides.isEmpty())
            return 0;

153 154
        QList<BasicProposalItem *> items;
        foreach (Symbol *symbol, overrides)
155 156
            items << itemFromSymbol(maybeDefinitionFor(symbol));
        items.first()->setOrder(1000); // Ensure top position for function of static type
157

158
        return new VirtualFunctionProposal(m_params.cursorPosition,
159
                                           new BasicProposalItemListModel(items),
160
                                           m_params.openInNextSplit);
161 162
    }

163 164 165
private:
    Symbol *maybeDefinitionFor(Symbol *symbol)
    {
166
        if (Function *definition = m_finder.findMatchingDefinition(symbol, m_params.snapshot))
167 168 169 170 171
            return definition;
        return symbol;
    }

    BasicProposalItem *itemFromSymbol(Symbol *symbol) const
172 173 174 175
    {
        const QString text = m_overview.prettyName(LookupContext::fullyQualifiedName(symbol));
        const CPPEditorWidget::Link link = CPPEditorWidget::linkToSymbol(symbol);

176
        BasicProposalItem *item = new VirtualFunctionProposalItem(link, m_params.openInNextSplit);
177 178 179 180 181 182
        item->setText(text);
        item->setIcon(m_icons.iconForSymbol(symbol));

        return item;
    }

183
    VirtualFunctionAssistProvider::Parameters m_params;
184 185
    Overview m_overview;
    Icons m_icons;
186
    CppTools::SymbolFinder m_finder;
187 188 189 190 191 192
};

VirtualFunctionAssistProvider::VirtualFunctionAssistProvider()
{
}

193
bool VirtualFunctionAssistProvider::configure(const Parameters &parameters)
194
{
195
    m_params = parameters;
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    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
{
211
    return new VirtualFunctionsAssistProcessor(m_params);
212
}