2023-01-05 20:25:55 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* gdscript_language_protocol.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2019-06-14 22:38:54 +08:00
|
|
|
|
|
|
|
#include "gdscript_language_protocol.h"
|
2020-06-08 17:00:07 +08:00
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/config/project_settings.h"
|
2020-11-29 11:42:06 +08:00
|
|
|
#include "editor/doc_tools.h"
|
2022-11-19 19:45:49 +08:00
|
|
|
#include "editor/editor_help.h"
|
2020-02-02 03:11:33 +08:00
|
|
|
#include "editor/editor_log.h"
|
2019-06-23 21:10:28 +08:00
|
|
|
#include "editor/editor_node.h"
|
2022-08-01 02:14:15 +08:00
|
|
|
#include "editor/editor_settings.h"
|
2019-06-14 22:38:54 +08:00
|
|
|
|
2020-04-02 07:20:12 +08:00
|
|
|
GDScriptLanguageProtocol *GDScriptLanguageProtocol::singleton = nullptr;
|
2019-06-14 22:38:54 +08:00
|
|
|
|
2020-02-02 03:11:33 +08:00
|
|
|
Error GDScriptLanguageProtocol::LSPeer::handle_data() {
|
|
|
|
int read = 0;
|
|
|
|
// Read headers
|
|
|
|
if (!has_header) {
|
|
|
|
while (true) {
|
|
|
|
if (req_pos >= LSP_MAX_BUFFER_SIZE) {
|
|
|
|
req_pos = 0;
|
2023-04-18 15:20:48 +08:00
|
|
|
ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Response header too big");
|
2020-02-02 03:11:33 +08:00
|
|
|
}
|
|
|
|
Error err = connection->get_partial_data(&req_buf[req_pos], 1, read);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (err != OK) {
|
2020-02-02 03:11:33 +08:00
|
|
|
return FAILED;
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (read != 1) { // Busy, wait until next poll
|
2020-02-02 03:11:33 +08:00
|
|
|
return ERR_BUSY;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2020-02-02 03:11:33 +08:00
|
|
|
char *r = (char *)req_buf;
|
|
|
|
int l = req_pos;
|
|
|
|
|
|
|
|
// End of headers
|
|
|
|
if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
|
|
|
|
r[l - 3] = '\0'; // Null terminate to read string
|
|
|
|
String header;
|
|
|
|
header.parse_utf8(r);
|
|
|
|
content_length = header.substr(16).to_int();
|
|
|
|
has_header = true;
|
|
|
|
req_pos = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
req_pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (has_header) {
|
|
|
|
while (req_pos < content_length) {
|
|
|
|
if (req_pos >= LSP_MAX_BUFFER_SIZE) {
|
|
|
|
req_pos = 0;
|
|
|
|
has_header = false;
|
|
|
|
ERR_FAIL_COND_V_MSG(req_pos >= LSP_MAX_BUFFER_SIZE, ERR_OUT_OF_MEMORY, "Response content too big");
|
|
|
|
}
|
|
|
|
Error err = connection->get_partial_data(&req_buf[req_pos], 1, read);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (err != OK) {
|
2020-02-02 03:11:33 +08:00
|
|
|
return FAILED;
|
2020-05-14 22:41:43 +08:00
|
|
|
} else if (read != 1) {
|
2020-02-02 03:11:33 +08:00
|
|
|
return ERR_BUSY;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2020-02-02 03:11:33 +08:00
|
|
|
req_pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse data
|
|
|
|
String msg;
|
|
|
|
msg.parse_utf8((const char *)req_buf, req_pos);
|
|
|
|
|
|
|
|
// Reset to read again
|
|
|
|
req_pos = 0;
|
|
|
|
has_header = false;
|
|
|
|
|
|
|
|
// Response
|
|
|
|
String output = GDScriptLanguageProtocol::get_singleton()->process_message(msg);
|
2020-12-15 20:04:21 +08:00
|
|
|
if (!output.is_empty()) {
|
2020-02-02 03:11:33 +08:00
|
|
|
res_queue.push_back(output.utf8());
|
2019-06-14 22:38:54 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 03:11:33 +08:00
|
|
|
return OK;
|
2019-06-14 22:38:54 +08:00
|
|
|
}
|
|
|
|
|
2020-02-02 03:11:33 +08:00
|
|
|
Error GDScriptLanguageProtocol::LSPeer::send_data() {
|
|
|
|
int sent = 0;
|
2020-12-15 20:04:21 +08:00
|
|
|
if (!res_queue.is_empty()) {
|
2020-02-02 03:11:33 +08:00
|
|
|
CharString c_res = res_queue[0];
|
|
|
|
if (res_sent < c_res.size()) {
|
|
|
|
Error err = connection->put_partial_data((const uint8_t *)c_res.get_data() + res_sent, c_res.size() - res_sent - 1, sent);
|
|
|
|
if (err != OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
res_sent += sent;
|
|
|
|
}
|
|
|
|
// Response sent
|
|
|
|
if (res_sent >= c_res.size() - 1) {
|
|
|
|
res_sent = 0;
|
2021-07-04 06:17:03 +08:00
|
|
|
res_queue.remove_at(0);
|
2020-02-02 03:11:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error GDScriptLanguageProtocol::on_client_connected() {
|
|
|
|
Ref<StreamPeerTCP> tcp_peer = server->take_connection();
|
|
|
|
ERR_FAIL_COND_V_MSG(clients.size() >= LSP_MAX_CLIENTS, FAILED, "Max client limits reached");
|
2020-02-24 01:16:24 +08:00
|
|
|
Ref<LSPeer> peer = memnew(LSPeer);
|
2020-02-02 03:11:33 +08:00
|
|
|
peer->connection = tcp_peer;
|
2022-05-08 16:09:19 +08:00
|
|
|
clients.insert(next_client_id, peer);
|
2020-02-02 03:11:33 +08:00
|
|
|
next_client_id++;
|
2021-06-05 02:39:38 +08:00
|
|
|
EditorNode::get_log()->add_message("[LSP] Connection Taken", EditorLog::MSG_TYPE_EDITOR);
|
2020-02-02 03:11:33 +08:00
|
|
|
return OK;
|
2019-06-14 22:38:54 +08:00
|
|
|
}
|
|
|
|
|
2020-02-24 01:16:24 +08:00
|
|
|
void GDScriptLanguageProtocol::on_client_disconnected(const int &p_client_id) {
|
2020-02-02 03:11:33 +08:00
|
|
|
clients.erase(p_client_id);
|
2021-06-05 02:39:38 +08:00
|
|
|
EditorNode::get_log()->add_message("[LSP] Disconnected", EditorLog::MSG_TYPE_EDITOR);
|
2019-06-14 22:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
String GDScriptLanguageProtocol::process_message(const String &p_text) {
|
|
|
|
String ret = process_string(p_text);
|
2020-12-15 20:04:21 +08:00
|
|
|
if (ret.is_empty()) {
|
2019-06-14 22:38:54 +08:00
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return format_output(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String GDScriptLanguageProtocol::format_output(const String &p_text) {
|
|
|
|
String header = "Content-Length: ";
|
|
|
|
CharString charstr = p_text.utf8();
|
|
|
|
size_t len = charstr.length();
|
|
|
|
header += itos(len);
|
|
|
|
header += "\r\n\r\n";
|
|
|
|
|
|
|
|
return header + p_text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptLanguageProtocol::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("initialize", "params"), &GDScriptLanguageProtocol::initialize);
|
|
|
|
ClassDB::bind_method(D_METHOD("initialized", "params"), &GDScriptLanguageProtocol::initialized);
|
|
|
|
ClassDB::bind_method(D_METHOD("on_client_connected"), &GDScriptLanguageProtocol::on_client_connected);
|
|
|
|
ClassDB::bind_method(D_METHOD("on_client_disconnected"), &GDScriptLanguageProtocol::on_client_disconnected);
|
2021-02-18 13:42:54 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("notify_client", "method", "params", "client_id"), &GDScriptLanguageProtocol::notify_client, DEFVAL(Variant()), DEFVAL(-1));
|
2019-06-26 20:21:42 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("is_smart_resolve_enabled"), &GDScriptLanguageProtocol::is_smart_resolve_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_text_document"), &GDScriptLanguageProtocol::get_text_document);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_workspace"), &GDScriptLanguageProtocol::get_workspace);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_initialized"), &GDScriptLanguageProtocol::is_initialized);
|
2019-06-14 22:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) {
|
|
|
|
lsp::InitializeResult ret;
|
2019-06-24 18:25:12 +08:00
|
|
|
|
|
|
|
String root_uri = p_params["rootUri"];
|
|
|
|
String root = p_params["rootPath"];
|
2019-09-23 00:45:08 +08:00
|
|
|
bool is_same_workspace;
|
|
|
|
#ifndef WINDOWS_ENABLED
|
2019-06-26 20:21:42 +08:00
|
|
|
is_same_workspace = root.to_lower() == workspace->root.to_lower();
|
2019-09-23 00:45:08 +08:00
|
|
|
#else
|
2019-06-26 20:21:42 +08:00
|
|
|
is_same_workspace = root.replace("\\", "/").to_lower() == workspace->root.to_lower();
|
2019-06-24 18:25:12 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (root_uri.length() && is_same_workspace) {
|
2019-06-26 20:21:42 +08:00
|
|
|
workspace->root_uri = root_uri;
|
2019-06-24 18:25:12 +08:00
|
|
|
} else {
|
2022-08-08 18:02:31 +08:00
|
|
|
String r_root = workspace->root;
|
|
|
|
r_root = r_root.lstrip("/");
|
|
|
|
workspace->root_uri = "file:///" + r_root;
|
2019-06-25 18:09:42 +08:00
|
|
|
|
|
|
|
Dictionary params;
|
2019-06-26 20:21:42 +08:00
|
|
|
params["path"] = workspace->root;
|
2020-06-08 17:00:07 +08:00
|
|
|
Dictionary request = make_notification("gdscript_client/changeWorkspace", params);
|
2020-02-02 03:11:33 +08:00
|
|
|
|
2020-06-15 15:51:53 +08:00
|
|
|
ERR_FAIL_COND_V_MSG(!clients.has(latest_client_id), ret.to_json(),
|
2020-06-08 17:00:07 +08:00
|
|
|
vformat("GDScriptLanguageProtocol: Can't initialize invalid peer '%d'.", latest_client_id));
|
2020-02-02 03:11:33 +08:00
|
|
|
Ref<LSPeer> peer = clients.get(latest_client_id);
|
2020-04-02 07:20:12 +08:00
|
|
|
if (peer != nullptr) {
|
2020-12-30 02:12:33 +08:00
|
|
|
String msg = Variant(request).to_json_string();
|
2019-06-25 18:09:42 +08:00
|
|
|
msg = format_output(msg);
|
2020-02-02 03:11:33 +08:00
|
|
|
(*peer)->res_queue.push_back(msg.utf8());
|
2019-06-25 18:09:42 +08:00
|
|
|
}
|
2019-06-24 18:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_initialized) {
|
2019-06-26 20:21:42 +08:00
|
|
|
workspace->initialize();
|
|
|
|
text_document->initialize();
|
2019-06-24 18:25:12 +08:00
|
|
|
_initialized = true;
|
|
|
|
}
|
|
|
|
|
2019-06-14 22:38:54 +08:00
|
|
|
return ret.to_json();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptLanguageProtocol::initialized(const Variant &p_params) {
|
2019-10-09 13:29:29 +08:00
|
|
|
lsp::GodotCapabilities capabilities;
|
|
|
|
|
2020-11-29 11:42:06 +08:00
|
|
|
DocTools *doc = EditorHelp::get_doc_data();
|
2021-08-10 04:13:42 +08:00
|
|
|
for (const KeyValue<String, DocData::ClassDoc> &E : doc->class_list) {
|
2019-10-09 13:29:29 +08:00
|
|
|
lsp::GodotNativeClassInfo gdclass;
|
2021-08-10 04:13:42 +08:00
|
|
|
gdclass.name = E.value.name;
|
|
|
|
gdclass.class_doc = &(E.value);
|
|
|
|
if (ClassDB::ClassInfo *ptr = ClassDB::classes.getptr(StringName(E.value.name))) {
|
2019-10-09 13:29:29 +08:00
|
|
|
gdclass.class_info = ptr;
|
|
|
|
}
|
|
|
|
capabilities.native_classes.push_back(gdclass);
|
|
|
|
}
|
|
|
|
|
|
|
|
notify_client("gdscript/capabilities", capabilities.to_json());
|
2019-06-14 22:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptLanguageProtocol::poll() {
|
2020-02-02 03:11:33 +08:00
|
|
|
if (server->is_connection_available()) {
|
|
|
|
on_client_connected();
|
|
|
|
}
|
2022-05-08 16:09:19 +08:00
|
|
|
|
|
|
|
HashMap<int, Ref<LSPeer>>::Iterator E = clients.begin();
|
|
|
|
while (E != clients.end()) {
|
|
|
|
Ref<LSPeer> peer = E->value;
|
2023-04-09 19:36:01 +08:00
|
|
|
peer->connection->poll();
|
2020-02-02 03:11:33 +08:00
|
|
|
StreamPeerTCP::Status status = peer->connection->get_status();
|
|
|
|
if (status == StreamPeerTCP::STATUS_NONE || status == StreamPeerTCP::STATUS_ERROR) {
|
2022-05-08 16:09:19 +08:00
|
|
|
on_client_disconnected(E->key);
|
|
|
|
E = clients.begin();
|
|
|
|
continue;
|
2020-02-02 03:11:33 +08:00
|
|
|
} else {
|
|
|
|
if (peer->connection->get_available_bytes() > 0) {
|
2022-05-08 16:09:19 +08:00
|
|
|
latest_client_id = E->key;
|
2020-02-02 03:11:33 +08:00
|
|
|
Error err = peer->handle_data();
|
|
|
|
if (err != OK && err != ERR_BUSY) {
|
2022-05-08 16:09:19 +08:00
|
|
|
on_client_disconnected(E->key);
|
|
|
|
E = clients.begin();
|
|
|
|
continue;
|
2020-02-02 03:11:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Error err = peer->send_data();
|
|
|
|
if (err != OK && err != ERR_BUSY) {
|
2022-05-08 16:09:19 +08:00
|
|
|
on_client_disconnected(E->key);
|
|
|
|
E = clients.begin();
|
|
|
|
continue;
|
2020-02-02 03:11:33 +08:00
|
|
|
}
|
|
|
|
}
|
2022-05-08 16:09:19 +08:00
|
|
|
++E;
|
2020-02-02 03:11:33 +08:00
|
|
|
}
|
2019-06-14 22:38:54 +08:00
|
|
|
}
|
|
|
|
|
2021-05-06 08:48:18 +08:00
|
|
|
Error GDScriptLanguageProtocol::start(int p_port, const IPAddress &p_bind_ip) {
|
2020-02-02 03:11:33 +08:00
|
|
|
return server->listen(p_port, p_bind_ip);
|
2019-06-14 22:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptLanguageProtocol::stop() {
|
2022-05-08 16:09:19 +08:00
|
|
|
for (const KeyValue<int, Ref<LSPeer>> &E : clients) {
|
|
|
|
Ref<LSPeer> peer = clients.get(E.key);
|
2020-02-02 03:11:33 +08:00
|
|
|
peer->connection->disconnect_from_host();
|
2019-10-04 19:15:26 +08:00
|
|
|
}
|
2019-06-14 22:38:54 +08:00
|
|
|
|
2020-02-02 03:11:33 +08:00
|
|
|
server->stop();
|
2019-06-14 22:38:54 +08:00
|
|
|
}
|
|
|
|
|
2020-02-02 03:11:33 +08:00
|
|
|
void GDScriptLanguageProtocol::notify_client(const String &p_method, const Variant &p_params, int p_client_id) {
|
|
|
|
if (p_client_id == -1) {
|
2020-06-08 17:00:07 +08:00
|
|
|
ERR_FAIL_COND_MSG(latest_client_id == -1,
|
|
|
|
"GDScript LSP: Can't notify client as none was connected.");
|
2020-02-02 03:11:33 +08:00
|
|
|
p_client_id = latest_client_id;
|
2019-06-14 22:38:54 +08:00
|
|
|
}
|
2020-06-15 15:51:53 +08:00
|
|
|
ERR_FAIL_COND(!clients.has(p_client_id));
|
2020-02-02 03:11:33 +08:00
|
|
|
Ref<LSPeer> peer = clients.get(p_client_id);
|
2020-04-02 07:20:12 +08:00
|
|
|
ERR_FAIL_COND(peer == nullptr);
|
2019-06-14 22:38:54 +08:00
|
|
|
|
|
|
|
Dictionary message = make_notification(p_method, p_params);
|
2020-12-30 02:12:33 +08:00
|
|
|
String msg = Variant(message).to_json_string();
|
2019-06-14 22:38:54 +08:00
|
|
|
msg = format_output(msg);
|
2020-02-02 03:11:33 +08:00
|
|
|
peer->res_queue.push_back(msg.utf8());
|
2019-06-14 22:38:54 +08:00
|
|
|
}
|
|
|
|
|
2021-07-12 03:51:31 +08:00
|
|
|
void GDScriptLanguageProtocol::request_client(const String &p_method, const Variant &p_params, int p_client_id) {
|
|
|
|
if (p_client_id == -1) {
|
|
|
|
ERR_FAIL_COND_MSG(latest_client_id == -1,
|
|
|
|
"GDScript LSP: Can't notify client as none was connected.");
|
|
|
|
p_client_id = latest_client_id;
|
|
|
|
}
|
|
|
|
ERR_FAIL_COND(!clients.has(p_client_id));
|
|
|
|
Ref<LSPeer> peer = clients.get(p_client_id);
|
|
|
|
ERR_FAIL_COND(peer == nullptr);
|
|
|
|
|
|
|
|
Dictionary message = make_request(p_method, p_params, next_server_id);
|
|
|
|
next_server_id++;
|
|
|
|
String msg = Variant(message).to_json_string();
|
|
|
|
msg = format_output(msg);
|
|
|
|
peer->res_queue.push_back(msg.utf8());
|
|
|
|
}
|
|
|
|
|
2019-06-23 21:10:28 +08:00
|
|
|
bool GDScriptLanguageProtocol::is_smart_resolve_enabled() const {
|
|
|
|
return bool(_EDITOR_GET("network/language_server/enable_smart_resolve"));
|
|
|
|
}
|
|
|
|
|
2019-06-30 16:10:13 +08:00
|
|
|
bool GDScriptLanguageProtocol::is_goto_native_symbols_enabled() const {
|
|
|
|
return bool(_EDITOR_GET("network/language_server/show_native_symbols_in_editor"));
|
|
|
|
}
|
|
|
|
|
2019-06-14 22:38:54 +08:00
|
|
|
GDScriptLanguageProtocol::GDScriptLanguageProtocol() {
|
2021-06-18 06:03:09 +08:00
|
|
|
server.instantiate();
|
2019-06-14 22:38:54 +08:00
|
|
|
singleton = this;
|
2021-06-18 06:03:09 +08:00
|
|
|
workspace.instantiate();
|
|
|
|
text_document.instantiate();
|
2019-06-26 20:21:42 +08:00
|
|
|
set_scope("textDocument", text_document.ptr());
|
|
|
|
set_scope("completionItem", text_document.ptr());
|
|
|
|
set_scope("workspace", workspace.ptr());
|
|
|
|
workspace->root = ProjectSettings::get_singleton()->get_resource_path();
|
2019-06-14 22:38:54 +08:00
|
|
|
}
|