Cleanup / optimizations
This commit is contained in:
@@ -2,19 +2,15 @@
|
||||
#define LSP_H
|
||||
|
||||
#include "editor/editor.h"
|
||||
#include "main.h"
|
||||
#include "pch.h"
|
||||
#include "utils/utils.h"
|
||||
#include <cstdint>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define LSP_TIMEOUT 3000
|
||||
|
||||
namespace lsp {
|
||||
extern std::mutex lsp_mutex;
|
||||
extern std::unordered_map<std::string, std::unique_ptr<LSPInstance>>
|
||||
active_lsps;
|
||||
extern Queue<std::string> need_opening;
|
||||
extern std::unordered_set<std::string> opened;
|
||||
extern std::vector<Editor *> new_editors;
|
||||
} // namespace lsp
|
||||
#define LSP_TIMEOUT_START 3000
|
||||
#define LSP_TIMEOUT_QUIT_NORMAL 300
|
||||
#define LSP_TIMEOUT_QUIT_FORCE 80
|
||||
|
||||
void lsp_worker();
|
||||
|
||||
@@ -48,6 +44,16 @@ struct LSPMessage {
|
||||
std::function<void(const LSPMessage &)> callback;
|
||||
};
|
||||
|
||||
namespace lsp {
|
||||
extern std::mutex lsp_mutex;
|
||||
extern std::unordered_map<std::string, std::unique_ptr<LSPInstance>>
|
||||
active_lsps;
|
||||
extern Queue<std::string> need_opening;
|
||||
extern std::unordered_set<std::string> opened;
|
||||
extern std::vector<Editor *> new_editors;
|
||||
extern Queue<std::unique_ptr<LSPMessage>> response_queue;
|
||||
} // namespace lsp
|
||||
|
||||
struct LSPInstance {
|
||||
const LSP *lsp_info;
|
||||
std::string root_dir;
|
||||
@@ -70,7 +76,6 @@ struct LSPInstance {
|
||||
Queue<json> inbox;
|
||||
Queue<json> outbox;
|
||||
std::unordered_map<uint32_t, std::unique_ptr<LSPMessage>> pending;
|
||||
std::vector<std::unique_ptr<LSPMessage>> lsp_response_queue;
|
||||
std::vector<Editor *> editors;
|
||||
|
||||
LSPInstance(std::string lsp_id) {
|
||||
@@ -89,7 +94,15 @@ struct LSPInstance {
|
||||
{"capabilities", client_capabilities}}}};
|
||||
send_raw(initialize_message);
|
||||
pollfd pfd{stdout_fd, POLLIN, 0};
|
||||
poll(&pfd, 1, LSP_TIMEOUT);
|
||||
uint32_t waited = 0;
|
||||
while (waited < LSP_TIMEOUT_START) {
|
||||
poll(&pfd, 1, 50);
|
||||
if (pfd.revents & POLLIN)
|
||||
break;
|
||||
if (!running)
|
||||
return;
|
||||
waited += 50;
|
||||
}
|
||||
if (!(pfd.revents & POLLIN)) {
|
||||
exited = true;
|
||||
return;
|
||||
@@ -179,34 +192,39 @@ struct LSPInstance {
|
||||
send_raw(initialized_message);
|
||||
}
|
||||
~LSPInstance() {
|
||||
uint32_t timeout =
|
||||
running ? LSP_TIMEOUT_QUIT_NORMAL : LSP_TIMEOUT_QUIT_FORCE;
|
||||
for (auto &ed : editors)
|
||||
ed->lsp.store(nullptr);
|
||||
initialized = false;
|
||||
exited = true;
|
||||
if (pid == -1)
|
||||
return;
|
||||
json shutdown = {{"id", ++last_id}, {"method", "shutdown"}};
|
||||
json shutdown = {
|
||||
{"jsonrpc", "2.0"}, {"id", ++last_id}, {"method", "shutdown"}};
|
||||
send_raw(shutdown);
|
||||
pollfd pfd{stdout_fd, POLLIN, 0};
|
||||
poll(&pfd, 1, 500);
|
||||
json exit_msg = {{"method", "exit"}};
|
||||
send_raw(exit_msg);
|
||||
int waited = 0;
|
||||
while (waited < 100) {
|
||||
int status;
|
||||
pid_t res = waitpid(pid, &status, WNOHANG);
|
||||
if (res == pid)
|
||||
break;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
waited += 10;
|
||||
poll(&pfd, 1, timeout);
|
||||
if (pfd.revents & POLLIN) {
|
||||
json exit_msg = {{"jsonrpc", "2.0"}, {"method", "exit"}};
|
||||
send_raw(exit_msg);
|
||||
int waited = 0;
|
||||
while (waited < timeout) {
|
||||
int status;
|
||||
pid_t res = waitpid(pid, &status, WNOHANG);
|
||||
if (res == pid)
|
||||
break;
|
||||
std::this_thread::sleep_for(10ms);
|
||||
waited += 10;
|
||||
}
|
||||
}
|
||||
close(stdin_fd);
|
||||
close(stdout_fd);
|
||||
if (kill(pid, 0) == 0) {
|
||||
kill(pid, SIGKILL);
|
||||
waitpid(pid, nullptr, 0);
|
||||
}
|
||||
pid = -1;
|
||||
close(stdin_fd);
|
||||
close(stdout_fd);
|
||||
}
|
||||
bool init_process() {
|
||||
int in_pipe[2];
|
||||
@@ -312,7 +330,7 @@ struct LSPInstance {
|
||||
if (it != pending.end()) {
|
||||
if (it->second->editor) {
|
||||
it->second->message = *msg;
|
||||
lsp_response_queue.push_back(std::move(it->second));
|
||||
lsp::response_queue.push(std::move(it->second));
|
||||
} else {
|
||||
auto message = *std::move(it->second);
|
||||
message.message = *msg;
|
||||
@@ -335,7 +353,7 @@ struct LSPInstance {
|
||||
response->message = *msg;
|
||||
response->callback = editor_handle_wrapper;
|
||||
if (ed)
|
||||
lsp_response_queue.push_back(std::move(response));
|
||||
lsp::response_queue.push(std::move(response));
|
||||
else
|
||||
lsp_handle(*msg);
|
||||
}
|
||||
@@ -344,11 +362,6 @@ struct LSPInstance {
|
||||
inline static void editor_handle_wrapper(const LSPMessage &message) {
|
||||
message.editor->lsp_handle(message.message);
|
||||
}
|
||||
void callbacks() {
|
||||
for (auto &message : lsp_response_queue)
|
||||
message->callback(*message);
|
||||
lsp_response_queue.clear();
|
||||
}
|
||||
inline void send_raw(const json &msg) {
|
||||
std::string payload = msg.dump();
|
||||
std::string header =
|
||||
|
||||
Reference in New Issue
Block a user