|
| 1 | +#include "mainwindow.hpp" |
| 2 | +#include <QPlainTextEdit> |
| 3 | +#include <QVBoxLayout> |
| 4 | +#include <QTemporaryFile> |
| 5 | +#include <QTimer> |
| 6 | +Mainwindow::Mainwindow(QWidget *parent) : QMainWindow(parent) |
| 7 | +{ |
| 8 | + auto out = new QVBoxLayout(); |
| 9 | + |
| 10 | + code = new QPlainTextEdit(); |
| 11 | + code->setReadOnly(false); |
| 12 | + |
| 13 | + info = new QPlainTextEdit(); |
| 14 | + info->setReadOnly(true); |
| 15 | + |
| 16 | + out->addWidget(code); |
| 17 | + out->addWidget(info); |
| 18 | + |
| 19 | + timer.setSingleShot(true); |
| 20 | + timer.setInterval(2000); |
| 21 | + timer.start(); |
| 22 | + |
| 23 | + auto widget = new QWidget(); |
| 24 | + widget->setLayout(out); |
| 25 | + setCentralWidget(widget); |
| 26 | + |
| 27 | + lsp = new LSPClient("clangd.exe", {}); |
| 28 | + setConnections(); |
| 29 | + file.open(); |
| 30 | + |
| 31 | + file.rename("C://Users/Ashar/AppData/Local/Temp/sol.cpp"); |
| 32 | + info->appendPlainText(file.fileName()); |
| 33 | + lsp->initialize(); |
| 34 | + lsp->didOpen("file://" + file.fileName().toStdString(), "", "cpp"); |
| 35 | + |
| 36 | +} |
| 37 | + |
| 38 | +Mainwindow::~Mainwindow() |
| 39 | +{ |
| 40 | + delete code; |
| 41 | + delete info; |
| 42 | + lsp->didClose("file://"+file.fileName().toStdString()); |
| 43 | + lsp->shutdown(); |
| 44 | + file.remove(); |
| 45 | + delete lsp; |
| 46 | +} |
| 47 | + |
| 48 | +void Mainwindow::setConnections() |
| 49 | +{ |
| 50 | + connect(code, &QPlainTextEdit::textChanged, this, &Mainwindow::textChanged); |
| 51 | + |
| 52 | + connect(lsp, &LSPClient::onError, this, &Mainwindow::OnError); |
| 53 | + connect(lsp, &LSPClient::onNotify, this, &Mainwindow::OnNotify); |
| 54 | + connect(lsp, &LSPClient::onRequest, this, &Mainwindow::OnRequest); |
| 55 | + connect(lsp, &LSPClient::onResponse, this, &Mainwindow::OnResponse); |
| 56 | + connect(&timer, &QTimer::timeout, this, &Mainwindow::requestDiagonistics); |
| 57 | + connect(lsp, &LSPClient::onServerError, this, &Mainwindow::OnServerError); |
| 58 | + connect(lsp, &LSPClient::onServerFinished, this, &Mainwindow::OnServerFinished); |
| 59 | +} |
| 60 | + |
| 61 | +QString Mainwindow::jsonObjectToString(QJsonObject& obj) |
| 62 | +{ |
| 63 | + return QJsonDocument::fromVariant(obj.toVariantMap()).toJson(); |
| 64 | +} |
| 65 | + |
| 66 | +//************ SLOTS ************** |
| 67 | + |
| 68 | +void Mainwindow::textChanged() |
| 69 | +{ |
| 70 | + timer.start(2000); |
| 71 | + timer.setSingleShot(true); |
| 72 | + |
| 73 | +// Trigger to do something, |
| 74 | +// file.resize(0); |
| 75 | +// file.write(code->toPlainText().toLocal8Bit()); |
| 76 | +// std::vector<TextDocumentContentChangeEvent> ch; |
| 77 | +// TextDocumentContentChangeEvent ev; |
| 78 | +// ev.text = code->toPlainText().toStdString(); |
| 79 | +// ch.push_back(ev); |
| 80 | +// lsp->didChange("file://" + file.fileName().toStdString(), ch, true); |
| 81 | +} |
| 82 | + |
| 83 | +void Mainwindow::requestDiagonistics() |
| 84 | +{ |
| 85 | + //file.resize(0); |
| 86 | + //file.write(code->toPlainText().toLocal8Bit()); |
| 87 | + std::vector<TextDocumentContentChangeEvent> ch; |
| 88 | + TextDocumentContentChangeEvent ev; |
| 89 | + ev.text = code->toPlainText().toStdString(); |
| 90 | + ch.push_back(ev); |
| 91 | + lsp->didChange("file://" + file.fileName().toStdString(), ch, true); |
| 92 | +} |
| 93 | + |
| 94 | +void Mainwindow::OnError(QJsonObject id, QJsonObject err) |
| 95 | +{ |
| 96 | + info->appendPlainText("Error ocurred[" + jsonObjectToString(id) + "]: " + jsonObjectToString(err)); |
| 97 | +} |
| 98 | + |
| 99 | +void Mainwindow::OnNotify(QString method, QJsonObject param) |
| 100 | +{ |
| 101 | + info->appendPlainText("Notification[" + method + "]: " + jsonObjectToString(param)); |
| 102 | + // diagonistic notifiacation arrives here! |
| 103 | +} |
| 104 | + |
| 105 | +void Mainwindow::OnRequest(QString method, QJsonObject param, QJsonObject id) |
| 106 | +{ |
| 107 | + info->appendPlainText("Request[" + method + "]: " + jsonObjectToString(param) + "id: " + jsonObjectToString(id)); |
| 108 | +} |
| 109 | + |
| 110 | +void Mainwindow::OnResponse(QJsonObject id, QJsonObject response) |
| 111 | +{ |
| 112 | + info->appendPlainText("Response[" + jsonObjectToString(id) + "]: " + jsonObjectToString(response)); |
| 113 | +} |
| 114 | + |
| 115 | +void Mainwindow::OnServerError(QProcess::ProcessError err) |
| 116 | +{ |
| 117 | + info->appendPlainText("LSP failed to fork process"); |
| 118 | +} |
| 119 | + |
| 120 | +void Mainwindow::OnServerFinished(int exitCode, QProcess::ExitStatus status) |
| 121 | +{ |
| 122 | + info->appendPlainText("LSP finished"); |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
0 commit comments