File: backgroundparser.h

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 (244 lines) | stat: -rw-r--r-- 9,226 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
/*
 * This file is part of KDevelop
 *
 * Copyright 2006 Adam Treat <treat@kde.org>
 * Copyright 2007 Kris Wong <kris.p.wong@gmail.com>
 * Copyright 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef KDEVPLATFORM_BACKGROUNDPARSER_H
#define KDEVPLATFORM_BACKGROUNDPARSER_H

#include <language/languageexport.h>
#include <interfaces/istatus.h>
#include <language/duchain/topducontext.h>
#include <language/interfaces/ilanguagesupport.h>
#include "parsejob.h"

namespace ThreadWeaver {
class Job;
class QObjectDecorator;
class Weaver;
}

namespace KDevelop {
class DocumentChangeTracker;

class IDocument;
class IProject;
class ILanguageController;
class ParserDependencyPolicy;

/**
 * This class handles the creation of parse jobs for given file URLs.
 *
 * For performance reasons you must always use clean, canonical URLs. If you do not do that,
 * issues might arise (and the debug build will assert).
 */
class KDEVPLATFORMLANGUAGE_EXPORT BackgroundParser
    : public QObject
    , public IStatus
{
    Q_OBJECT
    Q_INTERFACES(KDevelop::IStatus)

public:
    explicit BackgroundParser(ILanguageController* languageController);
    ~BackgroundParser() override;

    QString statusName() const override;

    enum {
        BestPriority = -10000,  ///Best possible job-priority. No jobs should actually have this.
        NormalPriority = 0,     ///Standard job-priority. This priority is used for parse-jobs caused by document-editing/opening.
        ///There is an additional parsing-thread reserved for jobs with this and better priority, to improve responsiveness.
        InitialParsePriority = 10000, ///Priority used when adding file on project loading
        WorstPriority = 100000  ///Worst possible job-priority.
    };

    /**
     * Queries the background parser as to whether there is currently
     * a parse job for @p document, and if so, returns it.
     *
     * This may not contain all of the parse jobs that are intended
     * unless you call in from your job's ThreadWeaver::Job::aboutToBeQueued()
     * function.
     */
    ParseJob* parseJobForDocument(const IndexedString& document) const;

    /**
     * Set how many ThreadWeaver threads the background parser should set up and use.
     */
    void setThreadCount(int threadCount);

    /**
     * Return how many ThreadWeaver threads the background parser should set up and use.
     */
    int threadCount() const;

    /**
     * Set the delay in milliseconds before the background parser starts parsing.
     */
    void setDelay(int milliseconds);

    /**
     * Returns all documents that were added through addManagedTopRange. This is typically the currently
     * open documents.
     */
    QList<IndexedString> managedDocuments();

    /**
     * Returns the tracker for the given url if the document is being tracked, else returns zero.
     * This function is thread-safe, but the returned object also isn't, so you must not use it
     * when you're in a background thread without the foreground lock acquired.
     * */
    DocumentChangeTracker* trackerForUrl(const IndexedString& url) const;

    bool waitForIdle() const;

Q_SIGNALS:
    /**
     * Emitted whenever a document parse-job has finished.
     * The job contains the du-chain(if one was created) etc.
     *
     * The job is deleted after this signal has been emitted.  Receivers should not hold
     * references to it.
     *
     * Note that if you want to be get updated for all DUChain updates, use
     * DUChain::updateReady instead, as a single ParseJob may update multiple
     * DUChain top contexts.
     *
     * @sa DUChain::updateReady
     */
    void parseJobFinished(KDevelop::ParseJob* job);

    // Implementations of IStatus signals
    void clearMessage(KDevelop::IStatus*) override;
    void showMessage(KDevelop::IStatus*, const QString& message, int timeout = 0) override;
    void hideProgress(KDevelop::IStatus*) override;
    void showProgress(KDevelop::IStatus*, int minimum, int maximum, int value) override;
    void showErrorMessage(const QString&, int) override;

public Q_SLOTS:
    /**
     * Aborts all parse jobs
     */
    void abortAllJobs();

    /**
     * Suspends execution of the background parser
     *
     * NOTE: Only the Core should call this
     */
    void suspend();

    /**
     * Resumes execution of the background parser
     *
     * NOTE: Only the Core should call this
     */
    void resume();

    /// Reverts all requests that were made for the given notification-target.
    /// priorities and requested features will be reverted as well.
    /// When @p notifyWhenReady is set to a nullptr, all requests will be reverted.
    void revertAllRequests(QObject* notifyWhenReady);

    /**
     * Queues up the @p url to be parsed.
     * @p features The minimum features that should be computed for this top-context
     * @p priority A value that manages the order of parsing. Documents with lowest priority are parsed first.
     * @param notifyWhenReady An optional pointer to a QObject that should contain a slot
     *                        "void updateReady(KDevelop::IndexedString url, KDevelop::ReferencedTopDUContext topContext)".
     *                        The notification is guaranteed to be called once for each call to addDocument. The given top-context
     *                        may be invalid if the update failed.
     * @param flags Flags indicating how the document should be treated in the queue
     * @param delay_ms The delay in milliseconds to add the job with, or one of the values of the
     *                 ILanguageSupport::ReparseDelaySpecialValues enum.
     */
    void addDocument(const IndexedString& url,
                     TopDUContext::Features features = TopDUContext::VisibleDeclarationsAndContexts,
                     int priority = 0,
                     QObject* notifyWhenReady = nullptr,
                     ParseJob::SequentialProcessingFlags flags = ParseJob::IgnoresSequentialProcessing,
                     int delay_ms = ILanguageSupport::DefaultDelay);

    /**
     * Removes the @p url that is registered for the given notification from the url.
     *
     * @param notifyWhenReady Notifier the document was added with.
     */
    void removeDocument(const IndexedString& url, QObject* notifyWhenReady = nullptr);

    /**
     * Forces the current queue to be parsed.
     */
    void parseDocuments();

    void updateProgressData();

    /// Disables processing for all jobs that have a worse priority than @p priority
    /// This can be used to temporarily limit the processing to only the most important jobs.
    /// To only enable processing for important jobs, call setNeededPriority(0).
    /// This should only be used to temporarily alter the processing. A progress-bar
    /// will still be shown for the not yet processed jobs.
    void setNeededPriority(int priority);
    /// Disables all processing of new jobs, equivalent to setNeededPriority(BestPriority)
    void disableProcessing();
    /// Enables all processing of new jobs, equivalent to setNeededPriority(WorstPriority)
    void enableProcessing();

    /// Returns true if the given url is queued for parsing
    bool isQueued(const IndexedString& url) const;

    /// Retrieve the current priority for the given URL.
    /// You need to check whether @param url is queued before calling this function.
    int priorityForDocument(const IndexedString& url) const;

    /// Returns the number of queued jobs (not yet running nor submitted to ThreadWeaver)
    int queuedCount() const;

    /// Returns true if there are no jobs running nor queued anywhere
    bool isIdle() const;

    void documentClosed(KDevelop::IDocument*);
    void documentLoaded(KDevelop::IDocument*);
    void documentUrlChanged(KDevelop::IDocument*);

    void loadSettings();

protected Q_SLOTS:
    void parseComplete(const ThreadWeaver::JobPointer& job);
    void parseProgress(KDevelop::ParseJob*, float value, const QString& text);
    void startTimer(int delay);
    void aboutToQuit();
    void updateProgressBar();

private:
    friend class BackgroundParserPrivate;
    class BackgroundParserPrivate* d_ptr;
    Q_DECLARE_PRIVATE(BackgroundParser)

private Q_SLOTS:
    /// Tracking of projects in state of loading.
    void projectAboutToBeOpened(KDevelop::IProject* project);
    void projectOpened(KDevelop::IProject* project);
    void projectOpeningAborted(KDevelop::IProject* project);
};
}
#endif