File: parseprojectjob.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (196 lines) | stat: -rw-r--r-- 6,949 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2009 David Nolden <david.nolden.kdevelop@art-master.de>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "parseprojectjob.h"

#include <debug.h>

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

#include <language/backgroundparser/backgroundparser.h>

#include <KLocalizedString>

#include <QCoreApplication>
#include <QPointer>
#include <QSet>
#include <QTimer>

using namespace KDevelop;

class KDevelop::ParseProjectJobPrivate
{
public:
    explicit ParseProjectJobPrivate(bool forceUpdate, bool parseAllProjectSources)
        : forceUpdate(forceUpdate)
        , parseAllProjectSources{parseAllProjectSources}
    {
    }

    const bool forceUpdate;
    const bool parseAllProjectSources;
    int fileCountLeftToParse = 0;
    QSet<IndexedString> filesToParse;
};

bool ParseProjectJob::doKill()
{
    qCDebug(LANGUAGE) << "stopping project parse job";
    ICore::self()->languageController()->backgroundParser()->revertAllRequests(this);
    return true;
}

ParseProjectJob::~ParseProjectJob() = default;

ParseProjectJob::ParseProjectJob(IProject* project, bool forceUpdate, bool parseAllProjectSources)
    : d_ptr{new ParseProjectJobPrivate(forceUpdate, parseAllProjectSources)}
{
    Q_D(ParseProjectJob);

    if (parseAllProjectSources) {
        d->filesToParse = project->fileSet();
    } else {
        // In case we don't want to parse the whole project, still add all currently open files that belong to the project to the background-parser
        const auto documents = ICore::self()->documentController()->openDocuments();
        const auto projectFiles = project->fileSet();
        for (auto* document : documents) {
            const auto path = IndexedString(document->url());
            if (projectFiles.contains(path)) {
                d->filesToParse.insert(path);
            }
        }
    }
    d->fileCountLeftToParse = d->filesToParse.size();

    setCapabilities(Killable);

    setObjectName(i18np("Process 1 file in %2", "Process %1 files in %2", d->filesToParse.size(), project->name()));
}

void ParseProjectJob::updateReady(const IndexedString& url, const ReferencedTopDUContext& topContext)
{
    Q_D(ParseProjectJob);

    Q_UNUSED(url);
    Q_UNUSED(topContext);
    --d->fileCountLeftToParse;
    Q_ASSERT(d->fileCountLeftToParse >= 0);
    if (d->fileCountLeftToParse == 0) {
        deleteLater();
    }
}

void ParseProjectJob::start()
{
    Q_D(ParseProjectJob);

    if (d->filesToParse.isEmpty()) {
        deleteLater();
        return;
    }

    qCDebug(LANGUAGE) << "starting project parse job";
    // Avoid calling QCoreApplication::processEvents() directly in start() to prevent
    // a crash in RunController::checkState().
    QTimer::singleShot(0, this, &ParseProjectJob::queueFilesToParse);
}

void ParseProjectJob::queueFilesToParse()
{
    Q_D(ParseProjectJob);

    const auto isJobKilled = [this] {
        if (Q_UNLIKELY(isFinished())) {
            qCDebug(LANGUAGE) << "Aborting queuing project files to parse."
                                 " This job has been killed:" << objectName();
            return true;
        }
        return false;
    };

    if (isJobKilled()) {
        return;
    }

    TopDUContext::Features processingLevel = d->filesToParse.size() <
                                             ICore::self()->languageController()->completionSettings()->
                                             minFilesForSimplifiedParsing() ?
                                             TopDUContext::VisibleDeclarationsAndContexts : TopDUContext::
                                             SimplifiedVisibleDeclarationsAndContexts;
    TopDUContext::Features openDocumentProcessingLevel{TopDUContext::AllDeclarationsContextsAndUses};

    if (d->forceUpdate) {
        if (processingLevel & TopDUContext::VisibleDeclarationsAndContexts) {
            processingLevel = TopDUContext::AllDeclarationsContextsAndUses;
        }
        processingLevel |= TopDUContext::ForceUpdate;
        openDocumentProcessingLevel |= TopDUContext::ForceUpdate;
    }

    if (auto currentDocument = ICore::self()->documentController()->activeDocument()) {
        const auto path = IndexedString(currentDocument->url());
        const auto fileIt = d->filesToParse.constFind(path);
        if (fileIt != d->filesToParse.cend()) {
            ICore::self()->languageController()->backgroundParser()->addDocument(path,
                    openDocumentProcessingLevel, BackgroundParser::BestPriority, this);
            d->filesToParse.erase(fileIt);
        }
    }

    int priority{BackgroundParser::InitialParsePriority};
    const int openDocumentPriority{10};
    if (d->parseAllProjectSources) {
        // Add all currently open files that belong to the project to the
        // background-parser, so that they'll be parsed first of all.
        const auto documents = ICore::self()->documentController()->openDocuments();
        for (auto* document : documents) {
            const auto path = IndexedString(document->url());
            const auto fileIt = d->filesToParse.constFind(path);
            if (fileIt != d->filesToParse.cend()) {
                ICore::self()->languageController()->backgroundParser()->addDocument(path,
                        openDocumentProcessingLevel, openDocumentPriority, this);
                d->filesToParse.erase(fileIt);
            }
        }
    } else {
        // In this case the constructor inserts only open documents into d->filesToParse.
        processingLevel = openDocumentProcessingLevel;
        priority = openDocumentPriority;
    }

    // prevent UI-lockup by processing events after some files
    // esp. noticeable when dealing with huge projects
    const int processAfter = 1000;
    int processed = 0;
    // guard against reentrancy issues, see also bug 345480
    auto crashGuard = QPointer<ParseProjectJob> {this};
    for (const IndexedString& url : std::as_const(d->filesToParse)) {
        ICore::self()->languageController()->backgroundParser()->addDocument(url, processingLevel,
                                                                             priority,
                                                                             this);
        ++processed;
        if (processed == processAfter) {
            QCoreApplication::processEvents();
            if (Q_UNLIKELY(!crashGuard)) {
                qCDebug(LANGUAGE) << "Aborting queuing project files to parse."
                                     " This job has been destroyed.";
                return;
            }
            if (isJobKilled()) {
                return;
            }
            processed = 0;
        }
    }

    d->filesToParse = {}; // free memory or prevent detaching
}

#include "moc_parseprojectjob.cpp"