Allow enable/disable threading for LSP server

Restart LSP server when configurations change without restart the editor
This commit is contained in:
geequlim 2020-01-11 23:50:32 +08:00
parent 02cd144222
commit d901d5993b
2 changed files with 42 additions and 13 deletions

View file

@ -36,10 +36,15 @@
GDScriptLanguageServer::GDScriptLanguageServer() { GDScriptLanguageServer::GDScriptLanguageServer() {
thread = NULL; thread = NULL;
thread_exit = false; thread_running = false;
_EDITOR_DEF("network/language_server/remote_port", 6008); started = false;
use_thread = false;
port = 6008;
_EDITOR_DEF("network/language_server/remote_port", port);
_EDITOR_DEF("network/language_server/enable_smart_resolve", true); _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/show_native_symbols_in_editor", false);
_EDITOR_DEF("network/language_server/use_thread", use_thread);
} }
void GDScriptLanguageServer::_notification(int p_what) { void GDScriptLanguageServer::_notification(int p_what) {
@ -51,12 +56,25 @@ void GDScriptLanguageServer::_notification(int p_what) {
case NOTIFICATION_EXIT_TREE: case NOTIFICATION_EXIT_TREE:
stop(); stop();
break; break;
case NOTIFICATION_INTERNAL_PROCESS: {
if (started && !use_thread) {
protocol.poll();
}
} break;
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
int port = (int)_EDITOR_GET("network/language_server/remote_port");
bool use_thread = (bool)_EDITOR_GET("network/language_server/use_thread");
if (port != this->port || use_thread != this->use_thread) {
this->stop();
this->start();
}
} break;
} }
} }
void GDScriptLanguageServer::thread_main(void *p_userdata) { void GDScriptLanguageServer::thread_main(void *p_userdata) {
GDScriptLanguageServer *self = static_cast<GDScriptLanguageServer *>(p_userdata); GDScriptLanguageServer *self = static_cast<GDScriptLanguageServer *>(p_userdata);
while (!self->thread_exit) { while (self->thread_running) {
// Poll 20 times per second // Poll 20 times per second
self->protocol.poll(); self->protocol.poll();
OS::get_singleton()->delay_usec(50000); OS::get_singleton()->delay_usec(50000);
@ -64,22 +82,30 @@ void GDScriptLanguageServer::thread_main(void *p_userdata) {
} }
void GDScriptLanguageServer::start() { void GDScriptLanguageServer::start() {
int port = (int)_EDITOR_GET("network/language_server/remote_port"); port = (int)_EDITOR_GET("network/language_server/remote_port");
use_thread = (bool)_EDITOR_GET("network/language_server/use_thread");
if (protocol.start(port) == OK) { if (protocol.start(port) == OK) {
EditorNode::get_log()->add_message("--- GDScript language server started ---", EditorLog::MSG_TYPE_EDITOR); EditorNode::get_log()->add_message("--- GDScript language server started ---", EditorLog::MSG_TYPE_EDITOR);
ERR_FAIL_COND(thread != NULL || thread_exit); if (use_thread) {
thread_exit = false; ERR_FAIL_COND(thread != NULL);
thread = Thread::create(GDScriptLanguageServer::thread_main, this); thread_running = true;
thread = Thread::create(GDScriptLanguageServer::thread_main, this);
}
set_process_internal(!use_thread);
started = true;
} }
} }
void GDScriptLanguageServer::stop() { void GDScriptLanguageServer::stop() {
ERR_FAIL_COND(NULL == thread || thread_exit); if (use_thread) {
thread_exit = true; ERR_FAIL_COND(NULL == thread);
Thread::wait_to_finish(thread); thread_running = false;
memdelete(thread); Thread::wait_to_finish(thread);
thread = NULL; memdelete(thread);
thread = NULL;
}
protocol.stop(); protocol.stop();
started = false;
EditorNode::get_log()->add_message("--- GDScript language server stopped ---", EditorLog::MSG_TYPE_EDITOR); EditorNode::get_log()->add_message("--- GDScript language server stopped ---", EditorLog::MSG_TYPE_EDITOR);
} }

View file

@ -41,7 +41,10 @@ class GDScriptLanguageServer : public EditorPlugin {
GDScriptLanguageProtocol protocol; GDScriptLanguageProtocol protocol;
Thread *thread; Thread *thread;
bool thread_exit; bool thread_running;
bool started;
bool use_thread;
int port;
static void thread_main(void *p_userdata); static void thread_main(void *p_userdata);
private: private: