File: staticassistantsmanager.cpp

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 (208 lines) | stat: -rw-r--r-- 6,970 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
/*
   Copyright 2009 David Nolden <david.nolden.kdevelop@art-master.de>
   Copyright 2014 Kevin Funk <kfunk@kde.org>

   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.
 */

#include "staticassistantsmanager.h"
#include <debug.h>

#include <KTextEditor/Document>
#include <KTextEditor/View>

#include <interfaces/icore.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/ilanguagecontroller.h>
#include <interfaces/ilanguagesupport.h>

#include <language/duchain/duchainlock.h>
#include <language/duchain/duchain.h>
#include <language/duchain/declaration.h>
#include <language/duchain/duchainutils.h>

#include <language/duchain/problem.h>
#include <language/editor/documentrange.h>

using namespace KDevelop;
using namespace KTextEditor;

class KDevelop::StaticAssistantsManagerPrivate
{
public:
    explicit StaticAssistantsManagerPrivate(StaticAssistantsManager* qq)
        : q(qq)
    { }

    void updateReady(const IndexedString& document, const KDevelop::ReferencedTopDUContext& topContext);
    void documentLoaded(KDevelop::IDocument*);
    void textInserted(KTextEditor::Document* document, const Cursor& cursor, const QString& text);
    void textRemoved(KTextEditor::Document* document, const Range& cursor, const QString& removedText);

    StaticAssistantsManager* q;

    QVector<StaticAssistant::Ptr> m_registeredAssistants;
};

StaticAssistantsManager::StaticAssistantsManager(QObject* parent)
    : QObject(parent)
    , d_ptr(new StaticAssistantsManagerPrivate(this))
{
    Q_D(StaticAssistantsManager);

    connect(KDevelop::ICore::self()->documentController(),
            &IDocumentController::documentLoaded,
            this, [this](IDocument* document) {
        Q_D(StaticAssistantsManager);
        d->documentLoaded(document);
    });
    const auto documents = ICore::self()->documentController()->openDocuments();
    for (IDocument* document : documents) {
        d->documentLoaded(document);
    }

    connect(DUChain::self(), &DUChain::updateReady,
            this, &StaticAssistantsManager::notifyAssistants);
}

StaticAssistantsManager::~StaticAssistantsManager()
{
}

void StaticAssistantsManager::registerAssistant(const StaticAssistant::Ptr& assistant)
{
    Q_D(StaticAssistantsManager);

    if (d->m_registeredAssistants.contains(assistant))
        return;

    d->m_registeredAssistants << assistant;
}

void StaticAssistantsManager::unregisterAssistant(const StaticAssistant::Ptr& assistant)
{
    Q_D(StaticAssistantsManager);

    d->m_registeredAssistants.removeOne(assistant);
}

QVector<StaticAssistant::Ptr> StaticAssistantsManager::registeredAssistants() const
{
    Q_D(const StaticAssistantsManager);

    return d->m_registeredAssistants;
}

void StaticAssistantsManagerPrivate::documentLoaded(IDocument* document)
{
    if (document->textDocument()) {
        auto doc = document->textDocument();
        QObject::connect(doc, &KTextEditor::Document::textInserted, q,
                         [&](KTextEditor::Document* doc, const Cursor& cursor, const QString& text) {
            textInserted(doc, cursor, text);
        });
        QObject::connect(doc, &KTextEditor::Document::textRemoved, q,
                         [&](KTextEditor::Document* doc, const Range& range, const QString& removedText) {
            textRemoved(doc, range, removedText);
        });
    }
}

void StaticAssistantsManagerPrivate::textInserted(Document* doc, const Cursor& cursor, const QString& text)
{
    auto changed = false;
    for (auto& assistant : qAsConst(m_registeredAssistants)) {
        auto range = Range(cursor, cursor + Cursor(0, text.size()));
        auto wasUseful = assistant->isUseful();
        assistant->textChanged(doc, range, {});
        if (wasUseful != assistant->isUseful()) {
            changed = true;
        }
    }

    if (changed) {
        Q_EMIT q->problemsChanged(IndexedString(doc->url()));
    }
}

void StaticAssistantsManagerPrivate::textRemoved(Document* doc, const Range& range,
                                                 const QString& removedText)
{
    auto changed = false;
    for (auto& assistant : qAsConst(m_registeredAssistants)) {
        auto wasUseful = assistant->isUseful();
        assistant->textChanged(doc, range, removedText);
        if (wasUseful != assistant->isUseful()) {
            changed = true;
        }
    }

    if (changed) {
        Q_EMIT q->problemsChanged(IndexedString(doc->url()));
    }
}

void StaticAssistantsManager::notifyAssistants(const IndexedString& url,
                                               const KDevelop::ReferencedTopDUContext& context)
{
    Q_D(StaticAssistantsManager);

    for (auto& assistant : qAsConst(d->m_registeredAssistants)) {
        assistant->updateReady(url, context);
    }
}

QVector<KDevelop::Problem::Ptr> KDevelop::StaticAssistantsManager::problemsForContext(
    const KDevelop::ReferencedTopDUContext& top) const
{
    Q_D(const StaticAssistantsManager);

    View* view = ICore::self()->documentController()->activeTextDocumentView();
    if (!view || !top || IndexedString(view->document()->url()) != top->url()) {
        return {};
    }

    auto doc = top->url();
    auto language = ICore::self()->languageController()->languagesForUrl(doc.toUrl()).value(0);
    if (!language) {
        return {};
    }

    auto ret = QVector<KDevelop::Problem::Ptr>();
    qCDebug(LANGUAGE) << "Trying to find assistants for language" << language->name();
    for (const auto& assistant : qAsConst(d->m_registeredAssistants)) {
        if (assistant->supportedLanguage() != language)
            continue;

        if (assistant->isUseful()) {
            qCDebug(LANGUAGE) << "assistant is now useful:" << assistant.data();

            auto p = new KDevelop::StaticAssistantProblem();
            auto range = assistant->displayRange();
            qCDebug(LANGUAGE) << "range:" << range;
            p->setFinalLocation(DocumentRange(doc, range));
            p->setSource(KDevelop::IProblem::SemanticAnalysis);
            p->setSeverity(KDevelop::IProblem::Warning);
            p->setDescription(assistant->title());
            p->setSolutionAssistant(IAssistant::Ptr(assistant.data()));

            ret.append(KDevelop::Problem::Ptr(p));
        }
    }

    return ret;
}

#include "moc_staticassistantsmanager.cpp"