gdscript_language_protocol.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*************************************************************************/
  2. /* gdscript_language_protocol.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_protocol.h"
  31. #include "core/io/json.h"
  32. #include "core/os/copymem.h"
  33. #include "core/project_settings.h"
  34. #include "editor/editor_node.h"
  35. GDScriptLanguageProtocol *GDScriptLanguageProtocol::singleton = NULL;
  36. void GDScriptLanguageProtocol::on_data_received(int p_id) {
  37. lastest_client_id = p_id;
  38. Ref<WebSocketPeer> peer = server->get_peer(p_id);
  39. PoolByteArray data;
  40. if (OK == peer->get_packet_buffer(data)) {
  41. String message;
  42. message.parse_utf8((const char *)data.read().ptr(), data.size());
  43. if (message.begins_with("Content-Length:")) return;
  44. String output = process_message(message);
  45. if (!output.empty()) {
  46. CharString charstr = output.utf8();
  47. peer->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
  48. }
  49. }
  50. }
  51. void GDScriptLanguageProtocol::on_client_connected(int p_id, const String &p_protocal) {
  52. clients.set(p_id, server->get_peer(p_id));
  53. }
  54. void GDScriptLanguageProtocol::on_client_disconnected(int p_id, bool p_was_clean_close) {
  55. clients.erase(p_id);
  56. }
  57. String GDScriptLanguageProtocol::process_message(const String &p_text) {
  58. String ret = process_string(p_text);
  59. if (ret.empty()) {
  60. return ret;
  61. } else {
  62. return format_output(ret);
  63. }
  64. }
  65. String GDScriptLanguageProtocol::format_output(const String &p_text) {
  66. String header = "Content-Length: ";
  67. CharString charstr = p_text.utf8();
  68. size_t len = charstr.length();
  69. header += itos(len);
  70. header += "\r\n\r\n";
  71. return header + p_text;
  72. }
  73. void GDScriptLanguageProtocol::_bind_methods() {
  74. ClassDB::bind_method(D_METHOD("initialize", "params"), &GDScriptLanguageProtocol::initialize);
  75. ClassDB::bind_method(D_METHOD("initialized", "params"), &GDScriptLanguageProtocol::initialized);
  76. ClassDB::bind_method(D_METHOD("on_data_received"), &GDScriptLanguageProtocol::on_data_received);
  77. ClassDB::bind_method(D_METHOD("on_client_connected"), &GDScriptLanguageProtocol::on_client_connected);
  78. ClassDB::bind_method(D_METHOD("on_client_disconnected"), &GDScriptLanguageProtocol::on_client_disconnected);
  79. }
  80. Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) {
  81. lsp::InitializeResult ret;
  82. String root_uri = p_params["rootUri"];
  83. String root = p_params["rootPath"];
  84. bool is_same_workspace = root == workspace.root;
  85. is_same_workspace = root.to_lower() == workspace.root.to_lower();
  86. #ifdef WINDOWS_ENABLED
  87. is_same_workspace = root.replace("\\", "/").to_lower() == workspace.root.to_lower();
  88. #endif
  89. if (root_uri.length() && is_same_workspace) {
  90. workspace.root_uri = root_uri;
  91. } else {
  92. workspace.root_uri = "file://" + workspace.root;
  93. }
  94. if (!_initialized) {
  95. workspace.initialize();
  96. text_document.initialize();
  97. _initialized = true;
  98. }
  99. return ret.to_json();
  100. }
  101. void GDScriptLanguageProtocol::initialized(const Variant &p_params) {
  102. Dictionary params;
  103. params["type"] = 3;
  104. params["message"] = "GDScript Language Server initialized!";
  105. Dictionary test_message = make_notification("window/showMessage", params);
  106. if (Ref<WebSocketPeer> *peer = clients.getptr(lastest_client_id)) {
  107. String msg = JSON::print(test_message);
  108. msg = format_output(msg);
  109. CharString charstr = msg.utf8();
  110. (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
  111. }
  112. }
  113. void GDScriptLanguageProtocol::poll() {
  114. server->poll();
  115. }
  116. Error GDScriptLanguageProtocol::start(int p_port) {
  117. if (server == NULL) {
  118. server = dynamic_cast<WebSocketServer *>(ClassDB::instance("WebSocketServer"));
  119. server->set_buffers(8192, 1024, 8192, 1024); // 8mb should be way more than enough
  120. server->connect("data_received", this, "on_data_received");
  121. server->connect("client_connected", this, "on_client_connected");
  122. server->connect("client_disconnected", this, "on_client_disconnected");
  123. }
  124. return server->listen(p_port);
  125. }
  126. void GDScriptLanguageProtocol::stop() {
  127. server->stop();
  128. }
  129. void GDScriptLanguageProtocol::notify_all_clients(const String &p_method, const Variant &p_params) {
  130. Dictionary message = make_notification(p_method, p_params);
  131. String msg = JSON::print(message);
  132. msg = format_output(msg);
  133. CharString charstr = msg.utf8();
  134. const int *p_id = clients.next(NULL);
  135. while (p_id != NULL) {
  136. Ref<WebSocketPeer> peer = clients.get(*p_id);
  137. (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
  138. p_id = clients.next(p_id);
  139. }
  140. }
  141. void GDScriptLanguageProtocol::notify_client(const String &p_method, const Variant &p_params, int p_client) {
  142. if (p_client == -1) {
  143. p_client = lastest_client_id;
  144. }
  145. Ref<WebSocketPeer> *peer = clients.getptr(p_client);
  146. ERR_FAIL_COND(peer == NULL);
  147. Dictionary message = make_notification(p_method, p_params);
  148. String msg = JSON::print(message);
  149. msg = format_output(msg);
  150. CharString charstr = msg.utf8();
  151. (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length());
  152. }
  153. bool GDScriptLanguageProtocol::is_smart_resolve_enabled() const {
  154. return bool(_EDITOR_GET("network/language_server/enable_smart_resolve"));
  155. }
  156. GDScriptLanguageProtocol::GDScriptLanguageProtocol() {
  157. server = NULL;
  158. singleton = this;
  159. _initialized = false;
  160. set_scope("textDocument", &text_document);
  161. set_scope("completionItem", &text_document);
  162. set_scope("workspace", &workspace);
  163. workspace.root = ProjectSettings::get_singleton()->get_resource_path();
  164. }
  165. GDScriptLanguageProtocol::~GDScriptLanguageProtocol() {
  166. memdelete(server);
  167. server = NULL;
  168. }