File: codegenerator.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 (241 lines) | stat: -rw-r--r-- 6,716 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
/*
   Copyright 2008 Hamish Rodda <rodda@kde.org>
   Copyright 2009 Ramon Zarazua <killerfox512 gmail.com>

   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 "codegenerator.h"

#include "documentchangeset.h"
#include "duchainchangeset.h"

#include <KLocalizedString>

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

#include <language/duchain/duchainlock.h>

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

namespace KDevelop {
class CodeGeneratorBasePrivate
{
public:

    CodeGeneratorBasePrivate() : autoGen(false)
        , context(0) {}

    QMap<IndexedString, DUChainChangeSet*> duchainChanges;
    DocumentChangeSet documentChanges;

    bool autoGen;
    DUContext* context;
    DocumentRange range;
    QString error;
};

CodeGeneratorBase::CodeGeneratorBase()
    : d_ptr(new CodeGeneratorBasePrivate)
{
}

CodeGeneratorBase::~CodeGeneratorBase()
{
    clearChangeSets();
}

void CodeGeneratorBase::autoGenerate(DUContext* context, const KDevelop::DocumentRange* range)
{
    Q_D(CodeGeneratorBase);

    d->autoGen = true;
    d->context = context;
    d->range = *range;
}

void CodeGeneratorBase::addChangeSet(DUChainChangeSet* duchainChange)
{
    Q_D(CodeGeneratorBase);

    IndexedString file = duchainChange->topDuContext().data()->url();

    QMap<IndexedString, DUChainChangeSet*>::iterator it = d->duchainChanges.find(file);

    //if we already have an entry for this file, merge it
    if (it != d->duchainChanges.end()) {
        **it << *duchainChange;
        delete duchainChange;
    } else
        d->duchainChanges.insert(file, duchainChange);
}

void CodeGeneratorBase::addChangeSet(DocumentChangeSet& docChangeSet)
{
    Q_D(CodeGeneratorBase);

    d->documentChanges << docChangeSet;
}

DocumentChangeSet& CodeGeneratorBase::documentChangeSet()
{
    Q_D(CodeGeneratorBase);

    return d->documentChanges;
}

const QString& CodeGeneratorBase::errorText() const
{
    Q_D(const CodeGeneratorBase);

    return d->error;
}

bool CodeGeneratorBase::autoGeneration() const
{
    Q_D(const CodeGeneratorBase);

    return d->autoGen;
}

void CodeGeneratorBase::setErrorText(const QString& errorText)
{
    Q_D(CodeGeneratorBase);

    d->error = errorText;
}

void CodeGeneratorBase::clearChangeSets()
{
    Q_D(CodeGeneratorBase);

    qCDebug(LANGUAGE) << "Cleaning up all the changesets registered by the generator";
    qDeleteAll(d->duchainChanges);

    d->duchainChanges.clear();

    d->documentChanges = DocumentChangeSet();
}

bool CodeGeneratorBase::execute()
{
    Q_D(CodeGeneratorBase);

    qCDebug(LANGUAGE) << "Checking Preconditions for the codegenerator";

    //Shouldn't there be a method in iDocument to get a DocumentRange as well?

    QUrl document;
    if (!d->autoGen) {
        if (!ICore::self()->documentController()->activeDocument()) {
            setErrorText(i18n("Could not find an open document"));
            return false;
        }

        document = ICore::self()->documentController()->activeDocument()->url();

        if (d->range.isEmpty()) {
            DUChainReadLocker lock(DUChain::lock());
            d->range = DocumentRange(document.url(),
                                     ICore::self()->documentController()->activeDocument()->textSelection());
        }
    }

    if (!d->context) {
        DUChainReadLocker lock(DUChain::lock());
        TopDUContext* documentChain = DUChain::self()->chainForDocument(document);
        if (!documentChain) {
            setErrorText(i18n("Could not find the chain for the selected document: %1", document.url()));
            return false;
        }
        d->context = documentChain->findContextIncluding(d->range);

        if (!d->context) {
            //Attempt to get the context again
            const QList<TopDUContext*> contexts = DUChain::self()->chainsForDocument(document);
            for (TopDUContext* top : contexts) {
                qCDebug(LANGUAGE) << "Checking top context with range: " << top->range() << " for a context";
                if ((d->context = top->findContextIncluding(d->range)))
                    break;
            }
        }
    }

    if (!d->context) {
        setErrorText(i18n("Error finding context for selection range"));
        return false;
    }

    if (!checkPreconditions(d->context, d->range)) {
        setErrorText(i18n("Error checking conditions to generate code: %1", errorText()));
        return false;
    }

    if (!d->autoGen) {
        qCDebug(LANGUAGE) << "Gathering user information for the codegenerator";
        if (!gatherInformation()) {
            setErrorText(i18n("Error Gathering user information: %1", errorText()));
            return false;
        }
    }

    qCDebug(LANGUAGE) << "Generating code";
    if (!process()) {
        setErrorText(i18n("Error generating code: %1", errorText()));
        return false;
    }

    if (!d->autoGen) {
        qCDebug(LANGUAGE) << "Submitting to the user for review";
        return displayChanges();
    }

    //If it is autogenerated, it shouldn't need to apply changes, instead return them to client that my be another generator
    DocumentChangeSet::ChangeResult result(true);
    if (!d->autoGen && !(result = d->documentChanges.applyAllChanges())) {
        setErrorText(result.m_failureReason);
        return false;
    }

    return true;
}

bool CodeGeneratorBase::displayChanges()
{
    Q_D(CodeGeneratorBase);

    DocumentChangeSet::ChangeResult result = d->documentChanges.applyAllToTemp();
    if (!result) {
        setErrorText(result.m_failureReason);
        return false;
    }

    ApplyChangesWidget widget;
    //TODO: Find some good information to put
    widget.setInformation("Info?");

    QMap<IndexedString, IndexedString> temps = d->documentChanges.tempNamesForAll();
    for (QMap<IndexedString, IndexedString>::iterator it = temps.begin();
         it != temps.end(); ++it)
        widget.addDocuments(it.key(), it.value());

    if (widget.exec())
        return widget.applyAllChanges();
    else
        return false;
}
}