| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "lspclientkeeper.h" |
| 6 | |
| 7 | #include <QDir> |
| 8 | #include <QCoreApplication> |
| 9 | |
| 10 | //#define AUTO_LANUCH_LSP_SERVER |
| 11 | |
| 12 | #define LANGUAGE_ADAPTER_NAME "languageadapter" |
| 13 | #define LANGUAGE_ADAPTER_PATH CustomPaths::global(CustomPaths::Tools) \ |
| 14 | + QDir::separator() + LANGUAGE_ADAPTER_NAME |
| 15 | |
| 16 | LSPClientKeeper::LSPClientKeeper() |
| 17 | { |
| 18 | #ifdef AUTO_LANUCH_LSP_SERVER |
| 19 | while (ProcessUtil::portOverhead(defPort)) { |
| 20 | defPort ++; |
| 21 | } |
| 22 | QString currDefPort = QString::number(defPort); |
| 23 | QString currPid = QString::number(qApp->applicationPid()); |
| 24 | lspServerProc = new QProcess; |
| 25 | lspServerProc->setProgram(LANGUAGE_ADAPTER_PATH); |
| 26 | lspServerProc->setArguments({"--port" , currDefPort, |
| 27 | "--parentPid" , currPid}); |
| 28 | lspServerProc->setReadChannelMode(QProcess::SeparateChannels); |
| 29 | lspServerProc->start(); |
| 30 | while (!lspServerProc->canReadLine()) { |
| 31 | lspServerProc->waitForReadyRead(); |
| 32 | QByteArray lineData = lspServerProc->readLine(); |
| 33 | qInfo() << "[lsp server]:" << lineData; |
| 34 | if ("Server started successfully\n" == lineData) { |
| 35 | QObject::connect(lspServerProc, &QProcess::readyRead, [=](){ |
| 36 | qInfo() << qPrintable(lspServerProc->readAll()); |
| 37 | }); |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | qInfo() << lspServerProc << lspServerProc->state(); |
| 42 | if (lspServerProc->state() != QProcess::ProcessState::Running) |
| 43 | qInfo() << lspServerProc->errorString() << LANGUAGE_ADAPTER_PATH; |
| 44 | #endif |
| 45 | } |
| 46 | |
| 47 | LSPClientKeeper::~LSPClientKeeper() |
| 48 | { |
| 49 | if (client) |
| 50 | delete client; |
| 51 | } |
| 52 | |
| 53 | LSPClientKeeper *LSPClientKeeper::instance() |
| 54 | { |
| 55 | static LSPClientKeeper ins; |
| 56 | return &ins; |
| 57 | } |
| 58 | |
| 59 | newlsp::Client *LSPClientKeeper::get(const newlsp::ProjectKey &key) |
| 60 | { |
| 61 | if (!key.isValid()) |
| 62 | return nullptr; |
| 63 | |
| 64 | if (client) { |
| 65 | qApp->metaObject()->invokeMethod(client, "selectLspServer" , Q_ARG(const newlsp::ProjectKey &, key)); |
| 66 | } else { |
| 67 | client = new newlsp::Client(); |
| 68 | qApp->metaObject()->invokeMethod(client, "selectLspServer" , Q_ARG(const newlsp::ProjectKey &, key)); |
| 69 | } |
| 70 | |
| 71 | if (!projectKeys.contains(key)) { |
| 72 | QString complieDB_Path = QString::fromStdString(key.workspace) + QDir::separator() + ".unioncode" ; |
| 73 | qApp->metaObject()->invokeMethod(client, "initRequest" , Q_ARG(const QString &, complieDB_Path)); |
| 74 | projectKeys.append(key); |
| 75 | } |
| 76 | |
| 77 | return client; |
| 78 | } |
| 79 | |