gdscript_language_server.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include "gdscript_language_server.h"
  31. #include "core/io/file_access.h"
  32. #include "core/os/os.h"
  33. #include "editor/editor_log.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_settings.h"
  36. int GDScriptLanguageServer::port_override = -1;
  37. GDScriptLanguageServer::GDScriptLanguageServer() {
  38. _EDITOR_DEF("network/language_server/remote_host", host);
  39. _EDITOR_DEF("network/language_server/remote_port", port);
  40. _EDITOR_DEF("network/language_server/enable_smart_resolve", true);
  41. _EDITOR_DEF("network/language_server/show_native_symbols_in_editor", false);
  42. _EDITOR_DEF("network/language_server/use_thread", use_thread);
  43. _EDITOR_DEF("network/language_server/poll_limit_usec", poll_limit_usec);
  44. }
  45. void GDScriptLanguageServer::_notification(int p_what) {
  46. switch (p_what) {
  47. case NOTIFICATION_ENTER_TREE: {
  48. start();
  49. } break;
  50. case NOTIFICATION_EXIT_TREE: {
  51. stop();
  52. } break;
  53. case NOTIFICATION_INTERNAL_PROCESS: {
  54. if (started && !use_thread) {
  55. protocol.poll(poll_limit_usec);
  56. }
  57. } break;
  58. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  59. String remote_host = String(_EDITOR_GET("network/language_server/remote_host"));
  60. int remote_port = (GDScriptLanguageServer::port_override > -1) ? GDScriptLanguageServer::port_override : (int)_EDITOR_GET("network/language_server/remote_port");
  61. bool remote_use_thread = (bool)_EDITOR_GET("network/language_server/use_thread");
  62. int remote_poll_limit = (int)_EDITOR_GET("network/language_server/poll_limit_usec");
  63. if (remote_host != host || remote_port != port || remote_use_thread != use_thread || remote_poll_limit != poll_limit_usec) {
  64. stop();
  65. start();
  66. }
  67. } break;
  68. }
  69. }
  70. void GDScriptLanguageServer::thread_main(void *p_userdata) {
  71. set_current_thread_safe_for_nodes(true);
  72. GDScriptLanguageServer *self = static_cast<GDScriptLanguageServer *>(p_userdata);
  73. while (self->thread_running) {
  74. // Poll 20 times per second
  75. self->protocol.poll(self->poll_limit_usec);
  76. OS::get_singleton()->delay_usec(50000);
  77. }
  78. }
  79. void GDScriptLanguageServer::start() {
  80. host = String(_EDITOR_GET("network/language_server/remote_host"));
  81. port = (GDScriptLanguageServer::port_override > -1) ? GDScriptLanguageServer::port_override : (int)_EDITOR_GET("network/language_server/remote_port");
  82. use_thread = (bool)_EDITOR_GET("network/language_server/use_thread");
  83. poll_limit_usec = (int)_EDITOR_GET("network/language_server/poll_limit_usec");
  84. if (protocol.start(port, IPAddress(host)) == OK) {
  85. EditorNode::get_log()->add_message("--- GDScript language server started on port " + itos(port) + " ---", EditorLog::MSG_TYPE_EDITOR);
  86. if (use_thread) {
  87. thread_running = true;
  88. thread.start(GDScriptLanguageServer::thread_main, this);
  89. }
  90. set_process_internal(!use_thread);
  91. started = true;
  92. }
  93. }
  94. void GDScriptLanguageServer::stop() {
  95. if (use_thread) {
  96. ERR_FAIL_COND(!thread.is_started());
  97. thread_running = false;
  98. thread.wait_to_finish();
  99. }
  100. protocol.stop();
  101. started = false;
  102. EditorNode::get_log()->add_message("--- GDScript language server stopped ---", EditorLog::MSG_TYPE_EDITOR);
  103. }
  104. void register_lsp_types() {
  105. GDREGISTER_CLASS(GDScriptLanguageProtocol);
  106. GDREGISTER_CLASS(GDScriptTextDocument);
  107. GDREGISTER_CLASS(GDScriptWorkspace);
  108. }