gdscript_language_protocol.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**************************************************************************/
  2. /* gdscript_language_protocol.h */
  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. #ifndef GDSCRIPT_LANGUAGE_PROTOCOL_H
  31. #define GDSCRIPT_LANGUAGE_PROTOCOL_H
  32. #include "gdscript_text_document.h"
  33. #include "gdscript_workspace.h"
  34. #include "core/io/stream_peer_tcp.h"
  35. #include "core/io/tcp_server.h"
  36. #include "modules/modules_enabled.gen.h" // For jsonrpc.
  37. #ifdef MODULE_JSONRPC_ENABLED
  38. #include "modules/jsonrpc/jsonrpc.h"
  39. #else
  40. #error "Can't build GDScript LSP without JSONRPC module."
  41. #endif
  42. #define LSP_MAX_BUFFER_SIZE 4194304
  43. #define LSP_MAX_CLIENTS 8
  44. class GDScriptLanguageProtocol : public JSONRPC {
  45. GDCLASS(GDScriptLanguageProtocol, JSONRPC)
  46. private:
  47. struct LSPeer : RefCounted {
  48. Ref<StreamPeerTCP> connection;
  49. uint8_t req_buf[LSP_MAX_BUFFER_SIZE];
  50. int req_pos = 0;
  51. bool has_header = false;
  52. bool has_content = false;
  53. int content_length = 0;
  54. Vector<CharString> res_queue;
  55. int res_sent = 0;
  56. Error handle_data();
  57. Error send_data();
  58. };
  59. enum LSPErrorCode {
  60. RequestCancelled = -32800,
  61. ContentModified = -32801,
  62. };
  63. static GDScriptLanguageProtocol *singleton;
  64. HashMap<int, Ref<LSPeer>> clients;
  65. Ref<TCPServer> server;
  66. int latest_client_id = 0;
  67. int next_client_id = 0;
  68. int next_server_id = 0;
  69. Ref<GDScriptTextDocument> text_document;
  70. Ref<GDScriptWorkspace> workspace;
  71. Error on_client_connected();
  72. void on_client_disconnected(const int &p_client_id);
  73. String process_message(const String &p_text);
  74. String format_output(const String &p_text);
  75. bool _initialized = false;
  76. protected:
  77. static void _bind_methods();
  78. Dictionary initialize(const Dictionary &p_params);
  79. void initialized(const Variant &p_params);
  80. public:
  81. _FORCE_INLINE_ static GDScriptLanguageProtocol *get_singleton() { return singleton; }
  82. _FORCE_INLINE_ Ref<GDScriptWorkspace> get_workspace() { return workspace; }
  83. _FORCE_INLINE_ Ref<GDScriptTextDocument> get_text_document() { return text_document; }
  84. _FORCE_INLINE_ bool is_initialized() const { return _initialized; }
  85. void poll(int p_limit_usec);
  86. Error start(int p_port, const IPAddress &p_bind_ip);
  87. void stop();
  88. void notify_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1);
  89. void request_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1);
  90. bool is_smart_resolve_enabled() const;
  91. bool is_goto_native_symbols_enabled() const;
  92. GDScriptLanguageProtocol();
  93. };
  94. #endif // GDSCRIPT_LANGUAGE_PROTOCOL_H