Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/language_server/gdscript_language_protocol.h
20816 views
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
#ifdef TESTS_ENABLED
51
friend class TestGDScriptLanguageProtocolInitializer;
52
#endif
53
54
private:
55
struct LSPeer : RefCounted {
56
Ref<StreamPeerTCP> connection;
57
58
uint8_t req_buf[LSP_MAX_BUFFER_SIZE];
59
int req_pos = 0;
60
bool has_header = false;
61
bool has_content = false;
62
int content_length = 0;
63
Vector<CharString> res_queue;
64
int res_sent = 0;
65
66
Error handle_data();
67
Error send_data();
68
69
/**
70
* Tracks all files that the client claimed, however for files deemed not relevant
71
* to the server the `text` might not be persisted.
72
*/
73
HashMap<String, LSP::TextDocumentItem> managed_files;
74
HashMap<String, ExtendGDScriptParser *> parse_results;
75
76
void remove_cached_parser(const String &p_path);
77
ExtendGDScriptParser *parse_script(const String &p_path);
78
79
~LSPeer();
80
81
private:
82
void clear_stale_parsers();
83
// Paths of parsers which we can't cache longterm.
84
// Can be cleared up using `clear_stale_parsers()`.
85
HashSet<String> stale_parsers;
86
};
87
88
enum LSPErrorCode {
89
RequestCancelled = -32800,
90
ContentModified = -32801,
91
};
92
93
static GDScriptLanguageProtocol *singleton;
94
95
HashMap<int, Ref<LSPeer>> clients;
96
SceneCache scene_cache;
97
Ref<TCPServer> server;
98
int latest_client_id = LSP_NO_CLIENT;
99
int next_client_id = 0;
100
101
int next_server_id = 0;
102
103
Ref<GDScriptTextDocument> text_document;
104
Ref<GDScriptWorkspace> workspace;
105
106
Error on_client_connected();
107
void on_client_disconnected(const int &p_client_id);
108
109
String process_message(const String &p_text);
110
String format_output(const String &p_text);
111
112
bool _initialized = false;
113
114
protected:
115
static void _bind_methods();
116
117
Dictionary initialize(const Dictionary &p_params);
118
void initialized(const Variant &p_params);
119
120
public:
121
_FORCE_INLINE_ static GDScriptLanguageProtocol *get_singleton() { return singleton; }
122
_FORCE_INLINE_ Ref<GDScriptWorkspace> get_workspace() { return workspace; }
123
_FORCE_INLINE_ Ref<GDScriptTextDocument> get_text_document() { return text_document; }
124
_FORCE_INLINE_ SceneCache *get_scene_cache() { return &scene_cache; }
125
126
_FORCE_INLINE_ bool is_initialized() const { return _initialized; }
127
128
void poll(int p_limit_usec);
129
Error start(int p_port, const IPAddress &p_bind_ip);
130
void stop();
131
132
void notify_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1);
133
void request_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1);
134
135
bool is_smart_resolve_enabled() const;
136
bool is_goto_native_symbols_enabled() const;
137
138
// Text Document Synchronization
139
void lsp_did_open(const Dictionary &p_params);
140
void lsp_did_change(const Dictionary &p_params);
141
void lsp_did_close(const Dictionary &p_params);
142
143
/**
144
* Returns a list of symbols that might be related to the document position.
145
*
146
* The result fulfills no semantic guarantees, nor is it guaranteed to be complete.
147
* Should only be used for "smart resolve".
148
*/
149
void resolve_related_symbols(const LSP::TextDocumentPositionParams &p_doc_pos, List<const LSP::DocumentSymbol *> &r_list);
150
151
/**
152
* Returns parse results for the given path, using the cache if available.
153
* If no such file exists, or the file is not a GDScript file a `nullptr` is returned.
154
*/
155
ExtendGDScriptParser *get_parse_result(const String &p_path);
156
157
GDScriptLanguageProtocol();
158
~GDScriptLanguageProtocol() {
159
clients.clear();
160
}
161
};
162
163