aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/android/javalanguageserver.cpp
blob: 4201f92f33b2fd370c4d7b1484a3a25f35cb6d47 (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
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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "javalanguageserver.h"

#include "androidconfigurations.h"
#include "androidconstants.h"
#include "androidmanager.h"

#include <languageclient/client.h>
#include <languageclient/languageclientinterface.h>
#include <languageclient/languageclientutils.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/target.h>
#include <qtsupport/qtkitinformation.h>
#include <utils/environment.h>
#include <utils/pathchooser.h>
#include <utils/temporarydirectory.h>
#include <utils/variablechooser.h>

#include <QGridLayout>
#include <QLineEdit>
#include <QXmlStreamWriter>

using namespace ProjectExplorer;
using namespace Utils;

constexpr char languageServerKey[] = "languageServer";

namespace Android {
namespace Internal {

class JLSSettingsWidget : public QWidget
{
    Q_DECLARE_TR_FUNCTIONS(JLSSettingsWidget)
public:
    JLSSettingsWidget(const JLSSettings *settings, QWidget *parent);

    QString name() const { return m_name->text(); }
    FilePath java() const { return m_java->filePath(); }
    FilePath languageServer() const { return m_ls->filePath(); }

private:
    QLineEdit *m_name = nullptr;
    PathChooser *m_java = nullptr;
    PathChooser *m_ls = nullptr;
};

JLSSettingsWidget::JLSSettingsWidget(const JLSSettings *settings, QWidget *parent)
    : QWidget(parent)
    , m_name(new QLineEdit(settings->m_name, this))
    , m_java(new PathChooser(this))
    , m_ls(new PathChooser(this))
{
    int row = 0;
    auto *mainLayout = new QGridLayout;
    mainLayout->addWidget(new QLabel(tr("Name:")), row, 0);
    mainLayout->addWidget(m_name, row, 1);
    auto chooser = new VariableChooser(this);
    chooser->addSupportedWidget(m_name);

    mainLayout->addWidget(new QLabel(tr("Java:")), ++row, 0);
    m_java->setExpectedKind(PathChooser::ExistingCommand);
    m_java->setFilePath(settings->m_executable);
    mainLayout->addWidget(m_java, row, 1);

    mainLayout->addWidget(new QLabel(tr("Java Language Server:")), ++row, 0);
    m_ls->setExpectedKind(PathChooser::File);
    m_ls->lineEdit()->setPlaceholderText(tr("Path to equinox launcher jar"));
    m_ls->setPromptDialogFilter("org.eclipse.equinox.launcher_*.jar");
    m_ls->setFilePath(settings->m_languageServer);
    mainLayout->addWidget(m_ls, row, 1);

    setLayout(mainLayout);
}

JLSSettings::JLSSettings()
{
    m_settingsTypeId = Constants::JLS_SETTINGS_ID;
    m_name = "Java Language Server";
    m_startBehavior = RequiresProject;
    m_languageFilter.mimeTypes = QStringList(Constants::JAVA_MIMETYPE);
    const FilePath &javaPath = Environment::systemEnvironment().searchInPath("java");
    if (javaPath.exists())
        m_executable = javaPath;
}

bool JLSSettings::applyFromSettingsWidget(QWidget *widget)
{
    bool changed = false;
    auto jlswidget = static_cast<JLSSettingsWidget *>(widget);
    changed |= m_name != jlswidget->name();
    m_name = jlswidget->name();

    changed |= m_languageServer != jlswidget->languageServer();
    m_languageServer = jlswidget->languageServer();

    changed |= m_executable != jlswidget->java();
    m_executable = jlswidget->java();

    QString arguments = "-Declipse.application=org.eclipse.jdt.ls.core.id1 "
                        "-Dosgi.bundles.defaultStartLevel=4 "
                        "-Declipse.product=org.eclipse.jdt.ls.core.product "
                        "-Dlog.level=WARNING "
                        "-noverify "
                        "-Xmx1G "
                        "-jar \"%1\" "
                        "-configuration \"%2\"";

    QDir configDir = m_languageServer.toFileInfo().absoluteDir();
    if (configDir.exists()) {
        configDir.cdUp();
        if constexpr (HostOsInfo::hostOs() == OsTypeWindows)
            configDir.cd("config_win");
        else if constexpr (HostOsInfo::hostOs() == OsTypeLinux)
            configDir.cd("config_linux");
        else if constexpr (HostOsInfo::hostOs() == OsTypeMac)
            configDir.cd("config_mac");
    }
    if (configDir.exists()) {
        arguments = arguments.arg(m_languageServer.toString(), configDir.absolutePath());
        changed |= m_arguments != arguments;
        m_arguments = arguments;
    }
    return changed;
}

QWidget *JLSSettings::createSettingsWidget(QWidget *parent) const
{
    return new JLSSettingsWidget(this, parent);
}

bool JLSSettings::isValid() const
{
    return StdIOSettings::isValid() && !m_languageServer.isEmpty();
}

QVariantMap JLSSettings::toMap() const
{
    QVariantMap map = StdIOSettings::toMap();
    map.insert(languageServerKey, m_languageServer.toVariant());
    return map;
}

void JLSSettings::fromMap(const QVariantMap &map)
{
    StdIOSettings::fromMap(map);
    m_languageServer = FilePath::fromVariant(map[languageServerKey]);
}

LanguageClient::BaseSettings *JLSSettings::copy() const
{
    return new JLSSettings(*this);
}

class JLSInterface : public LanguageClient::StdIOClientInterface
{
public:
    JLSInterface() = default;

    QString workspaceDir() const { return m_workspaceDir.path().path(); }

private:
    TemporaryDirectory m_workspaceDir = TemporaryDirectory("QtCreator-jls-XXXXXX");
};

LanguageClient::BaseClientInterface *JLSSettings::createInterface(ProjectExplorer::Project *) const
{
    auto interface = new JLSInterface();
    CommandLine cmd{m_executable, arguments(), CommandLine::Raw};
    cmd.addArgs({"-data", interface->workspaceDir()});
    interface->setCommandLine(cmd);
    return interface;
}

class JLSClient : public LanguageClient::Client
{
public:
    using Client::Client;

    void executeCommand(const LanguageServerProtocol::Command &command) override;
    void setCurrentProject(Project *project) override;
    void updateProjectFiles();
    void updateTarget(Target *target);

private:
    Target *m_currentTarget = nullptr;
};

void JLSClient::executeCommand(const LanguageServerProtocol::Command &command)
{
    if (command.command() == "java.apply.workspaceEdit") {
        const QJsonArray arguments = command.arguments().value_or(QJsonArray());
        for (const QJsonValue &argument : arguments) {
            if (!argument.isObject())
                continue;
            LanguageServerProtocol::WorkspaceEdit edit(argument.toObject());
            if (edit.isValid())
                LanguageClient::applyWorkspaceEdit(this, edit);
        }
    } else {
        Client::executeCommand(command);
    }
}

void JLSClient::setCurrentProject(Project *project)
{
    Client::setCurrentProject(project);
    QTC_ASSERT(project, return);
    updateTarget(project->activeTarget());
    updateProjectFiles();
    connect(project, &Project::activeTargetChanged, this, &JLSClient::updateTarget);
}

static void generateProjectFile(const FilePath &projectDir,
                                const QString &qtSrc,
                                const QString &projectName)
{
    const FilePath projectFilePath = projectDir.pathAppended(".project");
    QFile projectFile(projectFilePath.toString());
    if (projectFile.open(QFile::Truncate | QFile::WriteOnly)) {
        QXmlStreamWriter writer(&projectFile);
        writer.setAutoFormatting(true);
        writer.writeStartDocument();
        writer.writeComment("Autogenerated by Qt Creator. "
                            "Changes to this file will not be taken into account.");
        writer.writeStartElement("projectDescription");
        writer.writeTextElement("name", projectName);
        writer.writeStartElement("natures");
        writer.writeTextElement("nature", "org.eclipse.jdt.core.javanature");
        writer.writeEndElement(); // natures
        writer.writeStartElement("linkedResources");
        writer.writeStartElement("link");
        writer.writeTextElement("name", "qtSrc");
        writer.writeTextElement("type", "2");
        writer.writeTextElement("location", qtSrc);
        writer.writeEndElement(); // link
        writer.writeEndElement(); // linkedResources
        writer.writeEndElement(); // projectDescription
        writer.writeEndDocument();
        projectFile.close();
    }
}

static void generateClassPathFile(const FilePath &projectDir,
                                  const FilePath &sourceDir,
                                  const FilePaths &libs)
{
    const FilePath classPathFilePath = projectDir.pathAppended(".classpath");
    QFile classPathFile(classPathFilePath.toString());
    if (classPathFile.open(QFile::Truncate | QFile::WriteOnly)) {
        QXmlStreamWriter writer(&classPathFile);
        writer.setAutoFormatting(true);
        writer.writeStartDocument();
        writer.writeComment("Autogenerated by Qt Creator. "
                            "Changes to this file will not be taken into account.");
        writer.writeStartElement("classpath");
        writer.writeEmptyElement("classpathentry");
        writer.writeAttribute("kind", "src");
        writer.writeAttribute("path", sourceDir.toString());
        writer.writeEmptyElement("classpathentry");
        writer.writeAttribute("kind", "src");
        writer.writeAttribute("path", "qtSrc");
        for (const FilePath &lib : libs) {
            writer.writeEmptyElement("classpathentry");
            writer.writeAttribute("kind", "lib");
            writer.writeAttribute("path", lib.toString());
        }
        writer.writeEndElement(); // classpath
        writer.writeEndDocument();
        classPathFile.close();
    }
}

void JLSClient::updateProjectFiles()
{
    if (!m_currentTarget)
        return;
    if (Target *target = m_currentTarget) {
        Kit *kit = m_currentTarget->kit();
        if (DeviceTypeKitAspect::deviceTypeId(kit) != Android::Constants::ANDROID_DEVICE_TYPE)
            return;
        if (ProjectNode *node = project()->findNodeForBuildKey(target->activeBuildKey())) {
            QtSupport::QtVersion *version = QtSupport::QtKitAspect::qtVersion(kit);
            if (!version)
                return;
            const QString qtSrc = version->prefix().toString() + "/src/android/java/src";
            const FilePath &projectDir = project()->rootProjectDirectory();
            if (!projectDir.exists())
                return;
            const FilePath packageSourceDir = FilePath::fromVariant(
                node->data(Constants::AndroidPackageSourceDir));
            FilePath sourceDir = packageSourceDir.pathAppended("src");
            if (!sourceDir.exists())
                return;
            sourceDir = sourceDir.relativeChildPath(projectDir);
            const FilePath &sdkLocation = AndroidConfigurations::currentConfig().sdkLocation();
            const QString &targetSDK = AndroidManager::buildTargetSDK(m_currentTarget);
            const FilePath androidJar = sdkLocation / QString("platforms/%2/android.jar")
                                           .arg(targetSDK);
            FilePaths libs = {androidJar};
            libs << packageSourceDir.pathAppended("libs").dirEntries({{"*.jar"}, QDir::Files});
            generateProjectFile(projectDir, qtSrc, project()->displayName());
            generateClassPathFile(projectDir, sourceDir, libs);
        }
    }
}

void JLSClient::updateTarget(Target *target)
{
    if (m_currentTarget)
        disconnect(m_currentTarget, &Target::parsingFinished, this, &JLSClient::updateProjectFiles);

    m_currentTarget = target;

    if (m_currentTarget)
        connect(m_currentTarget, &Target::parsingFinished, this, &JLSClient::updateProjectFiles);

    updateProjectFiles();
}

LanguageClient::Client *JLSSettings::createClient(LanguageClient::BaseClientInterface *interface) const
{
    return new JLSClient(interface);
}

} // namespace Internal
} // namespace Android