| 1 | /**************************************************************************/ |
| 2 | /* gdscript_language_server.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "gdscript_language_server.h" |
| 32 | |
| 33 | #include "core/os/os.h" |
| 34 | #include "editor/editor_log.h" |
| 35 | #include "editor/editor_node.h" |
| 36 | #include "editor/settings/editor_settings.h" |
| 37 | |
| 38 | int GDScriptLanguageServer::port_override = -1; |
| 39 | |
| 40 | GDScriptLanguageServer::GDScriptLanguageServer() { |
| 41 | // TODO: Move to editor_settings.cpp |
| 42 | _EDITOR_DEF("network/language_server/remote_host", host); |
| 43 | _EDITOR_DEF("network/language_server/remote_port", port); |
| 44 | _EDITOR_DEF("network/language_server/enable_smart_resolve", true); |
| 45 | _EDITOR_DEF("network/language_server/show_native_symbols_in_editor", false); |
| 46 | _EDITOR_DEF("network/language_server/use_thread", use_thread); |
| 47 | _EDITOR_DEF("network/language_server/poll_limit_usec", poll_limit_usec); |
| 48 | |
| 49 | set_process_internal(true); |
| 50 | } |
| 51 | |
| 52 | void GDScriptLanguageServer::_notification(int p_what) { |
| 53 | switch (p_what) { |
| 54 | case NOTIFICATION_EXIT_TREE: { |
| 55 | stop(); |
| 56 | } break; |
| 57 | |
| 58 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 59 | if (!start_attempted && EditorNode::get_singleton()->is_editor_ready()) { |
| 60 | start_attempted = true; |
| 61 | start(); |
| 62 | } |
| 63 | |
| 64 | if (started && !use_thread) { |
| 65 | protocol.poll(poll_limit_usec); |
| 66 | } |
| 67 | } break; |
| 68 | |
| 69 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
| 70 | if (!EditorSettings::get_singleton()->check_changed_settings_in_group("network/language_server")) { |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | String remote_host = String(_EDITOR_GET("network/language_server/remote_host")); |
| 75 | int remote_port = (GDScriptLanguageServer::port_override > -1) ? GDScriptLanguageServer::port_override : (int)_EDITOR_GET("network/language_server/remote_port"); |
| 76 | bool remote_use_thread = (bool)_EDITOR_GET("network/language_server/use_thread"); |
| 77 | int remote_poll_limit = (int)_EDITOR_GET("network/language_server/poll_limit_usec"); |
| 78 | if (remote_host != host || remote_port != port || remote_use_thread != use_thread || remote_poll_limit != poll_limit_usec) { |
| 79 | stop(); |
| 80 | start(); |
| 81 | } |
| 82 | } break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void GDScriptLanguageServer::thread_main(void *p_userdata) { |
| 87 | set_current_thread_safe_for_nodes(true); |
| 88 | GDScriptLanguageServer *self = static_cast<GDScriptLanguageServer *>(p_userdata); |
| 89 | while (self->thread_running) { |
| 90 | // Poll 20 times per second |
| 91 | self->protocol.poll(self->poll_limit_usec); |
| 92 | OS::get_singleton()->delay_usec(50000); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void GDScriptLanguageServer::start() { |
| 97 | host = String(_EDITOR_GET("network/language_server/remote_host")); |
| 98 | port = (GDScriptLanguageServer::port_override > -1) ? GDScriptLanguageServer::port_override : (int)_EDITOR_GET("network/language_server/remote_port"); |
| 99 | use_thread = (bool)_EDITOR_GET("network/language_server/use_thread"); |
| 100 | poll_limit_usec = (int)_EDITOR_GET("network/language_server/poll_limit_usec"); |
| 101 | if (protocol.start(port, IPAddress(host)) == OK) { |
| 102 | EditorNode::get_log()->add_message("--- GDScript language server started on port " + itos(port) + " ---", EditorLog::MSG_TYPE_EDITOR); |
| 103 | if (use_thread) { |
| 104 | thread_running = true; |
| 105 | thread.start(GDScriptLanguageServer::thread_main, this); |
| 106 | } |
| 107 | set_process_internal(!use_thread); |
| 108 | started = true; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void GDScriptLanguageServer::stop() { |
| 113 | if (use_thread) { |
| 114 | ERR_FAIL_COND(!thread.is_started()); |
| 115 | thread_running = false; |
| 116 | thread.wait_to_finish(); |
| 117 | } |
| 118 | protocol.stop(); |
| 119 | started = false; |
| 120 | EditorNode::get_log()->add_message("--- GDScript language server stopped ---", EditorLog::MSG_TYPE_EDITOR); |
| 121 | } |
| 122 | |
| 123 | void register_lsp_types() { |
| 124 | GDREGISTER_CLASS(GDScriptLanguageProtocol); |
| 125 | GDREGISTER_CLASS(GDScriptTextDocument); |
| 126 | GDREGISTER_CLASS(GDScriptWorkspace); |
| 127 | } |