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