From f58560ac361fbe0fcc38df6a8f80818e55517aef Mon Sep 17 00:00:00 2001 From: geequlim Date: Fri, 14 Jun 2019 22:38:54 +0800 Subject: Add GDScript Language Protocol plugin --- .../language_server/gdscript_language_protocol.cpp | 173 +++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 modules/gdscript/language_server/gdscript_language_protocol.cpp (limited to 'modules/gdscript/language_server/gdscript_language_protocol.cpp') diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp new file mode 100644 index 0000000000..b4c4d7f236 --- /dev/null +++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp @@ -0,0 +1,173 @@ +/*************************************************************************/ +/* gdscript_language_protocol.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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. */ +/*************************************************************************/ + +#include "gdscript_language_protocol.h" +#include "core/io/json.h" +#include "core/os/copymem.h" +#include "core/project_settings.h" + +GDScriptLanguageProtocol *GDScriptLanguageProtocol::singleton = NULL; + +void GDScriptLanguageProtocol::on_data_received(int p_id) { + lastest_client_id = p_id; + Ref peer = server->get_peer(p_id); + PoolByteArray data; + if (OK == peer->get_packet_buffer(data)) { + String message; + message.parse_utf8((const char *)data.read().ptr(), data.size()); + if (message.begins_with("Content-Length:")) return; + String output = process_message(message); + if (!output.empty()) { + CharString charstr = output.utf8(); + peer->put_packet((const uint8_t *)charstr.ptr(), charstr.length()); + } + } +} + +void GDScriptLanguageProtocol::on_client_connected(int p_id, const String &p_protocal) { + clients.set(p_id, server->get_peer(p_id)); +} + +void GDScriptLanguageProtocol::on_client_disconnected(int p_id, bool p_was_clean_close) { + clients.erase(p_id); +} + +String GDScriptLanguageProtocol::process_message(const String &p_text) { + String ret = process_string(p_text); + if (ret.empty()) { + 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_data_received"), &GDScriptLanguageProtocol::on_data_received); + ClassDB::bind_method(D_METHOD("on_client_connected"), &GDScriptLanguageProtocol::on_client_connected); + ClassDB::bind_method(D_METHOD("on_client_disconnected"), &GDScriptLanguageProtocol::on_client_disconnected); +} + +Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) { + + lsp::InitializeResult ret; + + return ret.to_json(); +} + +void GDScriptLanguageProtocol::initialized(const Variant &p_params) { + + Dictionary params; + params["type"] = 3; + params["message"] = "GDScript Language Server initialized!"; + Dictionary test_message = make_notification("window/showMessage", params); + + if (Ref *peer = clients.getptr(lastest_client_id)) { + String msg = JSON::print(test_message); + msg = format_output(msg); + CharString charstr = msg.utf8(); + (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length()); + } +} + +void GDScriptLanguageProtocol::poll() { + server->poll(); +} + +Error GDScriptLanguageProtocol::start(int p_port) { + if (server == NULL) { + server = dynamic_cast(ClassDB::instance("WebSocketServer")); + server->set_buffers(8192, 1024, 8192, 1024); // 8mb should be way more than enough + server->connect("data_received", this, "on_data_received"); + server->connect("client_connected", this, "on_client_connected"); + server->connect("client_disconnected", this, "on_client_disconnected"); + } + return server->listen(p_port); +} + +void GDScriptLanguageProtocol::stop() { + server->stop(); +} + +void GDScriptLanguageProtocol::notify_all_clients(const String &p_method, const Variant &p_params) { + + Dictionary message = make_notification(p_method, p_params); + String msg = JSON::print(message); + msg = format_output(msg); + CharString charstr = msg.utf8(); + const int *p_id = clients.next(NULL); + while (p_id != NULL) { + Ref peer = clients.get(*p_id); + (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length()); + p_id = clients.next(p_id); + } +} + +void GDScriptLanguageProtocol::notify_client(const String &p_method, const Variant &p_params, int p_client) { + + if (p_client == -1) { + p_client = lastest_client_id; + } + + Ref *peer = clients.getptr(p_client); + ERR_FAIL_COND(peer == NULL); + + Dictionary message = make_notification(p_method, p_params); + String msg = JSON::print(message); + msg = format_output(msg); + CharString charstr = msg.utf8(); + + (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length()); +} + +GDScriptLanguageProtocol::GDScriptLanguageProtocol() { + server = NULL; + singleton = this; + set_scope("textDocument", &text_document); + set_scope("workspace", &workspace); + workspace.root = ProjectSettings::get_singleton()->get_resource_path(); +} + +GDScriptLanguageProtocol::~GDScriptLanguageProtocol() { + memdelete(server); + server = NULL; +} -- cgit v1.2.3 From 37aafaaa9cc7d66c85fd9395e46b2386d899ba12 Mon Sep 17 00:00:00 2001 From: geequlim Date: Sun, 23 Jun 2019 01:48:31 +0800 Subject: Add a symbol pool to cache all native symbols and workspackes symbols. Implement hover Implement completion documentation resolve Implement hover documentation Implement go to definition --- modules/gdscript/language_server/gdscript_language_protocol.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'modules/gdscript/language_server/gdscript_language_protocol.cpp') diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp index b4c4d7f236..7f74e68a94 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.cpp +++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp @@ -90,7 +90,7 @@ void GDScriptLanguageProtocol::_bind_methods() { Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) { lsp::InitializeResult ret; - + workspace.initialize(); return ret.to_json(); } @@ -163,6 +163,7 @@ GDScriptLanguageProtocol::GDScriptLanguageProtocol() { server = NULL; singleton = this; set_scope("textDocument", &text_document); + set_scope("completionItem", &text_document); set_scope("workspace", &workspace); workspace.root = ProjectSettings::get_singleton()->get_resource_path(); } -- cgit v1.2.3 From fa6d6a329c93224b5454b17603284913da0472a3 Mon Sep 17 00:00:00 2001 From: geequlim Date: Sun, 23 Jun 2019 21:10:28 +0800 Subject: Add optional smart resolve sulotion The smart resolvaion can guess most symbols but it might be slow so disabled by default users can turn on it in the editor setting --- modules/gdscript/language_server/gdscript_language_protocol.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'modules/gdscript/language_server/gdscript_language_protocol.cpp') diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp index 7f74e68a94..7c24efe450 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.cpp +++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp @@ -32,6 +32,7 @@ #include "core/io/json.h" #include "core/os/copymem.h" #include "core/project_settings.h" +#include "editor/editor_node.h" GDScriptLanguageProtocol *GDScriptLanguageProtocol::singleton = NULL; @@ -159,6 +160,10 @@ void GDScriptLanguageProtocol::notify_client(const String &p_method, const Varia (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length()); } +bool GDScriptLanguageProtocol::is_smart_resolve_enabled() const { + return bool(_EDITOR_GET("network/language_server/enable_smart_resolve")); +} + GDScriptLanguageProtocol::GDScriptLanguageProtocol() { server = NULL; singleton = this; -- cgit v1.2.3 From 76c9e4ceb73b02bd95ab0512e27229516208dc60 Mon Sep 17 00:00:00 2001 From: Geequlim Date: Mon, 24 Jun 2019 18:25:12 +0800 Subject: Improved performance for completion and symbol resolvation. Improved uri and workspace path translatation on windows platform. The smart resolvation is much faster than builtin's in the server side. The smart resolve mode is still disabled as default as the clients might be slow with a planty of completion items. --- .../language_server/gdscript_language_protocol.cpp | 23 +++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'modules/gdscript/language_server/gdscript_language_protocol.cpp') diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp index 7c24efe450..7fb336cc58 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.cpp +++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp @@ -91,7 +91,27 @@ void GDScriptLanguageProtocol::_bind_methods() { Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) { lsp::InitializeResult ret; - workspace.initialize(); + + String root_uri = p_params["rootUri"]; + String root = p_params["rootPath"]; + bool is_same_workspace = root == workspace.root; + is_same_workspace = root.to_lower() == workspace.root.to_lower(); +#ifdef WINDOWS_ENABLED + is_same_workspace = root.replace("\\", "/").to_lower() == workspace.root.to_lower(); +#endif + + if (root_uri.length() && is_same_workspace) { + workspace.root_uri = root_uri; + } else { + workspace.root_uri = "file://" + workspace.root; + } + + if (!_initialized) { + workspace.initialize(); + text_document.initialize(); + _initialized = true; + } + return ret.to_json(); } @@ -167,6 +187,7 @@ bool GDScriptLanguageProtocol::is_smart_resolve_enabled() const { GDScriptLanguageProtocol::GDScriptLanguageProtocol() { server = NULL; singleton = this; + _initialized = false; set_scope("textDocument", &text_document); set_scope("completionItem", &text_document); set_scope("workspace", &workspace); -- cgit v1.2.3 From 9618b0c63e3330865350bd8bbc6a9d2faf9dd26c Mon Sep 17 00:00:00 2001 From: Geequlim Date: Tue, 25 Jun 2019 18:09:42 +0800 Subject: Check client workspace directory is valid Drop test initialize message sent to client Remove unused code property for the parser class --- .../language_server/gdscript_language_protocol.cpp | 23 +++++++++++----------- 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'modules/gdscript/language_server/gdscript_language_protocol.cpp') diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp index 7fb336cc58..9ebabc276e 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.cpp +++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp @@ -103,7 +103,18 @@ Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) { if (root_uri.length() && is_same_workspace) { workspace.root_uri = root_uri; } else { + workspace.root_uri = "file://" + workspace.root; + + Dictionary params; + params["path"] = workspace.root; + Dictionary request = make_notification("gdscrip_client/changeWorkspace", params); + if (Ref *peer = clients.getptr(lastest_client_id)) { + String msg = JSON::print(request); + msg = format_output(msg); + CharString charstr = msg.utf8(); + (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length()); + } } if (!_initialized) { @@ -116,18 +127,6 @@ Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) { } void GDScriptLanguageProtocol::initialized(const Variant &p_params) { - - Dictionary params; - params["type"] = 3; - params["message"] = "GDScript Language Server initialized!"; - Dictionary test_message = make_notification("window/showMessage", params); - - if (Ref *peer = clients.getptr(lastest_client_id)) { - String msg = JSON::print(test_message); - msg = format_output(msg); - CharString charstr = msg.utf8(); - (*peer)->put_packet((const uint8_t *)charstr.ptr(), charstr.length()); - } } void GDScriptLanguageProtocol::poll() { -- cgit v1.2.3 From 666ed89011551ae7691c8eeeb3fff74e17b48020 Mon Sep 17 00:00:00 2001 From: Geequlim Date: Wed, 26 Jun 2019 20:21:42 +0800 Subject: Add generate script api to dictionary support Expose GDScriptLanguageProtocol singleton and classes for editor plugins (Not visiable in class tree) Fix minor bug in symbol resolve --- .../language_server/gdscript_language_protocol.cpp | 32 ++++++++++++++-------- 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'modules/gdscript/language_server/gdscript_language_protocol.cpp') diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp index 9ebabc276e..98b5c28130 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.cpp +++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp @@ -86,6 +86,12 @@ void GDScriptLanguageProtocol::_bind_methods() { ClassDB::bind_method(D_METHOD("on_data_received"), &GDScriptLanguageProtocol::on_data_received); ClassDB::bind_method(D_METHOD("on_client_connected"), &GDScriptLanguageProtocol::on_client_connected); ClassDB::bind_method(D_METHOD("on_client_disconnected"), &GDScriptLanguageProtocol::on_client_disconnected); + ClassDB::bind_method(D_METHOD("notify_all_clients", "p_method", "p_params"), &GDScriptLanguageProtocol::notify_all_clients, DEFVAL(Variant())); + ClassDB::bind_method(D_METHOD("notify_client", "p_method", "p_params", "p_client"), &GDScriptLanguageProtocol::notify_client, DEFVAL(Variant()), DEFVAL(-1)); + 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); } Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) { @@ -94,20 +100,20 @@ Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) { String root_uri = p_params["rootUri"]; String root = p_params["rootPath"]; - bool is_same_workspace = root == workspace.root; - is_same_workspace = root.to_lower() == workspace.root.to_lower(); + bool is_same_workspace = root == workspace->root; + is_same_workspace = root.to_lower() == workspace->root.to_lower(); #ifdef WINDOWS_ENABLED - is_same_workspace = root.replace("\\", "/").to_lower() == workspace.root.to_lower(); + is_same_workspace = root.replace("\\", "/").to_lower() == workspace->root.to_lower(); #endif if (root_uri.length() && is_same_workspace) { - workspace.root_uri = root_uri; + workspace->root_uri = root_uri; } else { - workspace.root_uri = "file://" + workspace.root; + workspace->root_uri = "file://" + workspace->root; Dictionary params; - params["path"] = workspace.root; + params["path"] = workspace->root; Dictionary request = make_notification("gdscrip_client/changeWorkspace", params); if (Ref *peer = clients.getptr(lastest_client_id)) { String msg = JSON::print(request); @@ -118,8 +124,8 @@ Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) { } if (!_initialized) { - workspace.initialize(); - text_document.initialize(); + workspace->initialize(); + text_document->initialize(); _initialized = true; } @@ -187,10 +193,12 @@ GDScriptLanguageProtocol::GDScriptLanguageProtocol() { server = NULL; singleton = this; _initialized = false; - set_scope("textDocument", &text_document); - set_scope("completionItem", &text_document); - set_scope("workspace", &workspace); - workspace.root = ProjectSettings::get_singleton()->get_resource_path(); + workspace.instance(); + text_document.instance(); + 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(); } GDScriptLanguageProtocol::~GDScriptLanguageProtocol() { -- cgit v1.2.3 From 72d11cd17355585bd3f2b6d467ebb45ad55b6759 Mon Sep 17 00:00:00 2001 From: geequlim Date: Sun, 30 Jun 2019 16:10:13 +0800 Subject: Add optional goto definition support for native symbols This action will show help for target symbol in godot editor and bring the godot editor window to foreground Improved markdown documentation for symbols. --- modules/gdscript/language_server/gdscript_language_protocol.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'modules/gdscript/language_server/gdscript_language_protocol.cpp') diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp index 98b5c28130..afe461b68e 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.cpp +++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp @@ -189,6 +189,10 @@ bool GDScriptLanguageProtocol::is_smart_resolve_enabled() const { return bool(_EDITOR_GET("network/language_server/enable_smart_resolve")); } +bool GDScriptLanguageProtocol::is_goto_native_symbols_enabled() const { + return bool(_EDITOR_GET("network/language_server/show_native_symbols_in_editor")); +} + GDScriptLanguageProtocol::GDScriptLanguageProtocol() { server = NULL; singleton = this; -- cgit v1.2.3