| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "gdscript_text_document.h" |
| 34 |
#include "gdscript_workspace.h" |
| 35 |
|
| 36 |
#include "core/io/stream_peer_tcp.h" |
| 37 |
#include "core/io/tcp_server.h" |
| 38 |
|
| 39 |
#include "modules/jsonrpc/jsonrpc.h" |
| 40 |
|
| 41 |
#define LSP_MAX_BUFFER_SIZE 4194304 |
| 42 |
#define LSP_MAX_CLIENTS 8 |
| 43 |
|
| 44 |
#define LSP_NO_CLIENT -1 |
| 45 |
|
| 46 |
class GDScriptLanguageProtocol : public JSONRPC { |
| 47 |
GDCLASS(GDScriptLanguageProtocol, JSONRPC) |
| 48 |
|
| 49 |
#ifdef TESTS_ENABLED |
| 50 |
friend class TestGDScriptLanguageProtocolInitializer; |
| 51 |
#endif |
| 52 |
|
| 53 |
private: |
| 54 |
struct LSPeer : RefCounted { |
| 55 |
Ref<StreamPeerTCP> connection; |
| 56 |
|
| 57 |
uint8_t req_buf[LSP_MAX_BUFFER_SIZE]; |
| 58 |
int req_pos = 0; |
| 59 |
bool has_header = false; |
| 60 |
bool has_content = false; |
| 61 |
int content_length = 0; |
| 62 |
Vector<CharString> res_queue; |
| 63 |
int res_sent = 0; |
| 64 |
|
| 65 |
Error handle_data(); |
| 66 |
Error send_data(); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
HashMap<String, LSP::TextDocumentItem> managed_files; |
| 73 |
HashMap<String, ExtendGDScriptParser *> parse_results; |
| 74 |
|
| 75 |
void remove_cached_parser(const String &p_path); |
| 76 |
ExtendGDScriptParser *parse_script(const String &p_path); |
| 77 |
|
| 78 |
~LSPeer(); |
| 79 |
|
| 80 |
private: |
| 81 |
void clear_stale_parsers(); |
| 82 |
|
| 83 |
|
| 84 |
HashSet<String> stale_parsers; |
| 85 |
}; |
| 86 |
|
| 87 |
enum LSPErrorCode { |
| 88 |
RequestCancelled = -32800, |
| 89 |
ContentModified = -32801, |
| 90 |
}; |
| 91 |
|
| 92 |
static GDScriptLanguageProtocol *singleton; |
| 93 |
|
| 94 |
HashMap<int, Ref<LSPeer>> clients; |
| 95 |
Ref<TCPServer> server; |
| 96 |
int latest_client_id = LSP_NO_CLIENT; |
| 97 |
int next_client_id = 0; |
| 98 |
|
| 99 |
int next_server_id = 0; |
| 100 |
|
| 101 |
Ref<GDScriptTextDocument> text_document; |
| 102 |
Ref<GDScriptWorkspace> workspace; |
| 103 |
|
| 104 |
Error on_client_connected(); |
| 105 |
void on_client_disconnected(const int &p_client_id); |
| 106 |
|
| 107 |
String process_message(const String &p_text); |
| 108 |
String format_output(const String &p_text); |
| 109 |
|
| 110 |
bool _initialized = false; |
| 111 |
|
| 112 |
protected: |
| 113 |
static void _bind_methods(); |
| 114 |
|
| 115 |
Dictionary initialize(const Dictionary &p_params); |
| 116 |
void initialized(const Variant &p_params); |
| 117 |
|
| 118 |
public: |
| 119 |
_FORCE_INLINE_ static GDScriptLanguageProtocol *get_singleton() { return singleton; } |
| 120 |
_FORCE_INLINE_ Ref<GDScriptWorkspace> get_workspace() { return workspace; } |
| 121 |
_FORCE_INLINE_ Ref<GDScriptTextDocument> get_text_document() { return text_document; } |
| 122 |
_FORCE_INLINE_ bool is_initialized() const { return _initialized; } |
| 123 |
|
| 124 |
void poll(int p_limit_usec); |
| 125 |
Error start(int p_port, const IPAddress &p_bind_ip); |
| 126 |
void stop(); |
| 127 |
|
| 128 |
void notify_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1); |
| 129 |
void request_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1); |
| 130 |
|
| 131 |
bool is_smart_resolve_enabled() const; |
| 132 |
bool is_goto_native_symbols_enabled() const; |
| 133 |
|
| 134 |
|
| 135 |
void lsp_did_open(const Dictionary &p_params); |
| 136 |
void lsp_did_change(const Dictionary &p_params); |
| 137 |
void lsp_did_close(const Dictionary &p_params); |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
void resolve_related_symbols(const LSP::TextDocumentPositionParams &p_doc_pos, List<const LSP::DocumentSymbol *> &r_list); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
ExtendGDScriptParser *get_parse_result(const String &p_path); |
| 152 |
|
| 153 |
GDScriptLanguageProtocol(); |
| 154 |
~GDScriptLanguageProtocol() { |
| 155 |
clients.clear(); |
| 156 |
} |
| 157 |
}; |