2019-06-14 16:38:54 +02:00
|
|
|
/**************************************************************************/
|
|
|
|
/* 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"
|
2020-06-08 11:00:07 +02:00
|
|
|
|
2019-06-14 16:38:54 +02:00
|
|
|
#include "../gdscript.h"
|
|
|
|
#include "../gdscript_parser.h"
|
2023-06-13 16:56:21 +02:00
|
|
|
#include "gdscript_language_protocol.h"
|
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/config/project_settings.h"
|
|
|
|
#include "core/object/script_language.h"
|
2020-11-29 04:42:06 +01:00
|
|
|
#include "editor/doc_tools.h"
|
2020-02-25 01:01:27 +01:00
|
|
|
#include "editor/editor_file_system.h"
|
2019-06-22 19:48:31 +02:00
|
|
|
#include "editor/editor_help.h"
|
2020-02-25 01:01:27 +01:00
|
|
|
#include "editor/editor_node.h"
|
2022-07-31 20:14:15 +02:00
|
|
|
#include "editor/editor_settings.h"
|
2020-02-25 01:01:27 +01:00
|
|
|
#include "scene/resources/packed_scene.h"
|
2019-06-14 16:38:54 +02:00
|
|
|
|
|
|
|
void GDScriptWorkspace::_bind_methods() {
|
2021-07-11 21:51:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("apply_new_signal"), &GDScriptWorkspace::apply_new_signal);
|
2021-04-10 21:10:29 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("didDeleteFiles"), &GDScriptWorkspace::did_delete_files);
|
2020-06-08 11:00:07 +02:00
|
|
|
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);
|
2019-06-14 16:38:54 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 21:51:31 +02:00
|
|
|
void GDScriptWorkspace::apply_new_signal(Object *obj, String function, PackedStringArray args) {
|
2022-09-29 11:53:28 +02:00
|
|
|
Ref<Script> scr = obj->get_script();
|
2021-07-11 21:51:31 +02:00
|
|
|
|
2022-09-29 11:53:28 +02:00
|
|
|
if (scr->get_language()->get_name() != "GDScript") {
|
2021-12-04 16:52:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String function_signature = "func " + function;
|
2022-09-29 11:53:28 +02:00
|
|
|
String source = scr->get_source_code();
|
2021-07-11 21:51:31 +02:00
|
|
|
|
2022-02-03 17:03:38 +01:00
|
|
|
if (source.contains(function_signature)) {
|
2021-07-11 21:51:31 +02:00
|
|
|
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;
|
|
|
|
|
2022-09-29 11:53:28 +02:00
|
|
|
String uri = get_file_uri(scr->get_path());
|
2021-07-11 21:51:31 +02:00
|
|
|
|
|
|
|
lsp::ApplyWorkspaceEditParams params;
|
|
|
|
params.edit.add_edit(uri, text_edit);
|
|
|
|
|
|
|
|
GDScriptLanguageProtocol::get_singleton()->request_client("workspace/applyEdit", params.to_json());
|
|
|
|
}
|
|
|
|
|
2021-04-10 21:10:29 +02:00
|
|
|
void GDScriptWorkspace::did_delete_files(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, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 16:38:54 +02:00
|
|
|
void GDScriptWorkspace::remove_cache_parser(const String &p_path) {
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<String, ExtendGDScriptParser *>::Iterator parser = parse_results.find(p_path);
|
2022-09-29 11:53:28 +02:00
|
|
|
HashMap<String, ExtendGDScriptParser *>::Iterator scr = scripts.find(p_path);
|
|
|
|
if (parser && scr) {
|
|
|
|
if (scr->value && scr->value == parser->value) {
|
|
|
|
memdelete(scr->value);
|
2019-06-14 16:38:54 +02:00
|
|
|
} else {
|
2022-09-29 11:53:28 +02:00
|
|
|
memdelete(scr->value);
|
2022-05-13 15:04:37 +02:00
|
|
|
memdelete(parser->value);
|
2019-06-14 16:38:54 +02:00
|
|
|
}
|
|
|
|
parse_results.erase(p_path);
|
|
|
|
scripts.erase(p_path);
|
|
|
|
} else if (parser) {
|
2022-05-13 15:04:37 +02:00
|
|
|
memdelete(parser->value);
|
2019-06-14 16:38:54 +02:00
|
|
|
parse_results.erase(p_path);
|
2022-09-29 11:53:28 +02:00
|
|
|
} else if (scr) {
|
|
|
|
memdelete(scr->value);
|
2019-06-14 16:38:54 +02:00
|
|
|
scripts.erase(p_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 19:48:31 +02:00
|
|
|
const lsp::DocumentSymbol *GDScriptWorkspace::get_native_symbol(const String &p_class, const String &p_member) const {
|
|
|
|
StringName class_name = p_class;
|
2019-06-24 12:25:12 +02:00
|
|
|
StringName empty;
|
2019-06-22 19:48:31 +02:00
|
|
|
|
2019-06-24 12:25:12 +02:00
|
|
|
while (class_name != empty) {
|
2022-05-13 15:04:37 +02:00
|
|
|
if (HashMap<StringName, lsp::DocumentSymbol>::ConstIterator E = native_symbols.find(class_name)) {
|
|
|
|
const lsp::DocumentSymbol &class_symbol = E->value;
|
2019-06-22 19:48:31 +02:00
|
|
|
|
2020-12-15 13:04:21 +01:00
|
|
|
if (p_member.is_empty()) {
|
2019-06-22 19:48:31 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class_name = ClassDB::get_parent_class(class_name);
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const lsp::DocumentSymbol *GDScriptWorkspace::get_script_symbol(const String &p_path) const {
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<String, ExtendGDScriptParser *>::ConstIterator S = scripts.find(p_path);
|
2019-06-22 19:48:31 +02:00
|
|
|
if (S) {
|
2022-05-13 15:04:37 +02:00
|
|
|
return &(S->value->get_symbols());
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
|
|
|
|
2021-04-27 18:53:55 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
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`.
|
2021-04-27 18:53:55 +02:00
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-04-27 18:53:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
return best_match;
|
2021-04-27 18:53:55 +02:00
|
|
|
}
|
|
|
|
|
2019-06-22 19:48:31 +02:00
|
|
|
void GDScriptWorkspace::reload_all_workspace_scripts() {
|
2019-11-22 08:35:03 +01:00
|
|
|
List<String> paths;
|
|
|
|
list_script_files("res://", paths);
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const String &path : paths) {
|
2019-06-22 19:48:31 +02:00
|
|
|
Error err;
|
|
|
|
String content = FileAccess::get_file_as_string(path, &err);
|
|
|
|
ERR_CONTINUE(err != OK);
|
|
|
|
err = parse_script(path, content);
|
|
|
|
|
|
|
|
if (err != OK) {
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<String, ExtendGDScriptParser *>::Iterator S = parse_results.find(path);
|
2019-06-22 19:48:31 +02:00
|
|
|
String err_msg = "Failed parse script " + path;
|
|
|
|
if (S) {
|
2022-05-13 15:04:37 +02:00
|
|
|
err_msg += "\n" + S->value->get_errors()[0].message;
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
2019-11-11 10:24:04 +01:00
|
|
|
ERR_CONTINUE_MSG(err != OK, err_msg);
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptWorkspace::list_script_files(const String &p_root_dir, List<String> &r_files) {
|
|
|
|
Error err;
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<DirAccess> dir = DirAccess::open(p_root_dir, &err);
|
2019-06-22 19:48:31 +02:00
|
|
|
if (OK == err) {
|
|
|
|
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 != "./") {
|
2022-08-30 02:34:01 +02:00
|
|
|
list_script_files(p_root_dir.path_join(file_name), r_files);
|
2019-06-22 19:48:31 +02:00
|
|
|
} else if (file_name.ends_with(".gd")) {
|
2022-08-30 02:34:01 +02:00
|
|
|
String script_file = p_root_dir.path_join(file_name);
|
2019-06-22 19:48:31 +02:00
|
|
|
r_files.push_back(script_file);
|
|
|
|
}
|
|
|
|
file_name = dir->get_next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExtendGDScriptParser *GDScriptWorkspace::get_parse_successed_script(const String &p_path) {
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<String, ExtendGDScriptParser *>::Iterator S = scripts.find(p_path);
|
2019-06-22 19:48:31 +02:00
|
|
|
if (!S) {
|
|
|
|
parse_local_script(p_path);
|
|
|
|
S = scripts.find(p_path);
|
|
|
|
}
|
|
|
|
if (S) {
|
2022-05-13 15:04:37 +02:00
|
|
|
return S->value;
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ExtendGDScriptParser *GDScriptWorkspace::get_parse_result(const String &p_path) {
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<String, ExtendGDScriptParser *>::Iterator S = parse_results.find(p_path);
|
2019-06-22 19:48:31 +02:00
|
|
|
if (!S) {
|
2019-06-23 15:10:28 +02:00
|
|
|
parse_local_script(p_path);
|
2019-06-22 19:48:31 +02:00
|
|
|
S = parse_results.find(p_path);
|
|
|
|
}
|
|
|
|
if (S) {
|
2022-05-13 15:04:37 +02:00
|
|
|
return S->value;
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Error GDScriptWorkspace::initialize() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (initialized) {
|
2019-06-22 19:48:31 +02:00
|
|
|
return OK;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-06-22 19:48:31 +02:00
|
|
|
|
2020-11-29 04:42:06 +01:00
|
|
|
DocTools *doc = EditorHelp::get_doc_data();
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<String, DocData::ClassDoc> &E : doc->class_list) {
|
|
|
|
const DocData::ClassDoc &class_data = E.value;
|
2019-06-22 19:48:31 +02:00
|
|
|
lsp::DocumentSymbol class_symbol;
|
2021-08-09 22:13:42 +02:00
|
|
|
String class_name = E.key;
|
2019-06-22 19:48:31 +02:00
|
|
|
class_symbol.name = class_name;
|
2019-06-30 10:10:13 +02:00
|
|
|
class_symbol.native_class = class_name;
|
2019-06-22 19:48:31 +02:00
|
|
|
class_symbol.kind = lsp::SymbolKind::Class;
|
|
|
|
class_symbol.detail = String("<Native> class ") + class_name;
|
2020-12-15 13:04:21 +01:00
|
|
|
if (!class_data.inherits.is_empty()) {
|
2019-06-22 19:48:31 +02:00
|
|
|
class_symbol.detail += " extends " + class_data.inherits;
|
|
|
|
}
|
2019-10-05 08:07:10 +02:00
|
|
|
class_symbol.documentation = class_data.brief_description + "\n" + class_data.description;
|
2019-06-22 19:48:31 +02:00
|
|
|
|
|
|
|
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;
|
2019-06-30 10:10:13 +02:00
|
|
|
symbol.native_class = class_name;
|
2019-06-22 19:48:31 +02:00
|
|
|
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;
|
2019-10-05 08:07:10 +02:00
|
|
|
symbol.documentation = const_data.description;
|
2019-06-22 19:48:31 +02:00
|
|
|
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;
|
2019-06-30 10:10:13 +02:00
|
|
|
symbol.native_class = class_name;
|
2019-06-22 19:48:31 +02:00
|
|
|
symbol.kind = lsp::SymbolKind::Property;
|
2021-08-04 18:54:41 +02:00
|
|
|
symbol.detail = "var " + class_name + "." + data.name;
|
2019-06-22 19:48:31 +02:00
|
|
|
if (data.enumeration.length()) {
|
|
|
|
symbol.detail += ": " + data.enumeration;
|
|
|
|
} else {
|
|
|
|
symbol.detail += ": " + data.type;
|
|
|
|
}
|
2019-10-05 08:07:10 +02:00
|
|
|
symbol.documentation = data.description;
|
2019-06-22 19:48:31 +02:00
|
|
|
class_symbol.children.push_back(symbol);
|
|
|
|
}
|
|
|
|
|
2021-08-04 18:54:41 +02:00
|
|
|
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 = data.description;
|
|
|
|
class_symbol.children.push_back(symbol);
|
|
|
|
}
|
|
|
|
|
2019-06-22 19:48:31 +02:00
|
|
|
Vector<DocData::MethodDoc> methods_signals;
|
2021-09-21 04:49:02 +02:00
|
|
|
methods_signals.append_array(class_data.constructors);
|
2019-06-22 19:48:31 +02:00
|
|
|
methods_signals.append_array(class_data.methods);
|
2021-09-21 04:49:02 +02:00
|
|
|
methods_signals.append_array(class_data.operators);
|
2019-06-22 19:48:31 +02:00
|
|
|
const int signal_start_idx = methods_signals.size();
|
|
|
|
methods_signals.append_array(class_data.signals);
|
|
|
|
|
|
|
|
for (int i = 0; i < methods_signals.size(); i++) {
|
|
|
|
const DocData::MethodDoc &data = methods_signals[i];
|
|
|
|
|
|
|
|
lsp::DocumentSymbol symbol;
|
|
|
|
symbol.name = data.name;
|
2019-06-30 10:10:13 +02:00
|
|
|
symbol.native_class = class_name;
|
2019-06-22 19:48:31 +02:00
|
|
|
symbol.kind = i >= signal_start_idx ? lsp::SymbolKind::Event : 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];
|
2020-01-08 17:44:32 +01:00
|
|
|
|
|
|
|
lsp::DocumentSymbol symbol_arg;
|
|
|
|
symbol_arg.name = arg.name;
|
|
|
|
symbol_arg.kind = lsp::SymbolKind::Variable;
|
|
|
|
symbol_arg.detail = arg.type;
|
|
|
|
|
2020-12-15 13:04:21 +01:00
|
|
|
if (!arg_default_value_started && !arg.default_value.is_empty()) {
|
2019-06-22 19:48:31 +02:00
|
|
|
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;
|
2020-01-08 17:44:32 +01:00
|
|
|
|
|
|
|
symbol.children.push_back(symbol_arg);
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
2022-02-03 17:03:38 +01:00
|
|
|
if (data.qualifiers.contains("vararg")) {
|
2020-12-15 13:04:21 +01:00
|
|
|
params += params.is_empty() ? "..." : ", ...";
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
|
|
|
|
2019-10-09 04:01:48 +02:00
|
|
|
String return_type = data.return_type;
|
2020-12-15 13:04:21 +01:00
|
|
|
if (return_type.is_empty()) {
|
2019-10-09 04:01:48 +02:00
|
|
|
return_type = "void";
|
|
|
|
}
|
|
|
|
symbol.detail = "func " + class_name + "." + data.name + "(" + params + ") -> " + return_type;
|
2019-10-05 08:07:10 +02:00
|
|
|
symbol.documentation = data.description;
|
2019-06-22 19:48:31 +02:00
|
|
|
class_symbol.children.push_back(symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
native_symbols.insert(class_name, class_symbol);
|
|
|
|
}
|
|
|
|
|
2019-06-25 06:12:41 +02:00
|
|
|
reload_all_workspace_scripts();
|
|
|
|
|
2019-06-23 15:10:28 +02:00
|
|
|
if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<StringName, lsp::DocumentSymbol> &E : native_symbols) {
|
2019-06-24 12:25:12 +02:00
|
|
|
ClassMembers members;
|
2021-08-09 22:13:42 +02:00
|
|
|
const lsp::DocumentSymbol &class_symbol = E.value;
|
2019-06-23 15:10:28 +02:00
|
|
|
for (int i = 0; i < class_symbol.children.size(); i++) {
|
|
|
|
const lsp::DocumentSymbol &symbol = class_symbol.children[i];
|
2022-05-08 10:09:19 +02:00
|
|
|
members.insert(symbol.name, &symbol);
|
2019-06-23 15:10:28 +02:00
|
|
|
}
|
2022-05-08 10:09:19 +02:00
|
|
|
native_members.insert(E.key, members);
|
2019-06-23 15:10:28 +02:00
|
|
|
}
|
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
// Cache member completions.
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<String, ExtendGDScriptParser *> &S : scripts) {
|
|
|
|
S.value->get_member_completions();
|
2019-06-25 06:12:41 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-22 19:48:31 +02:00
|
|
|
|
2021-07-11 21:51:31 +02:00
|
|
|
EditorNode *editor_node = EditorNode::get_singleton();
|
|
|
|
editor_node->connect("script_add_function_request", callable_mp(this, &GDScriptWorkspace::apply_new_signal));
|
|
|
|
|
2019-06-22 19:48:31 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2019-06-14 16:38:54 +02:00
|
|
|
Error GDScriptWorkspace::parse_script(const String &p_path, const String &p_content) {
|
|
|
|
ExtendGDScriptParser *parser = memnew(ExtendGDScriptParser);
|
|
|
|
Error err = parser->parse(p_content, p_path);
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<String, ExtendGDScriptParser *>::Iterator last_parser = parse_results.find(p_path);
|
|
|
|
HashMap<String, ExtendGDScriptParser *>::Iterator last_script = scripts.find(p_path);
|
2019-06-14 16:38:54 +02:00
|
|
|
|
|
|
|
if (err == OK) {
|
|
|
|
remove_cache_parser(p_path);
|
|
|
|
parse_results[p_path] = parser;
|
|
|
|
scripts[p_path] = parser;
|
2019-06-23 15:10:28 +02:00
|
|
|
|
2019-06-14 16:38:54 +02:00
|
|
|
} else {
|
2022-05-13 15:04:37 +02:00
|
|
|
if (last_parser && last_script && last_parser->value != last_script->value) {
|
|
|
|
memdelete(last_parser->value);
|
2019-06-14 16:38:54 +02:00
|
|
|
}
|
|
|
|
parse_results[p_path] = parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
publish_diagnostics(p_path);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
static bool is_valid_rename_target(const lsp::DocumentSymbol *p_symbol) {
|
|
|
|
// Must be valid symbol.
|
|
|
|
if (!p_symbol) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-27 18:53:55 +02:00
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
// 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) {
|
2021-04-27 18:53:55 +02:00
|
|
|
lsp::WorkspaceEdit edit;
|
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2021-04-27 18:53:55 +02:00
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
return edit.to_json();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDScriptWorkspace::can_rename(const lsp::TextDocumentPositionParams &p_doc_pos, lsp::DocumentSymbol &r_symbol, lsp::Range &r_range) {
|
2021-04-27 18:53:55 +02:00
|
|
|
const lsp::DocumentSymbol *reference_symbol = resolve_symbol(p_doc_pos);
|
2023-07-28 17:06:08 +02:00
|
|
|
if (!is_valid_rename_target(reference_symbol)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-27 18:53:55 +02:00
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
String path = get_file_path(p_doc_pos.textDocument.uri);
|
|
|
|
if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
|
|
|
|
parser->get_identifier_under_position(p_doc_pos.position, r_range);
|
|
|
|
r_symbol = *reference_symbol;
|
|
|
|
return true;
|
|
|
|
}
|
2021-04-27 18:53:55 +02:00
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-27 18:53:55 +02:00
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
Vector<lsp::Location> GDScriptWorkspace::find_usages_in_file(const lsp::DocumentSymbol &p_symbol, const String &p_file_path) {
|
|
|
|
Vector<lsp::Location> usages;
|
2021-04-27 18:53:55 +02:00
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
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];
|
2021-04-27 18:53:55 +02:00
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
int character = line.find(identifier);
|
|
|
|
while (character > -1) {
|
|
|
|
lsp::TextDocumentPositionParams params;
|
2021-04-27 18:53:55 +02:00
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
lsp::TextDocumentIdentifier text_doc;
|
|
|
|
text_doc.uri = get_file_uri(p_file_path);
|
2021-04-27 18:53:55 +02:00
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
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);
|
2021-04-27 18:53:55 +02:00
|
|
|
}
|
2023-07-28 17:06:08 +02:00
|
|
|
|
|
|
|
character = line.find(identifier, character + 1);
|
2021-04-27 18:53:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 17:06:08 +02:00
|
|
|
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 (List<String>::Element *PE = paths.front(); PE; PE = PE->next()) {
|
|
|
|
usages.append_array(find_usages_in_file(p_symbol, PE->get()));
|
|
|
|
}
|
|
|
|
return usages;
|
2021-04-27 18:53:55 +02:00
|
|
|
}
|
|
|
|
|
2019-06-22 19:48:31 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-14 16:38:54 +02:00
|
|
|
String GDScriptWorkspace::get_file_path(const String &p_uri) const {
|
2022-08-08 23:25:03 +02:00
|
|
|
String path = p_uri.uri_decode();
|
|
|
|
String base_uri = root_uri.uri_decode();
|
|
|
|
path = path.replacen(base_uri + "/", "res://");
|
2019-06-24 12:25:12 +02:00
|
|
|
return path;
|
2019-06-14 16:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String GDScriptWorkspace::get_file_uri(const String &p_path) const {
|
2019-06-24 12:25:12 +02:00
|
|
|
String uri = p_path;
|
|
|
|
uri = uri.replace("res://", root_uri + "/");
|
|
|
|
return uri;
|
2019-06-14 16:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptWorkspace::publish_diagnostics(const String &p_path) {
|
|
|
|
Dictionary params;
|
|
|
|
Array errors;
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<String, ExtendGDScriptParser *>::ConstIterator ele = parse_results.find(p_path);
|
2019-06-14 16:38:54 +02:00
|
|
|
if (ele) {
|
2022-05-13 15:04:37 +02:00
|
|
|
const Vector<lsp::Diagnostic> &list = ele->value->get_diagnostics();
|
2019-06-14 16:38:54 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-02-25 01:01:27 +01:00
|
|
|
void GDScriptWorkspace::_get_owners(EditorFileSystemDirectory *efsd, String p_path, List<String> &owners) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!efsd) {
|
2020-02-25 01:01:27 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-02-25 01:01:27 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!found) {
|
2020-02-25 01:01:27 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-02-25 01:01:27 +01:00
|
|
|
|
|
|
|
owners.push_back(efsd->get_file_path(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 19:53:30 +01:00
|
|
|
Node *GDScriptWorkspace::_get_owner_scene_node(String p_path) {
|
2020-04-02 01:20:12 +02:00
|
|
|
Node *owner_scene_node = nullptr;
|
2020-02-25 01:01:27 +01:00
|
|
|
List<String> owners;
|
|
|
|
|
|
|
|
_get_owners(EditorFileSystem::get_singleton()->get_filesystem(), p_path, owners);
|
|
|
|
|
2020-02-29 19:53:30 +01:00
|
|
|
for (int i = 0; i < owners.size(); i++) {
|
|
|
|
NodePath owner_path = owners[i];
|
2022-05-03 01:43:50 +02:00
|
|
|
Ref<Resource> owner_res = ResourceLoader::load(owner_path);
|
2020-02-29 19:53:30 +01:00
|
|
|
if (Object::cast_to<PackedScene>(owner_res.ptr())) {
|
|
|
|
Ref<PackedScene> owner_packed_scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*owner_res));
|
2021-06-18 00:03:09 +02:00
|
|
|
owner_scene_node = owner_packed_scene->instantiate();
|
2020-02-29 19:53:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-02-25 01:01:27 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 19:53:30 +01:00
|
|
|
return owner_scene_node;
|
2020-02-25 01:01:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 16:48:43 +01:00
|
|
|
void GDScriptWorkspace::completion(const lsp::CompletionParams &p_params, List<ScriptLanguage::CodeCompletionOption> *r_options) {
|
2019-06-14 16:38:54 +02:00
|
|
|
String path = get_file_path(p_params.textDocument.uri);
|
|
|
|
String call_hint;
|
|
|
|
bool forced = false;
|
2019-06-23 15:10:28 +02:00
|
|
|
|
|
|
|
if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
|
2020-02-29 19:53:30 +01:00
|
|
|
Node *owner_scene_node = _get_owner_scene_node(path);
|
2021-08-05 17:45:37 +02:00
|
|
|
|
|
|
|
Array stack;
|
|
|
|
Node *current = nullptr;
|
2021-08-06 20:55:05 +02:00
|
|
|
if (owner_scene_node != nullptr) {
|
|
|
|
stack.push_back(owner_scene_node);
|
|
|
|
|
|
|
|
while (!stack.is_empty()) {
|
2022-01-25 16:37:41 +01:00
|
|
|
current = Object::cast_to<Node>(stack.pop_back());
|
2022-09-29 11:53:28 +02:00
|
|
|
Ref<GDScript> scr = current->get_script();
|
2024-02-15 15:16:39 +01:00
|
|
|
if (scr.is_valid() && GDScript::is_equal_gdscript_paths(scr->get_path(), path)) {
|
2021-08-06 20:55:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < current->get_child_count(); ++i) {
|
|
|
|
stack.push_back(current->get_child(i));
|
|
|
|
}
|
|
|
|
}
|
2021-08-05 17:45:37 +02:00
|
|
|
|
2022-09-29 11:53:28 +02:00
|
|
|
Ref<GDScript> scr = current->get_script();
|
2024-02-15 15:16:39 +01:00
|
|
|
if (!scr.is_valid() || !GDScript::is_equal_gdscript_paths(scr->get_path(), path)) {
|
2021-08-06 20:55:05 +02:00
|
|
|
current = owner_scene_node;
|
2021-08-05 17:45:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 15:10:28 +02:00
|
|
|
String code = parser->get_text_for_completion(p_params.position);
|
2021-08-05 17:45:37 +02:00
|
|
|
GDScriptLanguage::get_singleton()->complete_code(code, path, current, r_options, forced, call_hint);
|
2020-02-29 19:53:30 +01:00
|
|
|
if (owner_scene_node) {
|
|
|
|
memdelete(owner_scene_node);
|
2020-02-25 01:01:27 +01:00
|
|
|
}
|
2019-06-14 16:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 18:15:00 +02:00
|
|
|
const lsp::DocumentSymbol *GDScriptWorkspace::resolve_symbol(const lsp::TextDocumentPositionParams &p_doc_pos, const String &p_symbol_name, bool p_func_required) {
|
2020-04-02 01:20:12 +02:00
|
|
|
const lsp::DocumentSymbol *symbol = nullptr;
|
2019-06-22 19:48:31 +02:00
|
|
|
|
|
|
|
String path = get_file_path(p_doc_pos.textDocument.uri);
|
|
|
|
if (const ExtendGDScriptParser *parser = get_parse_result(path)) {
|
|
|
|
String symbol_identifier = p_symbol_name;
|
2019-08-11 08:06:39 +02:00
|
|
|
Vector<String> identifier_parts = symbol_identifier.split("(");
|
|
|
|
if (identifier_parts.size()) {
|
|
|
|
symbol_identifier = identifier_parts[0];
|
|
|
|
}
|
|
|
|
|
2019-06-22 19:48:31 +02:00
|
|
|
lsp::Position pos = p_doc_pos.position;
|
2020-12-15 13:04:21 +01:00
|
|
|
if (symbol_identifier.is_empty()) {
|
2023-07-28 17:06:08 +02:00
|
|
|
lsp::Range range;
|
|
|
|
symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, range);
|
|
|
|
pos.character = range.end.character;
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
|
|
|
|
2020-12-15 13:04:21 +01:00
|
|
|
if (!symbol_identifier.is_empty()) {
|
2019-06-22 19:48:31 +02:00
|
|
|
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;
|
2021-09-26 18:15:00 +02:00
|
|
|
if (symbol_identifier == "new" && parser->get_lines()[p_doc_pos.position.line].replace(" ", "").replace("\t", "").find("new(") > -1) {
|
|
|
|
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)) {
|
2022-03-26 16:48:43 +01:00
|
|
|
if (ret.type == ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION) {
|
2019-06-22 19:48:31 +02:00
|
|
|
String target_script_path = path;
|
|
|
|
if (!ret.script.is_null()) {
|
|
|
|
target_script_path = ret.script->get_path();
|
2021-04-27 18:53:55 +02:00
|
|
|
} else if (!ret.class_path.is_empty()) {
|
|
|
|
target_script_path = ret.class_path;
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const ExtendGDScriptParser *target_parser = get_parse_result(target_script_path)) {
|
2023-07-28 17:06:08 +02:00
|
|
|
symbol = target_parser->get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(ret.location), symbol_identifier);
|
2021-04-27 18:53:55 +02:00
|
|
|
|
2023-04-15 18:29:11 +02:00
|
|
|
if (symbol) {
|
|
|
|
switch (symbol->kind) {
|
|
|
|
case lsp::SymbolKind::Function: {
|
|
|
|
if (symbol->name != symbol_identifier) {
|
|
|
|
symbol = get_parameter_symbol(symbol, symbol_identifier);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
2021-04-27 18:53:55 +02:00
|
|
|
}
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
String member = ret.class_member;
|
2020-12-15 13:04:21 +01:00
|
|
|
if (member.is_empty() && symbol_identifier != ret.class_name) {
|
2019-06-22 19:48:31 +02:00
|
|
|
member = symbol_identifier;
|
|
|
|
}
|
|
|
|
symbol = get_native_symbol(ret.class_name, member);
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-28 17:06:08 +02:00
|
|
|
symbol = get_local_symbol_at(parser, symbol_identifier, p_doc_pos.position);
|
2021-04-27 18:53:55 +02:00
|
|
|
if (!symbol) {
|
2023-07-28 17:06:08 +02:00
|
|
|
symbol = parser->get_member_symbol(symbol_identifier);
|
2021-04-27 18:53:55 +02:00
|
|
|
}
|
2019-06-22 19:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
2019-06-23 15:10:28 +02:00
|
|
|
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;
|
2023-07-28 17:06:08 +02:00
|
|
|
lsp::Range range;
|
|
|
|
symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, range);
|
2019-06-23 15:10:28 +02:00
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
for (const KeyValue<StringName, ClassMembers> &E : native_members) {
|
|
|
|
const ClassMembers &members = native_members.get(E.key);
|
2019-06-24 12:25:12 +02:00
|
|
|
if (const lsp::DocumentSymbol *const *symbol = members.getptr(symbol_identifier)) {
|
|
|
|
r_list.push_back(*symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<String, ExtendGDScriptParser *> &E : scripts) {
|
2022-09-29 11:53:28 +02:00
|
|
|
const ExtendGDScriptParser *scr = E.value;
|
|
|
|
const ClassMembers &members = scr->get_members();
|
2019-06-24 12:25:12 +02:00
|
|
|
if (const lsp::DocumentSymbol *const *symbol = members.getptr(symbol_identifier)) {
|
|
|
|
r_list.push_back(*symbol);
|
2019-06-23 15:10:28 +02:00
|
|
|
}
|
2019-06-25 06:12:41 +02:00
|
|
|
|
2022-09-29 11:53:28 +02:00
|
|
|
for (const KeyValue<String, ClassMembers> &F : scr->get_inner_classes()) {
|
2022-05-08 10:09:19 +02:00
|
|
|
const ClassMembers *inner_class = &F.value;
|
2019-06-25 06:12:41 +02:00
|
|
|
if (const lsp::DocumentSymbol *const *symbol = inner_class->getptr(symbol_identifier)) {
|
|
|
|
r_list.push_back(*symbol);
|
|
|
|
}
|
|
|
|
}
|
2019-06-23 15:10:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 14:53:40 +02:00
|
|
|
const lsp::DocumentSymbol *GDScriptWorkspace::resolve_native_symbol(const lsp::NativeSymbolInspectParams &p_params) {
|
2022-05-13 15:04:37 +02:00
|
|
|
if (HashMap<StringName, lsp::DocumentSymbol>::Iterator E = native_symbols.find(p_params.native_class)) {
|
|
|
|
const lsp::DocumentSymbol &symbol = E->value;
|
2020-12-15 13:04:21 +01:00
|
|
|
if (p_params.symbol_name.is_empty() || p_params.symbol_name == symbol.name) {
|
2019-10-04 14:53:40 +02:00
|
|
|
return &symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < symbol.children.size(); ++i) {
|
|
|
|
if (symbol.children[i].name == p_params.symbol_name) {
|
|
|
|
return &(symbol.children[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2019-10-04 14:53:40 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 10:26:55 +02:00
|
|
|
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();
|
2021-07-16 05:45:57 +02:00
|
|
|
for (const lsp::DocumentLink &E : links) {
|
|
|
|
r_list.push_back(E);
|
2019-10-03 10:26:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:21:42 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:44:32 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const lsp::DocumentSymbol *const &symbol : symbols) {
|
2020-01-08 17:44:32 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-14 16:38:54 +02:00
|
|
|
GDScriptWorkspace::GDScriptWorkspace() {
|
|
|
|
ProjectSettings::get_singleton()->get_resource_path();
|
|
|
|
}
|
|
|
|
|
|
|
|
GDScriptWorkspace::~GDScriptWorkspace() {
|
2022-05-19 17:00:06 +02:00
|
|
|
HashSet<String> cached_parsers;
|
2019-06-22 19:48:31 +02:00
|
|
|
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<String, ExtendGDScriptParser *> &E : parse_results) {
|
|
|
|
cached_parsers.insert(E.key);
|
2019-06-14 16:38:54 +02:00
|
|
|
}
|
2019-06-22 19:48:31 +02:00
|
|
|
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<String, ExtendGDScriptParser *> &E : scripts) {
|
|
|
|
cached_parsers.insert(E.key);
|
2019-06-14 16:38:54 +02:00
|
|
|
}
|
2019-06-22 19:48:31 +02:00
|
|
|
|
2022-05-19 01:43:40 +02:00
|
|
|
for (const String &E : cached_parsers) {
|
|
|
|
remove_cache_parser(E);
|
2019-06-14 16:38:54 +02:00
|
|
|
}
|
|
|
|
}
|