initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
1056
modules/gdscript/language_server/gdscript_extend_parser.cpp
Normal file
1056
modules/gdscript/language_server/gdscript_extend_parser.cpp
Normal file
File diff suppressed because it is too large
Load Diff
170
modules/gdscript/language_server/gdscript_extend_parser.h
Normal file
170
modules/gdscript/language_server/gdscript_extend_parser.h
Normal file
@@ -0,0 +1,170 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_extend_parser.h */
|
||||
/**************************************************************************/
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../gdscript_parser.h"
|
||||
#include "godot_lsp.h"
|
||||
|
||||
#include "core/variant/variant.h"
|
||||
|
||||
#ifndef LINE_NUMBER_TO_INDEX
|
||||
#define LINE_NUMBER_TO_INDEX(p_line) ((p_line) - 1)
|
||||
#endif
|
||||
#ifndef COLUMN_NUMBER_TO_INDEX
|
||||
#define COLUMN_NUMBER_TO_INDEX(p_column) ((p_column) - 1)
|
||||
#endif
|
||||
|
||||
#ifndef SYMBOL_SEPARATOR
|
||||
#define SYMBOL_SEPARATOR "::"
|
||||
#endif
|
||||
|
||||
#ifndef JOIN_SYMBOLS
|
||||
#define JOIN_SYMBOLS(p_path, name) ((p_path) + SYMBOL_SEPARATOR + (name))
|
||||
#endif
|
||||
|
||||
typedef HashMap<String, const LSP::DocumentSymbol *> ClassMembers;
|
||||
|
||||
/**
|
||||
* Represents a Position as used by GDScript Parser. Used for conversion to and from `LSP::Position`.
|
||||
*
|
||||
* Difference to `LSP::Position`:
|
||||
* * Line & Char/column: 1-based
|
||||
* * LSP: both 0-based
|
||||
* * Tabs are expanded to columns using tab size (`text_editor/behavior/indent/size`).
|
||||
* * LSP: tab is single char
|
||||
*
|
||||
* Example:
|
||||
* ```gdscript
|
||||
* →→var my_value = 42
|
||||
* ```
|
||||
* `_` is at:
|
||||
* * Godot: `column=12`
|
||||
* * using `indent/size=4`
|
||||
* * Note: counting starts at `1`
|
||||
* * LSP: `character=8`
|
||||
* * Note: counting starts at `0`
|
||||
*/
|
||||
struct GodotPosition {
|
||||
int line;
|
||||
int column;
|
||||
|
||||
GodotPosition(int p_line, int p_column) :
|
||||
line(p_line), column(p_column) {}
|
||||
|
||||
LSP::Position to_lsp(const Vector<String> &p_lines) const;
|
||||
static GodotPosition from_lsp(const LSP::Position p_pos, const Vector<String> &p_lines);
|
||||
|
||||
bool operator==(const GodotPosition &p_other) const {
|
||||
return line == p_other.line && column == p_other.column;
|
||||
}
|
||||
|
||||
String to_string() const {
|
||||
return vformat("(%d,%d)", line, column);
|
||||
}
|
||||
};
|
||||
|
||||
struct GodotRange {
|
||||
GodotPosition start;
|
||||
GodotPosition end;
|
||||
|
||||
GodotRange(GodotPosition p_start, GodotPosition p_end) :
|
||||
start(p_start), end(p_end) {}
|
||||
|
||||
LSP::Range to_lsp(const Vector<String> &p_lines) const;
|
||||
static GodotRange from_lsp(const LSP::Range &p_range, const Vector<String> &p_lines);
|
||||
|
||||
bool operator==(const GodotRange &p_other) const {
|
||||
return start == p_other.start && end == p_other.end;
|
||||
}
|
||||
|
||||
String to_string() const {
|
||||
return vformat("[%s:%s]", start.to_string(), end.to_string());
|
||||
}
|
||||
};
|
||||
|
||||
class ExtendGDScriptParser : public GDScriptParser {
|
||||
String path;
|
||||
Vector<String> lines;
|
||||
|
||||
LSP::DocumentSymbol class_symbol;
|
||||
Vector<LSP::Diagnostic> diagnostics;
|
||||
List<LSP::DocumentLink> document_links;
|
||||
ClassMembers members;
|
||||
HashMap<String, ClassMembers> inner_classes;
|
||||
|
||||
LSP::Range range_of_node(const GDScriptParser::Node *p_node) const;
|
||||
|
||||
void update_diagnostics();
|
||||
|
||||
void update_symbols();
|
||||
void update_document_links(const String &p_code);
|
||||
void parse_class_symbol(const GDScriptParser::ClassNode *p_class, LSP::DocumentSymbol &r_symbol);
|
||||
void parse_function_symbol(const GDScriptParser::FunctionNode *p_func, LSP::DocumentSymbol &r_symbol);
|
||||
|
||||
Dictionary dump_function_api(const GDScriptParser::FunctionNode *p_func) const;
|
||||
Dictionary dump_class_api(const GDScriptParser::ClassNode *p_class) const;
|
||||
|
||||
const LSP::DocumentSymbol *search_symbol_defined_at_line(int p_line, const LSP::DocumentSymbol &p_parent, const String &p_symbol_name = "") const;
|
||||
|
||||
Array member_completions;
|
||||
|
||||
public:
|
||||
_FORCE_INLINE_ const String &get_path() const { return path; }
|
||||
_FORCE_INLINE_ const Vector<String> &get_lines() const { return lines; }
|
||||
_FORCE_INLINE_ const LSP::DocumentSymbol &get_symbols() const { return class_symbol; }
|
||||
_FORCE_INLINE_ const Vector<LSP::Diagnostic> &get_diagnostics() const { return diagnostics; }
|
||||
_FORCE_INLINE_ const ClassMembers &get_members() const { return members; }
|
||||
_FORCE_INLINE_ const HashMap<String, ClassMembers> &get_inner_classes() const { return inner_classes; }
|
||||
|
||||
Error get_left_function_call(const LSP::Position &p_position, LSP::Position &r_func_pos, int &r_arg_index) const;
|
||||
|
||||
String get_text_for_completion(const LSP::Position &p_cursor) const;
|
||||
String get_text_for_lookup_symbol(const LSP::Position &p_cursor, const String &p_symbol = "", bool p_func_required = false) const;
|
||||
String get_identifier_under_position(const LSP::Position &p_position, LSP::Range &r_range) const;
|
||||
String get_uri() const;
|
||||
|
||||
/**
|
||||
* `p_symbol_name` gets ignored if empty. Otherwise symbol must match passed in named.
|
||||
*
|
||||
* Necessary when multiple symbols at same line for example with `func`:
|
||||
* `func handle_arg(arg: int):`
|
||||
* -> Without `p_symbol_name`: returns `handle_arg`. Even if parameter (`arg`) is wanted.
|
||||
* With `p_symbol_name`: symbol name MUST match `p_symbol_name`: returns `arg`.
|
||||
*/
|
||||
const LSP::DocumentSymbol *get_symbol_defined_at_line(int p_line, const String &p_symbol_name = "") const;
|
||||
const LSP::DocumentSymbol *get_member_symbol(const String &p_name, const String &p_subclass = "") const;
|
||||
const List<LSP::DocumentLink> &get_document_links() const;
|
||||
|
||||
const Array &get_member_completions();
|
||||
Dictionary generate_api() const;
|
||||
|
||||
Error parse(const String &p_code, const String &p_path);
|
||||
};
|
405
modules/gdscript/language_server/gdscript_language_protocol.cpp
Normal file
405
modules/gdscript/language_server/gdscript_language_protocol.cpp
Normal file
@@ -0,0 +1,405 @@
|
||||
/**************************************************************************/
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "gdscript_language_protocol.h"
|
||||
|
||||
#include "core/config/project_settings.h"
|
||||
#include "editor/doc/doc_tools.h"
|
||||
#include "editor/doc/editor_help.h"
|
||||
#include "editor/editor_log.h"
|
||||
#include "editor/editor_node.h"
|
||||
#include "editor/settings/editor_settings.h"
|
||||
|
||||
GDScriptLanguageProtocol *GDScriptLanguageProtocol::singleton = nullptr;
|
||||
|
||||
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;
|
||||
ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Response header too big");
|
||||
}
|
||||
Error err = connection->get_partial_data(&req_buf[req_pos], 1, read);
|
||||
if (err != OK) {
|
||||
return FAILED;
|
||||
} else if (read != 1) { // Busy, wait until next poll
|
||||
return ERR_BUSY;
|
||||
}
|
||||
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 = String::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);
|
||||
if (err != OK) {
|
||||
return FAILED;
|
||||
} else if (read != 1) {
|
||||
return ERR_BUSY;
|
||||
}
|
||||
req_pos++;
|
||||
}
|
||||
|
||||
// Parse data
|
||||
String msg = String::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);
|
||||
if (!output.is_empty()) {
|
||||
res_queue.push_back(output.utf8());
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error GDScriptLanguageProtocol::LSPeer::send_data() {
|
||||
int sent = 0;
|
||||
while (!res_queue.is_empty()) {
|
||||
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;
|
||||
res_queue.remove_at(0);
|
||||
}
|
||||
}
|
||||
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");
|
||||
Ref<LSPeer> peer = memnew(LSPeer);
|
||||
peer->connection = tcp_peer;
|
||||
clients.insert(next_client_id, peer);
|
||||
next_client_id++;
|
||||
EditorNode::get_log()->add_message("[LSP] Connection Taken", EditorLog::MSG_TYPE_EDITOR);
|
||||
return OK;
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::on_client_disconnected(const int &p_client_id) {
|
||||
clients.erase(p_client_id);
|
||||
EditorNode::get_log()->add_message("[LSP] Disconnected", EditorLog::MSG_TYPE_EDITOR);
|
||||
}
|
||||
|
||||
String GDScriptLanguageProtocol::process_message(const String &p_text) {
|
||||
String ret = process_string(p_text);
|
||||
if (ret.is_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_client_connected"), &GDScriptLanguageProtocol::on_client_connected);
|
||||
ClassDB::bind_method(D_METHOD("on_client_disconnected"), &GDScriptLanguageProtocol::on_client_disconnected);
|
||||
ClassDB::bind_method(D_METHOD("notify_client", "method", "params", "client_id"), &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) {
|
||||
LSP::InitializeResult ret;
|
||||
|
||||
{
|
||||
// Warn if the workspace root does not match with the project that is currently open in Godot,
|
||||
// since it might lead to unexpected behavior, like wrong warnings about duplicate class names.
|
||||
|
||||
String root;
|
||||
Variant root_uri_var = p_params["rootUri"];
|
||||
Variant root_var = p_params["rootPath"];
|
||||
if (root_uri_var.is_string()) {
|
||||
root = get_workspace()->get_file_path(root_uri_var);
|
||||
} else if (root_var.is_string()) {
|
||||
root = root_var;
|
||||
}
|
||||
|
||||
if (ProjectSettings::get_singleton()->localize_path(root) != "res://") {
|
||||
LSP::ShowMessageParams params{
|
||||
LSP::MessageType::Warning,
|
||||
"The GDScript Language Server might not work correctly with other projects than the one opened in Godot."
|
||||
};
|
||||
notify_client("window/showMessage", params.to_json());
|
||||
}
|
||||
}
|
||||
|
||||
String root_uri = p_params["rootUri"];
|
||||
String root = p_params["rootPath"];
|
||||
bool is_same_workspace;
|
||||
#ifndef WINDOWS_ENABLED
|
||||
is_same_workspace = root.to_lower() == workspace->root.to_lower();
|
||||
#else
|
||||
is_same_workspace = root.replace_char('\\', '/').to_lower() == workspace->root.to_lower();
|
||||
#endif
|
||||
|
||||
if (root_uri.length() && is_same_workspace) {
|
||||
workspace->root_uri = root_uri;
|
||||
} else {
|
||||
String r_root = workspace->root;
|
||||
r_root = r_root.lstrip("/");
|
||||
workspace->root_uri = "file:///" + r_root;
|
||||
|
||||
Dictionary params;
|
||||
params["path"] = workspace->root;
|
||||
Dictionary request = make_notification("gdscript_client/changeWorkspace", params);
|
||||
|
||||
ERR_FAIL_COND_V_MSG(!clients.has(latest_client_id), ret.to_json(),
|
||||
vformat("GDScriptLanguageProtocol: Can't initialize invalid peer '%d'.", latest_client_id));
|
||||
Ref<LSPeer> peer = clients.get(latest_client_id);
|
||||
if (peer.is_valid()) {
|
||||
String msg = Variant(request).to_json_string();
|
||||
msg = format_output(msg);
|
||||
(*peer)->res_queue.push_back(msg.utf8());
|
||||
}
|
||||
}
|
||||
|
||||
if (!_initialized) {
|
||||
workspace->initialize();
|
||||
text_document->initialize();
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
return ret.to_json();
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::initialized(const Variant &p_params) {
|
||||
LSP::GodotCapabilities capabilities;
|
||||
|
||||
DocTools *doc = EditorHelp::get_doc_data();
|
||||
for (const KeyValue<String, DocData::ClassDoc> &E : doc->class_list) {
|
||||
LSP::GodotNativeClassInfo gdclass;
|
||||
gdclass.name = E.value.name;
|
||||
gdclass.class_doc = &(E.value);
|
||||
if (ClassDB::ClassInfo *ptr = ClassDB::classes.getptr(StringName(E.value.name))) {
|
||||
gdclass.class_info = ptr;
|
||||
}
|
||||
capabilities.native_classes.push_back(gdclass);
|
||||
}
|
||||
|
||||
notify_client("gdscript/capabilities", capabilities.to_json());
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::poll(int p_limit_usec) {
|
||||
uint64_t target_ticks = OS::get_singleton()->get_ticks_usec() + p_limit_usec;
|
||||
|
||||
if (server->is_connection_available()) {
|
||||
on_client_connected();
|
||||
}
|
||||
|
||||
HashMap<int, Ref<LSPeer>>::Iterator E = clients.begin();
|
||||
while (E != clients.end()) {
|
||||
Ref<LSPeer> peer = E->value;
|
||||
peer->connection->poll();
|
||||
StreamPeerTCP::Status status = peer->connection->get_status();
|
||||
if (status == StreamPeerTCP::STATUS_NONE || status == StreamPeerTCP::STATUS_ERROR) {
|
||||
on_client_disconnected(E->key);
|
||||
E = clients.begin();
|
||||
continue;
|
||||
} else {
|
||||
Error err = OK;
|
||||
while (peer->connection->get_available_bytes() > 0) {
|
||||
latest_client_id = E->key;
|
||||
err = peer->handle_data();
|
||||
if (err != OK || OS::get_singleton()->get_ticks_usec() >= target_ticks) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (err != OK && err != ERR_BUSY) {
|
||||
on_client_disconnected(E->key);
|
||||
E = clients.begin();
|
||||
continue;
|
||||
}
|
||||
|
||||
err = peer->send_data();
|
||||
if (err != OK && err != ERR_BUSY) {
|
||||
on_client_disconnected(E->key);
|
||||
E = clients.begin();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
++E;
|
||||
}
|
||||
}
|
||||
|
||||
Error GDScriptLanguageProtocol::start(int p_port, const IPAddress &p_bind_ip) {
|
||||
return server->listen(p_port, p_bind_ip);
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::stop() {
|
||||
for (const KeyValue<int, Ref<LSPeer>> &E : clients) {
|
||||
Ref<LSPeer> peer = clients.get(E.key);
|
||||
peer->connection->disconnect_from_host();
|
||||
}
|
||||
|
||||
server->stop();
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::notify_client(const String &p_method, const Variant &p_params, int p_client_id) {
|
||||
#ifdef TESTS_ENABLED
|
||||
if (clients.is_empty()) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
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.is_null());
|
||||
|
||||
Dictionary message = make_notification(p_method, p_params);
|
||||
String msg = Variant(message).to_json_string();
|
||||
msg = format_output(msg);
|
||||
peer->res_queue.push_back(msg.utf8());
|
||||
}
|
||||
|
||||
void GDScriptLanguageProtocol::request_client(const String &p_method, const Variant &p_params, int p_client_id) {
|
||||
#ifdef TESTS_ENABLED
|
||||
if (clients.is_empty()) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
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.is_null());
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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"));
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
#define SET_DOCUMENT_METHOD(m_method) set_method(_STR(textDocument/m_method), callable_mp(text_document.ptr(), &GDScriptTextDocument::m_method))
|
||||
#define SET_COMPLETION_METHOD(m_method) set_method(_STR(completionItem/m_method), callable_mp(text_document.ptr(), &GDScriptTextDocument::m_method))
|
||||
#define SET_WORKSPACE_METHOD(m_method) set_method(_STR(workspace/m_method), callable_mp(workspace.ptr(), &GDScriptWorkspace::m_method))
|
||||
// clang-format on
|
||||
|
||||
GDScriptLanguageProtocol::GDScriptLanguageProtocol() {
|
||||
server.instantiate();
|
||||
singleton = this;
|
||||
workspace.instantiate();
|
||||
text_document.instantiate();
|
||||
|
||||
SET_DOCUMENT_METHOD(didOpen);
|
||||
SET_DOCUMENT_METHOD(didClose);
|
||||
SET_DOCUMENT_METHOD(didChange);
|
||||
SET_DOCUMENT_METHOD(willSaveWaitUntil);
|
||||
SET_DOCUMENT_METHOD(didSave);
|
||||
|
||||
SET_DOCUMENT_METHOD(documentSymbol);
|
||||
SET_DOCUMENT_METHOD(completion);
|
||||
SET_DOCUMENT_METHOD(rename);
|
||||
SET_DOCUMENT_METHOD(prepareRename);
|
||||
SET_DOCUMENT_METHOD(references);
|
||||
SET_DOCUMENT_METHOD(foldingRange);
|
||||
SET_DOCUMENT_METHOD(codeLens);
|
||||
SET_DOCUMENT_METHOD(documentLink);
|
||||
SET_DOCUMENT_METHOD(colorPresentation);
|
||||
SET_DOCUMENT_METHOD(hover);
|
||||
SET_DOCUMENT_METHOD(definition);
|
||||
SET_DOCUMENT_METHOD(declaration);
|
||||
SET_DOCUMENT_METHOD(signatureHelp);
|
||||
|
||||
SET_DOCUMENT_METHOD(nativeSymbol); // Custom method.
|
||||
|
||||
SET_COMPLETION_METHOD(resolve);
|
||||
|
||||
SET_WORKSPACE_METHOD(didDeleteFiles);
|
||||
|
||||
set_method("initialize", callable_mp(this, &GDScriptLanguageProtocol::initialize));
|
||||
set_method("initialized", callable_mp(this, &GDScriptLanguageProtocol::initialized));
|
||||
|
||||
workspace->root = ProjectSettings::get_singleton()->get_resource_path();
|
||||
}
|
||||
|
||||
#undef SET_DOCUMENT_METHOD
|
||||
#undef SET_COMPLETION_METHOD
|
||||
#undef SET_WORKSPACE_METHOD
|
111
modules/gdscript/language_server/gdscript_language_protocol.h
Normal file
111
modules/gdscript/language_server/gdscript_language_protocol.h
Normal file
@@ -0,0 +1,111 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_language_protocol.h */
|
||||
/**************************************************************************/
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "gdscript_text_document.h"
|
||||
#include "gdscript_workspace.h"
|
||||
|
||||
#include "core/io/stream_peer_tcp.h"
|
||||
#include "core/io/tcp_server.h"
|
||||
|
||||
#include "modules/jsonrpc/jsonrpc.h"
|
||||
|
||||
#define LSP_MAX_BUFFER_SIZE 4194304
|
||||
#define LSP_MAX_CLIENTS 8
|
||||
|
||||
class GDScriptLanguageProtocol : public JSONRPC {
|
||||
GDCLASS(GDScriptLanguageProtocol, JSONRPC)
|
||||
|
||||
private:
|
||||
struct LSPeer : RefCounted {
|
||||
Ref<StreamPeerTCP> connection;
|
||||
|
||||
uint8_t req_buf[LSP_MAX_BUFFER_SIZE];
|
||||
int req_pos = 0;
|
||||
bool has_header = false;
|
||||
bool has_content = false;
|
||||
int content_length = 0;
|
||||
Vector<CharString> res_queue;
|
||||
int res_sent = 0;
|
||||
|
||||
Error handle_data();
|
||||
Error send_data();
|
||||
};
|
||||
|
||||
enum LSPErrorCode {
|
||||
RequestCancelled = -32800,
|
||||
ContentModified = -32801,
|
||||
};
|
||||
|
||||
static GDScriptLanguageProtocol *singleton;
|
||||
|
||||
HashMap<int, Ref<LSPeer>> clients;
|
||||
Ref<TCPServer> server;
|
||||
int latest_client_id = 0;
|
||||
int next_client_id = 0;
|
||||
|
||||
int next_server_id = 0;
|
||||
|
||||
Ref<GDScriptTextDocument> text_document;
|
||||
Ref<GDScriptWorkspace> workspace;
|
||||
|
||||
Error on_client_connected();
|
||||
void on_client_disconnected(const int &p_client_id);
|
||||
|
||||
String process_message(const String &p_text);
|
||||
String format_output(const String &p_text);
|
||||
|
||||
bool _initialized = false;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
Dictionary initialize(const Dictionary &p_params);
|
||||
void initialized(const Variant &p_params);
|
||||
|
||||
public:
|
||||
_FORCE_INLINE_ static GDScriptLanguageProtocol *get_singleton() { return singleton; }
|
||||
_FORCE_INLINE_ Ref<GDScriptWorkspace> get_workspace() { return workspace; }
|
||||
_FORCE_INLINE_ Ref<GDScriptTextDocument> get_text_document() { return text_document; }
|
||||
_FORCE_INLINE_ bool is_initialized() const { return _initialized; }
|
||||
|
||||
void poll(int p_limit_usec);
|
||||
Error start(int p_port, const IPAddress &p_bind_ip);
|
||||
void stop();
|
||||
|
||||
void notify_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1);
|
||||
void request_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1);
|
||||
|
||||
bool is_smart_resolve_enabled() const;
|
||||
bool is_goto_native_symbols_enabled() const;
|
||||
|
||||
GDScriptLanguageProtocol();
|
||||
};
|
126
modules/gdscript/language_server/gdscript_language_server.cpp
Normal file
126
modules/gdscript/language_server/gdscript_language_server.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_language_server.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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "gdscript_language_server.h"
|
||||
|
||||
#include "core/os/os.h"
|
||||
#include "editor/editor_log.h"
|
||||
#include "editor/editor_node.h"
|
||||
#include "editor/settings/editor_settings.h"
|
||||
|
||||
int GDScriptLanguageServer::port_override = -1;
|
||||
|
||||
GDScriptLanguageServer::GDScriptLanguageServer() {
|
||||
// TODO: Move to editor_settings.cpp
|
||||
_EDITOR_DEF("network/language_server/remote_host", host);
|
||||
_EDITOR_DEF("network/language_server/remote_port", port);
|
||||
_EDITOR_DEF("network/language_server/enable_smart_resolve", true);
|
||||
_EDITOR_DEF("network/language_server/show_native_symbols_in_editor", false);
|
||||
_EDITOR_DEF("network/language_server/use_thread", use_thread);
|
||||
_EDITOR_DEF("network/language_server/poll_limit_usec", poll_limit_usec);
|
||||
|
||||
set_process_internal(true);
|
||||
}
|
||||
|
||||
void GDScriptLanguageServer::_notification(int p_what) {
|
||||
switch (p_what) {
|
||||
case NOTIFICATION_EXIT_TREE: {
|
||||
stop();
|
||||
} break;
|
||||
|
||||
case NOTIFICATION_INTERNAL_PROCESS: {
|
||||
if (!started && EditorNode::get_singleton()->is_editor_ready()) {
|
||||
start();
|
||||
}
|
||||
|
||||
if (started && !use_thread) {
|
||||
protocol.poll(poll_limit_usec);
|
||||
}
|
||||
} break;
|
||||
|
||||
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
|
||||
if (!EditorSettings::get_singleton()->check_changed_settings_in_group("network/language_server")) {
|
||||
break;
|
||||
}
|
||||
|
||||
String remote_host = String(_EDITOR_GET("network/language_server/remote_host"));
|
||||
int remote_port = (GDScriptLanguageServer::port_override > -1) ? GDScriptLanguageServer::port_override : (int)_EDITOR_GET("network/language_server/remote_port");
|
||||
bool remote_use_thread = (bool)_EDITOR_GET("network/language_server/use_thread");
|
||||
int remote_poll_limit = (int)_EDITOR_GET("network/language_server/poll_limit_usec");
|
||||
if (remote_host != host || remote_port != port || remote_use_thread != use_thread || remote_poll_limit != poll_limit_usec) {
|
||||
stop();
|
||||
start();
|
||||
}
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptLanguageServer::thread_main(void *p_userdata) {
|
||||
set_current_thread_safe_for_nodes(true);
|
||||
GDScriptLanguageServer *self = static_cast<GDScriptLanguageServer *>(p_userdata);
|
||||
while (self->thread_running) {
|
||||
// Poll 20 times per second
|
||||
self->protocol.poll(self->poll_limit_usec);
|
||||
OS::get_singleton()->delay_usec(50000);
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptLanguageServer::start() {
|
||||
host = String(_EDITOR_GET("network/language_server/remote_host"));
|
||||
port = (GDScriptLanguageServer::port_override > -1) ? GDScriptLanguageServer::port_override : (int)_EDITOR_GET("network/language_server/remote_port");
|
||||
use_thread = (bool)_EDITOR_GET("network/language_server/use_thread");
|
||||
poll_limit_usec = (int)_EDITOR_GET("network/language_server/poll_limit_usec");
|
||||
if (protocol.start(port, IPAddress(host)) == OK) {
|
||||
EditorNode::get_log()->add_message("--- GDScript language server started on port " + itos(port) + " ---", EditorLog::MSG_TYPE_EDITOR);
|
||||
if (use_thread) {
|
||||
thread_running = true;
|
||||
thread.start(GDScriptLanguageServer::thread_main, this);
|
||||
}
|
||||
set_process_internal(!use_thread);
|
||||
started = true;
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptLanguageServer::stop() {
|
||||
if (use_thread) {
|
||||
ERR_FAIL_COND(!thread.is_started());
|
||||
thread_running = false;
|
||||
thread.wait_to_finish();
|
||||
}
|
||||
protocol.stop();
|
||||
started = false;
|
||||
EditorNode::get_log()->add_message("--- GDScript language server stopped ---", EditorLog::MSG_TYPE_EDITOR);
|
||||
}
|
||||
|
||||
void register_lsp_types() {
|
||||
GDREGISTER_CLASS(GDScriptLanguageProtocol);
|
||||
GDREGISTER_CLASS(GDScriptTextDocument);
|
||||
GDREGISTER_CLASS(GDScriptWorkspace);
|
||||
}
|
61
modules/gdscript/language_server/gdscript_language_server.h
Normal file
61
modules/gdscript/language_server/gdscript_language_server.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_language_server.h */
|
||||
/**************************************************************************/
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "gdscript_language_protocol.h"
|
||||
|
||||
#include "editor/plugins/editor_plugin.h"
|
||||
|
||||
class GDScriptLanguageServer : public EditorPlugin {
|
||||
GDCLASS(GDScriptLanguageServer, EditorPlugin);
|
||||
|
||||
GDScriptLanguageProtocol protocol;
|
||||
|
||||
Thread thread;
|
||||
bool thread_running = false;
|
||||
bool started = false;
|
||||
bool use_thread = false;
|
||||
String host = "127.0.0.1";
|
||||
int port = 6005;
|
||||
int poll_limit_usec = 100000;
|
||||
static void thread_main(void *p_userdata);
|
||||
|
||||
private:
|
||||
void _notification(int p_what);
|
||||
|
||||
public:
|
||||
static int port_override;
|
||||
GDScriptLanguageServer();
|
||||
void start();
|
||||
void stop();
|
||||
};
|
||||
|
||||
void register_lsp_types();
|
521
modules/gdscript/language_server/gdscript_text_document.cpp
Normal file
521
modules/gdscript/language_server/gdscript_text_document.cpp
Normal file
@@ -0,0 +1,521 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_text_document.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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "gdscript_text_document.h"
|
||||
|
||||
#include "../gdscript.h"
|
||||
#include "gdscript_extend_parser.h"
|
||||
#include "gdscript_language_protocol.h"
|
||||
|
||||
#include "editor/script/script_text_editor.h"
|
||||
#include "editor/settings/editor_settings.h"
|
||||
#include "servers/display_server.h"
|
||||
|
||||
void GDScriptTextDocument::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("didOpen"), &GDScriptTextDocument::didOpen);
|
||||
ClassDB::bind_method(D_METHOD("didClose"), &GDScriptTextDocument::didClose);
|
||||
ClassDB::bind_method(D_METHOD("didChange"), &GDScriptTextDocument::didChange);
|
||||
ClassDB::bind_method(D_METHOD("willSaveWaitUntil"), &GDScriptTextDocument::willSaveWaitUntil);
|
||||
ClassDB::bind_method(D_METHOD("didSave"), &GDScriptTextDocument::didSave);
|
||||
ClassDB::bind_method(D_METHOD("nativeSymbol"), &GDScriptTextDocument::nativeSymbol);
|
||||
ClassDB::bind_method(D_METHOD("documentSymbol"), &GDScriptTextDocument::documentSymbol);
|
||||
ClassDB::bind_method(D_METHOD("completion"), &GDScriptTextDocument::completion);
|
||||
ClassDB::bind_method(D_METHOD("resolve"), &GDScriptTextDocument::resolve);
|
||||
ClassDB::bind_method(D_METHOD("rename"), &GDScriptTextDocument::rename);
|
||||
ClassDB::bind_method(D_METHOD("prepareRename"), &GDScriptTextDocument::prepareRename);
|
||||
ClassDB::bind_method(D_METHOD("references"), &GDScriptTextDocument::references);
|
||||
ClassDB::bind_method(D_METHOD("foldingRange"), &GDScriptTextDocument::foldingRange);
|
||||
ClassDB::bind_method(D_METHOD("codeLens"), &GDScriptTextDocument::codeLens);
|
||||
ClassDB::bind_method(D_METHOD("documentLink"), &GDScriptTextDocument::documentLink);
|
||||
ClassDB::bind_method(D_METHOD("colorPresentation"), &GDScriptTextDocument::colorPresentation);
|
||||
ClassDB::bind_method(D_METHOD("hover"), &GDScriptTextDocument::hover);
|
||||
ClassDB::bind_method(D_METHOD("definition"), &GDScriptTextDocument::definition);
|
||||
ClassDB::bind_method(D_METHOD("declaration"), &GDScriptTextDocument::declaration);
|
||||
ClassDB::bind_method(D_METHOD("signatureHelp"), &GDScriptTextDocument::signatureHelp);
|
||||
ClassDB::bind_method(D_METHOD("show_native_symbol_in_editor"), &GDScriptTextDocument::show_native_symbol_in_editor);
|
||||
}
|
||||
|
||||
void GDScriptTextDocument::didOpen(const Variant &p_param) {
|
||||
LSP::TextDocumentItem doc = load_document_item(p_param);
|
||||
sync_script_content(doc.uri, doc.text);
|
||||
}
|
||||
|
||||
void GDScriptTextDocument::didClose(const Variant &p_param) {
|
||||
// Left empty on purpose. Godot does nothing special on closing a document,
|
||||
// but it satisfies LSP clients that require didClose be implemented.
|
||||
}
|
||||
|
||||
void GDScriptTextDocument::didChange(const Variant &p_param) {
|
||||
LSP::TextDocumentItem doc = load_document_item(p_param);
|
||||
Dictionary dict = p_param;
|
||||
Array contentChanges = dict["contentChanges"];
|
||||
for (int i = 0; i < contentChanges.size(); ++i) {
|
||||
LSP::TextDocumentContentChangeEvent evt;
|
||||
evt.load(contentChanges[i]);
|
||||
doc.text = evt.text;
|
||||
}
|
||||
sync_script_content(doc.uri, doc.text);
|
||||
}
|
||||
|
||||
void GDScriptTextDocument::willSaveWaitUntil(const Variant &p_param) {
|
||||
LSP::TextDocumentItem doc = load_document_item(p_param);
|
||||
|
||||
String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(doc.uri);
|
||||
Ref<Script> scr = ResourceLoader::load(path);
|
||||
if (scr.is_valid()) {
|
||||
ScriptEditor::get_singleton()->clear_docs_from_script(scr);
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptTextDocument::didSave(const Variant &p_param) {
|
||||
LSP::TextDocumentItem doc = load_document_item(p_param);
|
||||
Dictionary dict = p_param;
|
||||
String text = dict["text"];
|
||||
|
||||
sync_script_content(doc.uri, text);
|
||||
|
||||
String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(doc.uri);
|
||||
Ref<GDScript> scr = ResourceLoader::load(path);
|
||||
if (scr.is_valid() && (scr->load_source_code(path) == OK)) {
|
||||
if (scr->is_tool()) {
|
||||
scr->get_language()->reload_tool_script(scr, true);
|
||||
} else {
|
||||
scr->reload(true);
|
||||
}
|
||||
|
||||
scr->update_exports();
|
||||
|
||||
if (!Thread::is_main_thread()) {
|
||||
callable_mp(this, &GDScriptTextDocument::reload_script).call_deferred(scr);
|
||||
} else {
|
||||
reload_script(scr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptTextDocument::reload_script(Ref<GDScript> p_to_reload_script) {
|
||||
ScriptEditor::get_singleton()->reload_scripts(true);
|
||||
ScriptEditor::get_singleton()->update_docs_from_script(p_to_reload_script);
|
||||
ScriptEditor::get_singleton()->trigger_live_script_reload(p_to_reload_script->get_path());
|
||||
}
|
||||
|
||||
LSP::TextDocumentItem GDScriptTextDocument::load_document_item(const Variant &p_param) {
|
||||
LSP::TextDocumentItem doc;
|
||||
Dictionary params = p_param;
|
||||
doc.load(params["textDocument"]);
|
||||
return doc;
|
||||
}
|
||||
|
||||
void GDScriptTextDocument::notify_client_show_symbol(const LSP::DocumentSymbol *symbol) {
|
||||
ERR_FAIL_NULL(symbol);
|
||||
GDScriptLanguageProtocol::get_singleton()->notify_client("gdscript/show_native_symbol", symbol->to_json(true));
|
||||
}
|
||||
|
||||
void GDScriptTextDocument::initialize() {
|
||||
if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
|
||||
for (const KeyValue<StringName, ClassMembers> &E : GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members) {
|
||||
const ClassMembers &members = E.value;
|
||||
|
||||
for (const KeyValue<String, const LSP::DocumentSymbol *> &F : members) {
|
||||
const LSP::DocumentSymbol *symbol = members.get(F.key);
|
||||
LSP::CompletionItem item = symbol->make_completion_item();
|
||||
item.data = JOIN_SYMBOLS(String(E.key), F.key);
|
||||
native_member_completions.push_back(item.to_json());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Variant GDScriptTextDocument::nativeSymbol(const Dictionary &p_params) {
|
||||
Variant ret;
|
||||
|
||||
LSP::NativeSymbolInspectParams params;
|
||||
params.load(p_params);
|
||||
|
||||
if (const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_native_symbol(params)) {
|
||||
ret = symbol->to_json(true);
|
||||
notify_client_show_symbol(symbol);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) {
|
||||
Dictionary params = p_params["textDocument"];
|
||||
String uri = params["uri"];
|
||||
String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(uri);
|
||||
Array arr;
|
||||
if (HashMap<String, ExtendGDScriptParser *>::ConstIterator parser = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(path)) {
|
||||
LSP::DocumentSymbol symbol = parser->value->get_symbols();
|
||||
arr.push_back(symbol.to_json(true));
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
Array GDScriptTextDocument::completion(const Dictionary &p_params) {
|
||||
Array arr;
|
||||
|
||||
LSP::CompletionParams params;
|
||||
params.load(p_params);
|
||||
Dictionary request_data = params.to_json();
|
||||
|
||||
List<ScriptLanguage::CodeCompletionOption> options;
|
||||
GDScriptLanguageProtocol::get_singleton()->get_workspace()->completion(params, &options);
|
||||
|
||||
if (!options.is_empty()) {
|
||||
int i = 0;
|
||||
arr.resize(options.size());
|
||||
|
||||
for (const ScriptLanguage::CodeCompletionOption &option : options) {
|
||||
LSP::CompletionItem item;
|
||||
item.label = option.display;
|
||||
item.data = request_data;
|
||||
item.insertText = option.insert_text;
|
||||
|
||||
switch (option.kind) {
|
||||
case ScriptLanguage::CODE_COMPLETION_KIND_ENUM:
|
||||
item.kind = LSP::CompletionItemKind::Enum;
|
||||
break;
|
||||
case ScriptLanguage::CODE_COMPLETION_KIND_CLASS:
|
||||
item.kind = LSP::CompletionItemKind::Class;
|
||||
break;
|
||||
case ScriptLanguage::CODE_COMPLETION_KIND_MEMBER:
|
||||
item.kind = LSP::CompletionItemKind::Property;
|
||||
break;
|
||||
case ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION:
|
||||
item.kind = LSP::CompletionItemKind::Method;
|
||||
break;
|
||||
case ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL:
|
||||
item.kind = LSP::CompletionItemKind::Event;
|
||||
break;
|
||||
case ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT:
|
||||
item.kind = LSP::CompletionItemKind::Constant;
|
||||
break;
|
||||
case ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE:
|
||||
item.kind = LSP::CompletionItemKind::Variable;
|
||||
break;
|
||||
case ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH:
|
||||
item.kind = LSP::CompletionItemKind::File;
|
||||
break;
|
||||
case ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH:
|
||||
item.kind = LSP::CompletionItemKind::Snippet;
|
||||
break;
|
||||
case ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT:
|
||||
item.kind = LSP::CompletionItemKind::Text;
|
||||
break;
|
||||
default: {
|
||||
}
|
||||
}
|
||||
|
||||
arr[i] = item.to_json();
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
Dictionary GDScriptTextDocument::rename(const Dictionary &p_params) {
|
||||
LSP::TextDocumentPositionParams params;
|
||||
params.load(p_params);
|
||||
String new_name = p_params["newName"];
|
||||
|
||||
return GDScriptLanguageProtocol::get_singleton()->get_workspace()->rename(params, new_name);
|
||||
}
|
||||
|
||||
Variant GDScriptTextDocument::prepareRename(const Dictionary &p_params) {
|
||||
LSP::TextDocumentPositionParams params;
|
||||
params.load(p_params);
|
||||
|
||||
LSP::DocumentSymbol symbol;
|
||||
LSP::Range range;
|
||||
if (GDScriptLanguageProtocol::get_singleton()->get_workspace()->can_rename(params, symbol, range)) {
|
||||
return Variant(range.to_json());
|
||||
}
|
||||
|
||||
// `null` -> rename not valid at current location.
|
||||
return Variant();
|
||||
}
|
||||
|
||||
Array GDScriptTextDocument::references(const Dictionary &p_params) {
|
||||
Array res;
|
||||
|
||||
LSP::ReferenceParams params;
|
||||
params.load(p_params);
|
||||
|
||||
const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);
|
||||
if (symbol) {
|
||||
Vector<LSP::Location> usages = GDScriptLanguageProtocol::get_singleton()->get_workspace()->find_all_usages(*symbol);
|
||||
res.resize(usages.size());
|
||||
int declaration_adjustment = 0;
|
||||
for (int i = 0; i < usages.size(); i++) {
|
||||
LSP::Location usage = usages[i];
|
||||
if (!params.context.includeDeclaration && usage.range == symbol->range) {
|
||||
declaration_adjustment++;
|
||||
continue;
|
||||
}
|
||||
res[i - declaration_adjustment] = usages[i].to_json();
|
||||
}
|
||||
|
||||
if (declaration_adjustment > 0) {
|
||||
res.resize(res.size() - declaration_adjustment);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
Dictionary GDScriptTextDocument::resolve(const Dictionary &p_params) {
|
||||
LSP::CompletionItem item;
|
||||
item.load(p_params);
|
||||
|
||||
LSP::CompletionParams params;
|
||||
Variant data = p_params["data"];
|
||||
|
||||
const LSP::DocumentSymbol *symbol = nullptr;
|
||||
|
||||
if (data.get_type() == Variant::DICTIONARY) {
|
||||
params.load(p_params["data"]);
|
||||
symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params, item.label, item.kind == LSP::CompletionItemKind::Method || item.kind == LSP::CompletionItemKind::Function);
|
||||
|
||||
} else if (data.is_string()) {
|
||||
String query = data;
|
||||
|
||||
Vector<String> param_symbols = query.split(SYMBOL_SEPARATOR, false);
|
||||
|
||||
if (param_symbols.size() >= 2) {
|
||||
StringName class_name = param_symbols[0];
|
||||
const String &member_name = param_symbols[param_symbols.size() - 1];
|
||||
String inner_class_name;
|
||||
if (param_symbols.size() >= 3) {
|
||||
inner_class_name = param_symbols[1];
|
||||
}
|
||||
|
||||
if (const ClassMembers *members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members.getptr(class_name)) {
|
||||
if (const LSP::DocumentSymbol *const *member = members->getptr(member_name)) {
|
||||
symbol = *member;
|
||||
}
|
||||
}
|
||||
|
||||
if (!symbol) {
|
||||
if (HashMap<String, ExtendGDScriptParser *>::ConstIterator E = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(class_name)) {
|
||||
symbol = E->value->get_member_symbol(member_name, inner_class_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (symbol) {
|
||||
item.documentation = symbol->render();
|
||||
}
|
||||
|
||||
if (item.kind == LSP::CompletionItemKind::Event) {
|
||||
if (params.context.triggerKind == LSP::CompletionTriggerKind::TriggerCharacter && (params.context.triggerCharacter == "(")) {
|
||||
const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
|
||||
item.insertText = item.label.quote(quote_style);
|
||||
}
|
||||
}
|
||||
|
||||
if (item.kind == LSP::CompletionItemKind::Method) {
|
||||
bool is_trigger_character = params.context.triggerKind == LSP::CompletionTriggerKind::TriggerCharacter;
|
||||
bool is_quote_character = params.context.triggerCharacter == "\"" || params.context.triggerCharacter == "'";
|
||||
|
||||
if (is_trigger_character && is_quote_character && item.insertText.is_quoted()) {
|
||||
item.insertText = item.insertText.unquote();
|
||||
}
|
||||
}
|
||||
|
||||
return item.to_json(true);
|
||||
}
|
||||
|
||||
Array GDScriptTextDocument::foldingRange(const Dictionary &p_params) {
|
||||
Array arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
Array GDScriptTextDocument::codeLens(const Dictionary &p_params) {
|
||||
Array arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
Array GDScriptTextDocument::documentLink(const Dictionary &p_params) {
|
||||
Array ret;
|
||||
|
||||
LSP::DocumentLinkParams params;
|
||||
params.load(p_params);
|
||||
|
||||
List<LSP::DocumentLink> links;
|
||||
GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_document_links(params.textDocument.uri, links);
|
||||
for (const LSP::DocumentLink &E : links) {
|
||||
ret.push_back(E.to_json());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
Array GDScriptTextDocument::colorPresentation(const Dictionary &p_params) {
|
||||
Array arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
Variant GDScriptTextDocument::hover(const Dictionary &p_params) {
|
||||
LSP::TextDocumentPositionParams params;
|
||||
params.load(p_params);
|
||||
|
||||
const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params);
|
||||
if (symbol) {
|
||||
LSP::Hover hover;
|
||||
hover.contents = symbol->render();
|
||||
hover.range.start = params.position;
|
||||
hover.range.end = params.position;
|
||||
return hover.to_json();
|
||||
|
||||
} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
|
||||
Dictionary ret;
|
||||
Array contents;
|
||||
List<const LSP::DocumentSymbol *> list;
|
||||
GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(params, list);
|
||||
for (const LSP::DocumentSymbol *&E : list) {
|
||||
if (const LSP::DocumentSymbol *s = E) {
|
||||
contents.push_back(s->render().value);
|
||||
}
|
||||
}
|
||||
ret["contents"] = contents;
|
||||
return ret;
|
||||
}
|
||||
|
||||
return Variant();
|
||||
}
|
||||
|
||||
Array GDScriptTextDocument::definition(const Dictionary &p_params) {
|
||||
LSP::TextDocumentPositionParams params;
|
||||
params.load(p_params);
|
||||
List<const LSP::DocumentSymbol *> symbols;
|
||||
Array arr = find_symbols(params, symbols);
|
||||
return arr;
|
||||
}
|
||||
|
||||
Variant GDScriptTextDocument::declaration(const Dictionary &p_params) {
|
||||
LSP::TextDocumentPositionParams params;
|
||||
params.load(p_params);
|
||||
List<const LSP::DocumentSymbol *> symbols;
|
||||
Array arr = find_symbols(params, symbols);
|
||||
if (arr.is_empty() && !symbols.is_empty() && !symbols.front()->get()->native_class.is_empty()) { // Find a native symbol
|
||||
const LSP::DocumentSymbol *symbol = symbols.front()->get();
|
||||
if (GDScriptLanguageProtocol::get_singleton()->is_goto_native_symbols_enabled()) {
|
||||
String id;
|
||||
switch (symbol->kind) {
|
||||
case LSP::SymbolKind::Class:
|
||||
id = "class_name:" + symbol->name;
|
||||
break;
|
||||
case LSP::SymbolKind::Constant:
|
||||
id = "class_constant:" + symbol->native_class + ":" + symbol->name;
|
||||
break;
|
||||
case LSP::SymbolKind::Property:
|
||||
case LSP::SymbolKind::Variable:
|
||||
id = "class_property:" + symbol->native_class + ":" + symbol->name;
|
||||
break;
|
||||
case LSP::SymbolKind::Enum:
|
||||
id = "class_enum:" + symbol->native_class + ":" + symbol->name;
|
||||
break;
|
||||
case LSP::SymbolKind::Method:
|
||||
case LSP::SymbolKind::Function:
|
||||
id = "class_method:" + symbol->native_class + ":" + symbol->name;
|
||||
break;
|
||||
default:
|
||||
id = "class_global:" + symbol->native_class + ":" + symbol->name;
|
||||
break;
|
||||
}
|
||||
callable_mp(this, &GDScriptTextDocument::show_native_symbol_in_editor).call_deferred(id);
|
||||
} else {
|
||||
notify_client_show_symbol(symbol);
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
Variant GDScriptTextDocument::signatureHelp(const Dictionary &p_params) {
|
||||
Variant ret;
|
||||
|
||||
LSP::TextDocumentPositionParams params;
|
||||
params.load(p_params);
|
||||
|
||||
LSP::SignatureHelp s;
|
||||
if (OK == GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_signature(params, s)) {
|
||||
ret = s.to_json();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
GDScriptTextDocument::GDScriptTextDocument() {
|
||||
file_checker = FileAccess::create(FileAccess::ACCESS_RESOURCES);
|
||||
}
|
||||
|
||||
void GDScriptTextDocument::sync_script_content(const String &p_path, const String &p_content) {
|
||||
String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(p_path);
|
||||
GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_script(path, p_content);
|
||||
}
|
||||
|
||||
void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_id) {
|
||||
callable_mp(ScriptEditor::get_singleton(), &ScriptEditor::goto_help).call_deferred(p_symbol_id);
|
||||
|
||||
DisplayServer::get_singleton()->window_move_to_foreground();
|
||||
}
|
||||
|
||||
Array GDScriptTextDocument::find_symbols(const LSP::TextDocumentPositionParams &p_location, List<const LSP::DocumentSymbol *> &r_list) {
|
||||
Array arr;
|
||||
const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(p_location);
|
||||
if (symbol) {
|
||||
LSP::Location location;
|
||||
location.uri = symbol->uri;
|
||||
if (!location.uri.is_empty()) {
|
||||
location.range = symbol->selectionRange;
|
||||
const String &path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(symbol->uri);
|
||||
if (file_checker->file_exists(path)) {
|
||||
arr.push_back(location.to_json());
|
||||
}
|
||||
r_list.push_back(symbol);
|
||||
}
|
||||
} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
|
||||
List<const LSP::DocumentSymbol *> list;
|
||||
GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(p_location, list);
|
||||
for (const LSP::DocumentSymbol *&E : list) {
|
||||
if (const LSP::DocumentSymbol *s = E) {
|
||||
if (!s->uri.is_empty()) {
|
||||
LSP::Location location;
|
||||
location.uri = s->uri;
|
||||
location.range = s->selectionRange;
|
||||
arr.push_back(location.to_json());
|
||||
r_list.push_back(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
84
modules/gdscript/language_server/gdscript_text_document.h
Normal file
84
modules/gdscript/language_server/gdscript_text_document.h
Normal file
@@ -0,0 +1,84 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_text_document.h */
|
||||
/**************************************************************************/
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "godot_lsp.h"
|
||||
|
||||
#include "core/io/file_access.h"
|
||||
#include "core/object/ref_counted.h"
|
||||
|
||||
class GDScript;
|
||||
|
||||
class GDScriptTextDocument : public RefCounted {
|
||||
GDCLASS(GDScriptTextDocument, RefCounted)
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
Ref<FileAccess> file_checker;
|
||||
|
||||
Array native_member_completions;
|
||||
|
||||
private:
|
||||
Array find_symbols(const LSP::TextDocumentPositionParams &p_location, List<const LSP::DocumentSymbol *> &r_list);
|
||||
LSP::TextDocumentItem load_document_item(const Variant &p_param);
|
||||
void notify_client_show_symbol(const LSP::DocumentSymbol *symbol);
|
||||
|
||||
public:
|
||||
void didOpen(const Variant &p_param);
|
||||
void didClose(const Variant &p_param);
|
||||
void didChange(const Variant &p_param);
|
||||
void willSaveWaitUntil(const Variant &p_param);
|
||||
void didSave(const Variant &p_param);
|
||||
|
||||
void reload_script(Ref<GDScript> p_to_reload_script);
|
||||
void sync_script_content(const String &p_path, const String &p_content);
|
||||
void show_native_symbol_in_editor(const String &p_symbol_id);
|
||||
|
||||
Variant nativeSymbol(const Dictionary &p_params);
|
||||
Array documentSymbol(const Dictionary &p_params);
|
||||
Array completion(const Dictionary &p_params);
|
||||
Dictionary resolve(const Dictionary &p_params);
|
||||
Dictionary rename(const Dictionary &p_params);
|
||||
Variant prepareRename(const Dictionary &p_params);
|
||||
Array references(const Dictionary &p_params);
|
||||
Array foldingRange(const Dictionary &p_params);
|
||||
Array codeLens(const Dictionary &p_params);
|
||||
Array documentLink(const Dictionary &p_params);
|
||||
Array colorPresentation(const Dictionary &p_params);
|
||||
Variant hover(const Dictionary &p_params);
|
||||
Array definition(const Dictionary &p_params);
|
||||
Variant declaration(const Dictionary &p_params);
|
||||
Variant signatureHelp(const Dictionary &p_params);
|
||||
|
||||
void initialize();
|
||||
|
||||
GDScriptTextDocument();
|
||||
};
|
960
modules/gdscript/language_server/gdscript_workspace.cpp
Normal file
960
modules/gdscript/language_server/gdscript_workspace.cpp
Normal file
@@ -0,0 +1,960 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_workspace.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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "gdscript_workspace.h"
|
||||
|
||||
#include "../gdscript.h"
|
||||
#include "../gdscript_parser.h"
|
||||
#include "gdscript_language_protocol.h"
|
||||
|
||||
#include "core/config/project_settings.h"
|
||||
#include "core/object/script_language.h"
|
||||
#include "editor/doc/doc_tools.h"
|
||||
#include "editor/doc/editor_help.h"
|
||||
#include "editor/editor_node.h"
|
||||
#include "editor/file_system/editor_file_system.h"
|
||||
#include "editor/settings/editor_settings.h"
|
||||
#include "scene/resources/packed_scene.h"
|
||||
|
||||
void GDScriptWorkspace::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("apply_new_signal"), &GDScriptWorkspace::apply_new_signal);
|
||||
ClassDB::bind_method(D_METHOD("didDeleteFiles"), &GDScriptWorkspace::didDeleteFiles);
|
||||
ClassDB::bind_method(D_METHOD("parse_script", "path", "content"), &GDScriptWorkspace::parse_script);
|
||||
ClassDB::bind_method(D_METHOD("parse_local_script", "path"), &GDScriptWorkspace::parse_local_script);
|
||||
ClassDB::bind_method(D_METHOD("get_file_path", "uri"), &GDScriptWorkspace::get_file_path);
|
||||
ClassDB::bind_method(D_METHOD("get_file_uri", "path"), &GDScriptWorkspace::get_file_uri);
|
||||
ClassDB::bind_method(D_METHOD("publish_diagnostics", "path"), &GDScriptWorkspace::publish_diagnostics);
|
||||
ClassDB::bind_method(D_METHOD("generate_script_api", "path"), &GDScriptWorkspace::generate_script_api);
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::apply_new_signal(Object *obj, String function, PackedStringArray args) {
|
||||
Ref<Script> scr = obj->get_script();
|
||||
|
||||
if (scr->get_language()->get_name() != "GDScript") {
|
||||
return;
|
||||
}
|
||||
|
||||
String function_signature = "func " + function;
|
||||
String source = scr->get_source_code();
|
||||
|
||||
if (source.contains(function_signature)) {
|
||||
return;
|
||||
}
|
||||
|
||||
int first_class = source.find("\nclass ");
|
||||
int start_line = 0;
|
||||
if (first_class != -1) {
|
||||
start_line = source.substr(0, first_class).split("\n").size();
|
||||
} else {
|
||||
start_line = source.split("\n").size();
|
||||
}
|
||||
|
||||
String function_body = "\n\n" + function_signature + "(";
|
||||
for (int i = 0; i < args.size(); ++i) {
|
||||
function_body += args[i];
|
||||
if (i < args.size() - 1) {
|
||||
function_body += ", ";
|
||||
}
|
||||
}
|
||||
function_body += ")";
|
||||
if (EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints")) {
|
||||
function_body += " -> void";
|
||||
}
|
||||
function_body += ":\n\tpass # Replace with function body.\n";
|
||||
|
||||
LSP::TextEdit text_edit;
|
||||
|
||||
if (first_class != -1) {
|
||||
function_body += "\n\n";
|
||||
}
|
||||
text_edit.range.end.line = text_edit.range.start.line = start_line;
|
||||
|
||||
text_edit.newText = function_body;
|
||||
|
||||
String uri = get_file_uri(scr->get_path());
|
||||
|
||||
LSP::ApplyWorkspaceEditParams params;
|
||||
params.edit.add_edit(uri, text_edit);
|
||||
|
||||
GDScriptLanguageProtocol::get_singleton()->request_client("workspace/applyEdit", params.to_json());
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::didDeleteFiles(const Dictionary &p_params) {
|
||||
Array files = p_params["files"];
|
||||
for (int i = 0; i < files.size(); ++i) {
|
||||
Dictionary file = files[i];
|
||||
String uri = file["uri"];
|
||||
String path = get_file_path(uri);
|
||||
parse_script(path, "");
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::remove_cache_parser(const String &p_path) {
|
||||
HashMap<String, ExtendGDScriptParser *>::Iterator parser = parse_results.find(p_path);
|
||||
HashMap<String, ExtendGDScriptParser *>::Iterator scr = scripts.find(p_path);
|
||||
if (parser && scr) {
|
||||
if (scr->value && scr->value == parser->value) {
|
||||
memdelete(scr->value);
|
||||
} else {
|
||||
memdelete(scr->value);
|
||||
memdelete(parser->value);
|
||||
}
|
||||
parse_results.erase(p_path);
|
||||
scripts.erase(p_path);
|
||||
} else if (parser) {
|
||||
memdelete(parser->value);
|
||||
parse_results.erase(p_path);
|
||||
} else if (scr) {
|
||||
memdelete(scr->value);
|
||||
scripts.erase(p_path);
|
||||
}
|
||||
}
|
||||
|
||||
const LSP::DocumentSymbol *GDScriptWorkspace::get_native_symbol(const String &p_class, const String &p_member) const {
|
||||
StringName class_name = p_class;
|
||||
StringName empty;
|
||||
|
||||
while (class_name != empty) {
|
||||
if (HashMap<StringName, LSP::DocumentSymbol>::ConstIterator E = native_symbols.find(class_name)) {
|
||||
const LSP::DocumentSymbol &class_symbol = E->value;
|
||||
|
||||
if (p_member.is_empty()) {
|
||||
return &class_symbol;
|
||||
} else {
|
||||
for (int i = 0; i < class_symbol.children.size(); i++) {
|
||||
const LSP::DocumentSymbol &symbol = class_symbol.children[i];
|
||||
if (symbol.name == p_member) {
|
||||
return &symbol;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Might contain pseudo classes like @GDScript that only exist in documentation.
|
||||
if (ClassDB::class_exists(class_name)) {
|
||||
class_name = ClassDB::get_parent_class(class_name);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const LSP::DocumentSymbol *GDScriptWorkspace::get_script_symbol(const String &p_path) const {
|
||||
HashMap<String, ExtendGDScriptParser *>::ConstIterator S = scripts.find(p_path);
|
||||
if (S) {
|
||||
return &(S->value->get_symbols());
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const LSP::DocumentSymbol *GDScriptWorkspace::get_parameter_symbol(const LSP::DocumentSymbol *p_parent, const String &symbol_identifier) {
|
||||
for (int i = 0; i < p_parent->children.size(); ++i) {
|
||||
const LSP::DocumentSymbol *parameter_symbol = &p_parent->children[i];
|
||||
if (!parameter_symbol->detail.is_empty() && parameter_symbol->name == symbol_identifier) {
|
||||
return parameter_symbol;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const LSP::DocumentSymbol *GDScriptWorkspace::get_local_symbol_at(const ExtendGDScriptParser *p_parser, const String &p_symbol_identifier, const LSP::Position p_position) {
|
||||
// Go down and pick closest `DocumentSymbol` with `p_symbol_identifier`.
|
||||
|
||||
const LSP::DocumentSymbol *current = &p_parser->get_symbols();
|
||||
const LSP::DocumentSymbol *best_match = nullptr;
|
||||
|
||||
while (current) {
|
||||
if (current->name == p_symbol_identifier) {
|
||||
if (current->selectionRange.contains(p_position)) {
|
||||
// Exact match: pos is ON symbol decl identifier.
|
||||
return current;
|
||||
}
|
||||
|
||||
best_match = current;
|
||||
}
|
||||
|
||||
const LSP::DocumentSymbol *parent = current;
|
||||
current = nullptr;
|
||||
for (const LSP::DocumentSymbol &child : parent->children) {
|
||||
if (child.range.contains(p_position)) {
|
||||
current = &child;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return best_match;
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::reload_all_workspace_scripts() {
|
||||
List<String> paths;
|
||||
list_script_files("res://", paths);
|
||||
for (const String &path : paths) {
|
||||
Error err;
|
||||
String content = FileAccess::get_file_as_string(path, &err);
|
||||
ERR_CONTINUE(err != OK);
|
||||
err = parse_script(path, content);
|
||||
|
||||
if (err != OK) {
|
||||
HashMap<String, ExtendGDScriptParser *>::Iterator S = parse_results.find(path);
|
||||
String err_msg = "Failed parse script " + path;
|
||||
if (S) {
|
||||
err_msg += "\n" + S->value->get_errors().front()->get().message;
|
||||
}
|
||||
ERR_CONTINUE_MSG(err != OK, err_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::list_script_files(const String &p_root_dir, List<String> &r_files) {
|
||||
Error err;
|
||||
Ref<DirAccess> dir = DirAccess::open(p_root_dir, &err);
|
||||
if (OK != err) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ignore scripts in directories with a .gdignore file.
|
||||
if (dir->file_exists(".gdignore")) {
|
||||
return;
|
||||
}
|
||||
|
||||
dir->list_dir_begin();
|
||||
String file_name = dir->get_next();
|
||||
while (file_name.length()) {
|
||||
if (dir->current_is_dir() && file_name != "." && file_name != ".." && file_name != "./") {
|
||||
list_script_files(p_root_dir.path_join(file_name), r_files);
|
||||
} else if (file_name.ends_with(".gd")) {
|
||||
String script_file = p_root_dir.path_join(file_name);
|
||||
r_files.push_back(script_file);
|
||||
}
|
||||
file_name = dir->get_next();
|
||||
}
|
||||
}
|
||||
|
||||
ExtendGDScriptParser *GDScriptWorkspace::get_parse_successed_script(const String &p_path) {
|
||||
HashMap<String, ExtendGDScriptParser *>::Iterator S = scripts.find(p_path);
|
||||
if (!S) {
|
||||
parse_local_script(p_path);
|
||||
S = scripts.find(p_path);
|
||||
}
|
||||
if (S) {
|
||||
return S->value;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ExtendGDScriptParser *GDScriptWorkspace::get_parse_result(const String &p_path) {
|
||||
HashMap<String, ExtendGDScriptParser *>::Iterator S = parse_results.find(p_path);
|
||||
if (!S) {
|
||||
parse_local_script(p_path);
|
||||
S = parse_results.find(p_path);
|
||||
}
|
||||
if (S) {
|
||||
return S->value;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#define HANDLE_DOC(m_string) ((is_native ? DTR(m_string) : (m_string)).strip_edges())
|
||||
|
||||
Error GDScriptWorkspace::initialize() {
|
||||
if (initialized) {
|
||||
return OK;
|
||||
}
|
||||
|
||||
DocTools *doc = EditorHelp::get_doc_data();
|
||||
for (const KeyValue<String, DocData::ClassDoc> &E : doc->class_list) {
|
||||
const DocData::ClassDoc &class_data = E.value;
|
||||
const bool is_native = !class_data.is_script_doc;
|
||||
LSP::DocumentSymbol class_symbol;
|
||||
String class_name = E.key;
|
||||
class_symbol.name = class_name;
|
||||
class_symbol.native_class = class_name;
|
||||
class_symbol.kind = LSP::SymbolKind::Class;
|
||||
class_symbol.detail = String("<Native> class ") + class_name;
|
||||
if (!class_data.inherits.is_empty()) {
|
||||
class_symbol.detail += " extends " + class_data.inherits;
|
||||
}
|
||||
class_symbol.documentation = HANDLE_DOC(class_data.brief_description) + "\n" + HANDLE_DOC(class_data.description);
|
||||
|
||||
for (int i = 0; i < class_data.constants.size(); i++) {
|
||||
const DocData::ConstantDoc &const_data = class_data.constants[i];
|
||||
LSP::DocumentSymbol symbol;
|
||||
symbol.name = const_data.name;
|
||||
symbol.native_class = class_name;
|
||||
symbol.kind = LSP::SymbolKind::Constant;
|
||||
symbol.detail = "const " + class_name + "." + const_data.name;
|
||||
if (const_data.enumeration.length()) {
|
||||
symbol.detail += ": " + const_data.enumeration;
|
||||
}
|
||||
symbol.detail += " = " + const_data.value;
|
||||
symbol.documentation = HANDLE_DOC(const_data.description);
|
||||
class_symbol.children.push_back(symbol);
|
||||
}
|
||||
|
||||
for (int i = 0; i < class_data.properties.size(); i++) {
|
||||
const DocData::PropertyDoc &data = class_data.properties[i];
|
||||
LSP::DocumentSymbol symbol;
|
||||
symbol.name = data.name;
|
||||
symbol.native_class = class_name;
|
||||
symbol.kind = LSP::SymbolKind::Property;
|
||||
symbol.detail = "var " + class_name + "." + data.name;
|
||||
if (data.enumeration.length()) {
|
||||
symbol.detail += ": " + data.enumeration;
|
||||
} else {
|
||||
symbol.detail += ": " + data.type;
|
||||
}
|
||||
symbol.documentation = HANDLE_DOC(data.description);
|
||||
class_symbol.children.push_back(symbol);
|
||||
}
|
||||
|
||||
for (int i = 0; i < class_data.theme_properties.size(); i++) {
|
||||
const DocData::ThemeItemDoc &data = class_data.theme_properties[i];
|
||||
LSP::DocumentSymbol symbol;
|
||||
symbol.name = data.name;
|
||||
symbol.native_class = class_name;
|
||||
symbol.kind = LSP::SymbolKind::Property;
|
||||
symbol.detail = "<Theme> var " + class_name + "." + data.name + ": " + data.type;
|
||||
symbol.documentation = HANDLE_DOC(data.description);
|
||||
class_symbol.children.push_back(symbol);
|
||||
}
|
||||
|
||||
Vector<DocData::MethodDoc> method_likes;
|
||||
method_likes.append_array(class_data.methods);
|
||||
method_likes.append_array(class_data.annotations);
|
||||
const int constructors_start_idx = method_likes.size();
|
||||
method_likes.append_array(class_data.constructors);
|
||||
const int operator_start_idx = method_likes.size();
|
||||
method_likes.append_array(class_data.operators);
|
||||
const int signal_start_idx = method_likes.size();
|
||||
method_likes.append_array(class_data.signals);
|
||||
|
||||
for (int i = 0; i < method_likes.size(); i++) {
|
||||
const DocData::MethodDoc &data = method_likes[i];
|
||||
|
||||
LSP::DocumentSymbol symbol;
|
||||
symbol.name = data.name;
|
||||
symbol.native_class = class_name;
|
||||
|
||||
if (i >= signal_start_idx) {
|
||||
symbol.kind = LSP::SymbolKind::Event;
|
||||
} else if (i >= operator_start_idx) {
|
||||
symbol.kind = LSP::SymbolKind::Operator;
|
||||
} else if (i >= constructors_start_idx) {
|
||||
symbol.kind = LSP::SymbolKind::Constructor;
|
||||
} else {
|
||||
symbol.kind = LSP::SymbolKind::Method;
|
||||
}
|
||||
|
||||
String params = "";
|
||||
bool arg_default_value_started = false;
|
||||
for (int j = 0; j < data.arguments.size(); j++) {
|
||||
const DocData::ArgumentDoc &arg = data.arguments[j];
|
||||
|
||||
LSP::DocumentSymbol symbol_arg;
|
||||
symbol_arg.name = arg.name;
|
||||
symbol_arg.kind = LSP::SymbolKind::Variable;
|
||||
symbol_arg.detail = arg.type;
|
||||
|
||||
if (!arg_default_value_started && !arg.default_value.is_empty()) {
|
||||
arg_default_value_started = true;
|
||||
}
|
||||
String arg_str = arg.name + ": " + arg.type;
|
||||
if (arg_default_value_started) {
|
||||
arg_str += " = " + arg.default_value;
|
||||
}
|
||||
if (j < data.arguments.size() - 1) {
|
||||
arg_str += ", ";
|
||||
}
|
||||
params += arg_str;
|
||||
|
||||
symbol.children.push_back(symbol_arg);
|
||||
}
|
||||
if (data.qualifiers.contains("vararg")) {
|
||||
params += params.is_empty() ? "..." : ", ...";
|
||||
}
|
||||
|
||||
String return_type = data.return_type;
|
||||
if (return_type.is_empty()) {
|
||||
return_type = "void";
|
||||
}
|
||||
symbol.detail = "func " + class_name + "." + data.name + "(" + params + ") -> " + return_type;
|
||||
symbol.documentation = HANDLE_DOC(data.description);
|
||||
class_symbol.children.push_back(symbol);
|
||||
}
|
||||
|
||||
native_symbols.insert(class_name, class_symbol);
|
||||
}
|
||||
|
||||
reload_all_workspace_scripts();
|
||||
|
||||
if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
|
||||
for (const KeyValue<StringName, LSP::DocumentSymbol> &E : native_symbols) {
|
||||
ClassMembers members;
|
||||
const LSP::DocumentSymbol &class_symbol = E.value;
|
||||
for (int i = 0; i < class_symbol.children.size(); i++) {
|
||||
const LSP::DocumentSymbol &symbol = class_symbol.children[i];
|
||||
members.insert(symbol.name, &symbol);
|
||||
}
|
||||
native_members.insert(E.key, members);
|
||||
}
|
||||
|
||||
// Cache member completions.
|
||||
for (const KeyValue<String, ExtendGDScriptParser *> &S : scripts) {
|
||||
S.value->get_member_completions();
|
||||
}
|
||||
}
|
||||
|
||||
EditorNode *editor_node = EditorNode::get_singleton();
|
||||
editor_node->connect("script_add_function_request", callable_mp(this, &GDScriptWorkspace::apply_new_signal));
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error GDScriptWorkspace::parse_script(const String &p_path, const String &p_content) {
|
||||
ExtendGDScriptParser *parser = memnew(ExtendGDScriptParser);
|
||||
Error err = parser->parse(p_content, p_path);
|
||||
HashMap<String, ExtendGDScriptParser *>::Iterator last_parser = parse_results.find(p_path);
|
||||
HashMap<String, ExtendGDScriptParser *>::Iterator last_script = scripts.find(p_path);
|
||||
|
||||
if (err == OK) {
|
||||
remove_cache_parser(p_path);
|
||||
parse_results[p_path] = parser;
|
||||
scripts[p_path] = parser;
|
||||
|
||||
} else {
|
||||
if (last_parser && last_script && last_parser->value != last_script->value) {
|
||||
memdelete(last_parser->value);
|
||||
}
|
||||
parse_results[p_path] = parser;
|
||||
}
|
||||
|
||||
publish_diagnostics(p_path);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static bool is_valid_rename_target(const LSP::DocumentSymbol *p_symbol) {
|
||||
// Must be valid symbol.
|
||||
if (!p_symbol) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Cannot rename builtin.
|
||||
if (!p_symbol->native_class.is_empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Source must be available.
|
||||
if (p_symbol->script_path.is_empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Dictionary GDScriptWorkspace::rename(const LSP::TextDocumentPositionParams &p_doc_pos, const String &new_name) {
|
||||
LSP::WorkspaceEdit edit;
|
||||
|
||||
const LSP::DocumentSymbol *reference_symbol = resolve_symbol(p_doc_pos);
|
||||
if (is_valid_rename_target(reference_symbol)) {
|
||||
Vector<LSP::Location> usages = find_all_usages(*reference_symbol);
|
||||
for (int i = 0; i < usages.size(); ++i) {
|
||||
LSP::Location loc = usages[i];
|
||||
|
||||
edit.add_change(loc.uri, loc.range.start.line, loc.range.start.character, loc.range.end.character, new_name);
|
||||
}
|
||||
}
|
||||
|
||||
return edit.to_json();
|
||||
}
|
||||
|
||||
bool GDScriptWorkspace::can_rename(const LSP::TextDocumentPositionParams &p_doc_pos, LSP::DocumentSymbol &r_symbol, LSP::Range &r_range) {
|
||||
const LSP::DocumentSymbol *reference_symbol = resolve_symbol(p_doc_pos);
|
||||
if (!is_valid_rename_target(reference_symbol)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String path = get_file_path(p_doc_pos.textDocument.uri);
|
||||
if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
|
||||
// We only care about the range.
|
||||
_ALLOW_DISCARD_ parser->get_identifier_under_position(p_doc_pos.position, r_range);
|
||||
r_symbol = *reference_symbol;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector<LSP::Location> GDScriptWorkspace::find_usages_in_file(const LSP::DocumentSymbol &p_symbol, const String &p_file_path) {
|
||||
Vector<LSP::Location> usages;
|
||||
|
||||
String identifier = p_symbol.name;
|
||||
if (const ExtendGDScriptParser *parser = get_parse_result(p_file_path)) {
|
||||
const PackedStringArray &content = parser->get_lines();
|
||||
for (int i = 0; i < content.size(); ++i) {
|
||||
String line = content[i];
|
||||
|
||||
int character = line.find(identifier);
|
||||
while (character > -1) {
|
||||
LSP::TextDocumentPositionParams params;
|
||||
|
||||
LSP::TextDocumentIdentifier text_doc;
|
||||
text_doc.uri = get_file_uri(p_file_path);
|
||||
|
||||
params.textDocument = text_doc;
|
||||
params.position.line = i;
|
||||
params.position.character = character;
|
||||
|
||||
const LSP::DocumentSymbol *other_symbol = resolve_symbol(params);
|
||||
|
||||
if (other_symbol == &p_symbol) {
|
||||
LSP::Location loc;
|
||||
loc.uri = text_doc.uri;
|
||||
loc.range.start = params.position;
|
||||
loc.range.end.line = params.position.line;
|
||||
loc.range.end.character = params.position.character + identifier.length();
|
||||
usages.append(loc);
|
||||
}
|
||||
|
||||
character = line.find(identifier, character + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return usages;
|
||||
}
|
||||
|
||||
Vector<LSP::Location> GDScriptWorkspace::find_all_usages(const LSP::DocumentSymbol &p_symbol) {
|
||||
if (p_symbol.local) {
|
||||
// Only search in current document.
|
||||
return find_usages_in_file(p_symbol, p_symbol.script_path);
|
||||
}
|
||||
// Search in all documents.
|
||||
List<String> paths;
|
||||
list_script_files("res://", paths);
|
||||
|
||||
Vector<LSP::Location> usages;
|
||||
for (const String &path : paths) {
|
||||
usages.append_array(find_usages_in_file(p_symbol, path));
|
||||
}
|
||||
return usages;
|
||||
}
|
||||
|
||||
Error GDScriptWorkspace::parse_local_script(const String &p_path) {
|
||||
Error err;
|
||||
String content = FileAccess::get_file_as_string(p_path, &err);
|
||||
if (err == OK) {
|
||||
err = parse_script(p_path, content);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
String GDScriptWorkspace::get_file_path(const String &p_uri) {
|
||||
int port;
|
||||
String scheme;
|
||||
String host;
|
||||
String encoded_path;
|
||||
String fragment;
|
||||
|
||||
// Don't use the returned error, the result isn't OK for URIs that are not valid web URLs.
|
||||
p_uri.parse_url(scheme, host, port, encoded_path, fragment);
|
||||
|
||||
// TODO: Make the parsing RFC-3986 compliant.
|
||||
ERR_FAIL_COND_V_MSG(scheme != "file" && scheme != "file:" && scheme != "file://", String(), "LSP: The language server only supports the file protocol: " + p_uri);
|
||||
|
||||
// Treat host like authority for now and ignore the port. It's an edge case for invalid file URI's anyway.
|
||||
ERR_FAIL_COND_V_MSG(host != "" && host != "localhost", String(), "LSP: The language server does not support nonlocal files: " + p_uri);
|
||||
|
||||
// If query or fragment are present, the URI is not a valid file URI as per RFC-8089.
|
||||
// We currently don't handle the query and it will be part of the path. However,
|
||||
// this should not be a problem for a correct file URI.
|
||||
ERR_FAIL_COND_V_MSG(fragment != "", String(), "LSP: Received malformed file URI: " + p_uri);
|
||||
|
||||
String canonical_res = ProjectSettings::get_singleton()->get_resource_path();
|
||||
String simple_path = encoded_path.uri_file_decode().simplify_path();
|
||||
|
||||
// First try known paths that point to res://, to reduce file system interaction.
|
||||
bool res_adjusted = false;
|
||||
for (const String &res_path : absolute_res_paths) {
|
||||
if (simple_path.begins_with(res_path)) {
|
||||
res_adjusted = true;
|
||||
simple_path = "res://" + simple_path.substr(res_path.size());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Traverse the path and compare each directory with res://
|
||||
if (!res_adjusted) {
|
||||
Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
|
||||
|
||||
int offset = 0;
|
||||
while (offset <= simple_path.length()) {
|
||||
offset = simple_path.find_char('/', offset);
|
||||
if (offset == -1) {
|
||||
offset = simple_path.length();
|
||||
}
|
||||
|
||||
String part = simple_path.substr(0, offset);
|
||||
|
||||
if (!part.is_empty()) {
|
||||
bool is_equal = dir->is_equivalent(canonical_res, part);
|
||||
|
||||
if (is_equal) {
|
||||
absolute_res_paths.insert(part);
|
||||
res_adjusted = true;
|
||||
simple_path = "res://" + simple_path.substr(offset + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
offset += 1;
|
||||
}
|
||||
|
||||
// Could not resolve the path to the project.
|
||||
if (!res_adjusted) {
|
||||
return simple_path;
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve the file inside of the project using EditorFileSystem.
|
||||
EditorFileSystemDirectory *editor_dir;
|
||||
int file_idx;
|
||||
editor_dir = EditorFileSystem::get_singleton()->find_file(simple_path, &file_idx);
|
||||
if (editor_dir) {
|
||||
return editor_dir->get_file_path(file_idx);
|
||||
}
|
||||
|
||||
return simple_path;
|
||||
}
|
||||
|
||||
String GDScriptWorkspace::get_file_uri(const String &p_path) const {
|
||||
String path = ProjectSettings::get_singleton()->globalize_path(p_path).lstrip("/");
|
||||
LocalVector<String> encoded_parts;
|
||||
for (const String &part : path.split("/")) {
|
||||
encoded_parts.push_back(part.uri_encode());
|
||||
}
|
||||
|
||||
// Always return file URI's with authority part (encoding drive letters with leading slash), to maintain compat with RFC-1738 which required it.
|
||||
return "file:///" + String("/").join(encoded_parts);
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::publish_diagnostics(const String &p_path) {
|
||||
Dictionary params;
|
||||
Array errors;
|
||||
HashMap<String, ExtendGDScriptParser *>::ConstIterator ele = parse_results.find(p_path);
|
||||
if (ele) {
|
||||
const Vector<LSP::Diagnostic> &list = ele->value->get_diagnostics();
|
||||
errors.resize(list.size());
|
||||
for (int i = 0; i < list.size(); ++i) {
|
||||
errors[i] = list[i].to_json();
|
||||
}
|
||||
}
|
||||
params["diagnostics"] = errors;
|
||||
params["uri"] = get_file_uri(p_path);
|
||||
GDScriptLanguageProtocol::get_singleton()->notify_client("textDocument/publishDiagnostics", params);
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::_get_owners(EditorFileSystemDirectory *efsd, String p_path, List<String> &owners) {
|
||||
if (!efsd) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < efsd->get_subdir_count(); i++) {
|
||||
_get_owners(efsd->get_subdir(i), p_path, owners);
|
||||
}
|
||||
|
||||
for (int i = 0; i < efsd->get_file_count(); i++) {
|
||||
Vector<String> deps = efsd->get_file_deps(i);
|
||||
bool found = false;
|
||||
for (int j = 0; j < deps.size(); j++) {
|
||||
if (deps[j] == p_path) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
continue;
|
||||
}
|
||||
|
||||
owners.push_back(efsd->get_file_path(i));
|
||||
}
|
||||
}
|
||||
|
||||
Node *GDScriptWorkspace::_get_owner_scene_node(String p_path) {
|
||||
Node *owner_scene_node = nullptr;
|
||||
List<String> owners;
|
||||
|
||||
_get_owners(EditorFileSystem::get_singleton()->get_filesystem(), p_path, owners);
|
||||
|
||||
for (const String &owner : owners) {
|
||||
NodePath owner_path = owner;
|
||||
Ref<Resource> owner_res = ResourceLoader::load(String(owner_path));
|
||||
if (Object::cast_to<PackedScene>(owner_res.ptr())) {
|
||||
Ref<PackedScene> owner_packed_scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*owner_res));
|
||||
owner_scene_node = owner_packed_scene->instantiate();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return owner_scene_node;
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::completion(const LSP::CompletionParams &p_params, List<ScriptLanguage::CodeCompletionOption> *r_options) {
|
||||
String path = get_file_path(p_params.textDocument.uri);
|
||||
String call_hint;
|
||||
bool forced = false;
|
||||
|
||||
if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
|
||||
Node *owner_scene_node = _get_owner_scene_node(path);
|
||||
|
||||
Array stack;
|
||||
Node *current = nullptr;
|
||||
if (owner_scene_node != nullptr) {
|
||||
stack.push_back(owner_scene_node);
|
||||
|
||||
while (!stack.is_empty()) {
|
||||
current = Object::cast_to<Node>(stack.pop_back());
|
||||
Ref<GDScript> scr = current->get_script();
|
||||
if (scr.is_valid() && GDScript::is_canonically_equal_paths(scr->get_path(), path)) {
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < current->get_child_count(); ++i) {
|
||||
stack.push_back(current->get_child(i));
|
||||
}
|
||||
}
|
||||
|
||||
Ref<GDScript> scr = current->get_script();
|
||||
if (scr.is_null() || !GDScript::is_canonically_equal_paths(scr->get_path(), path)) {
|
||||
current = owner_scene_node;
|
||||
}
|
||||
}
|
||||
|
||||
String code = parser->get_text_for_completion(p_params.position);
|
||||
GDScriptLanguage::get_singleton()->complete_code(code, path, current, r_options, forced, call_hint);
|
||||
if (owner_scene_node) {
|
||||
memdelete(owner_scene_node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const LSP::DocumentSymbol *GDScriptWorkspace::resolve_symbol(const LSP::TextDocumentPositionParams &p_doc_pos, const String &p_symbol_name, bool p_func_required) {
|
||||
const LSP::DocumentSymbol *symbol = nullptr;
|
||||
|
||||
String path = get_file_path(p_doc_pos.textDocument.uri);
|
||||
if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
|
||||
String symbol_identifier = p_symbol_name;
|
||||
Vector<String> identifier_parts = symbol_identifier.split("(");
|
||||
if (identifier_parts.size()) {
|
||||
symbol_identifier = identifier_parts[0];
|
||||
}
|
||||
|
||||
LSP::Position pos = p_doc_pos.position;
|
||||
if (symbol_identifier.is_empty()) {
|
||||
LSP::Range range;
|
||||
symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, range);
|
||||
pos.character = range.end.character;
|
||||
}
|
||||
|
||||
if (!symbol_identifier.is_empty()) {
|
||||
if (ScriptServer::is_global_class(symbol_identifier)) {
|
||||
String class_path = ScriptServer::get_global_class_path(symbol_identifier);
|
||||
symbol = get_script_symbol(class_path);
|
||||
|
||||
} else {
|
||||
ScriptLanguage::LookupResult ret;
|
||||
if (symbol_identifier == "new" && parser->get_lines()[p_doc_pos.position.line].remove_chars(" \t").contains("new(")) {
|
||||
symbol_identifier = "_init";
|
||||
}
|
||||
if (OK == GDScriptLanguage::get_singleton()->lookup_code(parser->get_text_for_lookup_symbol(pos, symbol_identifier, p_func_required), symbol_identifier, path, nullptr, ret)) {
|
||||
if (ret.location >= 0) {
|
||||
String target_script_path = path;
|
||||
if (ret.script.is_valid()) {
|
||||
target_script_path = ret.script->get_path();
|
||||
} else if (!ret.script_path.is_empty()) {
|
||||
target_script_path = ret.script_path;
|
||||
}
|
||||
|
||||
if (const ExtendGDScriptParser *target_parser = get_parse_result(target_script_path)) {
|
||||
symbol = target_parser->get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(ret.location), symbol_identifier);
|
||||
|
||||
if (symbol) {
|
||||
switch (symbol->kind) {
|
||||
case LSP::SymbolKind::Function: {
|
||||
if (symbol->name != symbol_identifier) {
|
||||
symbol = get_parameter_symbol(symbol, symbol_identifier);
|
||||
}
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
String member = ret.class_member;
|
||||
if (member.is_empty() && symbol_identifier != ret.class_name) {
|
||||
member = symbol_identifier;
|
||||
}
|
||||
symbol = get_native_symbol(ret.class_name, member);
|
||||
}
|
||||
} else {
|
||||
symbol = get_local_symbol_at(parser, symbol_identifier, p_doc_pos.position);
|
||||
if (!symbol) {
|
||||
symbol = parser->get_member_symbol(symbol_identifier);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return symbol;
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::resolve_related_symbols(const LSP::TextDocumentPositionParams &p_doc_pos, List<const LSP::DocumentSymbol *> &r_list) {
|
||||
String path = get_file_path(p_doc_pos.textDocument.uri);
|
||||
if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
|
||||
String symbol_identifier;
|
||||
LSP::Range range;
|
||||
symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, range);
|
||||
|
||||
for (const KeyValue<StringName, ClassMembers> &E : native_members) {
|
||||
const ClassMembers &members = native_members.get(E.key);
|
||||
if (const LSP::DocumentSymbol *const *symbol = members.getptr(symbol_identifier)) {
|
||||
r_list.push_back(*symbol);
|
||||
}
|
||||
}
|
||||
|
||||
for (const KeyValue<String, ExtendGDScriptParser *> &E : scripts) {
|
||||
const ExtendGDScriptParser *scr = E.value;
|
||||
const ClassMembers &members = scr->get_members();
|
||||
if (const LSP::DocumentSymbol *const *symbol = members.getptr(symbol_identifier)) {
|
||||
r_list.push_back(*symbol);
|
||||
}
|
||||
|
||||
for (const KeyValue<String, ClassMembers> &F : scr->get_inner_classes()) {
|
||||
const ClassMembers *inner_class = &F.value;
|
||||
if (const LSP::DocumentSymbol *const *symbol = inner_class->getptr(symbol_identifier)) {
|
||||
r_list.push_back(*symbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const LSP::DocumentSymbol *GDScriptWorkspace::resolve_native_symbol(const LSP::NativeSymbolInspectParams &p_params) {
|
||||
if (HashMap<StringName, LSP::DocumentSymbol>::Iterator E = native_symbols.find(p_params.native_class)) {
|
||||
const LSP::DocumentSymbol &symbol = E->value;
|
||||
if (p_params.symbol_name.is_empty() || p_params.symbol_name == symbol.name) {
|
||||
return &symbol;
|
||||
}
|
||||
|
||||
for (int i = 0; i < symbol.children.size(); ++i) {
|
||||
if (symbol.children[i].name == p_params.symbol_name) {
|
||||
return &(symbol.children[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void GDScriptWorkspace::resolve_document_links(const String &p_uri, List<LSP::DocumentLink> &r_list) {
|
||||
if (const ExtendGDScriptParser *parser = get_parse_successed_script(get_file_path(p_uri))) {
|
||||
const List<LSP::DocumentLink> &links = parser->get_document_links();
|
||||
for (const LSP::DocumentLink &E : links) {
|
||||
r_list.push_back(E);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Dictionary GDScriptWorkspace::generate_script_api(const String &p_path) {
|
||||
Dictionary api;
|
||||
if (const ExtendGDScriptParser *parser = get_parse_successed_script(p_path)) {
|
||||
api = parser->generate_api();
|
||||
}
|
||||
return api;
|
||||
}
|
||||
|
||||
Error GDScriptWorkspace::resolve_signature(const LSP::TextDocumentPositionParams &p_doc_pos, LSP::SignatureHelp &r_signature) {
|
||||
if (const ExtendGDScriptParser *parser = get_parse_result(get_file_path(p_doc_pos.textDocument.uri))) {
|
||||
LSP::TextDocumentPositionParams text_pos;
|
||||
text_pos.textDocument = p_doc_pos.textDocument;
|
||||
|
||||
if (parser->get_left_function_call(p_doc_pos.position, text_pos.position, r_signature.activeParameter) == OK) {
|
||||
List<const LSP::DocumentSymbol *> symbols;
|
||||
|
||||
if (const LSP::DocumentSymbol *symbol = resolve_symbol(text_pos)) {
|
||||
symbols.push_back(symbol);
|
||||
} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
|
||||
GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_related_symbols(text_pos, symbols);
|
||||
}
|
||||
|
||||
for (const LSP::DocumentSymbol *const &symbol : symbols) {
|
||||
if (symbol->kind == LSP::SymbolKind::Method || symbol->kind == LSP::SymbolKind::Function) {
|
||||
LSP::SignatureInformation signature_info;
|
||||
signature_info.label = symbol->detail;
|
||||
signature_info.documentation = symbol->render();
|
||||
|
||||
for (int i = 0; i < symbol->children.size(); i++) {
|
||||
const LSP::DocumentSymbol &arg = symbol->children[i];
|
||||
LSP::ParameterInformation arg_info;
|
||||
arg_info.label = arg.name;
|
||||
signature_info.parameters.push_back(arg_info);
|
||||
}
|
||||
r_signature.signatures.push_back(signature_info);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (r_signature.signatures.size()) {
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ERR_METHOD_NOT_FOUND;
|
||||
}
|
||||
|
||||
GDScriptWorkspace::GDScriptWorkspace() {}
|
||||
|
||||
GDScriptWorkspace::~GDScriptWorkspace() {
|
||||
HashSet<String> cached_parsers;
|
||||
|
||||
for (const KeyValue<String, ExtendGDScriptParser *> &E : parse_results) {
|
||||
cached_parsers.insert(E.key);
|
||||
}
|
||||
|
||||
for (const KeyValue<String, ExtendGDScriptParser *> &E : scripts) {
|
||||
cached_parsers.insert(E.key);
|
||||
}
|
||||
|
||||
for (const String &E : cached_parsers) {
|
||||
remove_cache_parser(E);
|
||||
}
|
||||
}
|
104
modules/gdscript/language_server/gdscript_workspace.h
Normal file
104
modules/gdscript/language_server/gdscript_workspace.h
Normal file
@@ -0,0 +1,104 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_workspace.h */
|
||||
/**************************************************************************/
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../gdscript_parser.h"
|
||||
#include "gdscript_extend_parser.h"
|
||||
#include "godot_lsp.h"
|
||||
|
||||
#include "core/variant/variant.h"
|
||||
#include "editor/file_system/editor_file_system.h"
|
||||
|
||||
class GDScriptWorkspace : public RefCounted {
|
||||
GDCLASS(GDScriptWorkspace, RefCounted);
|
||||
|
||||
private:
|
||||
void _get_owners(EditorFileSystemDirectory *efsd, String p_path, List<String> &owners);
|
||||
Node *_get_owner_scene_node(String p_path);
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
void remove_cache_parser(const String &p_path);
|
||||
bool initialized = false;
|
||||
HashMap<StringName, LSP::DocumentSymbol> native_symbols;
|
||||
|
||||
// Absolute paths that are known to point to res://
|
||||
HashSet<String> absolute_res_paths;
|
||||
|
||||
const LSP::DocumentSymbol *get_native_symbol(const String &p_class, const String &p_member = "") const;
|
||||
const LSP::DocumentSymbol *get_script_symbol(const String &p_path) const;
|
||||
const LSP::DocumentSymbol *get_parameter_symbol(const LSP::DocumentSymbol *p_parent, const String &symbol_identifier);
|
||||
const LSP::DocumentSymbol *get_local_symbol_at(const ExtendGDScriptParser *p_parser, const String &p_symbol_identifier, const LSP::Position p_position);
|
||||
|
||||
void reload_all_workspace_scripts();
|
||||
|
||||
ExtendGDScriptParser *get_parse_successed_script(const String &p_path);
|
||||
ExtendGDScriptParser *get_parse_result(const String &p_path);
|
||||
|
||||
void list_script_files(const String &p_root_dir, List<String> &r_files);
|
||||
|
||||
void apply_new_signal(Object *obj, String function, PackedStringArray args);
|
||||
|
||||
public:
|
||||
String root;
|
||||
String root_uri;
|
||||
|
||||
HashMap<String, ExtendGDScriptParser *> scripts;
|
||||
HashMap<String, ExtendGDScriptParser *> parse_results;
|
||||
HashMap<StringName, ClassMembers> native_members;
|
||||
|
||||
public:
|
||||
Error initialize();
|
||||
|
||||
Error parse_script(const String &p_path, const String &p_content);
|
||||
Error parse_local_script(const String &p_path);
|
||||
|
||||
String get_file_path(const String &p_uri);
|
||||
String get_file_uri(const String &p_path) const;
|
||||
|
||||
void publish_diagnostics(const String &p_path);
|
||||
void completion(const LSP::CompletionParams &p_params, List<ScriptLanguage::CodeCompletionOption> *r_options);
|
||||
|
||||
const LSP::DocumentSymbol *resolve_symbol(const LSP::TextDocumentPositionParams &p_doc_pos, const String &p_symbol_name = "", bool p_func_required = false);
|
||||
void resolve_related_symbols(const LSP::TextDocumentPositionParams &p_doc_pos, List<const LSP::DocumentSymbol *> &r_list);
|
||||
const LSP::DocumentSymbol *resolve_native_symbol(const LSP::NativeSymbolInspectParams &p_params);
|
||||
void resolve_document_links(const String &p_uri, List<LSP::DocumentLink> &r_list);
|
||||
Dictionary generate_script_api(const String &p_path);
|
||||
Error resolve_signature(const LSP::TextDocumentPositionParams &p_doc_pos, LSP::SignatureHelp &r_signature);
|
||||
void didDeleteFiles(const Dictionary &p_params);
|
||||
Dictionary rename(const LSP::TextDocumentPositionParams &p_doc_pos, const String &new_name);
|
||||
bool can_rename(const LSP::TextDocumentPositionParams &p_doc_pos, LSP::DocumentSymbol &r_symbol, LSP::Range &r_range);
|
||||
Vector<LSP::Location> find_usages_in_file(const LSP::DocumentSymbol &p_symbol, const String &p_file_path);
|
||||
Vector<LSP::Location> find_all_usages(const LSP::DocumentSymbol &p_symbol);
|
||||
|
||||
GDScriptWorkspace();
|
||||
~GDScriptWorkspace();
|
||||
};
|
1992
modules/gdscript/language_server/godot_lsp.h
Normal file
1992
modules/gdscript/language_server/godot_lsp.h
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user