Merge pull request #91003 from NicolasPetton/3.x

[3.x] Allow LSP to process multiple messages per poll
This commit is contained in:
Rémi Verschelde 2024-04-25 12:51:09 +02:00
commit 8304a7d4cb
No known key found for this signature in database
GPG key ID: C3336907360768E1
4 changed files with 25 additions and 12 deletions

View file

@ -103,7 +103,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);
@ -227,7 +227,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();
}
@ -239,15 +241,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();
err = peer->handle_data();
if (err != OK || OS::get_singleton()->get_ticks_usec() >= target_ticks) {
break;
}
}
if (err != OK && err != ERR_BUSY) {
on_client_disconnected(*id);
id = nullptr;
continue;
}
}
Error err = peer->send_data();
err = peer->send_data();
if (err != OK && err != ERR_BUSY) {
on_client_disconnected(*id);
id = nullptr;

View file

@ -97,7 +97,7 @@ public:
_FORCE_INLINE_ Ref<GDScriptTextDocument> 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();

View file

@ -49,6 +49,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) {
@ -61,14 +62,15 @@ 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: {
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();
}
@ -80,7 +82,7 @@ void GDScriptLanguageServer::thread_main(void *p_userdata) {
GDScriptLanguageServer *self = static_cast<GDScriptLanguageServer *>(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);
}
}
@ -89,6 +91,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 (protocol.start(port, IP_Address(host)) == OK) {
EditorNode::get_log()->add_message("--- GDScript language server started on port " + itos(port) + " ---", EditorLog::MSG_TYPE_EDITOR);
if (use_thread) {

View file

@ -46,6 +46,7 @@ class GDScriptLanguageServer : public EditorPlugin {
bool use_thread;
String host;
int port;
int poll_limit_usec = 100000;
static void thread_main(void *p_userdata);
private: