cppcompletionassist.h 8.03 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Leandro Melo's avatar
Leandro Melo committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Leandro Melo's avatar
Leandro Melo committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Leandro Melo's avatar
Leandro Melo committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12
** 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
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
Leandro Melo's avatar
Leandro Melo committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24 25 26
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
Leandro Melo's avatar
Leandro Melo committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
Leandro Melo's avatar
Leandro Melo committed
30 31 32 33

#ifndef CPPCOMPLETIONASSIST_H
#define CPPCOMPLETIONASSIST_H

34
#include "cppcompletionassistprocessor.h"
35
#include "cppcompletionassistprovider.h"
36
#include "cppmodelmanager.h"
37
#include "cppworkingcopy.h"
38

Leandro Melo's avatar
Leandro Melo committed
39
#include <cplusplus/Icons.h>
40
#include <cplusplus/Symbol.h>
Leandro Melo's avatar
Leandro Melo committed
41 42
#include <cplusplus/TypeOfExpression.h>

43
#include <texteditor/texteditor.h>
44 45
#include <texteditor/codeassist/genericproposalmodel.h>
#include <texteditor/codeassist/assistinterface.h>
Leandro Melo's avatar
Leandro Melo committed
46 47 48
#include <texteditor/codeassist/iassistprocessor.h>
#include <texteditor/snippets/snippetassistcollector.h>

49

50 51
#include <QStringList>
#include <QVariant>
Leandro Melo's avatar
Leandro Melo committed
52 53 54 55 56 57

namespace CPlusPlus {
class LookupItem;
class ClassOrNamespace;
class Function;
class LookupContext;
58
} // namespace CPlusPlus
Leandro Melo's avatar
Leandro Melo committed
59 60 61 62 63

namespace CppTools {
namespace Internal {

class CppCompletionAssistInterface;
64

65
class CppAssistProposalModel : public TextEditor::GenericProposalModel
66 67 68
{
public:
    CppAssistProposalModel()
69
        : TextEditor::GenericProposalModel()
70 71 72 73 74 75 76
        , m_completionOperator(CPlusPlus::T_EOF_SYMBOL)
        , m_replaceDotForArrow(false)
        , m_typeOfExpression(new CPlusPlus::TypeOfExpression)
    {
        m_typeOfExpression->setExpandTemplates(true);
    }

77 78
    bool isSortable(const QString &prefix) const Q_DECL_OVERRIDE;
    TextEditor::AssistProposalItem *proposalItem(int index) const Q_DECL_OVERRIDE;
79 80 81 82 83

    unsigned m_completionOperator;
    bool m_replaceDotForArrow;
    QSharedPointer<CPlusPlus::TypeOfExpression> m_typeOfExpression;
};
Leandro Melo's avatar
Leandro Melo committed
84

85
class InternalCompletionAssistProvider : public CppCompletionAssistProvider
Leandro Melo's avatar
Leandro Melo committed
86
{
87 88
    Q_OBJECT

Leandro Melo's avatar
Leandro Melo committed
89
public:
90
    TextEditor::IAssistProcessor *createProcessor() const Q_DECL_OVERRIDE;
91

92
    TextEditor::AssistInterface *createAssistInterface(
93
            const QString &filePath,
94
            QTextDocument *document,
95
            bool isObjCEnabled,
96
            int position,
97
            TextEditor::AssistReason reason) const Q_DECL_OVERRIDE;
Leandro Melo's avatar
Leandro Melo committed
98 99
};

100
class InternalCppCompletionAssistProcessor : public CppCompletionAssistProcessor
Leandro Melo's avatar
Leandro Melo committed
101 102
{
public:
103 104
    InternalCppCompletionAssistProcessor();
    ~InternalCppCompletionAssistProcessor();
Leandro Melo's avatar
Leandro Melo committed
105

106
    TextEditor::IAssistProposal *perform(const TextEditor::AssistInterface *interface) Q_DECL_OVERRIDE;
Leandro Melo's avatar
Leandro Melo committed
107 108 109 110 111 112 113 114 115 116 117

private:
    TextEditor::IAssistProposal *createContentProposal();
    TextEditor::IAssistProposal *createHintProposal(QList<CPlusPlus::Function *> symbols) const;
    bool accepts() const;

    int startOfOperator(int pos, unsigned *kind, bool wantFunctionCall) const;
    int findStartOfName(int pos = -1) const;
    int startCompletionHelper();
    bool tryObjCCompletion();
    bool objcKeywordsWanted() const;
118
    int startCompletionInternal(const QString &fileName,
Leandro Melo's avatar
Leandro Melo committed
119 120 121 122 123 124
                                unsigned line, unsigned column,
                                const QString &expression,
                                int endOfExpression);

    void completeObjCMsgSend(CPlusPlus::ClassOrNamespace *binding, bool staticClassAccess);
    bool completeInclude(const QTextCursor &cursor);
125
    void completeInclude(const QString &realPath, const QStringList &suffixes);
Leandro Melo's avatar
Leandro Melo committed
126 127 128 129 130 131 132 133
    void completePreprocessor();
    bool completeConstructorOrFunction(const QList<CPlusPlus::LookupItem> &results,
                                       int endOfExpression,
                                       bool toolTipOnly);
    bool completeMember(const QList<CPlusPlus::LookupItem> &results);
    bool completeScope(const QList<CPlusPlus::LookupItem> &results);
    void completeNamespace(CPlusPlus::ClassOrNamespace *binding);
    void completeClass(CPlusPlus::ClassOrNamespace *b, bool staticLookup = true);
134
    void addClassMembersToCompletion(CPlusPlus::Scope *scope, bool staticLookup);
Leandro Melo's avatar
Leandro Melo committed
135 136 137 138 139 140 141 142 143 144 145
    bool completeQtMethod(const QList<CPlusPlus::LookupItem> &results, bool wantSignals);
    bool completeSignal(const QList<CPlusPlus::LookupItem> &results)
    { return completeQtMethod(results, true); }
    bool completeSlot(const QList<CPlusPlus::LookupItem> &results)
    { return completeQtMethod(results, false); }
    void globalCompletion(CPlusPlus::Scope *scope);

    void addCompletionItem(const QString &text,
                           const QIcon &icon = QIcon(),
                           int order = 0,
                           const QVariant &data = QVariant());
146 147
    void addCompletionItem(CPlusPlus::Symbol *symbol,
                           int order = 0);
Leandro Melo's avatar
Leandro Melo committed
148 149 150 151 152 153 154
    void addKeywords();
    void addMacros(const QString &fileName, const CPlusPlus::Snapshot &snapshot);
    void addMacros_helper(const CPlusPlus::Snapshot &snapshot,
                          const QString &fileName,
                          QSet<QString> *processed,
                          QSet<QString> *definedMacros);

155
    CPlusPlus::LanguageFeatures m_languageFeatures;
Leandro Melo's avatar
Leandro Melo committed
156 157 158 159
    QScopedPointer<const CppCompletionAssistInterface> m_interface;
    QScopedPointer<CppAssistProposalModel> m_model;
};

160
class CppCompletionAssistInterface : public TextEditor::AssistInterface
Leandro Melo's avatar
Leandro Melo committed
161 162
{
public:
163
    CppCompletionAssistInterface(const QString &filePath,
164
                                 QTextDocument *textDocument,
165
                                 bool isObjCEnabled,
166
                                 int position,
167
                                 TextEditor::AssistReason reason,
168
                                 const WorkingCopy &workingCopy)
169
        : TextEditor::AssistInterface(textDocument, position, filePath, reason)
170
        , m_isObjCEnabled(isObjCEnabled)
171
        , m_gotCppSpecifics(false)
172
        , m_workingCopy(workingCopy)
173 174
    {}

175 176
    CppCompletionAssistInterface(const QString &filePath,
                                 QTextDocument *textDocument,
Leandro Melo's avatar
Leandro Melo committed
177 178 179
                                 int position,
                                 TextEditor::AssistReason reason,
                                 const CPlusPlus::Snapshot &snapshot,
180
                                 const ProjectPart::HeaderPaths &headerPaths)
181
        : TextEditor::AssistInterface(textDocument, position, filePath, reason)
182
        , m_isObjCEnabled(false)
183
        , m_gotCppSpecifics(true)
Leandro Melo's avatar
Leandro Melo committed
184
        , m_snapshot(snapshot)
185
        , m_headerPaths(headerPaths)
Leandro Melo's avatar
Leandro Melo committed
186 187
    {}

188 189
    bool isObjCEnabled() const { return m_isObjCEnabled; }

190
    const CPlusPlus::Snapshot &snapshot() const { getCppSpecifics(); return m_snapshot; }
191 192
    const ProjectPart::HeaderPaths &headerPaths() const
    { getCppSpecifics(); return m_headerPaths; }
Leandro Melo's avatar
Leandro Melo committed
193 194

private:
195 196
    void getCppSpecifics() const;

197
    mutable bool m_isObjCEnabled;
198
    mutable bool m_gotCppSpecifics;
199
    WorkingCopy m_workingCopy;
200
    mutable CPlusPlus::Snapshot m_snapshot;
201
    mutable ProjectPart::HeaderPaths m_headerPaths;
Leandro Melo's avatar
Leandro Melo committed
202 203 204 205 206 207 208 209
};

} // Internal
} // CppTools

Q_DECLARE_METATYPE(CPlusPlus::Symbol *)

#endif // CPPCOMPLETIONASSIST_H