aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qbsprojectmanager/qbseditor.cpp
blob: 7eed6977a2c95b6e6607630c86aefc7e4f014631 (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
57
58
59
60
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qbseditor.h"

#include "qbslanguageclient.h"
#include "qbsprojectmanagertr.h"

#include <languageclient/languageclientmanager.h>
#include <utils/mimeconstants.h>

#include <QPointer>

using namespace LanguageClient;
using namespace QmlJSEditor;
using namespace Utils;

namespace QbsProjectManager::Internal {

class QbsEditorWidget : public QmlJSEditorWidget
{
private:
    void findLinkAt(const QTextCursor &cursor,
                    const LinkHandler &processLinkCallback,
                    bool resolveTarget = true,
                    bool inNextSplit = false) override;
};

QbsEditorFactory::QbsEditorFactory() : QmlJSEditorFactory("QbsEditor.QbsEditor")
{
    setDisplayName(Tr::tr("Qbs Editor"));
    setMimeTypes({Utils::Constants::QBS_MIMETYPE});
    setEditorWidgetCreator([] { return new QbsEditorWidget; });
}

void QbsEditorWidget::findLinkAt(const QTextCursor &cursor, const LinkHandler &processLinkCallback,
                                 bool resolveTarget, bool inNextSplit)
{
    const LinkHandler extendedCallback = [self = QPointer(this), cursor, processLinkCallback,
                                          resolveTarget](const Link &link) {
        if (link.hasValidTarget())
            return processLinkCallback(link);
        if (!self)
            return;
        const auto doc = self->textDocument();
        if (!doc)
            return;
        const QList<Client *> &candidates = LanguageClientManager::clientsSupportingDocument(doc);
        for (Client * const candidate : candidates) {
            const auto qbsClient = qobject_cast<QbsLanguageClient *>(candidate);
            if (!qbsClient || !qbsClient->isActive() || !qbsClient->documentOpen(doc))
                continue;
            qbsClient->findLinkAt(doc, cursor, processLinkCallback, resolveTarget,
                               LinkTarget::SymbolDef);
        }
    };
    QmlJSEditorWidget::findLinkAt(cursor, extendedCallback, resolveTarget, inNextSplit);
}

} // namespace QbsProjectManager::Internal