gdscript_language_server.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*************************************************************************/
  2. /* gdscript_language_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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/os/file_access.h"
  32. #include "core/os/os.h"
  33. #include "editor/editor_log.h"
  34. #include "editor/editor_node.h"
  35. GDScriptLanguageServer::GDScriptLanguageServer() {
  36. thread = NULL;
  37. thread_exit = false;
  38. _EDITOR_DEF("network/language_server/remote_port", 6008);
  39. _EDITOR_DEF("network/language_server/enable_smart_resolve", true);
  40. _EDITOR_DEF("network/language_server/show_native_symbols_in_editor", false);
  41. }
  42. void GDScriptLanguageServer::_notification(int p_what) {
  43. switch (p_what) {
  44. case NOTIFICATION_ENTER_TREE:
  45. start();
  46. break;
  47. case NOTIFICATION_EXIT_TREE:
  48. stop();
  49. break;
  50. }
  51. }
  52. void GDScriptLanguageServer::thread_main(void *p_userdata) {
  53. GDScriptLanguageServer *self = static_cast<GDScriptLanguageServer *>(p_userdata);
  54. while (!self->thread_exit) {
  55. // Poll 20 times per second
  56. self->protocol.poll();
  57. OS::get_singleton()->delay_usec(50000);
  58. }
  59. }
  60. void GDScriptLanguageServer::start() {
  61. int port = (int)_EDITOR_GET("network/language_server/remote_port");
  62. if (protocol.start(port) == OK) {
  63. EditorNode::get_log()->add_message("--- GDScript language server started ---", EditorLog::MSG_TYPE_EDITOR);
  64. ERR_FAIL_COND(thread != NULL || thread_exit);
  65. thread_exit = false;
  66. thread = Thread::create(GDScriptLanguageServer::thread_main, this);
  67. }
  68. }
  69. void GDScriptLanguageServer::stop() {
  70. ERR_FAIL_COND(NULL == thread || thread_exit);
  71. thread_exit = true;
  72. Thread::wait_to_finish(thread);
  73. memdelete(thread);
  74. thread = NULL;
  75. protocol.stop();
  76. EditorNode::get_log()->add_message("--- GDScript language server stopped ---", EditorLog::MSG_TYPE_EDITOR);
  77. }
  78. void register_lsp_types() {
  79. ClassDB::register_class<GDScriptLanguageProtocol>();
  80. ClassDB::register_class<GDScriptTextDocument>();
  81. ClassDB::register_class<GDScriptWorkspace>();
  82. }