Debian Codegraph @

modules/gdscript/language_server/gdscript_language_protocol.h

raw symbols SCIP
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
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 * Tracks all files that the client claimed, however for files deemed not relevant
70 * to the server the `text` might not be persisted.
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 // Paths of parsers which we can't cache longterm.
83 // Can be cleared up using `clear_stale_parsers()`.
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 // Text Document Synchronization
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 * Returns a list of symbols that might be related to the document position.
141 *
142 * The result fulfills no semantic guarantees, nor is it guaranteed to be complete.
143 * Should only be used for "smart resolve".
144 */
145 void resolve_related_symbols(const LSP::TextDocumentPositionParams &p_doc_pos, List<const LSP::DocumentSymbol *> &r_list);
146
147 /**
148 * Returns parse results for the given path, using the cache if available.
149 * If no such file exists, or the file is not a GDScript file a `nullptr` is returned.
150 */
151 ExtendGDScriptParser *get_parse_result(const String &p_path);
152
153 GDScriptLanguageProtocol();
154 ~GDScriptLanguageProtocol() {
155 clients.clear();
156 }
157 };