aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qbsprojectmanager/qbslanguageclient.cpp
blob: 33072bbb9eeb029d74d3edae05f9ff5514784de6 (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
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
// 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 "qbslanguageclient.h"

#include "qbsproject.h"
#include "qbssettings.h"

#include <coreplugin/editormanager/documentmodel.h>
#include <coreplugin/editormanager/editormanager.h>
#include <languageclient/languageclientinterface.h>
#include <languageclient/languageclientsettings.h>
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/target.h>
#include <utils/filepath.h>
#include <utils/mimeconstants.h>

#include <QPointer>

using namespace Core;
using namespace LanguageClient;
using namespace TextEditor;
using namespace Utils;

namespace QbsProjectManager::Internal {

class QbsLanguageClientInterface : public LocalSocketClientInterface
{
public:
    QbsLanguageClientInterface(const QString &serverPath, const FilePath &qbsExecutable)
        : LocalSocketClientInterface(serverPath),
          m_qbsExecutable(qbsExecutable) {}

private:
    Utils::FilePath serverDeviceTemplate() const override{ return m_qbsExecutable; };

    const FilePath m_qbsExecutable;
};

class QbsLanguageClient::Private
{
public:
    Private(QbsLanguageClient * q) : q(q) {}

    void checkDocument(IDocument *document);

    QbsLanguageClient * const q;
    QPointer<QbsBuildSystem> buildSystem;
};

QbsLanguageClient::QbsLanguageClient(const QString &serverPath, QbsBuildSystem *buildSystem)
    : Client(new QbsLanguageClientInterface(
                 serverPath, QbsSettings::qbsExecutableFilePath(*buildSystem->kit())))
    , d(new Private(this))
{
    d->buildSystem = buildSystem;
    setName(QString::fromLatin1("qbs@%1").arg(serverPath));
    setCurrentBuildConfiguration(buildSystem->buildConfiguration());
    LanguageFilter langFilter;
    langFilter.mimeTypes << Utils::Constants::QBS_MIMETYPE;
    setSupportedLanguage(langFilter);
    connect(EditorManager::instance(), &EditorManager::documentOpened,
            this, [this](IDocument *document) { d->checkDocument(document); });
    const QList<IDocument *> &allDocuments = DocumentModel::openedDocuments();
    for (IDocument * const document : allDocuments)
        d->checkDocument(document);
    start();
}

QbsLanguageClient::~QbsLanguageClient() { delete d; }

bool QbsLanguageClient::isActive() const
{
    if (!d->buildSystem)
        return false;
    if (!d->buildSystem->project()->activeBuildConfiguration())
        return false;
    if (d->buildSystem->project()->activeBuildSystem() != d->buildSystem)
        return false;
    return true;
}

void QbsLanguageClient::Private::checkDocument(IDocument *document)
{
    if (const auto doc = qobject_cast<TextDocument *>(document))
        q->openDocument(doc);
}

} // namespace QbsProjectManager::Internal