From bffa53c6454e6ec52cabc404cac7a7513171cee0 Mon Sep 17 00:00:00 2001 From: Mikael Hermansson Date: Fri, 8 Mar 2024 15:24:51 +0100 Subject: [PATCH] Allow LSP to process multiple messages per poll (cherry-picked from commit e2485044a1b33628e6149d4f930b6fe065743c9d) --- .../gdscript_language_protocol.cpp | 25 +++++++++++++------ .../gdscript_language_protocol.h | 2 +- .../gdscript_language_server.cpp | 10 +++++--- .../gdscript_language_server.h | 1 + 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp index 8b9e2b196..472366ad3 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.cpp +++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp @@ -106,7 +106,7 @@ Error GDScriptLanguageProtocol::LSPeer::handle_data() { Error GDScriptLanguageProtocol::LSPeer::send_data() { int sent = 0; - if (!res_queue.empty()) { + while (!res_queue.empty()) { CharString c_res = res_queue[0]; if (res_sent < c_res.size()) { Error err = connection->put_partial_data((const uint8_t *)c_res.get_data() + res_sent, c_res.size() - res_sent - 1, sent); @@ -230,7 +230,9 @@ void GDScriptLanguageProtocol::initialized(const Variant &p_params) { notify_client("gdscript/capabilities", capabilities.to_json()); } -void GDScriptLanguageProtocol::poll() { +void GDScriptLanguageProtocol::poll(int p_limit_usec) { + uint64_t target_ticks = OS::get_singleton()->get_ticks_usec() + p_limit_usec; + if (server->is_connection_available()) { on_client_connected(); } @@ -242,15 +244,22 @@ void GDScriptLanguageProtocol::poll() { on_client_disconnected(*id); id = nullptr; } else { - if (peer->connection->get_available_bytes() > 0) { + Error err = OK; + while (peer->connection->get_available_bytes() > 0) { latest_client_id = *id; - Error err = peer->handle_data(); - if (err != OK && err != ERR_BUSY) { - on_client_disconnected(*id); - id = nullptr; + err = peer->handle_data(); + if (err != OK || OS::get_singleton()->get_ticks_usec() >= target_ticks) { + break; } } - Error err = peer->send_data(); + + if (err != OK && err != ERR_BUSY) { + on_client_disconnected(*id); + id = nullptr; + continue; + } + + err = peer->send_data(); if (err != OK && err != ERR_BUSY) { on_client_disconnected(*id); id = nullptr; diff --git a/modules/gdscript/language_server/gdscript_language_protocol.h b/modules/gdscript/language_server/gdscript_language_protocol.h index 785c22f0b..4d06afd14 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.h +++ b/modules/gdscript/language_server/gdscript_language_protocol.h @@ -98,7 +98,7 @@ public: _FORCE_INLINE_ Ref get_text_document() { return text_document; } _FORCE_INLINE_ bool is_initialized() const { return _initialized; } - void poll(); + void poll(int p_limit_usec); Error start(int p_port, const IP_Address &p_bind_ip); void stop(); diff --git a/modules/gdscript/language_server/gdscript_language_server.cpp b/modules/gdscript/language_server/gdscript_language_server.cpp index 1237c9db6..d6e1a2ec5 100644 --- a/modules/gdscript/language_server/gdscript_language_server.cpp +++ b/modules/gdscript/language_server/gdscript_language_server.cpp @@ -46,6 +46,7 @@ GDScriptLanguageServer::GDScriptLanguageServer() { use_thread = false; host = "127.0.0.1"; port = 6008; + poll_limit_usec = 100000; _EDITOR_DEF("network/language_server/enabled", enabled, true); _EDITOR_DEF("network/language_server/remote_host", host); @@ -53,6 +54,7 @@ GDScriptLanguageServer::GDScriptLanguageServer() { _EDITOR_DEF("network/language_server/enable_smart_resolve", true); _EDITOR_DEF("network/language_server/show_native_symbols_in_editor", false); _EDITOR_DEF("network/language_server/use_thread", use_thread); + _EDITOR_DEF("network/language_server/poll_limit_usec", poll_limit_usec); } void GDScriptLanguageServer::_notification(int p_what) { @@ -65,7 +67,7 @@ void GDScriptLanguageServer::_notification(int p_what) { break; case NOTIFICATION_INTERNAL_PROCESS: { if (started && !use_thread) { - protocol.poll(); + protocol.poll(poll_limit_usec); } } break; case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { @@ -76,7 +78,8 @@ void GDScriptLanguageServer::_notification(int p_what) { String host = String(_EDITOR_GET("network/language_server/remote_host")); int port = (GDScriptLanguageServer::port_override > -1) ? GDScriptLanguageServer::port_override : (int)_EDITOR_GET("network/language_server/remote_port"); bool use_thread = (bool)_EDITOR_GET("network/language_server/use_thread"); - if (host != this->host || port != this->port || use_thread != this->use_thread) { + int remote_poll_limit = (int)_EDITOR_GET("network/language_server/poll_limit_usec"); + if (host != this->host || port != this->port || use_thread != this->use_thread || remote_poll_limit != poll_limit_usec) { this->stop(); this->start(); } @@ -88,7 +91,7 @@ void GDScriptLanguageServer::thread_main(void *p_userdata) { GDScriptLanguageServer *self = static_cast(p_userdata); while (self->thread_running) { // Poll 20 times per second - self->protocol.poll(); + self->protocol.poll(self->poll_limit_usec); OS::get_singleton()->delay_usec(50000); } } @@ -98,6 +101,7 @@ void GDScriptLanguageServer::start() { host = String(_EDITOR_GET("network/language_server/remote_host")); port = (GDScriptLanguageServer::port_override > -1) ? GDScriptLanguageServer::port_override : (int)_EDITOR_GET("network/language_server/remote_port"); use_thread = (bool)_EDITOR_GET("network/language_server/use_thread"); + poll_limit_usec = (int)_EDITOR_GET("network/language_server/poll_limit_usec"); if (!enabled) { return; diff --git a/modules/gdscript/language_server/gdscript_language_server.h b/modules/gdscript/language_server/gdscript_language_server.h index 8bfcc0b69..158d7f60a 100644 --- a/modules/gdscript/language_server/gdscript_language_server.h +++ b/modules/gdscript/language_server/gdscript_language_server.h @@ -48,6 +48,7 @@ class GDScriptLanguageServer : public EditorPlugin { String host; int port; bool enabled; + int poll_limit_usec; static void thread_main(void *p_userdata);