diff --git a/include/lsp/lsp.h b/include/lsp/lsp.h index 3e66ed2..ac9bd37 100644 --- a/include/lsp/lsp.h +++ b/include/lsp/lsp.h @@ -2,19 +2,15 @@ #define LSP_H #include "editor/editor.h" +#include "main.h" #include "pch.h" #include "utils/utils.h" +#include +#include -#define LSP_TIMEOUT 3000 - -namespace lsp { -extern std::mutex lsp_mutex; -extern std::unordered_map> - active_lsps; -extern Queue need_opening; -extern std::unordered_set opened; -extern std::vector 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 callback; }; +namespace lsp { +extern std::mutex lsp_mutex; +extern std::unordered_map> + active_lsps; +extern Queue need_opening; +extern std::unordered_set opened; +extern std::vector new_editors; +extern Queue> response_queue; +} // namespace lsp + struct LSPInstance { const LSP *lsp_info; std::string root_dir; @@ -70,7 +76,6 @@ struct LSPInstance { Queue inbox; Queue outbox; std::unordered_map> pending; - std::vector> lsp_response_queue; std::vector 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 = diff --git a/include/syntax/decl.h b/include/syntax/decl.h index 71a8f97..8744882 100644 --- a/include/syntax/decl.h +++ b/include/syntax/decl.h @@ -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}; diff --git a/include/syntax/line_map.h b/include/syntax/line_map.h index 4e4f15b..6f2512b 100644 --- a/include/syntax/line_map.h +++ b/include/syntax/line_map.h @@ -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 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 resolve_line(uint32_t line) { uint32_t current_line = line; for (int64_t v = current_version; v >= 0; v--) { diff --git a/include/utils/utils.h b/include/utils/utils.h index bb7e574..bf811cf 100644 --- a/include/utils/utils.h +++ b/include/utils/utils.h @@ -6,18 +6,19 @@ template struct Queue { void push(T val) { std::lock_guard lock(m); - q.push(val); + q.push(std::move(val)); } std::optional front() { + std::lock_guard lock(m); if (q.empty()) return std::nullopt; - return q.front(); + return std::move(q.front()); } bool pop(T &val) { std::lock_guard lock(m); if (q.empty()) return false; - val = q.front(); + val = std::move(q.front()); q.pop(); return true; } diff --git a/samples/ruby.rb b/samples/ruby.rb index 9f86f8c..1c3184e 100644 --- a/samples/ruby.rb +++ b/samples/ruby.rb @@ -339,6 +339,5 @@ puts 'Ruby syntax highlighting test complete.' __END__ - Anything here should be ignored >><< {{{}}}[[[]]](((000))) diff --git a/src/lsp/worker.cc b/src/lsp/worker.cc index 9a2281f..98aabdc 100644 --- a/src/lsp/worker.cc +++ b/src/lsp/worker.cc @@ -6,6 +6,7 @@ std::unordered_map> active_lsps; std::unordered_set opened; Queue need_opening; std::vector new_editors; +Queue> response_queue; } // namespace lsp void lsp_worker() { diff --git a/src/main.cc b/src/main.cc index 18b6c0b..9b0bbb3 100644 --- a/src/main.cc +++ b/src/main.cc @@ -44,34 +44,41 @@ int main(int argc, char *argv[]) { std::thread lsp_thread(background_lsp); while (running) { - KeyEvent event = throttle(1ms, read_key); - if (event.key_type != KEY_NONE) { - if (event.key_type == KEY_CHAR && event.len == 1 && - event.c[0] == CTRL('q')) { - free(event.c); - running = false; - break; - } - if (mode != RUNNER) { - if (event.key_type == KEY_MOUSE) { - handle_click(event); - } else { - layout::focused_window->handle_event(event); + uint8_t consumed = 0; + KeyEvent event; + while (++consumed < 32 && (event = read_key()).key_type != KEY_NONE) { + if (event.key_type != KEY_NONE) { + if (event.key_type == KEY_CHAR && event.len == 1 && + event.c[0] == CTRL('q')) { + free(event.c); + running = false; + goto quit; } - } else { - ui::bar.handle_event(event); + if (mode != RUNNER) { + if (event.key_type == KEY_MOUSE) { + handle_click(event); + } else { + layout::focused_window->handle_event(event); + } + } else { + ui::bar.handle_event(event); + } + if ((event.key_type == KEY_CHAR || event.key_type == KEY_PASTE) && + event.c) + free(event.c); } - if ((event.key_type == KEY_CHAR || event.key_type == KEY_PASTE) && - event.c) - free(event.c); } - for (auto &lsp_inst : lsp::active_lsps) - lsp_inst.second->callbacks(); + std::unique_ptr msg; + while (lsp::response_queue.pop(msg)) { + msg->callback(*msg); + }; layout::focused_window->work(); - throttle(4ms, render); - throttle(4ms, io_render); + render(); + throttle(8ms, io_render); } +quit: + if (lsp_thread.joinable()) lsp_thread.join(); diff --git a/src/syntax/parser.cc b/src/syntax/parser.cc index cdd33a0..7c299e9 100644 --- a/src/syntax/parser.cc +++ b/src/syntax/parser.cc @@ -46,20 +46,26 @@ void Parser::work() { return; std::vector batch; uint32_t line_count = editor->root->line_count + 1; - int64_t start = MIN(scroll_max + 10, line_count - 1); - int64_t end = MAX(0, (int64_t)scroll_max - MAX_LINES_LOOKBEHIND); - for (int64_t i = start; i >= end; --i) { - auto l_opt = line_map.at(i); - if (!l_opt || (l_opt && !l_opt->out_state)) + uint32_t min_line = + scroll_max > MAX_LINES_LOOKAROUND ? scroll_max - MAX_LINES_LOOKAROUND : 0; + uint32_t max_line = MIN(scroll_max + MAX_LINES_LOOKAROUND, line_count - 1); + bool sequential = false; + for (uint32_t i = min_line; i <= max_line; ++i) { + LineData *ld = line_map.at(i); + if ((!ld || !ld->in_state || !ld->out_state) && !sequential) { batch.push_back(i); + sequential = true; + continue; + } + sequential = false; } for (uint32_t c_line : batch) { if (!running.load(std::memory_order_relaxed)) break; - uint32_t min_line = scroll_max > MAX_LINES_LOOKBEHIND - ? scroll_max - MAX_LINES_LOOKBEHIND + uint32_t min_line = scroll_max > MAX_LINES_LOOKAROUND + ? scroll_max - MAX_LINES_LOOKAROUND : 0; - uint32_t max_line = scroll_max + 10; + uint32_t max_line = scroll_max + MAX_LINES_LOOKAROUND; if (c_line < min_line || c_line > max_line) continue; uint32_t scroll_snapshot = scroll_max; @@ -78,8 +84,12 @@ void Parser::work() { break; if (scroll_snapshot != scroll_max) break; - if (cur_line < min_line || cur_line > max_line) + if (cur_line < min_line || cur_line > max_line) { + LineData *line_data = line_map.at(cur_line); + if (line_data) + line_data->out_state = nullptr; break; + } uint32_t len; char *line = next_line(it, &len); if (!line)