File: codecompletiontesthelper.h

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (251 lines) | stat: -rw-r--r-- 7,980 bytes parent folder | download
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* This file is part of KDevelop
    Copyright 2006 Hamish Rodda<rodda@kde.org>
    Copyright 2007-2009 David Nolden <david.nolden.kdevelop@art-master.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
 */

#ifndef KDEVPLATFORM_CODECOMPLETIONTESTHELPER_H
#define KDEVPLATFORM_CODECOMPLETIONTESTHELPER_H

#include <QTest>
#include <QStandardItemModel>

#include "../duchain/declaration.h"
#include "../duchain/duchain.h"
#include "codecompletionitem.h"
#include <language/codegen/coderepresentation.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/parsingenvironment.h>

#include <tests/testhelpers.h>

using namespace KTextEditor;

using namespace KDevelop;

/**
 * Helper-class for testing completion-items
 * Just initialize it with the context and the text, and then use the members, for simple cases only "names"
 * the template parameter is your language specific CodeCompletionContext
 */
template <class T>
struct CodeCompletionItemTester
{
    using Element = QExplicitlySharedDataPointer<KDevelop::CompletionTreeElement>;
    using Item = QExplicitlySharedDataPointer<KDevelop::CompletionTreeItem>;
    using Context = QExplicitlySharedDataPointer<T>;

    //Standard constructor
    CodeCompletionItemTester(DUContext* context, const QString& text = QStringLiteral( "; " ),
                             const QString& followingText = QString(),
                             const CursorInRevision& position = CursorInRevision::invalid())
        : completionContext(new T(DUContextPointer(context), text, followingText,
                position.isValid() ? position : context->range().end))
    {
        init();
    }

    //Can be used if you already have the completion context
    CodeCompletionItemTester(const Context& context)
        : completionContext(context)
    {
        init();
    }

    //Creates a CodeCompletionItemTester for the parent context
    CodeCompletionItemTester parent() const
    {
        Context parent = Context(dynamic_cast<T*>(completionContext->parentContext()));
        Q_ASSERT(parent);
        return CodeCompletionItemTester(parent);
    }

    void addElements(const QList<Element>& elements)
    {
        for (auto& element : elements) {
            Item item(dynamic_cast<CompletionTreeItem*>(element.data()));
            if (item)
                items << item;
            auto* node = dynamic_cast<CompletionTreeNode*>(element.data());
            if (node)
                addElements(node->children);
        }
    }

    bool containsDeclaration(Declaration* dec) const
    {
        for (auto& item : items) {
            if (item->declaration().data() == dec) {
                return true;
            }
        }

        return false;
    }

    QList<Item> items; // All items retrieved
    QStringList names; // Names of all completion-items
    Context completionContext;

    //Convenience-function to retrieve data from completion-items by name
    QVariant itemData(const QString& itemName, int column = KTextEditor::CodeCompletionModel::Name,
                      int role = Qt::DisplayRole) const
    {
        return itemData(names.indexOf(itemName), column, role);
    }

    QVariant itemData(int itemNumber, int column = KTextEditor::CodeCompletionModel::Name,
                      int role = Qt::DisplayRole) const
    {
        if (itemNumber < 0 || itemNumber >= items.size())
            return QVariant();

        return itemData(items[itemNumber], column, role);
    }

    QVariant itemData(Item item, int column = KTextEditor::CodeCompletionModel::Name, int role = Qt::DisplayRole) const
    {
        return item->data(fakeModel().index(0, column), role, nullptr);
    }

    Item findItem(const QString& itemName) const
    {
        const auto idx = names.indexOf(itemName);
        if (idx < 0) {
            return {};
        }
        return items[idx];
    }

private:
    void init()
    {
        if (!completionContext || !completionContext->isValid()) {
            qWarning() << "invalid completion context";
            return;
        }

        bool abort = false;
        items = completionContext->completionItems(abort);

        addElements(completionContext->ungroupedElements());

        names.reserve(items.size());
        for (const Item& i : qAsConst(items)) {
            names <<
                i->data(fakeModel().index(0, KTextEditor::CodeCompletionModel::Name), Qt::DisplayRole,
                        nullptr).toString();
        }
    }

    static QStandardItemModel& fakeModel()
    {
        static QStandardItemModel model;
        model.setColumnCount(10);
        model.setRowCount(10);
        return model;
    }
};

/**
 * Helper class that inserts the given text into the duchain under the specified name,
 * allows parsing it with a simple call to parse(), and automatically releases the top-context
 *
 * The duchain must not be locked when this object is destroyed
 */
struct InsertIntoDUChain
{
    ///Artificially inserts a file called @p name with the text @p text
    InsertIntoDUChain(const QString& name, const QString& text) : m_insertedCode(IndexedString(name), text)
        , m_topContext(nullptr)
    {
    }

    ~InsertIntoDUChain()
    {
        get();
        release();
    }

    ///The duchain must not be locked when this is called
    void release()
    {
        if (m_topContext) {
            DUChainWriteLocker lock;

            m_topContext = nullptr;

            const QList<TopDUContext*> chains = DUChain::self()->chainsForDocument(m_insertedCode.file());
            for (TopDUContext* top : chains) {
                DUChain::self()->removeDocumentChain(top);
            }
        }
    }

    TopDUContext* operator->()
    {
        get();
        return m_topContext.data();
    }

    TopDUContext* tryGet()
    {
        DUChainReadLocker lock;
        return DUChain::self()->chainForDocument(m_insertedCode.file(), false);
    }

    void get()
    {
        if (!m_topContext)
            m_topContext = tryGet();
    }

    ///Helper function: get a declaration based on its qualified identifier
    Declaration* declaration(const QString& id)
    {
        get();
        if (!topContext())
            return nullptr;
        return DeclarationId(IndexedQualifiedIdentifier(QualifiedIdentifier(id))).declaration(topContext());
    }

    TopDUContext* topContext()
    {
        return m_topContext.data();
    }

    /**
     * Parses this inserted code as a stand-alone top-context
     * The duchain must not be locked when this is called
     *
     * @param features The features that should be requested for the top-context
     * @param update Whether the top-context should be updated if it already exists. Else it will be deleted.
     */
    void parse(uint features = TopDUContext::AllDeclarationsContextsAndUses, bool update = false)
    {
        if (!update)
            release();
        m_topContext = DUChain::self()->waitForUpdate(m_insertedCode.file(), ( TopDUContext::Features )features, false);
        Q_ASSERT(m_topContext);
        DUChainReadLocker lock;
        Q_ASSERT(!m_topContext->parsingEnvironmentFile()->isProxyContext());
    }

    InsertArtificialCodeRepresentation m_insertedCode;
    ReferencedTopDUContext m_topContext;
};

#endif // KDEVPLATFORM_CODECOMPLETIONTESTHELPER_H