0.2.0-dev Download ZIP
184 lines ·6.9 KB Raw
| 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 | #pragma once |
| 32 | |
| 33 | #include "gdscript_text_document.h" |
| 34 | #include "gdscript_workspace.h" |
| 35 | #include "scene_cache.h" |
| 36 | |
| 37 | #include "core/io/stream_peer_tcp.h" |
| 38 | #include "core/io/tcp_server.h" |
| 39 | |
| 40 | #include "modules/jsonrpc/jsonrpc.h" |
| 41 | |
| 42 | #define LSP_MAX_BUFFER_SIZE 4194304 |
| 43 | #define LSP_MAX_CLIENTS 8 |
| 44 | |
| 45 | #define LSP_NO_CLIENT -1 |
| 46 | |
| 47 | class GDScriptLanguageProtocol : public JSONRPC { |
| 48 | GDCLASS(GDScriptLanguageProtocol, JSONRPC) |
| 49 | |
| 50 | friend class TestGDScriptLanguageProtocolInitializer; |
| 51 | |
| 52 | public: |
| 53 | struct ClientBehavior { |
| 54 | /** If `true` use snippet insert mode to position the cursor between braces of completion options. If `false` strip braces from completion options since we can't provide good UX for them. */ |
| 55 | bool use_snippets_for_brace_completion = false; |
| 56 | /** HTML tags, e.g. `span`, which the client is capable of rendering inside Markdown content. Assumes full support for attributes e.g. `style`. */ |
| 57 | HashSet<String> markdown_allowed_html_tags; |
| 58 | }; |
| 59 | |
| 60 | private: |
| 61 | struct LSPeer : RefCounted { |
| 62 | Ref<StreamPeerTCP> connection; |
| 63 | |
| 64 | uint8_t req_buf[LSP_MAX_BUFFER_SIZE]; |
| 65 | int req_pos = 0; |
| 66 | bool has_header = false; |
| 67 | bool has_content = false; |
| 68 | int content_length = 0; |
| 69 | Vector<CharString> res_queue; |
| 70 | int res_sent = 0; |
| 71 | |
| 72 | Error handle_data(); |
| 73 | Error send_data(); |
| 74 | |
| 75 | /** |
| 76 | * Represents how the server should behave towards this client in certain situations. |
| 77 | * This gets derived from client capabilities so the configured behavior is guaranteed to be supported by the client. |
| 78 | */ |
| 79 | ClientBehavior behavior; |
| 80 | |
| 81 | /** |
| 82 | * Tracks all files that the client claimed, however for files deemed not relevant |
| 83 | * to the server the `text` might not be persisted. |
| 84 | */ |
| 85 | HashMap<String, LSP::TextDocumentItem> managed_files; |
| 86 | HashMap<String, ExtendGDScriptParser *> parse_results; |
| 87 | |
| 88 | void remove_cached_parser(const String &p_path); |
| 89 | ExtendGDScriptParser *parse_script(const String &p_path); |
| 90 | |
| 91 | ~LSPeer(); |
| 92 | |
| 93 | private: |
| 94 | void clear_stale_parsers(); |
| 95 | // Paths of parsers which we can't cache longterm. |
| 96 | // Can be cleared up using `clear_stale_parsers()`. |
| 97 | HashSet<String> stale_parsers; |
| 98 | }; |
| 99 | |
| 100 | enum LSPErrorCode { |
| 101 | RequestCancelled = -32800, |
| 102 | ContentModified = -32801, |
| 103 | }; |
| 104 | |
| 105 | static GDScriptLanguageProtocol *singleton; |
| 106 | |
| 107 | HashMap<int, Ref<LSPeer>> clients; |
| 108 | SceneCache scene_cache; |
| 109 | Ref<TCPServer> server; |
| 110 | int latest_client_id = LSP_NO_CLIENT; |
| 111 | int next_client_id = 0; |
| 112 | |
| 113 | int next_server_id = 0; |
| 114 | |
| 115 | Ref<GDScriptTextDocument> text_document; |
| 116 | Ref<GDScriptWorkspace> workspace; |
| 117 | |
| 118 | Error on_client_connected(); |
| 119 | void on_client_disconnected(const int &p_client_id); |
| 120 | |
| 121 | String process_message(const String &p_text); |
| 122 | String format_output(const String &p_text); |
| 123 | |
| 124 | bool _initialized = false; |
| 125 | |
| 126 | protected: |
| 127 | static void _bind_methods(); |
| 128 | |
| 129 | Variant initialize(const Dictionary &p_params); |
| 130 | void initialized(const Variant &p_params); |
| 131 | |
| 132 | public: |
| 133 | _FORCE_INLINE_ static GDScriptLanguageProtocol *get_singleton() { return singleton; } |
| 134 | _FORCE_INLINE_ Ref<GDScriptWorkspace> get_workspace() { return workspace; } |
| 135 | _FORCE_INLINE_ Ref<GDScriptTextDocument> get_text_document() { return text_document; } |
| 136 | _FORCE_INLINE_ SceneCache *get_scene_cache() { return &scene_cache; } |
| 137 | |
| 138 | _FORCE_INLINE_ bool is_initialized() const { return _initialized; } |
| 139 | |
| 140 | void poll(int p_limit_usec); |
| 141 | Error start(int p_port, const IPAddress &p_bind_ip); |
| 142 | void stop(); |
| 143 | |
| 144 | void notify_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1); |
| 145 | void request_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1, const Callable &p_response_handler = Callable()); |
| 146 | |
| 147 | bool is_smart_resolve_enabled() const; |
| 148 | bool is_goto_native_symbols_enabled() const; |
| 149 | |
| 150 | // Text Document Synchronization |
| 151 | void lsp_did_open(const Dictionary &p_params); |
| 152 | void lsp_did_change(const Dictionary &p_params); |
| 153 | void lsp_did_close(const Dictionary &p_params); |
| 154 | |
| 155 | // Completion |
| 156 | Array lsp_completion(const Dictionary &p_params); |
| 157 | |
| 158 | /** |
| 159 | * Returns a list of symbols that might be related to the document position. |
| 160 | * |
| 161 | * The result fulfills no semantic guarantees, nor is it guaranteed to be complete. |
| 162 | * Should only be used for "smart resolve". |
| 163 | */ |
| 164 | void resolve_related_symbols(const LSP::TextDocumentPositionParams &p_doc_pos, List<const LSP::DocumentSymbol *> &r_list); |
| 165 | |
| 166 | /** |
| 167 | * Returns parse results for the given path, using the cache if available. |
| 168 | * If no such file exists, or the file is not a GDScript file a `nullptr` is returned. |
| 169 | */ |
| 170 | ExtendGDScriptParser *get_parse_result(const String &p_path); |
| 171 | |
| 172 | /** |
| 173 | * Returns the HTML tags the client can render inside Markdown content, e.g. `span`. |
| 174 | * Defaults to an empty set if no client is connected. |
| 175 | * |
| 176 | * TODO: Remove after moving endpoints into unified class. Access non-null client directly. |
| 177 | */ |
| 178 | const HashSet<String> &get_client_markdown_allowed_html_tags() const; |
| 179 | |
| 180 | GDScriptLanguageProtocol(); |
| 181 | ~GDScriptLanguageProtocol() { |
| 182 | clients.clear(); |
| 183 | } |
| 184 | }; |