File: grepjob.cpp

package info (click to toggle)
kdevelop 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 73,508 kB
  • sloc: cpp: 291,803; python: 4,322; javascript: 3,518; sh: 1,316; ansic: 703; xml: 414; php: 95; lisp: 66; makefile: 31; sed: 12
file content (356 lines) | stat: -rw-r--r-- 10,678 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
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
/*
    SPDX-FileCopyrightText: 1999-2001 Bernd Gehrmann <bernd@kdevelop.org>
    SPDX-FileCopyrightText: 2007 Dukju Ahn <dukjuahn@gmail.com>
    SPDX-FileCopyrightText: 2008 Hamish Rodda <rodda@kde.org>
    SPDX-FileCopyrightText: 2010 Silvère Lestang <silvere.lestang@gmail.com>
    SPDX-FileCopyrightText: 2010 Julien Desgats <julien.desgats@gmail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "grepjob.h"
#include "grepfindthread.h"
#include "grepoutputmodel.h"
#include "greputil.h"

#include "debug.h"

#include <QDebug>
#include <QFile>
#include <QTextStream>

#include <KEncodingProber>
#include <KLocalizedString>

#include <serialization/indexedstring.h>
#include <interfaces/icore.h>
#include <interfaces/iuicontroller.h>

using namespace KDevelop;

QDebug operator<<(QDebug debug, const GrepJobSettings& s)
{
    const QDebugStateSaver saver(debug);

    debug.nospace() << '{';

    bool firstDataMember = true;
    const auto printDataMember = [&debug, &firstDataMember](const char* name, const auto& value) {
        if (firstDataMember) {
            firstDataMember = false;
        } else {
            // Separate the members with vertical bars,
            // because commas and semicolons separate items within string data members.
            debug << " | ";
        }
        debug << name << ": " << value;
    };

#define p(memberName) printDataMember(#memberName, s.memberName)
    p(fromHistory);

    p(projectFilesOnly);
    p(caseSensitive);
    p(regexp);

    p(depth);

    p(pattern);
    p(searchTemplate);
    p(replacementTemplate);
    p(files);
    p(exclude);
    p(searchPaths);
#undef p

    debug << '}';

    return debug;
}

GrepOutputItem::List grepFile(const QString &filename, const QRegExp &re)
{
    GrepOutputItem::List res;
    QFile file(filename);

    if(!file.open(QIODevice::ReadOnly))
        return res;
    int lineno = 0;


    // detect encoding (unicode files can be feed forever, stops when confidence reachs 99%
    KEncodingProber prober;
    while(!file.atEnd() && prober.state() == KEncodingProber::Probing && prober.confidence() < 0.99) {
        prober.feed(file.read(0xFF));
    }

    // reads file with detected encoding
    file.seek(0);
    // TODO: consider using QIODevice::readLine() and QStringDecoder in place of QTextStream.
    // The reason is the following paragraph in the documentation of QStringConverter::encodingForName():
    //      If the name is not the name of a codec listed in the Encoding enumeration, std::nullopt
    //      is returned. Such a name may, none the less, be accepted by the QStringConverter
    //      constructor when Qt is built with ICU, if ICU provides a converter with the given name.
    // The code inside the following `if` block replaced the line `stream.setCodec(prober.encoding().constData());`
    // during porting from Qt 5 to Qt 6. The function QTextStream::setCodec(const char *codecName)
    // removed in Qt 6 had probably supported ICU codec names.
    QTextStream stream(&file);
    if (prober.confidence() > 0.7) {
        const auto encoding = QStringConverter::encodingForName(prober.encoding().constData());
        if (encoding) {
            stream.setEncoding(*encoding);
        }
    }
    while( !stream.atEnd() )
    {
        QString data = stream.readLine();

        // remove line terminators (in order to not match them)
        for (int pos = data.length()-1; pos >= 0 && (data[pos] == QLatin1Char('\r') || data[pos] == QLatin1Char('\n')); pos--) {
            data.chop(1);
        }

        int offset = 0;
        // allow empty string matching result in an infinite loop !
        while( re.indexIn(data, offset)!=-1 && re.cap(0).length() > 0 )
        {
            int start = re.pos(0);
            int end = start + re.cap(0).length();

            DocumentChangePointer change = DocumentChangePointer(new DocumentChange(
                IndexedString(filename),
                KTextEditor::Range(lineno, start, lineno, end),
                re.cap(0), QString()));

            res << GrepOutputItem(change, data, false);
            offset = end;
        }
        lineno++;
    }
    file.close();
    return res;
}

GrepJob::GrepJob( QObject* parent )
    : KJob( parent )
    , m_workState(WorkUnstarted)
    , m_fileIndex(0)
    , m_findThread(nullptr)
    , m_findSomething(false)
{
    qRegisterMetaType<GrepOutputItem::List>();

    setCapabilities(Killable);
    KDevelop::ICore::self()->uiController()->registerStatus(this);

    connect(this, &GrepJob::result, this, &GrepJob::testFinishState);
}

GrepJob::~GrepJob()
{
    Q_ASSERT(m_workState == WorkDead);
}

QString GrepJob::statusName() const
{
    return i18n("Find in Files");
}

void GrepJob::slotFindFinished()
{
    Q_ASSERT(m_findThread && m_findThread->isFinished());

    if (m_workState == WorkCancelled) {
        dieAfterCancellation();
        return;
    }
    Q_ASSERT(m_workState == WorkCollectFiles);

    m_fileList = m_findThread->takeFiles();
    m_findThread->deleteLater();
    m_findThread = nullptr;

    if(m_fileList.isEmpty())
    {
        m_errorMessage = i18n("No files found matching the wildcard patterns");
        die();
        return;
    }

    if(!m_settings.regexp)
    {
        m_settings.pattern = QRegExp::escape(m_settings.pattern);
    }

    if(m_settings.regexp && QRegExp(m_settings.pattern).captureCount() > 0)
    {
        m_errorMessage = i18nc("Capture is the text which is \"captured\" with () in regular expressions "
                                    "see https://doc.qt.io/qt-5/qregexp.html#capturedTexts",
                                    "Captures are not allowed in pattern string");
        die();
        return;
    }

    QString pattern = substitudePattern(m_settings.searchTemplate, m_settings.pattern);
    m_regExp.setPattern(pattern);
    m_regExp.setPatternSyntax(QRegExp::RegExp2);
    m_regExp.setCaseSensitivity( m_settings.caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive );
    if(pattern == QRegExp::escape(pattern))
    {
        // enable wildcard mode when possible
        // if pattern has already been escaped (raw text serch) a second escape will result in a different string anyway
        m_regExp.setPatternSyntax(QRegExp::Wildcard);
    }

    if (m_outputModel) {
        m_outputModel->setRegExp(m_regExp);
        m_outputModel->setReplacementTemplate(m_settings.replacementTemplate);
    }

    emit showMessage(this, i18np("Searching for <b>%2</b> in one file",
                                 "Searching for <b>%2</b> in %1 files",
                                 m_fileList.length(),
                                 m_regExp.pattern().toHtmlEscaped()));

    m_workState = WorkGrep;
    QMetaObject::invokeMethod( this, "slotWork", Qt::QueuedConnection);
}

void GrepJob::slotWork()
{
    Q_ASSERT(!m_findThread);

    switch(m_workState)
    {
        case WorkUnstarted:
        case WorkDead:
            Q_UNREACHABLE();
            break;
        case WorkStarting:
            m_workState = WorkCollectFiles;
            emit showProgress(this, 0,0,0);
            QMetaObject::invokeMethod(this, "slotWork", Qt::QueuedConnection);
            break;
        case WorkCollectFiles:
            m_findThread = new GrepFindFilesThread(this, m_directoryChoice, m_settings.depth, m_settings.files, m_settings.exclude, m_settings.projectFilesOnly);
            emit showMessage(this, i18n("Collecting files..."));
            connect(m_findThread, &GrepFindFilesThread::finished, this, &GrepJob::slotFindFinished);
            m_findThread->start();
            break;
        case WorkGrep:
            if(m_fileIndex < m_fileList.length())
            {
                emit showProgress(this, 0, m_fileList.length(), m_fileIndex);
                if(m_fileIndex < m_fileList.length()) {
                    QString file = m_fileList[m_fileIndex].toLocalFile();
                    GrepOutputItem::List items = grepFile(file, m_regExp);

                    if(!items.isEmpty())
                    {
                        m_findSomething = true;
                        emit foundMatches(file, items);
                    }

                    m_fileIndex++;
                }
                QMetaObject::invokeMethod(this, "slotWork", Qt::QueuedConnection);
            }
            else
            {
                die();
            }
            break;
        case WorkCancelled:
            dieAfterCancellation();
            break;
    }
}

void GrepJob::die()
{
    emit hideProgress(this);
    emit clearMessage(this);
    m_workState = WorkDead;
    emitResult();
}

void GrepJob::dieAfterCancellation()
{
    Q_ASSERT(m_workState == WorkCancelled);
    m_errorMessage = i18n("Search aborted");
    die();
}

void GrepJob::start()
{
    if (m_workState != WorkUnstarted) {
        qCWarning(PLUGIN_GREPVIEW) << "cannot start a grep job more than once, current state:" << m_workState;
        return;
    }

    m_workState = WorkStarting;

    m_outputModel->clear();

    connect(this, &GrepJob::foundMatches,
            m_outputModel, &GrepOutputModel::appendOutputs, Qt::QueuedConnection);

    QMetaObject::invokeMethod(this, "slotWork", Qt::QueuedConnection);
}

bool GrepJob::doKill()
{
    if (m_workState == WorkUnstarted || m_workState == WorkDead) {
        m_workState = WorkDead;
        return true;
    }
    if (m_workState != WorkCancelled) {
        if (m_findThread) {
            Q_ASSERT(m_workState == WorkCollectFiles);
            m_findThread->tryAbort();
        }
        m_workState = WorkCancelled;
    }
    // Do not let KJob finish immediately if the state was neither Unstarted nor Dead:
    // * If m_findThread != nullptr, let it finish first.
    // * Otherwise, slotWork() is about to be invoked. Don't want this to be destroyed by KJob before that happens.
    return false;
}

void GrepJob::testFinishState(KJob *job)
{
    Q_ASSERT(m_workState == WorkDead);
    if(!job->error())
    {
        if (!m_errorMessage.isEmpty()) {
            emit showErrorMessage(i18n("Failed: %1", m_errorMessage));
        }
        else if (!m_findSomething) {
            emit showMessage(this, i18n("No results found"), 3000);
        }
    }
}

void GrepJob::setOutputModel(GrepOutputModel* model)
{
    m_outputModel = model;
}

void GrepJob::setDirectoryChoice(const QList<QUrl>& choice)
{
    m_directoryChoice = choice;
}

void GrepJob::setSettings(const GrepJobSettings& settings)
{
    m_settings = settings;

    setObjectName(i18n("Grep: %1", m_settings.pattern));
}

GrepJobSettings GrepJob::settings() const
{
    return m_settings;
}

#include "moc_grepjob.cpp"