Skip to content

Commit 7f3fb8e

Browse files
committed
This commit adds example for on-fly diagonistics
It cab be used in the same way to check and parse the results.
1 parent 880c98d commit 7f3fb8e

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ endif(CMAKE_COMPILER_IS_GNUCXX)
4040
target_link_libraries(LSPClient
4141
Qt5::Core
4242
)
43+

example/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(LSPClientExample LANGUAGES CXX VERSION 1.0.0)
3+
4+
set(CMAKE_AUTOUIC ON)
5+
set(CMAKE_AUTOMOC ON)
6+
7+
set(CMAKE_CXX_STANDARD 14)
8+
9+
find_package(Qt5Core CONFIG REQUIRED)
10+
find_package(Qt5Widgets CONFIG REQUIRED)
11+
add_executable(LSPClientExample main.cpp mainwindow.cpp mainwindow.hpp)
12+
13+
target_include_directories(LSPClientExample PUBLIC
14+
../include
15+
mainwindow.hpp
16+
)
17+
18+
target_link_libraries(LSPClientExample
19+
Qt5::Core
20+
Qt5::Widgets
21+
LSPClient
22+
)
23+

example/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <QTextEdit>
2+
#include <QApplication>
3+
#include "mainwindow.hpp"
4+
5+
int main(int argc, char** argv)
6+
{
7+
QApplication app(argc, argv);
8+
Mainwindow w;
9+
w.show();
10+
return app.exec();
11+
}

example/mainwindow.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+

example/mainwindow.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef MAINWINDOW_HPP
2+
#define MAINWINDOW_HPP
3+
4+
#include <QMainWindow>
5+
#include <LSPClient.hpp>
6+
#include <QPlainTextEdit>
7+
#include <QTemporaryFile>
8+
#include <QTimer>
9+
class Mainwindow : public QMainWindow
10+
{
11+
Q_OBJECT
12+
public:
13+
explicit Mainwindow(QWidget *parent = nullptr);
14+
~Mainwindow() override;
15+
16+
public slots:
17+
void OnNotify(QString method, QJsonObject param);
18+
void OnResponse(QJsonObject id, QJsonObject response);
19+
void OnRequest(QString method, QJsonObject param, QJsonObject id);
20+
void OnError(QJsonObject id, QJsonObject error);
21+
void OnServerError(QProcess::ProcessError error);
22+
void OnServerFinished(int exitCode, QProcess::ExitStatus status);
23+
24+
void textChanged();
25+
void requestDiagonistics();
26+
27+
28+
private:
29+
QPlainTextEdit *code;
30+
QPlainTextEdit *info;
31+
LSPClient *lsp;
32+
33+
QTemporaryFile file;
34+
QTimer timer;
35+
36+
void setConnections();
37+
QString jsonObjectToString(QJsonObject&);
38+
};
39+
40+
#endif // MAINWINDOW_HPP

0 commit comments

Comments
 (0)