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 =
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include "pch.h"
|
||||
#include "syntax/trie.h"
|
||||
|
||||
#define MAX_LINES_LOOKBEHIND 80
|
||||
#define MAX_LINES_LOOKAROUND 512
|
||||
|
||||
struct Highlight {
|
||||
uint32_t fg{0xFFFFFF};
|
||||
|
||||
@@ -59,15 +59,8 @@ struct LineMap {
|
||||
}
|
||||
|
||||
void apply_edit(uint32_t start, int64_t delta) {
|
||||
if (delta < 0) {
|
||||
int64_t count = -delta;
|
||||
for (int64_t i = 0; i < count; i++) {
|
||||
auto key = resolve_line(start + i);
|
||||
if (!key)
|
||||
continue;
|
||||
lines.erase(*key);
|
||||
}
|
||||
}
|
||||
if (delta < 0)
|
||||
batch_remove(start, -delta);
|
||||
current_version++;
|
||||
edit_log.push_back({start, delta});
|
||||
}
|
||||
@@ -79,6 +72,45 @@ private:
|
||||
|
||||
uint32_t current_version;
|
||||
|
||||
void batch_remove(uint32_t line, uint32_t count) {
|
||||
std::vector<uint32_t> lines_t;
|
||||
lines_t.reserve(count);
|
||||
for (uint32_t i = 0; i < count; i++)
|
||||
lines_t.push_back(line + i);
|
||||
for (int64_t v = current_version; v >= 0; v--) {
|
||||
for (auto &l : lines_t) {
|
||||
if (l == UINT32_MAX)
|
||||
continue;
|
||||
LineKey key = {l, (uint32_t)v};
|
||||
if (lines.find(key) != lines.end()) {
|
||||
lines.erase(key);
|
||||
l = UINT32_MAX;
|
||||
}
|
||||
}
|
||||
bool all_removed = true;
|
||||
const auto &edit = edit_log[v];
|
||||
for (auto &l : lines_t) {
|
||||
if (l == UINT32_MAX)
|
||||
continue;
|
||||
all_removed = false;
|
||||
if (edit.delta > 0) {
|
||||
if (l >= edit.start_line) {
|
||||
if (l < edit.start_line + edit.delta)
|
||||
l = UINT32_MAX;
|
||||
else
|
||||
l -= edit.delta;
|
||||
}
|
||||
} else {
|
||||
if (l >= edit.start_line)
|
||||
l -= edit.delta;
|
||||
}
|
||||
}
|
||||
if (all_removed)
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
std::optional<LineKey> resolve_line(uint32_t line) {
|
||||
uint32_t current_line = line;
|
||||
for (int64_t v = current_version; v >= 0; v--) {
|
||||
|
||||
@@ -6,18 +6,19 @@
|
||||
template <typename T> struct Queue {
|
||||
void push(T val) {
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
q.push(val);
|
||||
q.push(std::move(val));
|
||||
}
|
||||
std::optional<T> front() {
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if (q.empty())
|
||||
return std::nullopt;
|
||||
return q.front();
|
||||
return std::move(q.front());
|
||||
}
|
||||
bool pop(T &val) {
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if (q.empty())
|
||||
return false;
|
||||
val = q.front();
|
||||
val = std::move(q.front());
|
||||
q.pop();
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user