~kdevelop/kdevplatform/master

« back to all changes in this revision

Viewing changes to language/codegen/coderepresentation.cpp

  • Committer: Friedrich W. H. Kossebau
  • Date: 2017-08-13 21:54:31 UTC
  • Revision ID: git-v1:8ce76bea4df0831deb2fb243af33cc90d3cc8043
Wipe master branch and point in README to new location

Summary: also add util script for moving over existing personal branches

Reviewers: #kdevelop, apol, kfunk

Reviewed By: #kdevelop, apol, kfunk

Subscribers: kfunk, kdevelop-devel

Differential Revision: https://phabricator.kde.org/D7244

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
   Copyright 2008 David Nolden <david.nolden.kdevelop@art-master.de>
3
 
 
4
 
   This library is free software; you can redistribute it and/or
5
 
   modify it under the terms of the GNU Library General Public
6
 
   License version 2 as published by the Free Software Foundation.
7
 
 
8
 
   This library is distributed in the hope that it will be useful,
9
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
 
   Library General Public License for more details.
12
 
 
13
 
   You should have received a copy of the GNU Library General Public License
14
 
   along with this library; see the file COPYING.LIB.  If not, write to
15
 
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16
 
   Boston, MA 02110-1301, USA.
17
 
*/
18
 
 
19
 
#include "coderepresentation.h"
20
 
 
21
 
#include <QFile>
22
 
#include <KTextEditor/Document>
23
 
 
24
 
#include <serialization/indexedstring.h>
25
 
#include <interfaces/idocumentcontroller.h>
26
 
#include <interfaces/icore.h>
27
 
#include <editor/modificationrevision.h>
28
 
#include <ktexteditor/movinginterface.h>
29
 
#include <ktexteditor/configinterface.h>
30
 
 
31
 
namespace KDevelop {
32
 
 
33
 
static bool onDiskChangesForbidden = false;
34
 
 
35
 
QString CodeRepresentation::rangeText(const KTextEditor::Range& range) const
36
 
{
37
 
    Q_ASSERT(range.end().line() < lines());
38
 
 
39
 
    //Easier for single line ranges which should happen most of the time
40
 
    if(range.onSingleLine())
41
 
        return QString( line( range.start().line() ).mid( range.start().column(), range.columnWidth() ) );
42
 
 
43
 
    //Add up al the requested lines
44
 
    QString rangedText = line(range.start().line()).mid(range.start().column());
45
 
 
46
 
    for(int i = range.start().line() + 1; i <= range.end().line(); ++i)
47
 
        rangedText += '\n' + ((i == range.end().line()) ? line(i).left(range.end().column()) : line(i));
48
 
 
49
 
    return rangedText;
50
 
}
51
 
 
52
 
static void grepLine(const QString& identifier, const QString& lineText, int lineNumber, QVector<KTextEditor::Range>& ret, bool surroundedByBoundary)
53
 
{
54
 
    if (identifier.isEmpty())
55
 
        return;
56
 
 
57
 
    int pos = 0;
58
 
    while(true)
59
 
    {
60
 
        pos = lineText.indexOf(identifier, pos);
61
 
        if(pos == -1)
62
 
            break;
63
 
        int start = pos;
64
 
        pos += identifier.length();
65
 
        int end = pos;
66
 
 
67
 
        if(!surroundedByBoundary || ( (end == lineText.length() || !lineText[end].isLetterOrNumber() || lineText[end] != '_')
68
 
                                        && (start-1 < 0 || !lineText[start-1].isLetterOrNumber() || lineText[start-1] != '_')) )
69
 
        {
70
 
            ret << KTextEditor::Range(lineNumber, start, lineNumber, end);
71
 
        }
72
 
    }
73
 
}
74
 
 
75
 
class EditorCodeRepresentation : public DynamicCodeRepresentation {
76
 
  public:
77
 
  explicit EditorCodeRepresentation(KTextEditor::Document* document) : m_document(document) {
78
 
      m_url = IndexedString(m_document->url());
79
 
  }
80
 
 
81
 
  QVector< KTextEditor::Range > grep ( const QString& identifier, bool surroundedByBoundary ) const override {
82
 
      QVector< KTextEditor::Range > ret;
83
 
 
84
 
      if (identifier.isEmpty())
85
 
        return ret;
86
 
 
87
 
      for(int line = 0; line < m_document->lines(); ++line)
88
 
        grepLine(identifier, m_document->line(line), line, ret, surroundedByBoundary);
89
 
 
90
 
      return ret;
91
 
  }
92
 
 
93
 
  KDevEditingTransaction::Ptr makeEditTransaction() override {
94
 
    return KDevEditingTransaction::Ptr(new KDevEditingTransaction(m_document));
95
 
  }
96
 
 
97
 
  QString line(int line) const override {
98
 
        if(line < 0 || line >= m_document->lines())
99
 
            return QString();
100
 
        return m_document->line(line);
101
 
  }
102
 
 
103
 
  int lines() const override {
104
 
      return m_document->lines();
105
 
  }
106
 
 
107
 
  QString text() const override {
108
 
    return m_document->text();
109
 
  }
110
 
 
111
 
  bool setText(const QString& text) override {
112
 
    bool ret;
113
 
    {
114
 
        KDevEditingTransaction t(m_document);
115
 
        ret = m_document->setText(text);
116
 
    }
117
 
    ModificationRevision::clearModificationCache(m_url);
118
 
    return ret;
119
 
  }
120
 
 
121
 
  bool fileExists() const override
122
 
  {
123
 
    return QFile(m_document->url().path()).exists();
124
 
  }
125
 
 
126
 
  bool replace(const KTextEditor::Range& range, const QString& oldText,
127
 
               const QString& newText, bool ignoreOldText) override {
128
 
      QString old = m_document->text(range);
129
 
      if(oldText != old && !ignoreOldText) {
130
 
          return false;
131
 
      }
132
 
 
133
 
      bool ret;
134
 
      {
135
 
          KDevEditingTransaction t(m_document);
136
 
          ret = m_document->replaceText(range, newText);
137
 
      }
138
 
 
139
 
      ModificationRevision::clearModificationCache(m_url);
140
 
 
141
 
      return ret;
142
 
  }
143
 
 
144
 
  QString rangeText(const KTextEditor::Range& range) const override {
145
 
      return m_document->text(range);
146
 
  }
147
 
 
148
 
  private:
149
 
    KTextEditor::Document* m_document;
150
 
    IndexedString m_url;
151
 
};
152
 
 
153
 
class FileCodeRepresentation : public CodeRepresentation {
154
 
  public:
155
 
    explicit FileCodeRepresentation(const IndexedString& document) : m_document(document) {
156
 
        QString localFile(document.toUrl().toLocalFile());
157
 
 
158
 
        QFile file( localFile );
159
 
        if ( file.open(QIODevice::ReadOnly) ) {
160
 
            data = QString::fromLocal8Bit(file.readAll());
161
 
            lineData = data.split('\n');
162
 
        }
163
 
        m_exists = file.exists();
164
 
    }
165
 
 
166
 
    QString line(int line) const override {
167
 
        if(line < 0 || line >= lineData.size())
168
 
            return QString();
169
 
 
170
 
      return lineData.at(line);
171
 
    }
172
 
 
173
 
    QVector< KTextEditor::Range > grep ( const QString& identifier, bool surroundedByBoundary ) const override {
174
 
        QVector< KTextEditor::Range > ret;
175
 
 
176
 
        if (identifier.isEmpty())
177
 
            return ret;
178
 
 
179
 
        for(int line = 0; line < lineData.count(); ++line)
180
 
            grepLine(identifier, lineData.at(line), line, ret, surroundedByBoundary);
181
 
 
182
 
        return ret;
183
 
    }
184
 
 
185
 
    int lines() const override {
186
 
        return lineData.count();
187
 
    }
188
 
 
189
 
    QString text() const override {
190
 
      return data;
191
 
    }
192
 
 
193
 
    bool setText(const QString& text) override {
194
 
      Q_ASSERT(!onDiskChangesForbidden);
195
 
      QString localFile(m_document.toUrl().toLocalFile());
196
 
 
197
 
      QFile file( localFile );
198
 
      if ( file.open(QIODevice::WriteOnly) )
199
 
      {
200
 
          QByteArray data = text.toLocal8Bit();
201
 
 
202
 
          if(file.write(data) == data.size())
203
 
          {
204
 
              ModificationRevision::clearModificationCache(m_document);
205
 
              return true;
206
 
          }
207
 
      }
208
 
      return false;
209
 
    }
210
 
 
211
 
    bool fileExists() const override
212
 
    {
213
 
      return m_exists;
214
 
    }
215
 
 
216
 
  private:
217
 
    //We use QByteArray, because the column-numbers are measured in utf-8
218
 
    IndexedString m_document;
219
 
    bool m_exists;
220
 
    QStringList lineData;
221
 
    QString data;
222
 
};
223
 
 
224
 
class ArtificialStringData : public QSharedData {
225
 
    public:
226
 
    explicit ArtificialStringData(const QString& data) {
227
 
        setData(data);
228
 
    }
229
 
    void setData(const QString& data) {
230
 
        m_data = data;
231
 
        m_lineData = m_data.split('\n');
232
 
    }
233
 
    QString data() const {
234
 
        return m_data;
235
 
    }
236
 
    const QStringList& lines() const {
237
 
        return m_lineData;
238
 
    }
239
 
 
240
 
    private:
241
 
    QString m_data;
242
 
    QStringList m_lineData;
243
 
};
244
 
 
245
 
class StringCodeRepresentation : public CodeRepresentation {
246
 
  public:
247
 
    explicit StringCodeRepresentation(QExplicitlySharedDataPointer<ArtificialStringData> _data) : data(_data) {
248
 
      Q_ASSERT(data);
249
 
    }
250
 
 
251
 
    QString line(int line) const override {
252
 
        if(line < 0 || line >= data->lines().size())
253
 
            return QString();
254
 
 
255
 
      return data->lines().at(line);
256
 
    }
257
 
 
258
 
    int lines() const override {
259
 
        return data->lines().count();
260
 
    }
261
 
 
262
 
    QString text() const override {
263
 
        return data->data();
264
 
    }
265
 
 
266
 
    bool setText(const QString& text) override {
267
 
        data->setData(text);
268
 
        return true;
269
 
    }
270
 
 
271
 
    bool fileExists() const override
272
 
    {
273
 
        return false;
274
 
    }
275
 
 
276
 
    QVector< KTextEditor::Range > grep ( const QString& identifier, bool surroundedByBoundary ) const override {
277
 
        QVector< KTextEditor::Range > ret;
278
 
 
279
 
        if (identifier.isEmpty())
280
 
            return ret;
281
 
 
282
 
        for(int line = 0; line < data->lines().count(); ++line)
283
 
            grepLine(identifier, data->lines().at(line), line, ret, surroundedByBoundary);
284
 
 
285
 
        return ret;
286
 
    }
287
 
 
288
 
  private:
289
 
    QExplicitlySharedDataPointer<ArtificialStringData> data;
290
 
};
291
 
 
292
 
static QHash<IndexedString, QExplicitlySharedDataPointer<ArtificialStringData> > artificialStrings;
293
 
 
294
 
//Return the representation for the given URL if it exists, or an empty pointer otherwise
295
 
static QExplicitlySharedDataPointer<ArtificialStringData> representationForPath(const IndexedString& path)
296
 
{
297
 
    if(artificialStrings.contains(path))
298
 
        return artificialStrings[path];
299
 
    else
300
 
    {
301
 
        IndexedString constructedPath(CodeRepresentation::artificialPath(path.str()));
302
 
        if(artificialStrings.contains(constructedPath))
303
 
            return artificialStrings[constructedPath];
304
 
        else
305
 
            return QExplicitlySharedDataPointer<ArtificialStringData>();
306
 
    }
307
 
}
308
 
 
309
 
bool artificialCodeRepresentationExists(const IndexedString& path)
310
 
{
311
 
    return representationForPath(path);
312
 
}
313
 
 
314
 
CodeRepresentation::Ptr createCodeRepresentation(const IndexedString& path) {
315
 
    if(artificialCodeRepresentationExists(path))
316
 
        return CodeRepresentation::Ptr(new StringCodeRepresentation(representationForPath(path)));
317
 
 
318
 
    IDocument* document = ICore::self()->documentController()->documentForUrl(path.toUrl());
319
 
    if(document && document->textDocument())
320
 
        return CodeRepresentation::Ptr(new EditorCodeRepresentation(document->textDocument()));
321
 
    else
322
 
        return CodeRepresentation::Ptr(new FileCodeRepresentation(path));
323
 
}
324
 
 
325
 
void CodeRepresentation::setDiskChangesForbidden(bool changesForbidden)
326
 
{
327
 
    onDiskChangesForbidden = changesForbidden;
328
 
}
329
 
 
330
 
QString CodeRepresentation::artificialPath(const QString& name)
331
 
{
332
 
    QUrl url = QUrl::fromLocalFile(name);
333
 
    return QLatin1String("/kdev-artificial/") + url.adjusted(QUrl::NormalizePathSegments).path();
334
 
}
335
 
 
336
 
InsertArtificialCodeRepresentation::InsertArtificialCodeRepresentation(const IndexedString& file,
337
 
                                                                       const QString& text)
338
 
: m_file(file)
339
 
{
340
 
    // make it simpler to use this by converting relative strings into artificial paths
341
 
    if(QUrl(m_file.str()).isRelative())
342
 
    {
343
 
        m_file = IndexedString(CodeRepresentation::artificialPath(file.str()));
344
 
 
345
 
        int idx = 0;
346
 
        while(artificialStrings.contains(m_file))
347
 
        {
348
 
            ++idx;
349
 
            m_file = IndexedString(CodeRepresentation::artificialPath(QStringLiteral("%1_%2").arg(idx).arg(file.str())));
350
 
        }
351
 
    }
352
 
 
353
 
    Q_ASSERT(!artificialStrings.contains(m_file));
354
 
 
355
 
    artificialStrings.insert(m_file, QExplicitlySharedDataPointer<ArtificialStringData>(new ArtificialStringData(text)));
356
 
}
357
 
 
358
 
IndexedString InsertArtificialCodeRepresentation::file()
359
 
{
360
 
    return m_file;
361
 
}
362
 
 
363
 
InsertArtificialCodeRepresentation::~InsertArtificialCodeRepresentation() {
364
 
    Q_ASSERT(artificialStrings.contains(m_file));
365
 
    artificialStrings.remove(m_file);
366
 
}
367
 
 
368
 
void InsertArtificialCodeRepresentation::setText(const QString& text) {
369
 
    Q_ASSERT(artificialStrings.contains(m_file));
370
 
    artificialStrings[m_file]->setData(text);
371
 
}
372
 
 
373
 
QString InsertArtificialCodeRepresentation::text() const
374
 
{
375
 
    Q_ASSERT(artificialStrings.contains(m_file));
376
 
    return artificialStrings[m_file]->data();
377
 
}
378
 
 
379
 
}
380
 
 
381
 
// kate: indent-width 4;