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
10278 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
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
class GDScriptLanguageProtocol : public JSONRPC {
45
GDCLASS(GDScriptLanguageProtocol, JSONRPC)
46
47
private:
48
struct LSPeer : RefCounted {
49
Ref<StreamPeerTCP> connection;
50
51
uint8_t req_buf[LSP_MAX_BUFFER_SIZE];
52
int req_pos = 0;
53
bool has_header = false;
54
bool has_content = false;
55
int content_length = 0;
56
Vector<CharString> res_queue;
57
int res_sent = 0;
58
59
Error handle_data();
60
Error send_data();
61
};
62
63
enum LSPErrorCode {
64
RequestCancelled = -32800,
65
ContentModified = -32801,
66
};
67
68
static GDScriptLanguageProtocol *singleton;
69
70
HashMap<int, Ref<LSPeer>> clients;
71
Ref<TCPServer> server;
72
int latest_client_id = 0;
73
int next_client_id = 0;
74
75
int next_server_id = 0;
76
77
Ref<GDScriptTextDocument> text_document;
78
Ref<GDScriptWorkspace> workspace;
79
80
Error on_client_connected();
81
void on_client_disconnected(const int &p_client_id);
82
83
String process_message(const String &p_text);
84
String format_output(const String &p_text);
85
86
bool _initialized = false;
87
88
protected:
89
static void _bind_methods();
90
91
Dictionary initialize(const Dictionary &p_params);
92
void initialized(const Variant &p_params);
93
94
public:
95
_FORCE_INLINE_ static GDScriptLanguageProtocol *get_singleton() { return singleton; }
96
_FORCE_INLINE_ Ref<GDScriptWorkspace> get_workspace() { return workspace; }
97
_FORCE_INLINE_ Ref<GDScriptTextDocument> get_text_document() { return text_document; }
98
_FORCE_INLINE_ bool is_initialized() const { return _initialized; }
99
100
void poll(int p_limit_usec);
101
Error start(int p_port, const IPAddress &p_bind_ip);
102
void stop();
103
104
void notify_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1);
105
void request_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1);
106
107
bool is_smart_resolve_enabled() const;
108
bool is_goto_native_symbols_enabled() const;
109
110
GDScriptLanguageProtocol();
111
};
112
113