File: documentchangeset.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 (618 lines) | stat: -rw-r--r-- 25,972 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
   Copyright 2008 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.
 */

#include "documentchangeset.h"

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

#include <algorithm>

#include <QStringList>
#include <QMimeDatabase>

#include <KLocalizedString>

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

#include <language/duchain/duchain.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/duchainutils.h>
#include <language/duchain/parsingenvironment.h>
#include <language/backgroundparser/backgroundparser.h>
#include <language/editor/modificationrevisionset.h>

#include <interfaces/isourceformattercontroller.h>
#include <interfaces/isourceformatter.h>
#include <interfaces/iproject.h>
#include <interfaces/iprojectcontroller.h>

#include <project/projectmodel.h>

#include <util/path.h>
#include <util/shellutils.h>

namespace KDevelop {
using ChangesList = QList<DocumentChangePointer>;
using ChangesHash = QHash<IndexedString, ChangesList>;

class DocumentChangeSetPrivate
{
public:
    DocumentChangeSet::ReplacementPolicy replacePolicy;
    DocumentChangeSet::FormatPolicy formatPolicy;
    DocumentChangeSet::DUChainUpdateHandling updatePolicy;
    DocumentChangeSet::ActivationPolicy activationPolicy;

    ChangesHash changes;
    QHash<IndexedString, IndexedString> documentsRename;

    DocumentChangeSet::ChangeResult addChange(const DocumentChangePointer& change);
    DocumentChangeSet::ChangeResult replaceOldText(CodeRepresentation* repr, const QString& newText,
                                                   const ChangesList& sortedChangesList);
    DocumentChangeSet::ChangeResult generateNewText(const IndexedString& file,
                                                    ChangesList& sortedChanges,
                                                    const CodeRepresentation* repr,
                                                    QString& output);
    DocumentChangeSet::ChangeResult removeDuplicates(const IndexedString& file,
                                                     ChangesList& filteredChanges);
    void formatChanges();
    void updateFiles();
};

// Simple helpers to clear up code clutter
namespace {
inline bool changeIsValid(const DocumentChange& change, const QStringList& textLines)
{
    return change.m_range.start() <= change.m_range.end() &&
           change.m_range.end().line() < textLines.size() &&
           change.m_range.start().line() >= 0 &&
           change.m_range.start().column() >= 0 &&
           change.m_range.start().column() <= textLines[change.m_range.start().line()].length() &&
           change.m_range.end().column() >= 0 &&
           change.m_range.end().column() <= textLines[change.m_range.end().line()].length();
}

inline bool duplicateChanges(const DocumentChangePointer& previous, const DocumentChangePointer& current)
{
    // Given the option of considering a duplicate two changes in the same range
    // but with different old texts to be ignored
    return previous->m_range == current->m_range &&
           previous->m_newText == current->m_newText &&
           (previous->m_oldText == current->m_oldText ||
            (previous->m_ignoreOldText && current->m_ignoreOldText));
}

inline QString rangeText(const KTextEditor::Range& range, const QStringList& textLines)
{
    QStringList ret;
    ret.reserve(range.end().line() - range.start().line() + 1);
    for (int line = range.start().line(); line <= range.end().line(); ++line) {
        const QString lineText = textLines.at(line);
        int startColumn = 0;
        int endColumn = lineText.length();
        if (line == range.start().line()) {
            startColumn = range.start().column();
        }
        if (line == range.end().line()) {
            endColumn = range.end().column();
        }
        ret << lineText.mid(startColumn, endColumn - startColumn);
    }

    return ret.join(QLatin1Char('\n'));
}

// need to have it as otherwise the arguments can exceed the maximum of 10
static QString printRange(const KTextEditor::Range& r)
{
    return i18nc("text range line:column->line:column", "%1:%2->%3:%4",
                 r.start().line(), r.start().column(),
                 r.end().line(), r.end().column());
}
}

DocumentChangeSet::DocumentChangeSet()
    : d_ptr(new DocumentChangeSetPrivate)
{
    Q_D(DocumentChangeSet);

    d->replacePolicy = StopOnFailedChange;
    d->formatPolicy = AutoFormatChanges;
    d->updatePolicy = SimpleUpdate;
    d->activationPolicy = DoNotActivate;
}

DocumentChangeSet::DocumentChangeSet(const DocumentChangeSet& rhs)
    : d_ptr(new DocumentChangeSetPrivate(*rhs.d_ptr))
{
}

DocumentChangeSet& DocumentChangeSet::operator=(const DocumentChangeSet& rhs)
{
    *d_ptr = *rhs.d_ptr;
    return *this;
}

DocumentChangeSet::~DocumentChangeSet() = default;

DocumentChangeSet::ChangeResult DocumentChangeSet::addChange(const DocumentChange& change)
{
    Q_D(DocumentChangeSet);

    return d->addChange(DocumentChangePointer(new DocumentChange(change)));
}

DocumentChangeSet::ChangeResult DocumentChangeSet::addChange(const DocumentChangePointer& change)
{
    Q_D(DocumentChangeSet);

    return d->addChange(change);
}

DocumentChangeSet::ChangeResult DocumentChangeSet::addDocumentRenameChange(const IndexedString& oldFile,
                                                                           const IndexedString& newname)
{
    Q_D(DocumentChangeSet);

    d->documentsRename.insert(oldFile, newname);
    return DocumentChangeSet::ChangeResult::successfulResult();
}

DocumentChangeSet::ChangeResult DocumentChangeSetPrivate::addChange(const DocumentChangePointer& change)
{
    changes[change->m_document].append(change);
    return DocumentChangeSet::ChangeResult::successfulResult();
}

void DocumentChangeSet::setReplacementPolicy(DocumentChangeSet::ReplacementPolicy policy)
{
    Q_D(DocumentChangeSet);

    d->replacePolicy = policy;
}

void DocumentChangeSet::setFormatPolicy(DocumentChangeSet::FormatPolicy policy)
{
    Q_D(DocumentChangeSet);

    d->formatPolicy = policy;
}

void DocumentChangeSet::setUpdateHandling(DocumentChangeSet::DUChainUpdateHandling policy)
{
    Q_D(DocumentChangeSet);

    d->updatePolicy = policy;
}

void DocumentChangeSet::setActivationPolicy(DocumentChangeSet::ActivationPolicy policy)
{
    Q_D(DocumentChangeSet);

    d->activationPolicy = policy;
}

DocumentChangeSet::ChangeResult DocumentChangeSet::applyAllChanges()
{
    Q_D(DocumentChangeSet);

    QUrl oldActiveDoc;
    if (IDocument* activeDoc = ICore::self()->documentController()->activeDocument()) {
        oldActiveDoc = activeDoc->url();
    }

    QList<QUrl> allFiles;
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
    const auto changedFiles =
        QSet<KDevelop::IndexedString>(d->documentsRename.keyBegin(), d->documentsRename.keyEnd()) +
        QSet<KDevelop::IndexedString>(d->changes.keyBegin(), d->changes.keyEnd());
#else
    const auto changedFiles = d->documentsRename.keys().toSet() + d->changes.keys().toSet();
#endif
    allFiles.reserve(changedFiles.size());
    for (const IndexedString& file : changedFiles) {
        allFiles << file.toUrl();
    }

    if (!KDevelop::ensureWritable(allFiles)) {
        return ChangeResult(QStringLiteral("some affected files are not writable"));
    }

    // rename files
    QHash<IndexedString, IndexedString>::const_iterator it = d->documentsRename.constBegin();
    for (; it != d->documentsRename.constEnd(); ++it) {
        QUrl url = it.key().toUrl();
        IProject* p = ICore::self()->projectController()->findProjectForUrl(url);
        if (p) {
            QList<ProjectFileItem*> files = p->filesForPath(it.key());
            if (!files.isEmpty()) {
                ProjectBaseItem::RenameStatus renamed = files.first()->rename(it.value().str());
                if (renamed == ProjectBaseItem::RenameOk) {
                    const QUrl newUrl = Path(Path(url).parent(), it.value().str()).toUrl();
                    if (url == oldActiveDoc) {
                        oldActiveDoc = newUrl;
                    }
                    IndexedString idxNewDoc(newUrl);

                    // ensure changes operate on new file name
                    ChangesHash::iterator iter = d->changes.find(it.key());
                    if (iter != d->changes.end()) {
                        // copy changes
                        ChangesList value = iter.value();
                        // remove old entry
                        d->changes.erase(iter);
                        // adapt to new url
                        for (auto& change : value) {
                            change->m_document = idxNewDoc;
                        }

                        d->changes[idxNewDoc] = value;
                    }
                } else {
                    ///FIXME: share code with project manager for the error code string representation
                    return ChangeResult(i18n("Could not rename '%1' to '%2'",
                                             url.toDisplayString(QUrl::PreferLocalFile), it.value().str()));
                }
            } else {
                //TODO: do it outside the project management?
                qCWarning(LANGUAGE) << "tried to rename file not tracked by project - not implemented";
            }
        } else {
            qCWarning(LANGUAGE) << "tried to rename a file outside of a project - not implemented";
        }
    }

    QMap<IndexedString, CodeRepresentation::Ptr> codeRepresentations;
    QMap<IndexedString, QString> newTexts;
    ChangesHash filteredSortedChanges;
    ChangeResult result = ChangeResult::successfulResult();

    const QList<IndexedString> files(d->changes.keys());

    for (const IndexedString& file : files) {
        CodeRepresentation::Ptr repr = createCodeRepresentation(file);
        if (!repr) {
            return ChangeResult(QStringLiteral("Could not create a Representation for %1").arg(file.str()));
        }

        codeRepresentations[file] = repr;

        QList<DocumentChangePointer>& sortedChangesList(filteredSortedChanges[file]);
        {
            result = d->removeDuplicates(file, sortedChangesList);
            if (!result)
                return result;
        }

        {
            result = d->generateNewText(file, sortedChangesList, repr.data(), newTexts[file]);
            if (!result)
                return result;
        }
    }

    QMap<IndexedString, QString> oldTexts;

    //Apply the changes to the files
    for (const IndexedString& file : files) {
        oldTexts[file] = codeRepresentations[file]->text();

        result = d->replaceOldText(codeRepresentations[file].data(), newTexts[file], filteredSortedChanges[file]);
        if (!result && d->replacePolicy == StopOnFailedChange) {
            //Revert all files
            for (auto it = oldTexts.constBegin(), end = oldTexts.constEnd(); it != end; ++it) {
                const IndexedString& revertFile = it.key();
                const QString& oldText = it.value();
                codeRepresentations[revertFile]->setText(oldText);
            }

            return result;
        }
    }

    d->updateFiles();

    if (d->activationPolicy == Activate) {
        for (const IndexedString& file : files) {
            ICore::self()->documentController()->openDocument(file.toUrl());
        }
    }

    // ensure the old document is still activated
    if (oldActiveDoc.isValid()) {
        ICore::self()->documentController()->openDocument(oldActiveDoc);
    }

    return result;
}

DocumentChangeSet::ChangeResult DocumentChangeSetPrivate::replaceOldText(CodeRepresentation* repr,
                                                                         const QString& newText,
                                                                         const ChangesList& sortedChangesList)
{
    auto* dynamic = dynamic_cast<DynamicCodeRepresentation*>(repr);
    if (dynamic) {
        auto transaction = dynamic->makeEditTransaction();
        //Replay the changes one by one

        for (int pos = sortedChangesList.size() - 1; pos >= 0; --pos) {
            const DocumentChange& change(*sortedChangesList[pos]);
            if (!dynamic->replace(change.m_range, change.m_oldText, change.m_newText, change.m_ignoreOldText)) {
                QString warningString = i18nc(
                    "Inconsistent change in <filename> between <range>, found <oldText> (encountered <foundText>) -> <newText>",
                    "Inconsistent change in %1 between %2, found %3 (encountered \"%4\") -> \"%5\"",
                    change.m_document.str(),
                    printRange(change.m_range),
                    change.m_oldText,
                    dynamic->rangeText(change.m_range),
                    change.m_newText);

                if (replacePolicy == DocumentChangeSet::WarnOnFailedChange) {
                    qCWarning(LANGUAGE) << warningString;
                } else if (replacePolicy == DocumentChangeSet::StopOnFailedChange) {
                    return DocumentChangeSet::ChangeResult(warningString);
                }
                //If set to ignore failed changes just continue with the others
            }
        }

        return DocumentChangeSet::ChangeResult::successfulResult();
    }

    //For files on disk
    if (!repr->setText(newText)) {
        QString warningString = i18n("Could not replace text in the document: %1",
                                     sortedChangesList.begin()->data()->m_document.str());
        if (replacePolicy == DocumentChangeSet::WarnOnFailedChange) {
            qCWarning(LANGUAGE) << warningString;
        }

        return DocumentChangeSet::ChangeResult(warningString);
    }

    return DocumentChangeSet::ChangeResult::successfulResult();
}

DocumentChangeSet::ChangeResult DocumentChangeSetPrivate::generateNewText(const IndexedString& file,
                                                                          ChangesList& sortedChanges,
                                                                          const CodeRepresentation* repr,
                                                                          QString& output)
{
    //Create the actual new modified file
    QStringList textLines = repr->text().split(QLatin1Char('\n'));

    QUrl url = file.toUrl();

    QMimeType mime = QMimeDatabase().mimeTypeForUrl(url);

    auto core = ICore::self();
    ISourceFormatter* formatter = core ? core->sourceFormatterController()->formatterForUrl(file.toUrl(), mime) : nullptr;

    QVector<int> removedLines;

    for (int pos = sortedChanges.size() - 1; pos >= 0; --pos) {
        DocumentChange& change(*sortedChanges[pos]);
        QString encountered;
        if (changeIsValid(change, textLines)  && //We demand this, although it should be fixed
            ((encountered = rangeText(change.m_range, textLines)) == change.m_oldText || change.m_ignoreOldText)) {
            ///Problem: This does not work if the other changes significantly alter the context @todo Use the changed context
            QString leftContext = QStringList(textLines.mid(0, change.m_range.start().line() + 1)).join(QLatin1Char(
                                                                                                            '\n'));
            leftContext.chop(textLines[change.m_range.start().line()].length() - change.m_range.start().column());

            QString rightContext = QStringList(textLines.mid(change.m_range.end().line())).join(QLatin1Char('\n')).mid(
                change.m_range.end().column());

            if (formatter && (formatPolicy == DocumentChangeSet::AutoFormatChanges
                              || formatPolicy == DocumentChangeSet::AutoFormatChangesKeepIndentation)) {
                QString oldNewText = change.m_newText;
                change.m_newText = formatter->formatSource(change.m_newText, url, mime, leftContext, rightContext);

                if (formatPolicy == DocumentChangeSet::AutoFormatChangesKeepIndentation) {
                    // Reproduce the previous indentation
                    const QStringList oldLines = oldNewText.split(QLatin1Char('\n'));
                    QStringList newLines = change.m_newText.split(QLatin1Char('\n'));

                    if (oldLines.size() == newLines.size()) {
                        for (int line = 0; line < newLines.size(); ++line) {
                            // Keep the previous indentation
                            QString oldIndentation;
                            for (const QChar a : oldLines[line]) {
                                if (a.isSpace()) {
                                    oldIndentation.append(a);
                                } else {
                                    break;
                                }
                            }

                            int newIndentationLength = 0;

                            for (int a = 0; a < newLines[line].size(); ++a) {
                                if (newLines[line][a].isSpace()) {
                                    newIndentationLength = a;
                                } else {
                                    break;
                                }
                            }

                            newLines[line].replace(0, newIndentationLength, oldIndentation);
                        }

                        change.m_newText = newLines.join(QLatin1Char('\n'));
                    } else {
                        qCDebug(LANGUAGE) << "Cannot keep the indentation because the line count has changed" <<
                            oldNewText;
                    }
                }
            }

            QString& line = textLines[change.m_range.start().line()];
            if (change.m_range.start().line() == change.m_range.end().line()) {
                // simply replace existing line content
                line.replace(change.m_range.start().column(),
                             change.m_range.end().column() - change.m_range.start().column(),
                             change.m_newText);
            } else {
                // replace first line contents
                line.replace(change.m_range.start().column(), line.length() - change.m_range.start().column(),
                             change.m_newText);
                // null other lines and remember for deletion
                const int firstLine = change.m_range.start().line() + 1;
                const int lastLine = change.m_range.end().line();
                removedLines.reserve(removedLines.size() + lastLine - firstLine + 1);
                for (int i = firstLine; i <= lastLine; ++i) {
                    textLines[i].clear();
                    removedLines << i;
                }
            }
        } else {
            QString warningString = i18nc("Inconsistent change in <document> at <range>"
                                          " = <oldText> (encountered <encountered>) -> <newText>",
                                          "Inconsistent change in %1 at %2"
                                          " = \"%3\"(encountered \"%4\") -> \"%5\"",
                                          file.str(),
                                          printRange(change.m_range),
                                          change.m_oldText,
                                          encountered,
                                          change.m_newText);

            if (replacePolicy == DocumentChangeSet::IgnoreFailedChange) {
                //Just don't do the replacement
            } else if (replacePolicy == DocumentChangeSet::WarnOnFailedChange) {
                qCWarning(LANGUAGE) << warningString;
            } else {
                return DocumentChangeSet::ChangeResult(warningString, sortedChanges[pos]);
            }
        }
    }

    if (!removedLines.isEmpty()) {
        int offset = 0;
        std::sort(removedLines.begin(), removedLines.end());
        for (int l : qAsConst(removedLines)) {
            textLines.removeAt(l - offset);
            ++offset;
        }
    }
    output = textLines.join(QLatin1Char('\n'));
    return DocumentChangeSet::ChangeResult::successfulResult();
}

//Removes all duplicate changes for a single file, and then returns (via filteredChanges) the filtered duplicates
DocumentChangeSet::ChangeResult DocumentChangeSetPrivate::removeDuplicates(const IndexedString& file,
                                                                           ChangesList& filteredChanges)
{
    using ChangesMap = QMultiMap<KTextEditor::Cursor, DocumentChangePointer>;
    ChangesMap sortedChanges;

    for (const DocumentChangePointer& change : qAsConst(changes[file])) {
        sortedChanges.insert(change->m_range.end(), change);
    }

    //Remove duplicates
    ChangesMap::iterator previous = sortedChanges.begin();
    for (ChangesMap::iterator it = ++sortedChanges.begin(); it != sortedChanges.end();) {
        if ((*previous) && (*previous)->m_range.end() > (*it)->m_range.start()) {
            //intersection
            if (duplicateChanges((*previous), *it)) {
                //duplicate, remove one
                it = sortedChanges.erase(it);
                continue;
            }
            //When two changes contain each other, and the container change is set to ignore old text, then it should be safe to
            //just ignore the contained change, and apply the bigger change
            else if ((*it)->m_range.contains((*previous)->m_range) && (*it)->m_ignoreOldText) {
                qCDebug(LANGUAGE) << "Removing change: " << (*previous)->m_oldText << "->" << (*previous)->m_newText
                                  << ", because it is contained by change: " << (*it)->m_oldText << "->" <<
                (*it)->m_newText;
                sortedChanges.erase(previous);
            }
            //This case is for when both have the same end, either of them could be the containing range
            else if ((*previous)->m_range.contains((*it)->m_range) && (*previous)->m_ignoreOldText) {
                qCDebug(LANGUAGE) << "Removing change: " << (*it)->m_oldText << "->" << (*it)->m_newText
                                  << ", because it is contained by change: " << (*previous)->m_oldText
                                  << "->" << (*previous)->m_newText;
                it = sortedChanges.erase(it);
                continue;
            } else {
                return DocumentChangeSet::ChangeResult(
                    i18nc("Inconsistent change-request at <document>:"
                          "intersecting changes: "
                          "<previous-oldText> -> <previous-newText> @<range> & "
                          "<new-oldText> -> <new-newText> @<range>",
                          "Inconsistent change-request at %1; "
                          "intersecting changes: "
                          "\"%2\"->\"%3\"@%4 & \"%5\"->\"%6\"@%7 ",
                          file.str(),
                          (*previous)->m_oldText,
                          (*previous)->m_newText,
                          printRange((*previous)->m_range),
                          (*it)->m_oldText,
                          (*it)->m_newText,
                          printRange((*it)->m_range)));
            }
        }
        previous = it;
        ++it;
    }

    filteredChanges = sortedChanges.values();
    return DocumentChangeSet::ChangeResult::successfulResult();
}

void DocumentChangeSetPrivate::updateFiles()
{
    ModificationRevisionSet::clearCache();
    const auto files = changes.keys();
    for (const IndexedString& file : files) {
        ModificationRevision::clearModificationCache(file);
    }

    if (updatePolicy != DocumentChangeSet::NoUpdate && ICore::self()) {
        // The active document should be updated first, so that the user sees the results instantly
        if (IDocument* activeDoc = ICore::self()->documentController()->activeDocument()) {
            ICore::self()->languageController()->backgroundParser()->addDocument(IndexedString(activeDoc->url()));
        }

        // If there are currently open documents that now need an update, update them too
        const auto documents = ICore::self()->languageController()->backgroundParser()->managedDocuments();
        for (const IndexedString& doc : documents) {
            DUChainReadLocker lock(DUChain::lock());
            TopDUContext* top = DUChainUtils::standardContextForUrl(doc.toUrl(), true);
            if ((top && top->parsingEnvironmentFile() && top->parsingEnvironmentFile()->needsUpdate()) || !top) {
                lock.unlock();
                ICore::self()->languageController()->backgroundParser()->addDocument(doc);
            }
        }

        // Eventually update _all_ affected files
        const auto files = changes.keys();
        for (const IndexedString& file : files) {
            if (!file.toUrl().isValid()) {
                qCWarning(LANGUAGE) << "Trying to apply changes to an invalid document";
                continue;
            }

            ICore::self()->languageController()->backgroundParser()->addDocument(file);
        }
    }
}
}