aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/texteditor/codeassist/asyncprocessor.cpp
blob: 90f993a39c762946988ce7d7bcb345d08ee7128b (plain)
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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "asyncprocessor.h"

#include "assistinterface.h"
#include "iassistproposal.h"

#include <utils/runextensions.h>

namespace TextEditor {

AsyncProcessor::AsyncProcessor()
{
    QObject::connect(&m_watcher, &QFutureWatcher<IAssistProposal *>::finished, &m_watcher, [this] {
        setAsyncProposalAvailable(m_watcher.result());
    });
}

IAssistProposal *AsyncProcessor::perform()
{
    IAssistProposal *result = immediateProposal();
    interface()->prepareForAsyncUse();
    m_watcher.setFuture(Utils::runAsync([this] {
        interface()->recreateTextDocument();
        return performAsync();
    }));
    return result;
}

bool AsyncProcessor::running()
{
    return m_watcher.isRunning();
}

void AsyncProcessor::cancel()
{
    setAsyncCompletionAvailableHandler([this](IAssistProposal *proposal){
        delete proposal;
        QMetaObject::invokeMethod(QCoreApplication::instance(), [this] {
            delete this;
        }, Qt::QueuedConnection);
    });
}

IAssistProposal *AsyncProcessor::immediateProposal()
{
    return nullptr;
}

bool AsyncProcessor::isCanceled() const
{
    return m_watcher.isCanceled();
}

} // namespace TextEditor