gdscript_language_protocol.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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-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. // FIXME: Reenable LSP.
  31. #if 0
  32. #include "gdscript_language_protocol.h"
  33. #include "core/io/json.h"
  34. #include "core/os/copymem.h"
  35. #include "core/project_settings.h"
  36. #include "editor/editor_log.h"
  37. #include "editor/editor_node.h"
  38. GDScriptLanguageProtocol *GDScriptLanguageProtocol::singleton = nullptr;
  39. Error GDScriptLanguageProtocol::LSPeer::handle_data() {
  40. int read = 0;
  41. // Read headers
  42. if (!has_header) {
  43. while (true) {
  44. if (req_pos >= LSP_MAX_BUFFER_SIZE) {
  45. req_pos = 0;
  46. ERR_FAIL_COND_V_MSG(true, ERR_OUT_OF_MEMORY, "Response header too big");
  47. }
  48. Error err = connection->get_partial_data(&req_buf[req_pos], 1, read);
  49. if (err != OK) {
  50. return FAILED;
  51. } else if (read != 1) { // Busy, wait until next poll
  52. return ERR_BUSY;
  53. }
  54. char *r = (char *)req_buf;
  55. int l = req_pos;
  56. // End of headers
  57. if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
  58. r[l - 3] = '\0'; // Null terminate to read string
  59. String header;
  60. header.parse_utf8(r);
  61. content_length = header.substr(16).to_int();
  62. has_header = true;
  63. req_pos = 0;
  64. break;
  65. }
  66. req_pos++;
  67. }
  68. }
  69. if (has_header) {
  70. while (req_pos < content_length) {
  71. if (req_pos >= LSP_MAX_BUFFER_SIZE) {
  72. req_pos = 0;
  73. has_header = false;
  74. ERR_FAIL_COND_V_MSG(req_pos >= LSP_MAX_BUFFER_SIZE, ERR_OUT_OF_MEMORY, "Response content too big");
  75. }
  76. Error err = connection->get_partial_data(&req_buf[req_pos], 1, read);
  77. if (err != OK) {
  78. return FAILED;
  79. } else if (read != 1) {
  80. return ERR_BUSY;
  81. }
  82. req_pos++;
  83. }
  84. // Parse data
  85. String msg;
  86. msg.parse_utf8((const char *)req_buf, req_pos);
  87. // Reset to read again
  88. req_pos = 0;
  89. has_header = false;
  90. // Response
  91. String output = GDScriptLanguageProtocol::get_singleton()->process_message(msg);
  92. if (!output.empty()) {
  93. res_queue.push_back(output.utf8());
  94. }
  95. }
  96. return OK;
  97. }
  98. Error GDScriptLanguageProtocol::LSPeer::send_data() {
  99. int sent = 0;
  100. if (!res_queue.empty()) {
  101. CharString c_res = res_queue[0];
  102. if (res_sent < c_res.size()) {
  103. Error err = connection->put_partial_data((const uint8_t *)c_res.get_data() + res_sent, c_res.size() - res_sent - 1, sent);
  104. if (err != OK) {
  105. return err;
  106. }
  107. res_sent += sent;
  108. }
  109. // Response sent
  110. if (res_sent >= c_res.size() - 1) {
  111. res_sent = 0;
  112. res_queue.remove(0);
  113. }
  114. }
  115. return OK;
  116. }
  117. Error GDScriptLanguageProtocol::on_client_connected() {
  118. Ref<StreamPeerTCP> tcp_peer = server->take_connection();
  119. ERR_FAIL_COND_V_MSG(clients.size() >= LSP_MAX_CLIENTS, FAILED, "Max client limits reached");
  120. Ref<LSPeer> peer = memnew(LSPeer);
  121. peer->connection = tcp_peer;
  122. clients.set(next_client_id, peer);
  123. next_client_id++;
  124. EditorNode::get_log()->add_message("Connection Taken", EditorLog::MSG_TYPE_EDITOR);
  125. return OK;
  126. }
  127. void GDScriptLanguageProtocol::on_client_disconnected(const int &p_client_id) {
  128. clients.erase(p_client_id);
  129. EditorNode::get_log()->add_message("Disconnected", EditorLog::MSG_TYPE_EDITOR);
  130. }
  131. String GDScriptLanguageProtocol::process_message(const String &p_text) {
  132. String ret = process_string(p_text);
  133. if (ret.empty()) {
  134. return ret;
  135. } else {
  136. return format_output(ret);
  137. }
  138. }
  139. String GDScriptLanguageProtocol::format_output(const String &p_text) {
  140. String header = "Content-Length: ";
  141. CharString charstr = p_text.utf8();
  142. size_t len = charstr.length();
  143. header += itos(len);
  144. header += "\r\n\r\n";
  145. return header + p_text;
  146. }
  147. void GDScriptLanguageProtocol::_bind_methods() {
  148. ClassDB::bind_method(D_METHOD("initialize", "params"), &GDScriptLanguageProtocol::initialize);
  149. ClassDB::bind_method(D_METHOD("initialized", "params"), &GDScriptLanguageProtocol::initialized);
  150. ClassDB::bind_method(D_METHOD("on_client_connected"), &GDScriptLanguageProtocol::on_client_connected);
  151. ClassDB::bind_method(D_METHOD("on_client_disconnected"), &GDScriptLanguageProtocol::on_client_disconnected);
  152. ClassDB::bind_method(D_METHOD("notify_client", "method", "params"), &GDScriptLanguageProtocol::notify_client, DEFVAL(Variant()), DEFVAL(-1));
  153. ClassDB::bind_method(D_METHOD("is_smart_resolve_enabled"), &GDScriptLanguageProtocol::is_smart_resolve_enabled);
  154. ClassDB::bind_method(D_METHOD("get_text_document"), &GDScriptLanguageProtocol::get_text_document);
  155. ClassDB::bind_method(D_METHOD("get_workspace"), &GDScriptLanguageProtocol::get_workspace);
  156. ClassDB::bind_method(D_METHOD("is_initialized"), &GDScriptLanguageProtocol::is_initialized);
  157. }
  158. Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) {
  159. lsp::InitializeResult ret;
  160. String root_uri = p_params["rootUri"];
  161. String root = p_params["rootPath"];
  162. bool is_same_workspace;
  163. #ifndef WINDOWS_ENABLED
  164. is_same_workspace = root.to_lower() == workspace->root.to_lower();
  165. #else
  166. is_same_workspace = root.replace("\\", "/").to_lower() == workspace->root.to_lower();
  167. #endif
  168. if (root_uri.length() && is_same_workspace) {
  169. workspace->root_uri = root_uri;
  170. } else {
  171. workspace->root_uri = "file://" + workspace->root;
  172. Dictionary params;
  173. params["path"] = workspace->root;
  174. Dictionary request = make_notification("gdscript_client/changeWorkspace", params);
  175. ERR_FAIL_COND_V_MSG(!clients.has(latest_client_id), ret.to_json(),
  176. vformat("GDScriptLanguageProtocol: Can't initialize invalid peer '%d'.", latest_client_id));
  177. Ref<LSPeer> peer = clients.get(latest_client_id);
  178. if (peer != nullptr) {
  179. String msg = JSON::print(request);
  180. msg = format_output(msg);
  181. (*peer)->res_queue.push_back(msg.utf8());
  182. }
  183. }
  184. if (!_initialized) {
  185. workspace->initialize();
  186. text_document->initialize();
  187. _initialized = true;
  188. }
  189. return ret.to_json();
  190. }
  191. void GDScriptLanguageProtocol::initialized(const Variant &p_params) {
  192. lsp::GodotCapabilities capabilities;
  193. DocData *doc = EditorHelp::get_doc_data();
  194. for (Map<String, DocData::ClassDoc>::Element *E = doc->class_list.front(); E; E = E->next()) {
  195. lsp::GodotNativeClassInfo gdclass;
  196. gdclass.name = E->get().name;
  197. gdclass.class_doc = &(E->get());
  198. if (ClassDB::ClassInfo *ptr = ClassDB::classes.getptr(StringName(E->get().name))) {
  199. gdclass.class_info = ptr;
  200. }
  201. capabilities.native_classes.push_back(gdclass);
  202. }
  203. notify_client("gdscript/capabilities", capabilities.to_json());
  204. }
  205. void GDScriptLanguageProtocol::poll() {
  206. if (server->is_connection_available()) {
  207. on_client_connected();
  208. }
  209. const int *id = nullptr;
  210. while ((id = clients.next(id))) {
  211. Ref<LSPeer> peer = clients.get(*id);
  212. StreamPeerTCP::Status status = peer->connection->get_status();
  213. if (status == StreamPeerTCP::STATUS_NONE || status == StreamPeerTCP::STATUS_ERROR) {
  214. on_client_disconnected(*id);
  215. id = nullptr;
  216. } else {
  217. if (peer->connection->get_available_bytes() > 0) {
  218. latest_client_id = *id;
  219. Error err = peer->handle_data();
  220. if (err != OK && err != ERR_BUSY) {
  221. on_client_disconnected(*id);
  222. id = nullptr;
  223. }
  224. }
  225. Error err = peer->send_data();
  226. if (err != OK && err != ERR_BUSY) {
  227. on_client_disconnected(*id);
  228. id = nullptr;
  229. }
  230. }
  231. }
  232. }
  233. Error GDScriptLanguageProtocol::start(int p_port, const IP_Address &p_bind_ip) {
  234. return server->listen(p_port, p_bind_ip);
  235. }
  236. void GDScriptLanguageProtocol::stop() {
  237. const int *id = nullptr;
  238. while ((id = clients.next(id))) {
  239. Ref<LSPeer> peer = clients.get(*id);
  240. peer->connection->disconnect_from_host();
  241. }
  242. server->stop();
  243. }
  244. void GDScriptLanguageProtocol::notify_client(const String &p_method, const Variant &p_params, int p_client_id) {
  245. if (p_client_id == -1) {
  246. ERR_FAIL_COND_MSG(latest_client_id == -1,
  247. "GDScript LSP: Can't notify client as none was connected.");
  248. p_client_id = latest_client_id;
  249. }
  250. ERR_FAIL_COND(!clients.has(p_client_id));
  251. Ref<LSPeer> peer = clients.get(p_client_id);
  252. ERR_FAIL_COND(peer == nullptr);
  253. Dictionary message = make_notification(p_method, p_params);
  254. String msg = JSON::print(message);
  255. msg = format_output(msg);
  256. peer->res_queue.push_back(msg.utf8());
  257. }
  258. bool GDScriptLanguageProtocol::is_smart_resolve_enabled() const {
  259. return bool(_EDITOR_GET("network/language_server/enable_smart_resolve"));
  260. }
  261. bool GDScriptLanguageProtocol::is_goto_native_symbols_enabled() const {
  262. return bool(_EDITOR_GET("network/language_server/show_native_symbols_in_editor"));
  263. }
  264. GDScriptLanguageProtocol::GDScriptLanguageProtocol() {
  265. server.instance();
  266. singleton = this;
  267. workspace.instance();
  268. text_document.instance();
  269. set_scope("textDocument", text_document.ptr());
  270. set_scope("completionItem", text_document.ptr());
  271. set_scope("workspace", workspace.ptr());
  272. workspace->root = ProjectSettings::get_singleton()->get_resource_path();
  273. }
  274. #endif