Removed gdscript's language server.

This commit is contained in:
Relintai 2023-12-16 18:51:51 +01:00
parent 6016061c54
commit f54791d022
12 changed files with 0 additions and 4991 deletions

View File

@ -1,818 +0,0 @@
/**************************************************************************/
/* gdscript_extend_parser.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_extend_parser.h"
#include "../gdscript.h"
#include "core/io/json.h"
#include "gdscript_language_protocol.h"
#include "gdscript_workspace.h"
void ExtendGDScriptParser::update_diagnostics() {
diagnostics.clear();
if (has_error()) {
lsp::Diagnostic diagnostic;
diagnostic.severity = lsp::DiagnosticSeverity::Error;
diagnostic.message = get_error();
diagnostic.source = "gdscript";
diagnostic.code = -1;
lsp::Range range;
lsp::Position pos;
int line = LINE_NUMBER_TO_INDEX(get_error_line());
const String &line_text = get_lines()[line];
pos.line = line;
pos.character = line_text.length() - line_text.strip_edges(true, false).length();
range.start = pos;
range.end = range.start;
range.end.character = line_text.strip_edges(false).length();
diagnostic.range = range;
diagnostics.push_back(diagnostic);
}
const List<GDScriptWarning> &warnings = get_warnings();
for (const List<GDScriptWarning>::Element *E = warnings.front(); E; E = E->next()) {
const GDScriptWarning &warning = E->get();
lsp::Diagnostic diagnostic;
diagnostic.severity = lsp::DiagnosticSeverity::Warning;
diagnostic.message = "(" + warning.get_name() + "): " + warning.get_message();
diagnostic.source = "gdscript";
diagnostic.code = warning.code;
lsp::Range range;
lsp::Position pos;
int line = LINE_NUMBER_TO_INDEX(warning.line);
const String &line_text = get_lines()[line];
pos.line = line;
pos.character = line_text.length() - line_text.strip_edges(true, false).length();
range.start = pos;
range.end = pos;
range.end.character = line_text.strip_edges(false).length();
diagnostic.range = range;
diagnostics.push_back(diagnostic);
}
}
void ExtendGDScriptParser::update_symbols() {
members.clear();
const GDScriptParser::Node *head = get_parse_tree();
if (head && head->type == Node::TYPE_CLASS) {
const GDScriptParser::ClassNode *gdclass = static_cast<const GDScriptParser::ClassNode *>(head);
parse_class_symbol(gdclass, class_symbol);
for (int i = 0; i < class_symbol.children.size(); i++) {
const lsp::DocumentSymbol &symbol = class_symbol.children[i];
members.set(symbol.name, &symbol);
// cache level one inner classes
if (symbol.kind == lsp::SymbolKind::Class) {
ClassMembers inner_class;
for (int j = 0; j < symbol.children.size(); j++) {
const lsp::DocumentSymbol &s = symbol.children[j];
inner_class.set(s.name, &s);
}
inner_classes.set(symbol.name, inner_class);
}
}
}
}
void ExtendGDScriptParser::update_document_links(const String &p_code) {
document_links.clear();
GDScriptTokenizerText tokenizer;
FileAccessRef fs = FileAccess::create(FileAccess::ACCESS_RESOURCES);
tokenizer.set_code(p_code);
while (true) {
GDScriptTokenizerText::Token token = tokenizer.get_token();
if (token == GDScriptTokenizer::TK_EOF || token == GDScriptTokenizer::TK_ERROR) {
break;
} else if (token == GDScriptTokenizer::TK_CONSTANT) {
const Variant &const_val = tokenizer.get_token_constant();
if (const_val.get_type() == Variant::STRING) {
String path = const_val;
bool exists = fs->file_exists(path);
if (!exists) {
path = get_path().get_base_dir() + "/" + path;
exists = fs->file_exists(path);
}
if (exists) {
String value = const_val;
lsp::DocumentLink link;
link.target = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_uri(path);
link.range.start.line = LINE_NUMBER_TO_INDEX(tokenizer.get_token_line());
link.range.end.line = link.range.start.line;
link.range.end.character = LINE_NUMBER_TO_INDEX(tokenizer.get_token_column());
link.range.start.character = link.range.end.character - value.length();
document_links.push_back(link);
}
}
}
tokenizer.advance();
}
}
void ExtendGDScriptParser::parse_class_symbol(const GDScriptParser::ClassNode *p_class, lsp::DocumentSymbol &r_symbol) {
const String uri = get_uri();
r_symbol.uri = uri;
r_symbol.script_path = path;
r_symbol.children.clear();
r_symbol.name = p_class->name;
if (r_symbol.name.empty()) {
r_symbol.name = path.get_file();
}
r_symbol.kind = lsp::SymbolKind::Class;
r_symbol.deprecated = false;
r_symbol.range.start.line = LINE_NUMBER_TO_INDEX(p_class->line);
r_symbol.range.start.character = p_class->column;
r_symbol.range.end.line = LINE_NUMBER_TO_INDEX(p_class->end_line);
r_symbol.selectionRange.start.line = r_symbol.range.start.line;
r_symbol.detail = "class " + r_symbol.name;
bool is_root_class = &r_symbol == &class_symbol;
r_symbol.documentation = parse_documentation(is_root_class ? 0 : LINE_NUMBER_TO_INDEX(p_class->line), is_root_class);
for (int i = 0; i < p_class->variables.size(); ++i) {
const GDScriptParser::ClassNode::Member &m = p_class->variables[i];
lsp::DocumentSymbol symbol;
symbol.name = m.identifier;
symbol.kind = m.setter == "" && m.getter == "" ? lsp::SymbolKind::Variable : lsp::SymbolKind::Property;
symbol.deprecated = false;
const int line = LINE_NUMBER_TO_INDEX(m.line);
symbol.range.start.line = line;
symbol.range.start.character = lines[line].length() - lines[line].strip_edges(true, false).length();
symbol.range.end.line = line;
symbol.range.end.character = lines[line].length();
symbol.selectionRange.start.line = symbol.range.start.line;
if (m._export.type != Variant::NIL) {
symbol.detail += "export ";
}
symbol.detail += "var " + m.identifier;
if (m.data_type.kind != GDScriptParser::DataType::UNRESOLVED) {
symbol.detail += ": " + m.data_type.to_string();
}
if (m.default_value.get_type() != Variant::NIL) {
symbol.detail += " = " + JSON::print(m.default_value);
}
symbol.documentation = parse_documentation(line);
symbol.uri = uri;
symbol.script_path = path;
r_symbol.children.push_back(symbol);
}
for (int i = 0; i < p_class->_signals.size(); ++i) {
const GDScriptParser::ClassNode::Signal &signal = p_class->_signals[i];
lsp::DocumentSymbol symbol;
symbol.name = signal.name;
symbol.kind = lsp::SymbolKind::Event;
symbol.deprecated = false;
const int line = LINE_NUMBER_TO_INDEX(signal.line);
symbol.range.start.line = line;
symbol.range.start.character = lines[line].length() - lines[line].strip_edges(true, false).length();
symbol.range.end.line = symbol.range.start.line;
symbol.range.end.character = lines[line].length();
symbol.selectionRange.start.line = symbol.range.start.line;
symbol.documentation = parse_documentation(line);
symbol.uri = uri;
symbol.script_path = path;
symbol.detail = "signal " + signal.name + "(";
for (int j = 0; j < signal.arguments.size(); j++) {
if (j > 0) {
symbol.detail += ", ";
}
symbol.detail += signal.arguments[j];
}
symbol.detail += ")";
r_symbol.children.push_back(symbol);
}
for (RBMap<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
lsp::DocumentSymbol symbol;
const GDScriptParser::ClassNode::Constant &c = E->value();
ERR_FAIL_COND(c.expression->type != Node::TYPE_CONSTANT);
const GDScriptParser::ConstantNode *node = static_cast<const GDScriptParser::ConstantNode *>(c.expression);
symbol.name = E->key();
symbol.kind = lsp::SymbolKind::Constant;
symbol.deprecated = false;
const int line = LINE_NUMBER_TO_INDEX(E->get().expression->line);
symbol.range.start.line = line;
symbol.range.start.character = E->get().expression->column;
symbol.range.end.line = symbol.range.start.line;
symbol.range.end.character = lines[line].length();
symbol.selectionRange.start.line = symbol.range.start.line;
symbol.documentation = parse_documentation(line);
symbol.uri = uri;
symbol.script_path = path;
symbol.detail = "const " + symbol.name;
if (c.type.kind != GDScriptParser::DataType::UNRESOLVED) {
symbol.detail += ": " + c.type.to_string();
}
String value_text;
if (node->value.get_type() == Variant::OBJECT) {
RES res = node->value;
if (res.is_valid() && !res->get_path().empty()) {
value_text = "preload(\"" + res->get_path() + "\")";
if (symbol.documentation.empty()) {
if (RBMap<String, ExtendGDScriptParser *>::Element *S = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(res->get_path())) {
symbol.documentation = S->get()->class_symbol.documentation;
}
}
} else {
value_text = JSON::print(node->value);
}
} else {
value_text = JSON::print(node->value);
}
if (!value_text.empty()) {
symbol.detail += " = " + value_text;
}
r_symbol.children.push_back(symbol);
}
for (int i = 0; i < p_class->functions.size(); ++i) {
const GDScriptParser::FunctionNode *func = p_class->functions[i];
lsp::DocumentSymbol symbol;
parse_function_symbol(func, symbol);
r_symbol.children.push_back(symbol);
}
for (int i = 0; i < p_class->static_functions.size(); ++i) {
const GDScriptParser::FunctionNode *func = p_class->static_functions[i];
lsp::DocumentSymbol symbol;
parse_function_symbol(func, symbol);
r_symbol.children.push_back(symbol);
}
for (int i = 0; i < p_class->subclasses.size(); ++i) {
const GDScriptParser::ClassNode *subclass = p_class->subclasses[i];
lsp::DocumentSymbol symbol;
parse_class_symbol(subclass, symbol);
r_symbol.children.push_back(symbol);
}
}
void ExtendGDScriptParser::parse_function_symbol(const GDScriptParser::FunctionNode *p_func, lsp::DocumentSymbol &r_symbol) {
const String uri = get_uri();
r_symbol.name = p_func->name;
r_symbol.kind = p_func->_static ? lsp::SymbolKind::Function : lsp::SymbolKind::Method;
r_symbol.detail = "func " + p_func->name + "(";
r_symbol.deprecated = false;
const int line = LINE_NUMBER_TO_INDEX(p_func->line);
r_symbol.range.start.line = line;
r_symbol.range.start.character = p_func->column;
r_symbol.range.end.line = MAX(p_func->body->end_line - 2, r_symbol.range.start.line);
r_symbol.range.end.character = lines[r_symbol.range.end.line].length();
r_symbol.selectionRange.start.line = r_symbol.range.start.line;
r_symbol.documentation = parse_documentation(line);
r_symbol.uri = uri;
r_symbol.script_path = path;
String arguments;
for (int i = 0; i < p_func->arguments.size(); i++) {
lsp::DocumentSymbol symbol;
symbol.kind = lsp::SymbolKind::Variable;
symbol.name = p_func->arguments[i];
symbol.range.start.line = LINE_NUMBER_TO_INDEX(p_func->body->line);
symbol.range.start.character = p_func->body->column;
symbol.range.end = symbol.range.start;
symbol.uri = uri;
symbol.script_path = path;
r_symbol.children.push_back(symbol);
if (i > 0) {
arguments += ", ";
}
arguments += String(p_func->arguments[i]);
if (p_func->argument_types[i].kind != GDScriptParser::DataType::UNRESOLVED) {
arguments += ": " + p_func->argument_types[i].to_string();
}
int default_value_idx = i - (p_func->arguments.size() - p_func->default_values.size());
if (default_value_idx >= 0) {
const GDScriptParser::Node *current_defval_node = p_func->default_values[default_value_idx];
const GDScriptParser::ConstantNode *const_node = NULL;
if (current_defval_node->type == Node::TYPE_OPERATOR) {
const GDScriptParser::OperatorNode *operator_node = static_cast<const GDScriptParser::OperatorNode *>(current_defval_node);
if (operator_node->next->type == Node::TYPE_CONSTANT) {
const_node = static_cast<const GDScriptParser::ConstantNode *>(operator_node->next);
}
} else if (current_defval_node->type == Node::TYPE_CONSTANT) {
const_node = static_cast<const GDScriptParser::ConstantNode *>(current_defval_node);
}
if (const_node) {
String value = JSON::print(const_node->value);
arguments += " = " + value;
}
}
}
r_symbol.detail += arguments + ")";
if (p_func->return_type.kind != GDScriptParser::DataType::UNRESOLVED) {
r_symbol.detail += " -> " + p_func->return_type.to_string();
}
List<GDScriptParser::BlockNode *> function_blocks;
List<GDScriptParser::BlockNode *> block_stack;
block_stack.push_back(p_func->body);
while (!block_stack.empty()) {
GDScriptParser::BlockNode *block = block_stack[0];
block_stack.pop_front();
function_blocks.push_back(block);
for (const List<GDScriptParser::BlockNode *>::Element *E = block->sub_blocks.front(); E; E = E->next()) {
block_stack.push_back(E->get());
}
}
for (const List<GDScriptParser::BlockNode *>::Element *B = function_blocks.front(); B; B = B->next()) {
for (const RBMap<StringName, LocalVarNode *>::Element *E = B->get()->variables.front(); E; E = E->next()) {
lsp::DocumentSymbol symbol;
const GDScriptParser::LocalVarNode *var = E->value();
symbol.name = E->key();
symbol.kind = lsp::SymbolKind::Variable;
symbol.range.start.line = LINE_NUMBER_TO_INDEX(E->get()->line);
symbol.range.start.character = E->get()->column;
symbol.range.end.line = symbol.range.start.line;
symbol.range.end.character = lines[symbol.range.end.line].length();
symbol.uri = uri;
symbol.script_path = path;
symbol.detail = "var " + symbol.name;
if (var->datatype.kind != GDScriptParser::DataType::UNRESOLVED) {
symbol.detail += ": " + var->datatype.to_string();
}
symbol.documentation = parse_documentation(line);
r_symbol.children.push_back(symbol);
}
}
}
String ExtendGDScriptParser::parse_documentation(int p_line, bool p_docs_down) {
ERR_FAIL_INDEX_V(p_line, lines.size(), String());
List<String> doc_lines;
if (!p_docs_down) { // inline comment
String inline_comment = lines[p_line];
int comment_start = inline_comment.find("#");
if (comment_start != -1) {
inline_comment = inline_comment.substr(comment_start, inline_comment.length()).strip_edges();
if (inline_comment.length() > 1) {
doc_lines.push_back(inline_comment.substr(1, inline_comment.length()));
}
}
}
int step = p_docs_down ? 1 : -1;
int start_line = p_docs_down ? p_line : p_line - 1;
for (int i = start_line; true; i += step) {
if (i < 0 || i >= lines.size()) {
break;
}
String line_comment = lines[i].strip_edges(true, false);
if (line_comment.begins_with("#")) {
line_comment = line_comment.substr(1, line_comment.length());
if (p_docs_down) {
doc_lines.push_back(line_comment);
} else {
doc_lines.push_front(line_comment);
}
} else {
if (i > 0 && i < lines.size() - 1) {
String next_line = lines[i + step].strip_edges(true, false);
if (next_line.begins_with("#")) {
continue;
}
}
break;
}
}
String doc;
for (List<String>::Element *E = doc_lines.front(); E; E = E->next()) {
doc += E->get() + "\n";
}
return doc;
}
String ExtendGDScriptParser::get_text_for_completion(const lsp::Position &p_cursor) const {
String longthing;
int len = lines.size();
for (int i = 0; i < len; i++) {
if (i == p_cursor.line) {
longthing += lines[i].substr(0, p_cursor.character);
longthing += String::chr(0xFFFF); //not unicode, represents the cursor
longthing += lines[i].substr(p_cursor.character, lines[i].size());
} else {
longthing += lines[i];
}
if (i != len - 1) {
longthing += "\n";
}
}
return longthing;
}
String ExtendGDScriptParser::get_text_for_lookup_symbol(const lsp::Position &p_cursor, const String &p_symbol, bool p_func_required) const {
String longthing;
int len = lines.size();
for (int i = 0; i < len; i++) {
if (i == p_cursor.line) {
String line = lines[i];
String first_part = line.substr(0, p_cursor.character);
String last_part = line.substr(p_cursor.character + 1, lines[i].length());
if (!p_symbol.empty()) {
String left_cursor_text;
for (int c = p_cursor.character - 1; c >= 0; c--) {
left_cursor_text = line.substr(c, p_cursor.character - c);
if (p_symbol.begins_with(left_cursor_text)) {
first_part = line.substr(0, c);
first_part += p_symbol;
break;
}
}
}
longthing += first_part;
longthing += String::chr(0xFFFF); //not unicode, represents the cursor
if (p_func_required) {
longthing += "("; // tell the parser this is a function call
}
longthing += last_part;
} else {
longthing += lines[i];
}
if (i != len - 1) {
longthing += "\n";
}
}
return longthing;
}
String ExtendGDScriptParser::get_identifier_under_position(const lsp::Position &p_position, Vector2i &p_offset) const {
ERR_FAIL_INDEX_V(p_position.line, lines.size(), "");
String line = lines[p_position.line];
if (line.empty()) {
return "";
}
ERR_FAIL_INDEX_V(p_position.character, line.size(), "");
int start_pos = p_position.character;
for (int c = p_position.character; c >= 0; c--) {
start_pos = c;
CharType ch = line[c];
bool valid_char = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
if (!valid_char) {
break;
}
}
int end_pos = p_position.character;
for (int c = p_position.character; c < line.length(); c++) {
CharType ch = line[c];
bool valid_char = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
if (!valid_char) {
break;
}
end_pos = c;
}
if (start_pos < end_pos) {
p_offset.x = start_pos - p_position.character;
p_offset.y = end_pos - p_position.character;
return line.substr(start_pos + 1, end_pos - start_pos);
}
return "";
}
String ExtendGDScriptParser::get_uri() const {
return GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_uri(path);
}
const lsp::DocumentSymbol *ExtendGDScriptParser::search_symbol_defined_at_line(int p_line, const lsp::DocumentSymbol &p_parent) const {
const lsp::DocumentSymbol *ret = nullptr;
if (p_line < p_parent.range.start.line) {
return ret;
} else if (p_parent.range.start.line == p_line) {
return &p_parent;
} else {
for (int i = 0; i < p_parent.children.size(); i++) {
ret = search_symbol_defined_at_line(p_line, p_parent.children[i]);
if (ret) {
break;
}
}
}
return ret;
}
Error ExtendGDScriptParser::get_left_function_call(const lsp::Position &p_position, lsp::Position &r_func_pos, int &r_arg_index) const {
ERR_FAIL_INDEX_V(p_position.line, lines.size(), ERR_INVALID_PARAMETER);
int bracket_stack = 0;
int index = 0;
bool found = false;
for (int l = p_position.line; l >= 0; --l) {
String line = lines[l];
int c = line.length() - 1;
if (l == p_position.line) {
c = MIN(c, p_position.character - 1);
}
while (c >= 0) {
const CharType &character = line[c];
if (character == ')') {
++bracket_stack;
} else if (character == '(') {
--bracket_stack;
if (bracket_stack < 0) {
found = true;
}
}
if (bracket_stack <= 0 && character == ',') {
++index;
}
--c;
if (found) {
r_func_pos.character = c;
break;
}
}
if (found) {
r_func_pos.line = l;
r_arg_index = index;
return OK;
}
}
return ERR_METHOD_NOT_FOUND;
}
const lsp::DocumentSymbol *ExtendGDScriptParser::get_symbol_defined_at_line(int p_line) const {
if (p_line <= 0) {
return &class_symbol;
}
return search_symbol_defined_at_line(p_line, class_symbol);
}
const lsp::DocumentSymbol *ExtendGDScriptParser::get_member_symbol(const String &p_name, const String &p_subclass) const {
if (p_subclass.empty()) {
const lsp::DocumentSymbol *const *ptr = members.getptr(p_name);
if (ptr) {
return *ptr;
}
} else {
if (const ClassMembers *_class = inner_classes.getptr(p_subclass)) {
const lsp::DocumentSymbol *const *ptr = _class->getptr(p_name);
if (ptr) {
return *ptr;
}
}
}
return nullptr;
}
const List<lsp::DocumentLink> &ExtendGDScriptParser::get_document_links() const {
return document_links;
}
const Array &ExtendGDScriptParser::get_member_completions() {
if (member_completions.empty()) {
const String *name = members.next(nullptr);
while (name) {
const lsp::DocumentSymbol *symbol = members.get(*name);
lsp::CompletionItem item = symbol->make_completion_item();
item.data = JOIN_SYMBOLS(path, *name);
member_completions.push_back(item.to_json());
name = members.next(name);
}
const String *_class = inner_classes.next(nullptr);
while (_class) {
const ClassMembers *inner_class = inner_classes.getptr(*_class);
const String *member_name = inner_class->next(nullptr);
while (member_name) {
const lsp::DocumentSymbol *symbol = inner_class->get(*member_name);
lsp::CompletionItem item = symbol->make_completion_item();
item.data = JOIN_SYMBOLS(path, JOIN_SYMBOLS(*_class, *member_name));
member_completions.push_back(item.to_json());
member_name = inner_class->next(member_name);
}
_class = inner_classes.next(_class);
}
}
return member_completions;
}
Dictionary ExtendGDScriptParser::dump_function_api(const GDScriptParser::FunctionNode *p_func) const {
Dictionary func;
ERR_FAIL_NULL_V(p_func, func);
func["name"] = p_func->name;
func["return_type"] = p_func->return_type.to_string();
Array arguments;
for (int i = 0; i < p_func->arguments.size(); i++) {
Dictionary arg;
arg["name"] = p_func->arguments[i];
arg["type"] = p_func->argument_types[i].to_string();
int default_value_idx = i - (p_func->arguments.size() - p_func->default_values.size());
if (default_value_idx >= 0) {
const GDScriptParser::Node *current_defval_node = p_func->default_values[default_value_idx];
const GDScriptParser::ConstantNode *const_node = NULL;
if (current_defval_node->type == Node::TYPE_OPERATOR) {
const GDScriptParser::OperatorNode *operator_node = static_cast<const GDScriptParser::OperatorNode *>(current_defval_node);
if (operator_node->next->type == Node::TYPE_CONSTANT) {
const_node = static_cast<const GDScriptParser::ConstantNode *>(operator_node->next);
}
} else if (current_defval_node->type == Node::TYPE_CONSTANT) {
const_node = static_cast<const GDScriptParser::ConstantNode *>(current_defval_node);
}
if (const_node) {
arg["default_value"] = const_node->value;
}
}
arguments.push_back(arg);
}
if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(p_func->line))) {
func["signature"] = symbol->detail;
func["description"] = symbol->documentation;
}
func["arguments"] = arguments;
return func;
}
Dictionary ExtendGDScriptParser::dump_class_api(const GDScriptParser::ClassNode *p_class) const {
Dictionary class_api;
ERR_FAIL_NULL_V(p_class, class_api);
class_api["name"] = String(p_class->name);
class_api["path"] = path;
Array extends_class;
for (int i = 0; i < p_class->extends_class.size(); i++) {
extends_class.append(String(p_class->extends_class[i]));
}
class_api["extends_class"] = extends_class;
class_api["extends_file"] = String(p_class->extends_file);
class_api["icon"] = String(p_class->icon_path);
if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(p_class->line))) {
class_api["signature"] = symbol->detail;
class_api["description"] = symbol->documentation;
}
Array subclasses;
for (int i = 0; i < p_class->subclasses.size(); i++) {
subclasses.push_back(dump_class_api(p_class->subclasses[i]));
}
class_api["sub_classes"] = subclasses;
Array constants;
for (RBMap<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_class->constant_expressions.front(); E; E = E->next()) {
const GDScriptParser::ClassNode::Constant &c = E->value();
ERR_FAIL_COND_V(c.expression->type != Node::TYPE_CONSTANT, class_api);
const GDScriptParser::ConstantNode *node = static_cast<const GDScriptParser::ConstantNode *>(c.expression);
Dictionary api;
api["name"] = E->key();
api["value"] = node->value;
api["data_type"] = node->datatype.to_string();
if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(node->line))) {
api["signature"] = symbol->detail;
api["description"] = symbol->documentation;
}
constants.push_back(api);
}
class_api["constants"] = constants;
Array members;
for (int i = 0; i < p_class->variables.size(); ++i) {
const GDScriptParser::ClassNode::Member &m = p_class->variables[i];
Dictionary api;
api["name"] = m.identifier;
api["data_type"] = m.data_type.to_string();
api["default_value"] = m.default_value;
api["setter"] = String(m.setter);
api["getter"] = String(m.getter);
api["export"] = m._export.type != Variant::NIL;
if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(m.line))) {
api["signature"] = symbol->detail;
api["description"] = symbol->documentation;
}
members.push_back(api);
}
class_api["members"] = members;
Array signals;
for (int i = 0; i < p_class->_signals.size(); ++i) {
const GDScriptParser::ClassNode::Signal &signal = p_class->_signals[i];
Dictionary api;
api["name"] = signal.name;
Array args;
for (int j = 0; j < signal.arguments.size(); j++) {
args.append(signal.arguments[j]);
}
api["arguments"] = args;
if (const lsp::DocumentSymbol *symbol = get_symbol_defined_at_line(LINE_NUMBER_TO_INDEX(signal.line))) {
api["signature"] = symbol->detail;
api["description"] = symbol->documentation;
}
signals.push_back(api);
}
class_api["signals"] = signals;
Array methods;
for (int i = 0; i < p_class->functions.size(); ++i) {
methods.append(dump_function_api(p_class->functions[i]));
}
class_api["methods"] = methods;
Array static_functions;
for (int i = 0; i < p_class->static_functions.size(); ++i) {
static_functions.append(dump_function_api(p_class->static_functions[i]));
}
class_api["static_functions"] = static_functions;
return class_api;
}
Dictionary ExtendGDScriptParser::generate_api() const {
Dictionary api;
const GDScriptParser::Node *head = get_parse_tree();
if (head && head->type == Node::TYPE_CLASS) {
const GDScriptParser::ClassNode *gdclass = static_cast<const GDScriptParser::ClassNode *>(head);
api = dump_class_api(gdclass);
}
return api;
}
Error ExtendGDScriptParser::parse(const String &p_code, const String &p_path) {
path = p_path;
lines = p_code.split("\n");
Error err = GDScriptParser::parse(p_code, p_path.get_base_dir(), false, p_path, false, nullptr, false);
update_diagnostics();
update_symbols();
update_document_links(p_code);
return err;
}

View File

@ -1,102 +0,0 @@
/**************************************************************************/
/* 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. */
/**************************************************************************/
#ifndef GDSCRIPT_EXTEND_PARSER_H
#define GDSCRIPT_EXTEND_PARSER_H
#include "../gdscript_parser.h"
#include "core/variant/variant.h"
#include "lsp.hpp"
#ifndef LINE_NUMBER_TO_INDEX
#define LINE_NUMBER_TO_INDEX(p_line) ((p_line)-1)
#endif
#ifndef SYMBOL_SEPERATOR
#define SYMBOL_SEPERATOR "::"
#endif
#ifndef JOIN_SYMBOLS
#define JOIN_SYMBOLS(p_path, name) ((p_path) + SYMBOL_SEPERATOR + (name))
#endif
typedef HashMap<String, const lsp::DocumentSymbol *> ClassMembers;
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;
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;
String parse_documentation(int p_line, bool p_docs_down = false);
const lsp::DocumentSymbol *search_symbol_defined_at_line(int p_line, const lsp::DocumentSymbol &p_parent) 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, Vector2i &p_offset) const;
String get_uri() const;
const lsp::DocumentSymbol *get_symbol_defined_at_line(int p_line) 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);
};
#endif // GDSCRIPT_EXTEND_PARSER_H

View File

@ -1,325 +0,0 @@
/**************************************************************************/
/* 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/io/json.h"
#include "core/config/project_settings.h"
#include "editor/editor_log.h"
#include "editor/editor_node.h"
#include "editor/editor_settings.h"
#include "editor/editor_help.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_COND_V_MSG(true, 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;
header.parse_utf8(r);
content_length = header.substr(16).to_int();
has_header = true;
req_pos = 0;
break;
}
req_pos++;
}
}
if (has_header) {
while (req_pos < content_length) {
if (req_pos >= LSP_MAX_BUFFER_SIZE) {
req_pos = 0;
has_header = false;
ERR_FAIL_COND_V_MSG(req_pos >= LSP_MAX_BUFFER_SIZE, ERR_OUT_OF_MEMORY, "Response content too big");
}
Error err = connection->get_partial_data(&req_buf[req_pos], 1, read);
if (err != OK) {
return FAILED;
} else if (read != 1) {
return ERR_BUSY;
}
req_pos++;
}
// Parse data
String msg;
msg.parse_utf8((const char *)req_buf, req_pos);
// Reset to read again
req_pos = 0;
has_header = false;
// Response
String output = GDScriptLanguageProtocol::get_singleton()->process_message(msg);
if (!output.empty()) {
res_queue.push_back(output.utf8());
}
}
return OK;
}
Error GDScriptLanguageProtocol::LSPeer::send_data() {
int sent = 0;
if (!res_queue.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(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.set(next_client_id, peer);
next_client_id++;
EditorNode::get_log()->add_message("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("Disconnected", EditorLog::MSG_TYPE_EDITOR);
}
String GDScriptLanguageProtocol::process_message(const String &p_text) {
String ret = process_string(p_text);
if (ret.empty()) {
return ret;
} else {
return format_output(ret);
}
}
String GDScriptLanguageProtocol::format_output(const String &p_text) {
String header = "Content-Length: ";
CharString charstr = p_text.utf8();
size_t len = charstr.length();
header += itos(len);
header += "\r\n\r\n";
return header + p_text;
}
void GDScriptLanguageProtocol::_bind_methods() {
ClassDB::bind_method(D_METHOD("initialize", "params"), &GDScriptLanguageProtocol::initialize);
ClassDB::bind_method(D_METHOD("initialized", "params"), &GDScriptLanguageProtocol::initialized);
ClassDB::bind_method(D_METHOD("on_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;
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("\\", "/").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 != nullptr) {
String msg = JSON::print(request);
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;
DocData *doc = EditorHelp::get_doc_data();
for (RBMap<String, DocData::ClassDoc>::Element *E = doc->class_list.front(); E; E = E->next()) {
lsp::GodotNativeClassInfo gdclass;
gdclass.name = E->get().name;
gdclass.class_doc = &(E->get());
if (ClassDB::ClassInfo *ptr = ClassDB::classes.getptr(StringName(E->get().name))) {
gdclass.class_info = ptr;
}
capabilities.native_classes.push_back(gdclass);
}
notify_client("gdscript/capabilities", capabilities.to_json());
}
void GDScriptLanguageProtocol::poll() {
if (server->is_connection_available()) {
on_client_connected();
}
const int *id = nullptr;
while ((id = clients.next(id))) {
Ref<LSPeer> peer = clients.get(*id);
StreamPeerTCP::Status status = peer->connection->get_status();
if (status == StreamPeerTCP::STATUS_NONE || status == StreamPeerTCP::STATUS_ERROR) {
on_client_disconnected(*id);
id = nullptr;
} else {
if (peer->connection->get_available_bytes() > 0) {
latest_client_id = *id;
Error err = peer->handle_data();
if (err != OK && err != ERR_BUSY) {
on_client_disconnected(*id);
id = nullptr;
}
}
Error err = peer->send_data();
if (err != OK && err != ERR_BUSY) {
on_client_disconnected(*id);
id = nullptr;
}
}
}
}
Error GDScriptLanguageProtocol::start(int p_port, const IP_Address &p_bind_ip) {
return server->listen(p_port, p_bind_ip);
}
void GDScriptLanguageProtocol::stop() {
const int *id = nullptr;
while ((id = clients.next(id))) {
Ref<LSPeer> peer = clients.get(*id);
peer->connection->disconnect_from_host();
}
server->stop();
}
void GDScriptLanguageProtocol::notify_client(const String &p_method, const Variant &p_params, int p_client_id) {
if (p_client_id == -1) {
ERR_FAIL_COND_MSG(latest_client_id == -1,
"GDScript LSP: Can't notify client as none was connected.");
p_client_id = latest_client_id;
}
ERR_FAIL_COND(!clients.has(p_client_id));
Ref<LSPeer> peer = clients.get(p_client_id);
ERR_FAIL_COND(peer == nullptr);
Dictionary message = make_notification(p_method, p_params);
String msg = JSON::print(message);
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) {
if (p_client_id == -1) {
ERR_FAIL_COND_MSG(latest_client_id == -1,
"GDScript LSP: Can't notify client as none was connected.");
p_client_id = latest_client_id;
}
ERR_FAIL_COND(!clients.has(p_client_id));
Ref<LSPeer> peer = clients.get(p_client_id);
ERR_FAIL_COND(peer == nullptr);
Dictionary message = make_request(p_method, p_params, next_server_id);
next_server_id++;
String msg = JSON::print(message);
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"));
}
GDScriptLanguageProtocol::GDScriptLanguageProtocol() {
server.instance();
singleton = this;
workspace.instance();
text_document.instance();
set_scope("textDocument", text_document.ptr());
set_scope("completionItem", text_document.ptr());
set_scope("workspace", workspace.ptr());
workspace->root = ProjectSettings::get_singleton()->get_resource_path();
}

View File

@ -1,113 +0,0 @@
/**************************************************************************/
/* 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. */
/**************************************************************************/
#ifndef GDSCRIPT_LANGUAGE_PROTOCOL_H
#define GDSCRIPT_LANGUAGE_PROTOCOL_H
#include "core/io/stream_peer.h"
#include "core/io/stream_peer_tcp.h"
#include "core/io/tcp_server.h"
#include "gdscript_text_document.h"
#include "gdscript_workspace.h"
#include "lsp.hpp"
#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 : Reference {
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<TCP_Server> 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();
Error start(int p_port, const IP_Address &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();
};
#endif // GDSCRIPT_LANGUAGE_PROTOCOL_H

View File

@ -1,135 +0,0 @@
/**************************************************************************/
/* 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/file_access.h"
#include "core/os/os.h"
#include "editor/editor_log.h"
#include "editor/editor_node.h"
#include "editor/editor_settings.h"
int GDScriptLanguageServer::port_override = -1;
GDScriptLanguageServer::GDScriptLanguageServer() {
enabled = false;
thread_running = false;
started = false;
use_thread = false;
host = "127.0.0.1";
port = 6008;
_EDITOR_DEF("network/language_server/enabled", enabled, true);
_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);
}
void GDScriptLanguageServer::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE:
start();
break;
case NOTIFICATION_EXIT_TREE:
stop();
break;
case NOTIFICATION_INTERNAL_PROCESS: {
if (started && !use_thread) {
protocol.poll();
}
} break;
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
if (!enabled) {
return;
}
String host = String(_EDITOR_GET("network/language_server/remote_host"));
int port = (GDScriptLanguageServer::port_override > -1) ? GDScriptLanguageServer::port_override : (int)_EDITOR_GET("network/language_server/remote_port");
bool use_thread = (bool)_EDITOR_GET("network/language_server/use_thread");
if (host != this->host || port != this->port || use_thread != this->use_thread) {
this->stop();
this->start();
}
} break;
}
}
void GDScriptLanguageServer::thread_main(void *p_userdata) {
GDScriptLanguageServer *self = static_cast<GDScriptLanguageServer *>(p_userdata);
while (self->thread_running) {
// Poll 20 times per second
self->protocol.poll();
OS::get_singleton()->delay_usec(50000);
}
}
void GDScriptLanguageServer::start() {
enabled = _EDITOR_GET("network/language_server/enabled");
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");
if (!enabled) {
return;
}
if (protocol.start(port, IP_Address(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 (!enabled) {
return;
}
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() {
ClassDB::register_class<GDScriptLanguageProtocol>();
ClassDB::register_class<GDScriptTextDocument>();
ClassDB::register_class<GDScriptWorkspace>();
}

View File

@ -1,65 +0,0 @@
/**************************************************************************/
/* 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. */
/**************************************************************************/
#ifndef GDSCRIPT_LANGUAGE_SERVER_H
#define GDSCRIPT_LANGUAGE_SERVER_H
#include "../gdscript_parser.h"
#include "editor/editor_plugin.h"
#include "gdscript_language_protocol.h"
class GDScriptLanguageServer : public EditorPlugin {
GDCLASS(GDScriptLanguageServer, EditorPlugin);
GDScriptLanguageProtocol protocol;
Thread thread;
bool thread_running;
bool started;
bool use_thread;
String host;
int port;
bool enabled;
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();
#endif // GDSCRIPT_LANGUAGE_SERVER_H

View File

@ -1,464 +0,0 @@
/**************************************************************************/
/* 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 "core/os/os.h"
#include "editor/editor_settings.h"
#include "gdscript_extend_parser.h"
#include "gdscript_language_protocol.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("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("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::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);
}
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()) {
const HashMap<StringName, ClassMembers> &native_members = GDScriptLanguageProtocol::get_singleton()->get_workspace()->native_members;
const StringName *class_ptr = native_members.next(nullptr);
while (class_ptr) {
const ClassMembers &members = native_members.get(*class_ptr);
const String *name = members.next(nullptr);
while (name) {
const lsp::DocumentSymbol *symbol = members.get(*name);
lsp::CompletionItem item = symbol->make_completion_item();
item.data = JOIN_SYMBOLS(String(*class_ptr), *name);
native_member_completions.push_back(item.to_json());
name = members.next(name);
}
class_ptr = native_members.next(class_ptr);
}
}
}
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 (const RBMap<String, ExtendGDScriptParser *>::Element *parser = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(path)) {
Vector<lsp::DocumentedSymbolInformation> list;
parser->get()->get_symbols().symbol_tree_as_list(uri, list);
for (int i = 0; i < list.size(); i++) {
arr.push_back(list[i].to_json());
}
}
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<ScriptCodeCompletionOption> options;
GDScriptLanguageProtocol::get_singleton()->get_workspace()->completion(params, &options);
if (!options.empty()) {
int i = 0;
arr.resize(options.size());
for (const List<ScriptCodeCompletionOption>::Element *E = options.front(); E; E = E->next()) {
const ScriptCodeCompletionOption &option = E->get();
lsp::CompletionItem item;
item.label = option.display;
item.data = request_data;
item.insertText = option.insert_text;
switch (option.kind) {
case ScriptCodeCompletionOption::KIND_ENUM:
item.kind = lsp::CompletionItemKind::Enum;
break;
case ScriptCodeCompletionOption::KIND_CLASS:
item.kind = lsp::CompletionItemKind::Class;
break;
case ScriptCodeCompletionOption::KIND_MEMBER:
item.kind = lsp::CompletionItemKind::Property;
break;
case ScriptCodeCompletionOption::KIND_FUNCTION:
item.kind = lsp::CompletionItemKind::Method;
break;
case ScriptCodeCompletionOption::KIND_SIGNAL:
item.kind = lsp::CompletionItemKind::Event;
break;
case ScriptCodeCompletionOption::KIND_CONSTANT:
item.kind = lsp::CompletionItemKind::Constant;
break;
case ScriptCodeCompletionOption::KIND_VARIABLE:
item.kind = lsp::CompletionItemKind::Variable;
break;
case ScriptCodeCompletionOption::KIND_FILE_PATH:
item.kind = lsp::CompletionItemKind::File;
break;
case ScriptCodeCompletionOption::KIND_NODE_PATH:
item.kind = lsp::CompletionItemKind::Snippet;
break;
case ScriptCodeCompletionOption::KIND_PLAIN_TEXT:
item.kind = lsp::CompletionItemKind::Text;
break;
}
arr[i] = item.to_json();
i++;
}
} else if (GDScriptLanguageProtocol::get_singleton()->is_smart_resolve_enabled()) {
arr = native_member_completions.duplicate();
for (RBMap<String, ExtendGDScriptParser *>::Element *E = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.front(); E; E = E->next()) {
ExtendGDScriptParser *script = E->get();
const Array &items = script->get_member_completions();
const int start_size = arr.size();
arr.resize(start_size + items.size());
for (int i = start_size; i < arr.size(); i++) {
arr[i] = items[i - start_size];
}
}
}
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);
}
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.get_type() == Variant::STRING) {
String query = data;
Vector<String> param_symbols = query.split(SYMBOL_SEPERATOR, false);
if (param_symbols.size() >= 2) {
String class_ = param_symbols[0];
StringName class_name = class_;
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 (const RBMap<String, ExtendGDScriptParser *>::Element *E = GDScriptLanguageProtocol::get_singleton()->get_workspace()->scripts.find(class_name)) {
symbol = E->get()->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 = quote_style + item.label + quote_style;
}
}
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 List<lsp::DocumentLink>::Element *E = links.front(); E; E = E->next()) {
ret.push_back(E->get().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 (List<const lsp::DocumentSymbol *>::Element *E = list.front(); E; E = E->next()) {
if (const lsp::DocumentSymbol *s = E->get()) {
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 = this->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 = this->find_symbols(params, symbols);
if (arr.empty() && !symbols.empty() && !symbols.front()->get()->native_class.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;
}
call_deferred("show_native_symbol_in_editor", 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);
}
GDScriptTextDocument::~GDScriptTextDocument() {
memdelete(file_checker);
}
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);
EditorFileSystem::get_singleton()->update_file(path);
Error error;
Ref<GDScript> script = ResourceLoader::load(path, "", false, &error);
if (error == OK) {
if (script->load_source_code(path) == OK) {
script->reload(true);
}
}
}
void GDScriptTextDocument::show_native_symbol_in_editor(const String &p_symbol_id) {
OS::get_singleton()->move_window_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;
location.range = symbol->range;
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 (List<const lsp::DocumentSymbol *>::Element *E = list.front(); E; E = E->next()) {
if (const lsp::DocumentSymbol *s = E->get()) {
if (!s->uri.empty()) {
lsp::Location location;
location.uri = s->uri;
location.range = s->range;
arr.push_back(location.to_json());
r_list.push_back(s);
}
}
}
}
return arr;
}

View File

@ -1,81 +0,0 @@
/**************************************************************************/
/* 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. */
/**************************************************************************/
#ifndef GDSCRIPT_TEXT_DOCUMENT_H
#define GDSCRIPT_TEXT_DOCUMENT_H
#include "core/os/file_access.h"
#include "core/object/reference.h"
#include "lsp.hpp"
class GDScriptTextDocument : public Reference {
GDCLASS(GDScriptTextDocument, Reference)
protected:
static void _bind_methods();
FileAccess *file_checker;
void didOpen(const Variant &p_param);
void didClose(const Variant &p_param);
void didChange(const Variant &p_param);
void didSave(const Variant &p_param);
void sync_script_content(const String &p_path, const String &p_content);
void show_native_symbol_in_editor(const String &p_symbol_id);
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:
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);
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();
virtual ~GDScriptTextDocument();
};
#endif // GDSCRIPT_TEXT_DOCUMENT_H

View File

@ -1,804 +0,0 @@
/**************************************************************************/
/* 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 "core/config/project_settings.h"
#include "core/object/script_language.h"
#include "editor/editor_file_system.h"
#include "editor/editor_help.h"
#include "editor/editor_node.h"
#include "editor/editor_settings.h"
#include "gdscript_language_protocol.h"
#include "scene/resources/packed_scene.h"
void GDScriptWorkspace::_bind_methods() {
ClassDB::bind_method(D_METHOD("didDeleteFiles"), &GDScriptWorkspace::did_delete_files);
ClassDB::bind_method(D_METHOD("symbol"), &GDScriptWorkspace::symbol);
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);
ClassDB::bind_method(D_METHOD("apply_new_signal", "obj", "function", "args"), &GDScriptWorkspace::apply_new_signal);
}
void GDScriptWorkspace::apply_new_signal(Object *obj, String function, PoolStringArray args) {
Ref<Script> script = obj->get_script();
if (script->get_language()->get_name() != "GDScript") {
return;
}
String function_signature = "func " + function;
String source = script->get_source_code();
if (source.find(function_signature) != -1) {
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(script->get_path());
lsp::ApplyWorkspaceEditParams params;
params.edit.add_edit(uri, text_edit);
GDScriptLanguageProtocol::get_singleton()->request_client("workspace/applyEdit", params.to_json());
}
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, "");
}
}
void GDScriptWorkspace::remove_cache_parser(const String &p_path) {
RBMap<String, ExtendGDScriptParser *>::Element *parser = parse_results.find(p_path);
RBMap<String, ExtendGDScriptParser *>::Element *script = scripts.find(p_path);
if (parser && script) {
if (script->get() && script->get() == parser->get()) {
memdelete(script->get());
} else {
memdelete(script->get());
memdelete(parser->get());
}
parse_results.erase(p_path);
scripts.erase(p_path);
} else if (parser) {
memdelete(parser->get());
parse_results.erase(p_path);
} else if (script) {
memdelete(script->get());
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 (const RBMap<StringName, lsp::DocumentSymbol>::Element *E = native_symbols.find(class_name)) {
const lsp::DocumentSymbol &class_symbol = E->value();
if (p_member.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;
}
}
}
}
class_name = ClassDB::get_parent_class(class_name);
}
return nullptr;
}
const lsp::DocumentSymbol *GDScriptWorkspace::get_script_symbol(const String &p_path) const {
const RBMap<String, ExtendGDScriptParser *>::Element *S = scripts.find(p_path);
if (S) {
return &(S->get()->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.empty() && parameter_symbol->name == symbol_identifier) {
return parameter_symbol;
}
}
return nullptr;
}
const lsp::DocumentSymbol *GDScriptWorkspace::get_local_symbol(const ExtendGDScriptParser *p_parser, const String &p_symbol_identifier) {
const lsp::DocumentSymbol *class_symbol = &p_parser->get_symbols();
for (int i = 0; i < class_symbol->children.size(); ++i) {
if (class_symbol->children[i].kind == lsp::SymbolKind::Function || class_symbol->children[i].kind == lsp::SymbolKind::Class) {
const lsp::DocumentSymbol *function_symbol = &class_symbol->children[i];
for (int l = 0; l < function_symbol->children.size(); ++l) {
const lsp::DocumentSymbol *local = &function_symbol->children[l];
if (!local->detail.empty() && local->name == p_symbol_identifier) {
return local;
}
}
}
}
return nullptr;
}
void GDScriptWorkspace::reload_all_workspace_scripts() {
List<String> paths;
list_script_files("res://", paths);
for (List<String>::Element *E = paths.front(); E; E = E->next()) {
const String &path = E->get();
Error err;
String content = FileAccess::get_file_as_string(path, &err);
ERR_CONTINUE(err != OK);
err = parse_script(path, content);
if (err != OK) {
RBMap<String, ExtendGDScriptParser *>::Element *S = parse_results.find(path);
String err_msg = "Failed parse script " + path;
if (S) {
err_msg += "\n" + S->get()->get_error();
}
ERR_CONTINUE_MSG(err != OK, err_msg);
}
}
}
void GDScriptWorkspace::list_script_files(const String &p_root_dir, List<String> &r_files) {
Error err;
DirAccessRef dir = DirAccess::open(p_root_dir, &err);
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 != "./") {
list_script_files(p_root_dir.plus_file(file_name), r_files);
} else if (file_name.ends_with(".gd")) {
String script_file = p_root_dir.plus_file(file_name);
r_files.push_back(script_file);
}
file_name = dir->get_next();
}
}
}
ExtendGDScriptParser *GDScriptWorkspace::get_parse_successed_script(const String &p_path) {
const RBMap<String, ExtendGDScriptParser *>::Element *S = scripts.find(p_path);
if (!S) {
parse_local_script(p_path);
S = scripts.find(p_path);
}
if (S) {
return S->get();
}
return nullptr;
}
ExtendGDScriptParser *GDScriptWorkspace::get_parse_result(const String &p_path) {
const RBMap<String, ExtendGDScriptParser *>::Element *S = parse_results.find(p_path);
if (!S) {
parse_local_script(p_path);
S = parse_results.find(p_path);
}
if (S) {
return S->get();
}
return nullptr;
}
Array GDScriptWorkspace::symbol(const Dictionary &p_params) {
String query = p_params["query"];
Array arr;
if (!query.empty()) {
for (RBMap<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
Vector<lsp::DocumentedSymbolInformation> script_symbols;
E->get()->get_symbols().symbol_tree_as_list(E->key(), script_symbols);
for (int i = 0; i < script_symbols.size(); ++i) {
if (query.is_subsequence_ofi(script_symbols[i].name)) {
lsp::DocumentedSymbolInformation symbol = script_symbols[i];
symbol.location.uri = get_file_uri(symbol.location.uri);
arr.push_back(symbol.to_json());
}
}
}
}
return arr;
}
Error GDScriptWorkspace::initialize() {
if (initialized) {
return OK;
}
DocData *doc = EditorHelp::get_doc_data();
for (RBMap<String, DocData::ClassDoc>::Element *E = doc->class_list.front(); E; E = E->next()) {
const DocData::ClassDoc &class_data = E->value();
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.empty()) {
class_symbol.detail += " extends " + class_data.inherits;
}
class_symbol.documentation = class_data.brief_description + "\n" + 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 = 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 = 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 = data.description;
class_symbol.children.push_back(symbol);
}
Vector<DocData::MethodDoc> methods_signals;
methods_signals.append_array(class_data.methods);
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;
symbol.native_class = class_name;
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];
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.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.find("vararg") != -1) {
params += params.empty() ? "..." : ", ...";
}
String return_type = data.return_type;
if (return_type.empty()) {
return_type = "void";
}
symbol.detail = "func " + class_name + "." + data.name + "(" + params + ") -> " + return_type;
symbol.documentation = 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 (RBMap<StringName, lsp::DocumentSymbol>::Element *E = native_symbols.front(); E; E = E->next()) {
ClassMembers members;
const lsp::DocumentSymbol &class_symbol = E->get();
for (int i = 0; i < class_symbol.children.size(); i++) {
const lsp::DocumentSymbol &symbol = class_symbol.children[i];
members.set(symbol.name, &symbol);
}
native_members.set(E->key(), members);
}
// cache member completions
for (RBMap<String, ExtendGDScriptParser *>::Element *S = scripts.front(); S; S = S->next()) {
S->get()->get_member_completions();
}
}
EditorNode *editor_node = EditorNode::get_singleton();
editor_node->connect("script_add_function_request", this, "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);
RBMap<String, ExtendGDScriptParser *>::Element *last_parser = parse_results.find(p_path);
RBMap<String, ExtendGDScriptParser *>::Element *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->get() != last_script->get()) {
memdelete(last_parser->get());
}
parse_results[p_path] = parser;
}
publish_diagnostics(p_path);
return err;
}
Dictionary GDScriptWorkspace::rename(const lsp::TextDocumentPositionParams &p_doc_pos, const String &new_name) {
Error err;
String path = get_file_path(p_doc_pos.textDocument.uri);
lsp::WorkspaceEdit edit;
List<String> paths;
list_script_files("res://", paths);
const lsp::DocumentSymbol *reference_symbol = resolve_symbol(p_doc_pos);
if (reference_symbol) {
String identifier = reference_symbol->name;
for (List<String>::Element *PE = paths.front(); PE; PE = PE->next()) {
Vector<String> content = FileAccess::get_file_as_string(PE->get(), &err).split("\n");
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(PE->get());
params.textDocument = text_doc;
params.position.line = i;
params.position.character = character;
const lsp::DocumentSymbol *other_symbol = resolve_symbol(params);
if (other_symbol == reference_symbol) {
edit.add_change(text_doc.uri, i, character, character + identifier.length(), new_name);
}
character = line.find(identifier, character + 1);
}
}
}
}
return edit.to_json();
}
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) const {
String path = p_uri.http_unescape();
String base_uri = root_uri.http_unescape();
path = path.replacen(base_uri + "/", "res://");
return path;
}
String GDScriptWorkspace::get_file_uri(const String &p_path) const {
String uri = p_path;
uri = uri.replace("res://", root_uri + "/");
return uri;
}
void GDScriptWorkspace::publish_diagnostics(const String &p_path) {
Dictionary params;
Array errors;
const RBMap<String, ExtendGDScriptParser *>::Element *ele = parse_results.find(p_path);
if (ele) {
const Vector<lsp::Diagnostic> &list = ele->get()->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 (int i = 0; i < owners.size(); i++) {
NodePath owner_path = owners[i];
RES owner_res = ResourceLoader::load(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->instance();
break;
}
}
return owner_scene_node;
}
void GDScriptWorkspace::completion(const lsp::CompletionParams &p_params, List<ScriptCodeCompletionOption> *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) {
stack.push_back(owner_scene_node);
while (!stack.empty()) {
current = stack.pop_back();
Ref<GDScript> script = current->get_script();
if (script.is_valid() && script->get_path() == path) {
break;
}
for (int i = 0; i < current->get_child_count(); ++i) {
stack.push_back(current->get_child(i));
}
}
Ref<GDScript> script = current->get_script();
if (!script.is_valid() || script->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.empty()) {
Vector2i offset;
symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, offset);
pos.character += offset.y;
}
if (!symbol_identifier.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].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)) {
if (ret.type == ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION) {
String target_script_path = path;
if (!ret.script.is_null()) {
target_script_path = ret.script->get_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));
if (symbol && symbol->kind == lsp::SymbolKind::Function && symbol->name != symbol_identifier) {
symbol = get_parameter_symbol(symbol, symbol_identifier);
}
}
} else {
String member = ret.class_member;
if (member.empty() && symbol_identifier != ret.class_name) {
member = symbol_identifier;
}
symbol = get_native_symbol(ret.class_name, member);
}
} else {
symbol = parser->get_member_symbol(symbol_identifier);
if (!symbol) {
symbol = get_local_symbol(parser, 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;
Vector2i offset;
symbol_identifier = parser->get_identifier_under_position(p_doc_pos.position, offset);
const StringName *class_ptr = native_members.next(nullptr);
while (class_ptr) {
const ClassMembers &members = native_members.get(*class_ptr);
if (const lsp::DocumentSymbol *const *symbol = members.getptr(symbol_identifier)) {
r_list.push_back(*symbol);
}
class_ptr = native_members.next(class_ptr);
}
for (RBMap<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
const ExtendGDScriptParser *script = E->get();
const ClassMembers &members = script->get_members();
if (const lsp::DocumentSymbol *const *symbol = members.getptr(symbol_identifier)) {
r_list.push_back(*symbol);
}
const HashMap<String, ClassMembers> &inner_classes = script->get_inner_classes();
const String *_class = inner_classes.next(nullptr);
while (_class) {
const ClassMembers *inner_class = inner_classes.getptr(*_class);
if (const lsp::DocumentSymbol *const *symbol = inner_class->getptr(symbol_identifier)) {
r_list.push_back(*symbol);
}
_class = inner_classes.next(_class);
}
}
}
}
const lsp::DocumentSymbol *GDScriptWorkspace::resolve_native_symbol(const lsp::NativeSymbolInspectParams &p_params) {
if (RBMap<StringName, lsp::DocumentSymbol>::Element *E = native_symbols.find(p_params.native_class)) {
const lsp::DocumentSymbol &symbol = E->get();
if (p_params.symbol_name.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 List<lsp::DocumentLink>::Element *E = links.front(); E; E = E->next()) {
r_list.push_back(E->get());
}
}
}
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 (List<const lsp::DocumentSymbol *>::Element *E = symbols.front(); E; E = E->next()) {
const lsp::DocumentSymbol *symbol = E->get();
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() {
ProjectSettings::get_singleton()->get_resource_path();
}
GDScriptWorkspace::~GDScriptWorkspace() {
RBSet<String> cached_parsers;
for (RBMap<String, ExtendGDScriptParser *>::Element *E = parse_results.front(); E; E = E->next()) {
cached_parsers.insert(E->key());
}
for (RBMap<String, ExtendGDScriptParser *>::Element *E = scripts.front(); E; E = E->next()) {
cached_parsers.insert(E->key());
}
for (RBSet<String>::Element *E = cached_parsers.front(); E; E = E->next()) {
remove_cache_parser(E->get());
}
}

View File

@ -1,104 +0,0 @@
/**************************************************************************/
/* 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. */
/**************************************************************************/
#ifndef GDSCRIPT_WORKSPACE_H
#define GDSCRIPT_WORKSPACE_H
#include "../gdscript_parser.h"
#include "core/variant/variant.h"
#include "editor/editor_file_system.h"
#include "gdscript_extend_parser.h"
#include "lsp.hpp"
class GDScriptWorkspace : public Reference {
GDCLASS(GDScriptWorkspace, Reference);
private:
void _get_owners(EditorFileSystemDirectory *efsd, String p_path, List<String> &owners);
Node *_get_owner_scene_node(String p_path);
const lsp::DocumentSymbol *get_parameter_symbol(const lsp::DocumentSymbol *p_parent, const String &symbol_identifier);
const lsp::DocumentSymbol *get_local_symbol(const ExtendGDScriptParser *p_parser, const String &p_symbol_identifier);
protected:
static void _bind_methods();
void remove_cache_parser(const String &p_path);
bool initialized = false;
RBMap<StringName, lsp::DocumentSymbol> native_symbols;
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;
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, PoolStringArray args);
public:
String root;
String root_uri;
RBMap<String, ExtendGDScriptParser *> scripts;
RBMap<String, ExtendGDScriptParser *> parse_results;
HashMap<StringName, ClassMembers> native_members;
public:
Array symbol(const Dictionary &p_params);
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) const;
String get_file_uri(const String &p_path) const;
void publish_diagnostics(const String &p_path);
void completion(const lsp::CompletionParams &p_params, List<ScriptCodeCompletionOption> *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);
Dictionary rename(const lsp::TextDocumentPositionParams &p_doc_pos, const String &new_name);
void did_delete_files(const Dictionary &p_params);
GDScriptWorkspace();
~GDScriptWorkspace();
};
#endif // GDSCRIPT_WORKSPACE_H

File diff suppressed because it is too large Load Diff

View File

@ -329,7 +329,6 @@ def generate_scu_files(verbose, is_release_build, env):
#TODO These should be moved to module conpig.py s
process_folder(["modules/gdscript"])
process_folder(["modules/gdscript/language_server"])
process_folder(["modules/freetype", "brotli/common", "brotli/dec", "freetype/src/autofit", "freetype/src/base",
"freetype/src/bdf", "freetype/src/bzip2", "freetype/src/cache", "freetype/src/cff", "freetype/src/cid", "freetype/src/gxvalid"