initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
610
modules/gdscript/editor/gdscript_docgen.cpp
Normal file
610
modules/gdscript/editor/gdscript_docgen.cpp
Normal file
@@ -0,0 +1,610 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_docgen.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_docgen.h"
|
||||
|
||||
#include "../gdscript.h"
|
||||
|
||||
#include "core/config/project_settings.h"
|
||||
|
||||
HashMap<String, String> GDScriptDocGen::singletons;
|
||||
|
||||
String GDScriptDocGen::_get_script_name(const String &p_path) {
|
||||
const HashMap<String, String>::ConstIterator E = singletons.find(p_path);
|
||||
if (E) {
|
||||
return E->value;
|
||||
}
|
||||
return p_path.trim_prefix("res://").quote();
|
||||
}
|
||||
|
||||
String GDScriptDocGen::_get_class_name(const GDP::ClassNode &p_class) {
|
||||
const GDP::ClassNode *curr_class = &p_class;
|
||||
if (!curr_class->identifier) { // All inner classes have an identifier, so this is the outer class.
|
||||
return _get_script_name(curr_class->fqcn);
|
||||
}
|
||||
|
||||
String full_name = curr_class->identifier->name;
|
||||
while (curr_class->outer) {
|
||||
curr_class = curr_class->outer;
|
||||
if (!curr_class->identifier) { // All inner classes have an identifier, so this is the outer class.
|
||||
return vformat("%s.%s", _get_script_name(curr_class->fqcn), full_name);
|
||||
}
|
||||
full_name = vformat("%s.%s", curr_class->identifier->name, full_name);
|
||||
}
|
||||
return full_name;
|
||||
}
|
||||
|
||||
void GDScriptDocGen::_doctype_from_gdtype(const GDType &p_gdtype, String &r_type, String &r_enum, bool p_is_return) {
|
||||
if (!p_gdtype.is_hard_type()) {
|
||||
r_type = "Variant";
|
||||
return;
|
||||
}
|
||||
switch (p_gdtype.kind) {
|
||||
case GDType::BUILTIN:
|
||||
if (p_gdtype.builtin_type == Variant::NIL) {
|
||||
r_type = p_is_return ? "void" : "null";
|
||||
return;
|
||||
}
|
||||
if (p_gdtype.builtin_type == Variant::ARRAY && p_gdtype.has_container_element_type(0)) {
|
||||
_doctype_from_gdtype(p_gdtype.get_container_element_type(0), r_type, r_enum);
|
||||
if (!r_enum.is_empty()) {
|
||||
r_type = "int[]";
|
||||
r_enum += "[]";
|
||||
return;
|
||||
}
|
||||
if (!r_type.is_empty() && r_type != "Variant") {
|
||||
r_type += "[]";
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (p_gdtype.builtin_type == Variant::DICTIONARY && p_gdtype.has_container_element_types()) {
|
||||
String key, value;
|
||||
_doctype_from_gdtype(p_gdtype.get_container_element_type_or_variant(0), key, r_enum);
|
||||
_doctype_from_gdtype(p_gdtype.get_container_element_type_or_variant(1), value, r_enum);
|
||||
if (key != "Variant" || value != "Variant") {
|
||||
r_type = "Dictionary[" + key + ", " + value + "]";
|
||||
return;
|
||||
}
|
||||
}
|
||||
r_type = Variant::get_type_name(p_gdtype.builtin_type);
|
||||
return;
|
||||
case GDType::NATIVE:
|
||||
if (p_gdtype.is_meta_type) {
|
||||
//r_type = GDScriptNativeClass::get_class_static();
|
||||
r_type = "Object"; // "GDScriptNativeClass" refers to a blank page.
|
||||
return;
|
||||
}
|
||||
r_type = p_gdtype.native_type;
|
||||
return;
|
||||
case GDType::SCRIPT:
|
||||
if (p_gdtype.is_meta_type) {
|
||||
r_type = p_gdtype.script_type.is_valid() ? p_gdtype.script_type->get_class_name() : Script::get_class_static();
|
||||
return;
|
||||
}
|
||||
if (p_gdtype.script_type.is_valid()) {
|
||||
if (p_gdtype.script_type->get_global_name() != StringName()) {
|
||||
r_type = p_gdtype.script_type->get_global_name();
|
||||
return;
|
||||
}
|
||||
if (!p_gdtype.script_type->get_path().is_empty()) {
|
||||
r_type = _get_script_name(p_gdtype.script_type->get_path());
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!p_gdtype.script_path.is_empty()) {
|
||||
r_type = _get_script_name(p_gdtype.script_path);
|
||||
return;
|
||||
}
|
||||
r_type = "Object";
|
||||
return;
|
||||
case GDType::CLASS:
|
||||
if (p_gdtype.is_meta_type) {
|
||||
r_type = GDScript::get_class_static();
|
||||
return;
|
||||
}
|
||||
r_type = _get_class_name(*p_gdtype.class_type);
|
||||
return;
|
||||
case GDType::ENUM:
|
||||
if (p_gdtype.is_meta_type) {
|
||||
r_type = "Dictionary";
|
||||
return;
|
||||
}
|
||||
r_type = "int";
|
||||
r_enum = String(p_gdtype.native_type).replace("::", ".");
|
||||
if (r_enum.begins_with("res://")) {
|
||||
int dot_pos = r_enum.rfind_char('.');
|
||||
if (dot_pos >= 0) {
|
||||
r_enum = _get_script_name(r_enum.left(dot_pos)) + r_enum.substr(dot_pos);
|
||||
} else {
|
||||
r_enum = _get_script_name(r_enum);
|
||||
}
|
||||
}
|
||||
return;
|
||||
case GDType::VARIANT:
|
||||
case GDType::RESOLVING:
|
||||
case GDType::UNRESOLVED:
|
||||
r_type = "Variant";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
String GDScriptDocGen::_docvalue_from_variant(const Variant &p_variant, int p_recursion_level) {
|
||||
constexpr int MAX_RECURSION_LEVEL = 2;
|
||||
|
||||
switch (p_variant.get_type()) {
|
||||
case Variant::STRING:
|
||||
return String(p_variant).c_escape().quote();
|
||||
case Variant::OBJECT:
|
||||
return "<Object>";
|
||||
case Variant::DICTIONARY: {
|
||||
const Dictionary dict = p_variant;
|
||||
String result;
|
||||
|
||||
if (dict.is_typed()) {
|
||||
result += "Dictionary[";
|
||||
|
||||
Ref<Script> key_script = dict.get_typed_key_script();
|
||||
if (key_script.is_valid()) {
|
||||
if (key_script->get_global_name() != StringName()) {
|
||||
result += key_script->get_global_name();
|
||||
} else if (!key_script->get_path().get_file().is_empty()) {
|
||||
result += key_script->get_path().get_file();
|
||||
} else {
|
||||
result += dict.get_typed_key_class_name();
|
||||
}
|
||||
} else if (dict.get_typed_key_class_name() != StringName()) {
|
||||
result += dict.get_typed_key_class_name();
|
||||
} else if (dict.is_typed_key()) {
|
||||
result += Variant::get_type_name((Variant::Type)dict.get_typed_key_builtin());
|
||||
} else {
|
||||
result += "Variant";
|
||||
}
|
||||
|
||||
result += ", ";
|
||||
|
||||
Ref<Script> value_script = dict.get_typed_value_script();
|
||||
if (value_script.is_valid()) {
|
||||
if (value_script->get_global_name() != StringName()) {
|
||||
result += value_script->get_global_name();
|
||||
} else if (!value_script->get_path().get_file().is_empty()) {
|
||||
result += value_script->get_path().get_file();
|
||||
} else {
|
||||
result += dict.get_typed_value_class_name();
|
||||
}
|
||||
} else if (dict.get_typed_value_class_name() != StringName()) {
|
||||
result += dict.get_typed_value_class_name();
|
||||
} else if (dict.is_typed_value()) {
|
||||
result += Variant::get_type_name((Variant::Type)dict.get_typed_value_builtin());
|
||||
} else {
|
||||
result += "Variant";
|
||||
}
|
||||
|
||||
result += "](";
|
||||
}
|
||||
|
||||
if (dict.is_empty()) {
|
||||
result += "{}";
|
||||
} else if (p_recursion_level > MAX_RECURSION_LEVEL) {
|
||||
result += "{...}";
|
||||
} else {
|
||||
result += "{";
|
||||
|
||||
LocalVector<Variant> keys = dict.get_key_list();
|
||||
keys.sort_custom<StringLikeVariantOrder>();
|
||||
|
||||
for (uint32_t i = 0; i < keys.size(); i++) {
|
||||
const Variant &key = keys[i];
|
||||
if (i > 0) {
|
||||
result += ", ";
|
||||
}
|
||||
result += _docvalue_from_variant(key, p_recursion_level + 1) + ": " + _docvalue_from_variant(dict[key], p_recursion_level + 1);
|
||||
}
|
||||
|
||||
result += "}";
|
||||
}
|
||||
|
||||
if (dict.is_typed()) {
|
||||
result += ")";
|
||||
}
|
||||
|
||||
return result;
|
||||
} break;
|
||||
case Variant::ARRAY: {
|
||||
const Array array = p_variant;
|
||||
String result;
|
||||
|
||||
if (array.is_typed()) {
|
||||
result += "Array[";
|
||||
|
||||
Ref<Script> script = array.get_typed_script();
|
||||
if (script.is_valid()) {
|
||||
if (script->get_global_name() != StringName()) {
|
||||
result += script->get_global_name();
|
||||
} else if (!script->get_path().get_file().is_empty()) {
|
||||
result += script->get_path().get_file();
|
||||
} else {
|
||||
result += array.get_typed_class_name();
|
||||
}
|
||||
} else if (array.get_typed_class_name() != StringName()) {
|
||||
result += array.get_typed_class_name();
|
||||
} else {
|
||||
result += Variant::get_type_name((Variant::Type)array.get_typed_builtin());
|
||||
}
|
||||
|
||||
result += "](";
|
||||
}
|
||||
|
||||
if (array.is_empty()) {
|
||||
result += "[]";
|
||||
} else if (p_recursion_level > MAX_RECURSION_LEVEL) {
|
||||
result += "[...]";
|
||||
} else {
|
||||
result += "[";
|
||||
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
if (i > 0) {
|
||||
result += ", ";
|
||||
}
|
||||
result += _docvalue_from_variant(array[i], p_recursion_level + 1);
|
||||
}
|
||||
|
||||
result += "]";
|
||||
}
|
||||
|
||||
if (array.is_typed()) {
|
||||
result += ")";
|
||||
}
|
||||
|
||||
return result;
|
||||
} break;
|
||||
default:
|
||||
return p_variant.get_construct_string();
|
||||
}
|
||||
}
|
||||
|
||||
String GDScriptDocGen::docvalue_from_expression(const GDP::ExpressionNode *p_expression) {
|
||||
ERR_FAIL_NULL_V(p_expression, String());
|
||||
|
||||
if (p_expression->is_constant) {
|
||||
return _docvalue_from_variant(p_expression->reduced_value);
|
||||
}
|
||||
|
||||
switch (p_expression->type) {
|
||||
case GDP::Node::ARRAY: {
|
||||
const GDP::ArrayNode *array = static_cast<const GDP::ArrayNode *>(p_expression);
|
||||
return array->elements.is_empty() ? "[]" : "[...]";
|
||||
} break;
|
||||
case GDP::Node::CALL: {
|
||||
const GDP::CallNode *call = static_cast<const GDP::CallNode *>(p_expression);
|
||||
if (call->get_callee_type() == GDP::Node::IDENTIFIER) {
|
||||
return call->function_name.operator String() + (call->arguments.is_empty() ? "()" : "(...)");
|
||||
}
|
||||
} break;
|
||||
case GDP::Node::DICTIONARY: {
|
||||
const GDP::DictionaryNode *dict = static_cast<const GDP::DictionaryNode *>(p_expression);
|
||||
return dict->elements.is_empty() ? "{}" : "{...}";
|
||||
} break;
|
||||
case GDP::Node::IDENTIFIER: {
|
||||
const GDP::IdentifierNode *id = static_cast<const GDP::IdentifierNode *>(p_expression);
|
||||
return id->name;
|
||||
} break;
|
||||
default: {
|
||||
// Nothing to do.
|
||||
} break;
|
||||
}
|
||||
|
||||
return "<unknown>";
|
||||
}
|
||||
|
||||
void GDScriptDocGen::_generate_docs(GDScript *p_script, const GDP::ClassNode *p_class) {
|
||||
p_script->_clear_doc();
|
||||
|
||||
DocData::ClassDoc &doc = p_script->doc;
|
||||
|
||||
doc.is_script_doc = true;
|
||||
|
||||
if (p_script->local_name == StringName()) {
|
||||
// This is an outer unnamed class.
|
||||
doc.name = _get_script_name(p_script->get_script_path());
|
||||
} else {
|
||||
// This is an inner or global outer class.
|
||||
doc.name = p_script->local_name;
|
||||
if (p_script->_owner) {
|
||||
doc.name = p_script->_owner->doc.name + "." + doc.name;
|
||||
}
|
||||
}
|
||||
|
||||
doc.script_path = p_script->get_script_path();
|
||||
|
||||
if (p_script->base.is_valid() && p_script->base->is_valid()) {
|
||||
if (!p_script->base->doc.name.is_empty()) {
|
||||
doc.inherits = p_script->base->doc.name;
|
||||
} else {
|
||||
doc.inherits = p_script->base->get_instance_base_type();
|
||||
}
|
||||
} else if (p_script->native.is_valid()) {
|
||||
doc.inherits = p_script->native->get_name();
|
||||
}
|
||||
|
||||
doc.brief_description = p_class->doc_data.brief;
|
||||
doc.description = p_class->doc_data.description;
|
||||
for (const Pair<String, String> &p : p_class->doc_data.tutorials) {
|
||||
DocData::TutorialDoc td;
|
||||
td.title = p.first;
|
||||
td.link = p.second;
|
||||
doc.tutorials.append(td);
|
||||
}
|
||||
doc.is_deprecated = p_class->doc_data.is_deprecated;
|
||||
doc.deprecated_message = p_class->doc_data.deprecated_message;
|
||||
doc.is_experimental = p_class->doc_data.is_experimental;
|
||||
doc.experimental_message = p_class->doc_data.experimental_message;
|
||||
|
||||
for (const GDP::ClassNode::Member &member : p_class->members) {
|
||||
switch (member.type) {
|
||||
case GDP::ClassNode::Member::CLASS: {
|
||||
const GDP::ClassNode *inner_class = member.m_class;
|
||||
const StringName &class_name = inner_class->identifier->name;
|
||||
|
||||
p_script->member_lines[class_name] = inner_class->start_line;
|
||||
|
||||
// Recursively generate inner class docs.
|
||||
// Needs inner GDScripts to exist: previously generated in GDScriptCompiler::make_scripts().
|
||||
GDScriptDocGen::_generate_docs(*p_script->subclasses[class_name], inner_class);
|
||||
} break;
|
||||
|
||||
case GDP::ClassNode::Member::CONSTANT: {
|
||||
const GDP::ConstantNode *m_const = member.constant;
|
||||
const StringName &const_name = member.constant->identifier->name;
|
||||
|
||||
p_script->member_lines[const_name] = m_const->start_line;
|
||||
|
||||
DocData::ConstantDoc const_doc;
|
||||
const_doc.name = const_name;
|
||||
const_doc.value = _docvalue_from_variant(m_const->initializer->reduced_value);
|
||||
const_doc.is_value_valid = true;
|
||||
_doctype_from_gdtype(m_const->get_datatype(), const_doc.type, const_doc.enumeration);
|
||||
const_doc.description = m_const->doc_data.description;
|
||||
const_doc.is_deprecated = m_const->doc_data.is_deprecated;
|
||||
const_doc.deprecated_message = m_const->doc_data.deprecated_message;
|
||||
const_doc.is_experimental = m_const->doc_data.is_experimental;
|
||||
const_doc.experimental_message = m_const->doc_data.experimental_message;
|
||||
doc.constants.push_back(const_doc);
|
||||
} break;
|
||||
|
||||
case GDP::ClassNode::Member::FUNCTION: {
|
||||
const GDP::FunctionNode *m_func = member.function;
|
||||
const StringName &func_name = m_func->identifier->name;
|
||||
|
||||
p_script->member_lines[func_name] = m_func->start_line;
|
||||
|
||||
DocData::MethodDoc method_doc;
|
||||
method_doc.name = func_name;
|
||||
method_doc.description = m_func->doc_data.description;
|
||||
method_doc.is_deprecated = m_func->doc_data.is_deprecated;
|
||||
method_doc.deprecated_message = m_func->doc_data.deprecated_message;
|
||||
method_doc.is_experimental = m_func->doc_data.is_experimental;
|
||||
method_doc.experimental_message = m_func->doc_data.experimental_message;
|
||||
|
||||
if (m_func->is_vararg()) {
|
||||
if (!method_doc.qualifiers.is_empty()) {
|
||||
method_doc.qualifiers += " ";
|
||||
}
|
||||
method_doc.qualifiers += "vararg";
|
||||
method_doc.rest_argument.name = m_func->rest_parameter->identifier->name;
|
||||
_doctype_from_gdtype(m_func->rest_parameter->get_datatype(), method_doc.rest_argument.type, method_doc.rest_argument.enumeration);
|
||||
}
|
||||
if (m_func->is_abstract) {
|
||||
if (!method_doc.qualifiers.is_empty()) {
|
||||
method_doc.qualifiers += " ";
|
||||
}
|
||||
method_doc.qualifiers += "abstract";
|
||||
}
|
||||
if (m_func->is_static) {
|
||||
if (!method_doc.qualifiers.is_empty()) {
|
||||
method_doc.qualifiers += " ";
|
||||
}
|
||||
method_doc.qualifiers += "static";
|
||||
}
|
||||
|
||||
if (func_name == "_init") {
|
||||
method_doc.return_type = "void";
|
||||
} else if (m_func->return_type) {
|
||||
// `m_func->return_type->get_datatype()` is a metatype.
|
||||
_doctype_from_gdtype(m_func->get_datatype(), method_doc.return_type, method_doc.return_enum, true);
|
||||
} else if (!m_func->body->has_return) {
|
||||
// If no `return` statement, then return type is `void`, not `Variant`.
|
||||
method_doc.return_type = "void";
|
||||
} else {
|
||||
method_doc.return_type = "Variant";
|
||||
}
|
||||
|
||||
for (const GDP::ParameterNode *p : m_func->parameters) {
|
||||
DocData::ArgumentDoc arg_doc;
|
||||
arg_doc.name = p->identifier->name;
|
||||
_doctype_from_gdtype(p->get_datatype(), arg_doc.type, arg_doc.enumeration);
|
||||
if (p->initializer != nullptr) {
|
||||
arg_doc.default_value = docvalue_from_expression(p->initializer);
|
||||
}
|
||||
method_doc.arguments.push_back(arg_doc);
|
||||
}
|
||||
|
||||
doc.methods.push_back(method_doc);
|
||||
} break;
|
||||
|
||||
case GDP::ClassNode::Member::SIGNAL: {
|
||||
const GDP::SignalNode *m_signal = member.signal;
|
||||
const StringName &signal_name = m_signal->identifier->name;
|
||||
|
||||
p_script->member_lines[signal_name] = m_signal->start_line;
|
||||
|
||||
DocData::MethodDoc signal_doc;
|
||||
signal_doc.name = signal_name;
|
||||
signal_doc.description = m_signal->doc_data.description;
|
||||
signal_doc.is_deprecated = m_signal->doc_data.is_deprecated;
|
||||
signal_doc.deprecated_message = m_signal->doc_data.deprecated_message;
|
||||
signal_doc.is_experimental = m_signal->doc_data.is_experimental;
|
||||
signal_doc.experimental_message = m_signal->doc_data.experimental_message;
|
||||
|
||||
for (const GDP::ParameterNode *p : m_signal->parameters) {
|
||||
DocData::ArgumentDoc arg_doc;
|
||||
arg_doc.name = p->identifier->name;
|
||||
_doctype_from_gdtype(p->get_datatype(), arg_doc.type, arg_doc.enumeration);
|
||||
signal_doc.arguments.push_back(arg_doc);
|
||||
}
|
||||
|
||||
doc.signals.push_back(signal_doc);
|
||||
} break;
|
||||
|
||||
case GDP::ClassNode::Member::VARIABLE: {
|
||||
const GDP::VariableNode *m_var = member.variable;
|
||||
const StringName &var_name = m_var->identifier->name;
|
||||
|
||||
p_script->member_lines[var_name] = m_var->start_line;
|
||||
|
||||
DocData::PropertyDoc prop_doc;
|
||||
prop_doc.name = var_name;
|
||||
prop_doc.description = m_var->doc_data.description;
|
||||
prop_doc.is_deprecated = m_var->doc_data.is_deprecated;
|
||||
prop_doc.deprecated_message = m_var->doc_data.deprecated_message;
|
||||
prop_doc.is_experimental = m_var->doc_data.is_experimental;
|
||||
prop_doc.experimental_message = m_var->doc_data.experimental_message;
|
||||
_doctype_from_gdtype(m_var->get_datatype(), prop_doc.type, prop_doc.enumeration);
|
||||
|
||||
switch (m_var->property) {
|
||||
case GDP::VariableNode::PROP_NONE:
|
||||
break;
|
||||
case GDP::VariableNode::PROP_INLINE:
|
||||
if (m_var->setter != nullptr) {
|
||||
prop_doc.setter = m_var->setter->identifier->name;
|
||||
}
|
||||
if (m_var->getter != nullptr) {
|
||||
prop_doc.getter = m_var->getter->identifier->name;
|
||||
}
|
||||
break;
|
||||
case GDP::VariableNode::PROP_SETGET:
|
||||
if (m_var->setter_pointer != nullptr) {
|
||||
prop_doc.setter = m_var->setter_pointer->name;
|
||||
}
|
||||
if (m_var->getter_pointer != nullptr) {
|
||||
prop_doc.getter = m_var->getter_pointer->name;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (m_var->initializer != nullptr) {
|
||||
prop_doc.default_value = docvalue_from_expression(m_var->initializer);
|
||||
}
|
||||
|
||||
prop_doc.overridden = false;
|
||||
|
||||
doc.properties.push_back(prop_doc);
|
||||
} break;
|
||||
|
||||
case GDP::ClassNode::Member::ENUM: {
|
||||
const GDP::EnumNode *m_enum = member.m_enum;
|
||||
StringName name = m_enum->identifier->name;
|
||||
|
||||
p_script->member_lines[name] = m_enum->start_line;
|
||||
|
||||
DocData::EnumDoc enum_doc;
|
||||
enum_doc.description = m_enum->doc_data.description;
|
||||
enum_doc.is_deprecated = m_enum->doc_data.is_deprecated;
|
||||
enum_doc.deprecated_message = m_enum->doc_data.deprecated_message;
|
||||
enum_doc.is_experimental = m_enum->doc_data.is_experimental;
|
||||
enum_doc.experimental_message = m_enum->doc_data.experimental_message;
|
||||
doc.enums[name] = enum_doc;
|
||||
|
||||
for (const GDP::EnumNode::Value &val : m_enum->values) {
|
||||
DocData::ConstantDoc const_doc;
|
||||
const_doc.name = val.identifier->name;
|
||||
const_doc.value = _docvalue_from_variant(val.value);
|
||||
const_doc.is_value_valid = true;
|
||||
const_doc.type = "int";
|
||||
const_doc.enumeration = name;
|
||||
const_doc.description = val.doc_data.description;
|
||||
const_doc.is_deprecated = val.doc_data.is_deprecated;
|
||||
const_doc.deprecated_message = val.doc_data.deprecated_message;
|
||||
const_doc.is_experimental = val.doc_data.is_experimental;
|
||||
const_doc.experimental_message = val.doc_data.experimental_message;
|
||||
|
||||
doc.constants.push_back(const_doc);
|
||||
}
|
||||
|
||||
} break;
|
||||
|
||||
case GDP::ClassNode::Member::ENUM_VALUE: {
|
||||
const GDP::EnumNode::Value &m_enum_val = member.enum_value;
|
||||
const StringName &name = m_enum_val.identifier->name;
|
||||
|
||||
p_script->member_lines[name] = m_enum_val.identifier->start_line;
|
||||
|
||||
DocData::ConstantDoc const_doc;
|
||||
const_doc.name = name;
|
||||
const_doc.value = _docvalue_from_variant(m_enum_val.value);
|
||||
const_doc.is_value_valid = true;
|
||||
const_doc.type = "int";
|
||||
const_doc.enumeration = "@unnamed_enums";
|
||||
const_doc.description = m_enum_val.doc_data.description;
|
||||
const_doc.is_deprecated = m_enum_val.doc_data.is_deprecated;
|
||||
const_doc.deprecated_message = m_enum_val.doc_data.deprecated_message;
|
||||
const_doc.is_experimental = m_enum_val.doc_data.is_experimental;
|
||||
const_doc.experimental_message = m_enum_val.doc_data.experimental_message;
|
||||
doc.constants.push_back(const_doc);
|
||||
} break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Add doc to the outer-most class.
|
||||
p_script->_add_doc(doc);
|
||||
}
|
||||
|
||||
void GDScriptDocGen::generate_docs(GDScript *p_script, const GDP::ClassNode *p_class) {
|
||||
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
|
||||
if (E.value.is_singleton) {
|
||||
singletons[E.value.path] = E.key;
|
||||
}
|
||||
}
|
||||
_generate_docs(p_script, p_class);
|
||||
singletons.clear();
|
||||
}
|
||||
|
||||
// This method is needed for the editor, since during autocompletion the script is not compiled, only analyzed.
|
||||
void GDScriptDocGen::doctype_from_gdtype(const GDType &p_gdtype, String &r_type, String &r_enum, bool p_is_return) {
|
||||
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
|
||||
if (E.value.is_singleton) {
|
||||
singletons[E.value.path] = E.key;
|
||||
}
|
||||
}
|
||||
_doctype_from_gdtype(p_gdtype, r_type, r_enum, p_is_return);
|
||||
singletons.clear();
|
||||
}
|
53
modules/gdscript/editor/gdscript_docgen.h
Normal file
53
modules/gdscript/editor/gdscript_docgen.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_docgen.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../gdscript_parser.h"
|
||||
|
||||
#include "core/doc_data.h"
|
||||
|
||||
class GDScriptDocGen {
|
||||
using GDP = GDScriptParser;
|
||||
using GDType = GDP::DataType;
|
||||
|
||||
static HashMap<String, String> singletons; // Script path to singleton name.
|
||||
|
||||
static String _get_script_name(const String &p_path);
|
||||
static String _get_class_name(const GDP::ClassNode &p_class);
|
||||
static void _doctype_from_gdtype(const GDType &p_gdtype, String &r_type, String &r_enum, bool p_is_return = false);
|
||||
static String _docvalue_from_variant(const Variant &p_variant, int p_recursion_level = 1);
|
||||
static void _generate_docs(GDScript *p_script, const GDP::ClassNode *p_class);
|
||||
|
||||
public:
|
||||
static void generate_docs(GDScript *p_script, const GDP::ClassNode *p_class);
|
||||
static void doctype_from_gdtype(const GDType &p_gdtype, String &r_type, String &r_enum, bool p_is_return = false);
|
||||
static String docvalue_from_expression(const GDP::ExpressionNode *p_expression);
|
||||
};
|
965
modules/gdscript/editor/gdscript_highlighter.cpp
Normal file
965
modules/gdscript/editor/gdscript_highlighter.cpp
Normal file
@@ -0,0 +1,965 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_highlighter.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_highlighter.h"
|
||||
|
||||
#include "../gdscript.h"
|
||||
#include "../gdscript_tokenizer.h"
|
||||
|
||||
#include "core/config/project_settings.h"
|
||||
#include "core/core_constants.h"
|
||||
#include "editor/settings/editor_settings.h"
|
||||
#include "editor/themes/editor_theme_manager.h"
|
||||
#include "scene/gui/text_edit.h"
|
||||
|
||||
Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_line) {
|
||||
Dictionary color_map;
|
||||
|
||||
Type next_type = NONE;
|
||||
Type current_type = NONE;
|
||||
Type prev_type = NONE;
|
||||
|
||||
String prev_text = "";
|
||||
int prev_column = 0;
|
||||
bool prev_is_char = false;
|
||||
bool prev_is_digit = false;
|
||||
bool prev_is_binary_op = false;
|
||||
|
||||
bool in_keyword = false;
|
||||
bool in_word = false;
|
||||
bool in_number = false;
|
||||
bool in_node_path = false;
|
||||
bool in_node_ref = false;
|
||||
bool in_annotation = false;
|
||||
bool in_string_name = false;
|
||||
bool is_hex_notation = false;
|
||||
bool is_bin_notation = false;
|
||||
bool in_member_variable = false;
|
||||
bool in_lambda = false;
|
||||
|
||||
bool in_function_name = false; // Any call.
|
||||
bool in_function_declaration = false; // Only declaration.
|
||||
bool in_signal_declaration = false;
|
||||
bool is_after_func_signal_declaration = false;
|
||||
bool in_var_const_declaration = false;
|
||||
bool is_after_var_const_declaration = false;
|
||||
bool expect_type = false;
|
||||
|
||||
int in_declaration_params = 0; // The number of opened `(` after func/signal name.
|
||||
int in_declaration_param_dicts = 0; // The number of opened `{` inside func params.
|
||||
int in_type_params = 0; // The number of opened `[` after type name.
|
||||
|
||||
Color keyword_color;
|
||||
Color color;
|
||||
|
||||
color_region_cache[p_line] = -1;
|
||||
int in_region = -1;
|
||||
if (p_line != 0) {
|
||||
int prev_region_line = p_line - 1;
|
||||
while (prev_region_line > 0 && !color_region_cache.has(prev_region_line)) {
|
||||
prev_region_line--;
|
||||
}
|
||||
for (int i = prev_region_line; i < p_line - 1; i++) {
|
||||
get_line_syntax_highlighting(i);
|
||||
}
|
||||
if (!color_region_cache.has(p_line - 1)) {
|
||||
get_line_syntax_highlighting(p_line - 1);
|
||||
}
|
||||
in_region = color_region_cache[p_line - 1];
|
||||
}
|
||||
|
||||
const String &str = text_edit->get_line_with_ime(p_line);
|
||||
const int line_length = str.length();
|
||||
Color prev_color;
|
||||
|
||||
if (in_region != -1 && line_length == 0) {
|
||||
color_region_cache[p_line] = in_region;
|
||||
}
|
||||
for (int j = 0; j < line_length; j++) {
|
||||
Dictionary highlighter_info;
|
||||
|
||||
color = font_color;
|
||||
bool is_char = !is_symbol(str[j]);
|
||||
bool is_a_symbol = is_symbol(str[j]);
|
||||
bool is_a_digit = is_digit(str[j]);
|
||||
bool is_binary_op = false;
|
||||
|
||||
/* color regions */
|
||||
if (is_a_symbol || in_region != -1) {
|
||||
int from = j;
|
||||
|
||||
if (in_region == -1) {
|
||||
for (; from < line_length; from++) {
|
||||
if (str[from] == '\\') {
|
||||
from++;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (from != line_length) {
|
||||
// Check if we are in entering a region.
|
||||
if (in_region == -1) {
|
||||
const bool r_prefix = from > 0 && str[from - 1] == 'r';
|
||||
for (int c = 0; c < color_regions.size(); c++) {
|
||||
// Check there is enough room.
|
||||
int chars_left = line_length - from;
|
||||
int start_key_length = color_regions[c].start_key.length();
|
||||
int end_key_length = color_regions[c].end_key.length();
|
||||
if (chars_left < start_key_length) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (color_regions[c].is_string && color_regions[c].r_prefix != r_prefix) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Search the line.
|
||||
bool match = true;
|
||||
const char32_t *start_key = color_regions[c].start_key.get_data();
|
||||
for (int k = 0; k < start_key_length; k++) {
|
||||
if (start_key[k] != str[from + k]) {
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// "#region" and "#endregion" only highlighted if they're the first region on the line.
|
||||
if (color_regions[c].type == ColorRegion::TYPE_CODE_REGION) {
|
||||
Vector<String> str_stripped_split = str.strip_edges().split_spaces(1);
|
||||
if (!str_stripped_split.is_empty() &&
|
||||
str_stripped_split[0] != "#region" &&
|
||||
str_stripped_split[0] != "#endregion") {
|
||||
match = false;
|
||||
}
|
||||
}
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
in_region = c;
|
||||
from += start_key_length;
|
||||
|
||||
// Check if it's the whole line.
|
||||
if (end_key_length == 0 || color_regions[c].line_only || from + end_key_length > line_length) {
|
||||
// Don't skip comments, for highlighting markers.
|
||||
if (color_regions[in_region].is_comment) {
|
||||
break;
|
||||
}
|
||||
if (from + end_key_length > line_length) {
|
||||
// If it's key length and there is a '\', dont skip to highlight esc chars.
|
||||
if (str.find_char('\\', from) >= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
prev_color = color_regions[in_region].color;
|
||||
highlighter_info["color"] = color_regions[c].color;
|
||||
color_map[j] = highlighter_info;
|
||||
|
||||
j = line_length;
|
||||
if (!color_regions[c].line_only) {
|
||||
color_region_cache[p_line] = c;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Don't skip comments, for highlighting markers.
|
||||
if (j == line_length && !color_regions[in_region].is_comment) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// If we are in one, find the end key.
|
||||
if (in_region != -1) {
|
||||
Color region_color = color_regions[in_region].color;
|
||||
if (in_node_path && color_regions[in_region].type == ColorRegion::TYPE_STRING) {
|
||||
region_color = node_path_color;
|
||||
}
|
||||
if (in_node_ref && color_regions[in_region].type == ColorRegion::TYPE_STRING) {
|
||||
region_color = node_ref_color;
|
||||
}
|
||||
if (in_string_name && color_regions[in_region].type == ColorRegion::TYPE_STRING) {
|
||||
region_color = string_name_color;
|
||||
}
|
||||
|
||||
prev_color = region_color;
|
||||
highlighter_info["color"] = region_color;
|
||||
color_map[j] = highlighter_info;
|
||||
|
||||
if (color_regions[in_region].is_comment) {
|
||||
int marker_start_pos = from;
|
||||
int marker_len = 0;
|
||||
while (from <= line_length) {
|
||||
if (from < line_length && is_unicode_identifier_continue(str[from])) {
|
||||
marker_len++;
|
||||
} else {
|
||||
if (marker_len > 0) {
|
||||
HashMap<String, CommentMarkerLevel>::ConstIterator E = comment_markers.find(str.substr(marker_start_pos, marker_len));
|
||||
if (E) {
|
||||
Dictionary marker_highlighter_info;
|
||||
marker_highlighter_info["color"] = comment_marker_colors[E->value];
|
||||
color_map[marker_start_pos] = marker_highlighter_info;
|
||||
|
||||
Dictionary marker_continue_highlighter_info;
|
||||
marker_continue_highlighter_info["color"] = region_color;
|
||||
color_map[from] = marker_continue_highlighter_info;
|
||||
}
|
||||
}
|
||||
marker_start_pos = from + 1;
|
||||
marker_len = 0;
|
||||
}
|
||||
from++;
|
||||
}
|
||||
from = line_length - 1;
|
||||
j = from;
|
||||
} else {
|
||||
// Search the line.
|
||||
int region_end_index = -1;
|
||||
int end_key_length = color_regions[in_region].end_key.length();
|
||||
const char32_t *end_key = color_regions[in_region].end_key.get_data();
|
||||
for (; from < line_length; from++) {
|
||||
if (line_length - from < end_key_length) {
|
||||
// Don't break if '\' to highlight esc chars.
|
||||
if (str.find_char('\\', from) < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_symbol(str[from])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (str[from] == '\\') {
|
||||
if (!color_regions[in_region].r_prefix) {
|
||||
Dictionary escape_char_highlighter_info;
|
||||
escape_char_highlighter_info["color"] = symbol_color;
|
||||
color_map[from] = escape_char_highlighter_info;
|
||||
}
|
||||
|
||||
from++;
|
||||
|
||||
if (!color_regions[in_region].r_prefix) {
|
||||
int esc_len = 0;
|
||||
if (str[from] == 'u') {
|
||||
esc_len = 4;
|
||||
} else if (str[from] == 'U') {
|
||||
esc_len = 6;
|
||||
}
|
||||
for (int k = 0; k < esc_len && from < line_length - 1; k++) {
|
||||
if (!is_hex_digit(str[from + 1])) {
|
||||
break;
|
||||
}
|
||||
from++;
|
||||
}
|
||||
|
||||
Dictionary region_continue_highlighter_info;
|
||||
region_continue_highlighter_info["color"] = region_color;
|
||||
color_map[from + 1] = region_continue_highlighter_info;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
region_end_index = from;
|
||||
for (int k = 0; k < end_key_length; k++) {
|
||||
if (end_key[k] != str[from + k]) {
|
||||
region_end_index = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (region_end_index != -1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
j = from + (end_key_length - 1);
|
||||
if (region_end_index == -1) {
|
||||
color_region_cache[p_line] = in_region;
|
||||
}
|
||||
}
|
||||
|
||||
prev_type = REGION;
|
||||
prev_text = "";
|
||||
prev_column = j;
|
||||
|
||||
in_region = -1;
|
||||
prev_is_char = false;
|
||||
prev_is_digit = false;
|
||||
prev_is_binary_op = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// VERY hacky... but couldn't come up with anything better.
|
||||
if (j > 0 && (str[j] == '&' || str[j] == '^' || str[j] == '%' || str[j] == '+' || str[j] == '-' || str[j] == '~' || str[j] == '.')) {
|
||||
int to = j - 1;
|
||||
// Find what the last text was (prev_text won't work if there's no whitespace, so we need to do it manually).
|
||||
while (to > 0 && is_whitespace(str[to])) {
|
||||
to--;
|
||||
}
|
||||
int from = to;
|
||||
while (from > 0 && !is_symbol(str[from])) {
|
||||
from--;
|
||||
}
|
||||
String word = str.substr(from + 1, to - from);
|
||||
// Keywords need to be exceptions, except for keywords that represent a value.
|
||||
if (word == "true" || word == "false" || word == "null" || word == "PI" || word == "TAU" || word == "INF" || word == "NAN" || word == "self" || word == "super" || !reserved_keywords.has(word)) {
|
||||
if (!is_symbol(str[to]) || str[to] == '"' || str[to] == '\'' || str[to] == ')' || str[to] == ']' || str[to] == '}') {
|
||||
is_binary_op = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_char) {
|
||||
in_keyword = false;
|
||||
}
|
||||
|
||||
// Allow ABCDEF in hex notation.
|
||||
if (is_hex_notation && (is_hex_digit(str[j]) || is_a_digit)) {
|
||||
is_a_digit = true;
|
||||
} else if (str[j] != '_') {
|
||||
is_hex_notation = false;
|
||||
}
|
||||
|
||||
// Disallow anything not a 0 or 1 in binary notation.
|
||||
if (is_bin_notation && !is_binary_digit(str[j])) {
|
||||
is_a_digit = false;
|
||||
is_bin_notation = false;
|
||||
}
|
||||
|
||||
if (!in_number && !in_word && is_a_digit) {
|
||||
in_number = true;
|
||||
}
|
||||
|
||||
// Special cases for numbers.
|
||||
if (in_number && !is_a_digit) {
|
||||
if ((str[j] == 'b' || str[j] == 'B') && str[j - 1] == '0') {
|
||||
is_bin_notation = true;
|
||||
} else if ((str[j] == 'x' || str[j] == 'X') && str[j - 1] == '0') {
|
||||
is_hex_notation = true;
|
||||
} else if (!((str[j] == '-' || str[j] == '+') && (str[j - 1] == 'e' || str[j - 1] == 'E') && !prev_is_digit) &&
|
||||
!(str[j] == '_' && (prev_is_digit || str[j - 1] == 'b' || str[j - 1] == 'B' || str[j - 1] == 'x' || str[j - 1] == 'X' || str[j - 1] == '.')) &&
|
||||
!((str[j] == 'e' || str[j] == 'E') && (prev_is_digit || str[j - 1] == '_')) &&
|
||||
!(str[j] == '.' && (prev_is_digit || (!prev_is_binary_op && (j > 0 && (str[j - 1] == '_' || str[j - 1] == '-' || str[j - 1] == '+' || str[j - 1] == '~'))))) &&
|
||||
!((str[j] == '-' || str[j] == '+' || str[j] == '~') && !is_binary_op && !prev_is_binary_op && str[j - 1] != 'e' && str[j - 1] != 'E')) {
|
||||
/* This condition continues number highlighting in special cases.
|
||||
1st row: '+' or '-' after scientific notation (like 3e-4);
|
||||
2nd row: '_' as a numeric separator;
|
||||
3rd row: Scientific notation 'e' and floating points;
|
||||
4th row: Floating points inside the number, or leading if after a unary mathematical operator;
|
||||
5th row: Multiple unary mathematical operators (like ~-7) */
|
||||
in_number = false;
|
||||
}
|
||||
} else if (str[j] == '.' && !is_binary_op && is_digit(str[j + 1]) && (j == 0 || (j > 0 && str[j - 1] != '.'))) {
|
||||
// Start number highlighting from leading decimal points (like .42)
|
||||
in_number = true;
|
||||
} else if ((str[j] == '-' || str[j] == '+' || str[j] == '~') && !is_binary_op) {
|
||||
// Only start number highlighting on unary operators if a digit follows them.
|
||||
int non_op = j + 1;
|
||||
while (str[non_op] == '-' || str[non_op] == '+' || str[non_op] == '~') {
|
||||
non_op++;
|
||||
}
|
||||
if (is_digit(str[non_op]) || (str[non_op] == '.' && non_op < line_length && is_digit(str[non_op + 1]))) {
|
||||
in_number = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!in_word && is_unicode_identifier_start(str[j]) && !in_number) {
|
||||
in_word = true;
|
||||
}
|
||||
|
||||
if (is_a_symbol && str[j] != '.' && in_word) {
|
||||
in_word = false;
|
||||
}
|
||||
|
||||
if (!in_keyword && is_char && !prev_is_char) {
|
||||
int to = j;
|
||||
while (to < line_length && !is_symbol(str[to])) {
|
||||
to++;
|
||||
}
|
||||
|
||||
String word = str.substr(j, to - j);
|
||||
Color col;
|
||||
if (global_functions.has(word)) {
|
||||
// "assert" and "preload" are reserved, so highlight even if not followed by a bracket.
|
||||
if (word == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::ASSERT) || word == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::PRELOAD)) {
|
||||
col = global_function_color;
|
||||
} else {
|
||||
// For other global functions, check if followed by bracket.
|
||||
int k = to;
|
||||
while (k < line_length && is_whitespace(str[k])) {
|
||||
k++;
|
||||
}
|
||||
|
||||
if (str[k] == '(') {
|
||||
col = global_function_color;
|
||||
}
|
||||
}
|
||||
} else if (class_names.has(word)) {
|
||||
col = class_names[word];
|
||||
} else if (reserved_keywords.has(word)) {
|
||||
col = reserved_keywords[word];
|
||||
// Don't highlight `list` as a type in `for elem: Type in list`.
|
||||
expect_type = false;
|
||||
} else if (member_keywords.has(word)) {
|
||||
col = member_keywords[word];
|
||||
in_member_variable = true;
|
||||
}
|
||||
|
||||
if (col != Color()) {
|
||||
for (int k = j - 1; k >= 0; k--) {
|
||||
if (str[k] == '.') {
|
||||
col = Color(); // Keyword, member & global func indexing not allowed.
|
||||
break;
|
||||
} else if (str[k] > 32) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!in_member_variable && col != Color()) {
|
||||
in_keyword = true;
|
||||
keyword_color = col;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!in_function_name && in_word && !in_keyword) {
|
||||
if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::SIGNAL)) {
|
||||
in_signal_declaration = true;
|
||||
} else {
|
||||
int k = j;
|
||||
while (k < line_length && !is_symbol(str[k]) && !is_whitespace(str[k])) {
|
||||
k++;
|
||||
}
|
||||
|
||||
// Check for space between name and bracket.
|
||||
while (k < line_length && is_whitespace(str[k])) {
|
||||
k++;
|
||||
}
|
||||
|
||||
if (str[k] == '(') {
|
||||
in_function_name = true;
|
||||
if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
|
||||
in_function_declaration = true;
|
||||
}
|
||||
} else if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::VAR) || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FOR) || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::TK_CONST)) {
|
||||
in_var_const_declaration = true;
|
||||
}
|
||||
|
||||
// Check for lambda.
|
||||
if (in_function_declaration) {
|
||||
k = j - 1;
|
||||
while (k > 0 && is_whitespace(str[k])) {
|
||||
k--;
|
||||
}
|
||||
|
||||
if (str[k] == ':') {
|
||||
in_lambda = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!in_function_name && !in_member_variable && !in_keyword && !in_number && in_word) {
|
||||
int k = j;
|
||||
while (k > 0 && !is_symbol(str[k]) && !is_whitespace(str[k])) {
|
||||
k--;
|
||||
}
|
||||
|
||||
if (str[k] == '.' && (k < 1 || str[k - 1] != '.')) {
|
||||
in_member_variable = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_a_symbol) {
|
||||
if (in_function_declaration || in_signal_declaration) {
|
||||
is_after_func_signal_declaration = true;
|
||||
}
|
||||
if (in_var_const_declaration) {
|
||||
is_after_var_const_declaration = true;
|
||||
}
|
||||
|
||||
if (in_declaration_params > 0) {
|
||||
switch (str[j]) {
|
||||
case '(':
|
||||
in_declaration_params += 1;
|
||||
break;
|
||||
case ')':
|
||||
in_declaration_params -= 1;
|
||||
break;
|
||||
case '{':
|
||||
in_declaration_param_dicts += 1;
|
||||
break;
|
||||
case '}':
|
||||
in_declaration_param_dicts -= 1;
|
||||
break;
|
||||
}
|
||||
} else if ((is_after_func_signal_declaration || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) && str[j] == '(') {
|
||||
in_declaration_params = 1;
|
||||
in_declaration_param_dicts = 0;
|
||||
}
|
||||
|
||||
if (expect_type) {
|
||||
switch (str[j]) {
|
||||
case '[':
|
||||
in_type_params += 1;
|
||||
break;
|
||||
case ']':
|
||||
in_type_params -= 1;
|
||||
break;
|
||||
case ',':
|
||||
if (in_type_params <= 0) {
|
||||
expect_type = false;
|
||||
}
|
||||
break;
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '.':
|
||||
break;
|
||||
default:
|
||||
expect_type = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (j > 0 && str[j - 1] == '-' && str[j] == '>') {
|
||||
expect_type = true;
|
||||
in_type_params = 0;
|
||||
}
|
||||
if ((is_after_var_const_declaration || (in_declaration_params == 1 && in_declaration_param_dicts == 0)) && str[j] == ':') {
|
||||
expect_type = true;
|
||||
in_type_params = 0;
|
||||
}
|
||||
}
|
||||
|
||||
in_function_name = false;
|
||||
in_function_declaration = false;
|
||||
in_signal_declaration = false;
|
||||
in_var_const_declaration = false;
|
||||
in_lambda = false;
|
||||
in_member_variable = false;
|
||||
|
||||
if (!is_whitespace(str[j])) {
|
||||
is_after_func_signal_declaration = false;
|
||||
is_after_var_const_declaration = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Set color of StringName, keeping symbol color for binary '&&' and '&'.
|
||||
if (!in_string_name && in_region == -1 && str[j] == '&' && !is_binary_op) {
|
||||
if (j + 1 <= line_length - 1 && (str[j + 1] == '\'' || str[j + 1] == '"')) {
|
||||
in_string_name = true;
|
||||
// Cover edge cases of i.e. '+&""' and '&&&""', so the StringName is properly colored.
|
||||
if (prev_is_binary_op && j >= 2 && str[j - 1] == '&' && str[j - 2] != '&') {
|
||||
in_string_name = false;
|
||||
is_binary_op = true;
|
||||
}
|
||||
} else {
|
||||
is_binary_op = true;
|
||||
}
|
||||
} else if (in_region != -1 || is_a_symbol) {
|
||||
in_string_name = false;
|
||||
}
|
||||
|
||||
// '^^' has no special meaning, so unlike StringName, when binary, use NodePath color for the last caret.
|
||||
if (!in_node_path && in_region == -1 && str[j] == '^' && !is_binary_op && (j == 0 || (j > 0 && str[j - 1] != '^') || prev_is_binary_op)) {
|
||||
in_node_path = true;
|
||||
} else if (in_region != -1 || is_a_symbol) {
|
||||
in_node_path = false;
|
||||
}
|
||||
|
||||
if (!in_node_ref && in_region == -1 && (str[j] == '$' || (str[j] == '%' && !is_binary_op))) {
|
||||
in_node_ref = true;
|
||||
} else if (in_region != -1 || (is_a_symbol && str[j] != '/' && str[j] != '%') || (is_a_digit && j > 0 && (str[j - 1] == '$' || str[j - 1] == '/' || str[j - 1] == '%'))) {
|
||||
// NodeRefs can't start with digits, so point out wrong syntax immediately.
|
||||
in_node_ref = false;
|
||||
}
|
||||
|
||||
if (!in_annotation && in_region == -1 && str[j] == '@') {
|
||||
in_annotation = true;
|
||||
} else if (in_region != -1 || is_a_symbol) {
|
||||
in_annotation = false;
|
||||
}
|
||||
|
||||
const bool in_raw_string_prefix = in_region == -1 && str[j] == 'r' && j + 1 < line_length && (str[j + 1] == '"' || str[j + 1] == '\'');
|
||||
|
||||
if (in_raw_string_prefix) {
|
||||
color = string_color;
|
||||
} else if (in_node_ref) {
|
||||
next_type = NODE_REF;
|
||||
color = node_ref_color;
|
||||
} else if (in_annotation) {
|
||||
next_type = ANNOTATION;
|
||||
color = annotation_color;
|
||||
} else if (in_string_name) {
|
||||
next_type = STRING_NAME;
|
||||
color = string_name_color;
|
||||
} else if (in_node_path) {
|
||||
next_type = NODE_PATH;
|
||||
color = node_path_color;
|
||||
} else if (in_keyword) {
|
||||
next_type = KEYWORD;
|
||||
color = keyword_color;
|
||||
} else if (in_signal_declaration) {
|
||||
next_type = SIGNAL;
|
||||
color = member_variable_color;
|
||||
} else if (in_function_name) {
|
||||
next_type = FUNCTION;
|
||||
if (!in_lambda && in_function_declaration) {
|
||||
color = function_definition_color;
|
||||
} else {
|
||||
color = function_color;
|
||||
}
|
||||
} else if (in_number) {
|
||||
next_type = NUMBER;
|
||||
color = number_color;
|
||||
} else if (is_a_symbol) {
|
||||
next_type = SYMBOL;
|
||||
color = symbol_color;
|
||||
} else if (expect_type) {
|
||||
next_type = TYPE;
|
||||
color = type_color;
|
||||
} else if (in_member_variable) {
|
||||
next_type = MEMBER;
|
||||
color = member_variable_color;
|
||||
} else {
|
||||
next_type = IDENTIFIER;
|
||||
}
|
||||
|
||||
if (next_type != current_type) {
|
||||
if (current_type == NONE) {
|
||||
current_type = next_type;
|
||||
} else {
|
||||
prev_type = current_type;
|
||||
current_type = next_type;
|
||||
|
||||
// No need to store regions...
|
||||
if (prev_type == REGION) {
|
||||
prev_text = "";
|
||||
prev_column = j;
|
||||
} else {
|
||||
String text = str.substr(prev_column, j - prev_column).strip_edges();
|
||||
prev_column = j;
|
||||
|
||||
// Ignore if just whitespace.
|
||||
if (!text.is_empty()) {
|
||||
prev_text = text;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
prev_is_char = is_char;
|
||||
prev_is_digit = is_a_digit;
|
||||
prev_is_binary_op = is_binary_op;
|
||||
|
||||
if (color != prev_color) {
|
||||
prev_color = color;
|
||||
highlighter_info["color"] = color;
|
||||
color_map[j] = highlighter_info;
|
||||
}
|
||||
}
|
||||
return color_map;
|
||||
}
|
||||
|
||||
String GDScriptSyntaxHighlighter::_get_name() const {
|
||||
return "GDScript";
|
||||
}
|
||||
|
||||
PackedStringArray GDScriptSyntaxHighlighter::_get_supported_languages() const {
|
||||
PackedStringArray languages;
|
||||
languages.push_back("GDScript");
|
||||
return languages;
|
||||
}
|
||||
|
||||
void GDScriptSyntaxHighlighter::_update_cache() {
|
||||
class_names.clear();
|
||||
reserved_keywords.clear();
|
||||
member_keywords.clear();
|
||||
global_functions.clear();
|
||||
color_regions.clear();
|
||||
color_region_cache.clear();
|
||||
|
||||
font_color = text_edit->get_theme_color(SceneStringName(font_color));
|
||||
symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");
|
||||
function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");
|
||||
number_color = EDITOR_GET("text_editor/theme/highlighting/number_color");
|
||||
member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
|
||||
|
||||
/* Engine types. */
|
||||
const Color types_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
|
||||
List<StringName> types;
|
||||
ClassDB::get_class_list(&types);
|
||||
for (const StringName &E : types) {
|
||||
if (ClassDB::is_class_exposed(E)) {
|
||||
class_names[E] = types_color;
|
||||
}
|
||||
}
|
||||
|
||||
/* Global enums. */
|
||||
List<StringName> global_enums;
|
||||
CoreConstants::get_global_enums(&global_enums);
|
||||
for (const StringName &enum_name : global_enums) {
|
||||
class_names[enum_name] = types_color;
|
||||
}
|
||||
|
||||
/* User types. */
|
||||
const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");
|
||||
List<StringName> global_classes;
|
||||
ScriptServer::get_global_class_list(&global_classes);
|
||||
for (const StringName &E : global_classes) {
|
||||
class_names[E] = usertype_color;
|
||||
}
|
||||
|
||||
/* Autoloads. */
|
||||
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
|
||||
const ProjectSettings::AutoloadInfo &info = E.value;
|
||||
if (info.is_singleton) {
|
||||
class_names[info.name] = usertype_color;
|
||||
}
|
||||
}
|
||||
|
||||
const GDScriptLanguage *gdscript = GDScriptLanguage::get_singleton();
|
||||
|
||||
/* Core types. */
|
||||
const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
|
||||
List<String> core_types;
|
||||
gdscript->get_core_type_words(&core_types);
|
||||
for (const String &E : core_types) {
|
||||
class_names[StringName(E)] = basetype_color;
|
||||
}
|
||||
class_names[SNAME("Variant")] = basetype_color;
|
||||
class_names[SNAME("void")] = basetype_color;
|
||||
// `get_core_type_words()` doesn't return primitive types.
|
||||
class_names[SNAME("bool")] = basetype_color;
|
||||
class_names[SNAME("int")] = basetype_color;
|
||||
class_names[SNAME("float")] = basetype_color;
|
||||
|
||||
/* Reserved words. */
|
||||
const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
|
||||
const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
|
||||
for (const String &keyword : gdscript->get_reserved_words()) {
|
||||
if (gdscript->is_control_flow_keyword(keyword)) {
|
||||
reserved_keywords[StringName(keyword)] = control_flow_keyword_color;
|
||||
} else {
|
||||
reserved_keywords[StringName(keyword)] = keyword_color;
|
||||
}
|
||||
}
|
||||
|
||||
// Highlight `set` and `get` as "keywords" with the function color to avoid conflicts with method calls.
|
||||
reserved_keywords[SNAME("set")] = function_color;
|
||||
reserved_keywords[SNAME("get")] = function_color;
|
||||
|
||||
/* Global functions. */
|
||||
List<StringName> global_function_list;
|
||||
GDScriptUtilityFunctions::get_function_list(&global_function_list);
|
||||
Variant::get_utility_function_list(&global_function_list);
|
||||
// "assert" and "preload" are not utility functions, but are global nonetheless, so insert them.
|
||||
global_functions.insert(SNAME("assert"));
|
||||
global_functions.insert(SNAME("preload"));
|
||||
for (const StringName &E : global_function_list) {
|
||||
global_functions.insert(E);
|
||||
}
|
||||
|
||||
/* Comments. */
|
||||
const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
|
||||
for (const String &comment : gdscript->get_comment_delimiters()) {
|
||||
String beg = comment.get_slicec(' ', 0);
|
||||
String end = comment.get_slice_count(" ") > 1 ? comment.get_slicec(' ', 1) : String();
|
||||
add_color_region(ColorRegion::TYPE_COMMENT, beg, end, comment_color, end.is_empty());
|
||||
}
|
||||
|
||||
/* Doc comments */
|
||||
const Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
|
||||
for (const String &doc_comment : gdscript->get_doc_comment_delimiters()) {
|
||||
String beg = doc_comment.get_slicec(' ', 0);
|
||||
String end = doc_comment.get_slice_count(" ") > 1 ? doc_comment.get_slicec(' ', 1) : String();
|
||||
add_color_region(ColorRegion::TYPE_COMMENT, beg, end, doc_comment_color, end.is_empty());
|
||||
}
|
||||
|
||||
/* Code regions */
|
||||
const Color code_region_color = Color(EDITOR_GET("text_editor/theme/highlighting/folded_code_region_color").operator Color(), 1.0);
|
||||
add_color_region(ColorRegion::TYPE_CODE_REGION, "#region", "", code_region_color, true);
|
||||
add_color_region(ColorRegion::TYPE_CODE_REGION, "#endregion", "", code_region_color, true);
|
||||
|
||||
/* Strings */
|
||||
string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
|
||||
add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color);
|
||||
add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color);
|
||||
add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color);
|
||||
add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "'''", "'''", string_color);
|
||||
add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color, false, true);
|
||||
add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color, false, true);
|
||||
add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color, false, true);
|
||||
add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "'''", "'''", string_color, false, true);
|
||||
|
||||
/* Members. */
|
||||
Ref<Script> scr = _get_edited_resource();
|
||||
if (scr.is_valid()) {
|
||||
StringName instance_base = scr->get_instance_base_type();
|
||||
if (instance_base != StringName()) {
|
||||
List<PropertyInfo> property_list;
|
||||
ClassDB::get_property_list(instance_base, &property_list);
|
||||
for (const PropertyInfo &E : property_list) {
|
||||
String prop_name = E.name;
|
||||
if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
|
||||
continue;
|
||||
}
|
||||
if (prop_name.contains_char('/')) {
|
||||
continue;
|
||||
}
|
||||
member_keywords[prop_name] = member_variable_color;
|
||||
}
|
||||
|
||||
List<MethodInfo> signal_list;
|
||||
ClassDB::get_signal_list(instance_base, &signal_list);
|
||||
for (const MethodInfo &E : signal_list) {
|
||||
member_keywords[E.name] = member_variable_color;
|
||||
}
|
||||
|
||||
// For callables.
|
||||
List<MethodInfo> method_list;
|
||||
ClassDB::get_method_list(instance_base, &method_list);
|
||||
for (const MethodInfo &E : method_list) {
|
||||
member_keywords[E.name] = member_variable_color;
|
||||
}
|
||||
|
||||
List<String> constant_list;
|
||||
ClassDB::get_integer_constant_list(instance_base, &constant_list);
|
||||
for (const String &E : constant_list) {
|
||||
member_keywords[E] = member_variable_color;
|
||||
}
|
||||
|
||||
List<StringName> builtin_enums;
|
||||
ClassDB::get_enum_list(instance_base, &builtin_enums);
|
||||
for (const StringName &E : builtin_enums) {
|
||||
member_keywords[E] = types_color;
|
||||
}
|
||||
}
|
||||
|
||||
List<PropertyInfo> scr_property_list;
|
||||
scr->get_script_property_list(&scr_property_list);
|
||||
for (const PropertyInfo &E : scr_property_list) {
|
||||
String prop_name = E.name;
|
||||
if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
|
||||
continue;
|
||||
}
|
||||
if (prop_name.contains_char('/')) {
|
||||
continue;
|
||||
}
|
||||
member_keywords[prop_name] = member_variable_color;
|
||||
}
|
||||
|
||||
List<MethodInfo> scr_signal_list;
|
||||
scr->get_script_signal_list(&scr_signal_list);
|
||||
for (const MethodInfo &E : scr_signal_list) {
|
||||
member_keywords[E.name] = member_variable_color;
|
||||
}
|
||||
|
||||
// For callables.
|
||||
List<MethodInfo> scr_method_list;
|
||||
scr->get_script_method_list(&scr_method_list);
|
||||
for (const MethodInfo &E : scr_method_list) {
|
||||
member_keywords[E.name] = member_variable_color;
|
||||
}
|
||||
|
||||
Ref<Script> scr_class = scr;
|
||||
while (scr_class.is_valid()) {
|
||||
HashMap<StringName, Variant> scr_constant_list;
|
||||
scr_class->get_constants(&scr_constant_list);
|
||||
for (const KeyValue<StringName, Variant> &E : scr_constant_list) {
|
||||
member_keywords[E.key.operator String()] = member_variable_color;
|
||||
}
|
||||
scr_class = scr_class->get_base_script();
|
||||
}
|
||||
}
|
||||
|
||||
function_definition_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/function_definition_color");
|
||||
global_function_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/global_function_color");
|
||||
node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");
|
||||
node_ref_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_reference_color");
|
||||
annotation_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/annotation_color");
|
||||
string_name_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/string_name_color");
|
||||
type_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
|
||||
comment_marker_colors[COMMENT_MARKER_CRITICAL] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/critical_color");
|
||||
comment_marker_colors[COMMENT_MARKER_WARNING] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/warning_color");
|
||||
comment_marker_colors[COMMENT_MARKER_NOTICE] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/notice_color");
|
||||
|
||||
comment_markers.clear();
|
||||
Vector<String> critical_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/critical_list").operator String().split(",", false);
|
||||
for (int i = 0; i < critical_list.size(); i++) {
|
||||
comment_markers[critical_list[i]] = COMMENT_MARKER_CRITICAL;
|
||||
}
|
||||
Vector<String> warning_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/warning_list").operator String().split(",", false);
|
||||
for (int i = 0; i < warning_list.size(); i++) {
|
||||
comment_markers[warning_list[i]] = COMMENT_MARKER_WARNING;
|
||||
}
|
||||
Vector<String> notice_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/notice_list").operator String().split(",", false);
|
||||
for (int i = 0; i < notice_list.size(); i++) {
|
||||
comment_markers[notice_list[i]] = COMMENT_MARKER_NOTICE;
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptSyntaxHighlighter::add_color_region(ColorRegion::Type p_type, const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only, bool p_r_prefix) {
|
||||
ERR_FAIL_COND_MSG(p_start_key.is_empty(), "Color region start key cannot be empty.");
|
||||
ERR_FAIL_COND_MSG(!is_symbol(p_start_key[0]), "Color region start key must start with a symbol.");
|
||||
|
||||
if (!p_end_key.is_empty()) {
|
||||
ERR_FAIL_COND_MSG(!is_symbol(p_end_key[0]), "Color region end key must start with a symbol.");
|
||||
}
|
||||
|
||||
int at = 0;
|
||||
for (const ColorRegion ®ion : color_regions) {
|
||||
ERR_FAIL_COND_MSG(region.start_key == p_start_key && region.r_prefix == p_r_prefix, "Color region with start key '" + p_start_key + "' already exists.");
|
||||
if (p_start_key.length() < region.start_key.length()) {
|
||||
at++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ColorRegion color_region;
|
||||
color_region.type = p_type;
|
||||
color_region.color = p_color;
|
||||
color_region.start_key = p_start_key;
|
||||
color_region.end_key = p_end_key;
|
||||
color_region.line_only = p_line_only;
|
||||
color_region.r_prefix = p_r_prefix;
|
||||
color_region.is_string = p_type == ColorRegion::TYPE_STRING || p_type == ColorRegion::TYPE_MULTILINE_STRING;
|
||||
color_region.is_comment = p_type == ColorRegion::TYPE_COMMENT || p_type == ColorRegion::TYPE_CODE_REGION;
|
||||
color_regions.insert(at, color_region);
|
||||
clear_highlighting_cache();
|
||||
}
|
||||
|
||||
Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {
|
||||
Ref<GDScriptSyntaxHighlighter> syntax_highlighter;
|
||||
syntax_highlighter.instantiate();
|
||||
return syntax_highlighter;
|
||||
}
|
117
modules/gdscript/editor/gdscript_highlighter.h
Normal file
117
modules/gdscript/editor/gdscript_highlighter.h
Normal file
@@ -0,0 +1,117 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_highlighter.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "editor/script/script_editor_plugin.h"
|
||||
|
||||
class GDScriptSyntaxHighlighter : public EditorSyntaxHighlighter {
|
||||
GDCLASS(GDScriptSyntaxHighlighter, EditorSyntaxHighlighter)
|
||||
|
||||
private:
|
||||
struct ColorRegion {
|
||||
enum Type {
|
||||
TYPE_NONE,
|
||||
TYPE_STRING, // `"` and `'`, optional prefix `&`, `^`, or `r`.
|
||||
TYPE_MULTILINE_STRING, // `"""` and `'''`, optional prefix `r`.
|
||||
TYPE_COMMENT, // `#` and `##`.
|
||||
TYPE_CODE_REGION, // `#region` and `#endregion`.
|
||||
};
|
||||
|
||||
Type type = TYPE_NONE;
|
||||
Color color;
|
||||
String start_key;
|
||||
String end_key;
|
||||
bool line_only = false;
|
||||
bool r_prefix = false;
|
||||
bool is_string = false; // `TYPE_STRING` or `TYPE_MULTILINE_STRING`.
|
||||
bool is_comment = false; // `TYPE_COMMENT` or `TYPE_CODE_REGION`.
|
||||
};
|
||||
Vector<ColorRegion> color_regions;
|
||||
HashMap<int, int> color_region_cache;
|
||||
|
||||
HashMap<StringName, Color> class_names;
|
||||
HashMap<StringName, Color> reserved_keywords;
|
||||
HashMap<StringName, Color> member_keywords;
|
||||
HashSet<StringName> global_functions;
|
||||
|
||||
enum Type {
|
||||
NONE,
|
||||
REGION,
|
||||
NODE_PATH,
|
||||
NODE_REF,
|
||||
ANNOTATION,
|
||||
STRING_NAME,
|
||||
SYMBOL,
|
||||
NUMBER,
|
||||
FUNCTION,
|
||||
SIGNAL,
|
||||
KEYWORD,
|
||||
MEMBER,
|
||||
IDENTIFIER,
|
||||
TYPE,
|
||||
};
|
||||
|
||||
// Colors.
|
||||
Color font_color;
|
||||
Color symbol_color;
|
||||
Color function_color;
|
||||
Color global_function_color;
|
||||
Color function_definition_color;
|
||||
Color built_in_type_color;
|
||||
Color number_color;
|
||||
Color member_variable_color;
|
||||
Color string_color;
|
||||
Color node_path_color;
|
||||
Color node_ref_color;
|
||||
Color annotation_color;
|
||||
Color string_name_color;
|
||||
Color type_color;
|
||||
|
||||
enum CommentMarkerLevel {
|
||||
COMMENT_MARKER_CRITICAL,
|
||||
COMMENT_MARKER_WARNING,
|
||||
COMMENT_MARKER_NOTICE,
|
||||
COMMENT_MARKER_MAX,
|
||||
};
|
||||
Color comment_marker_colors[COMMENT_MARKER_MAX];
|
||||
HashMap<String, CommentMarkerLevel> comment_markers;
|
||||
|
||||
void add_color_region(ColorRegion::Type p_type, const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only = false, bool p_r_prefix = false);
|
||||
|
||||
public:
|
||||
virtual void _update_cache() override;
|
||||
virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override;
|
||||
|
||||
virtual String _get_name() const override;
|
||||
virtual PackedStringArray _get_supported_languages() const override;
|
||||
|
||||
virtual Ref<EditorSyntaxHighlighter> _create() const override;
|
||||
};
|
449
modules/gdscript/editor/gdscript_translation_parser_plugin.cpp
Normal file
449
modules/gdscript/editor/gdscript_translation_parser_plugin.cpp
Normal file
@@ -0,0 +1,449 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_translation_parser_plugin.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_translation_parser_plugin.h"
|
||||
|
||||
#include "../gdscript.h"
|
||||
#include "../gdscript_analyzer.h"
|
||||
|
||||
#include "core/io/resource_loader.h"
|
||||
|
||||
void GDScriptEditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const {
|
||||
GDScriptLanguage::get_singleton()->get_recognized_extensions(r_extensions);
|
||||
}
|
||||
|
||||
Error GDScriptEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<Vector<String>> *r_translations) {
|
||||
// Extract all translatable strings using the parsed tree from GDScriptParser.
|
||||
// The strategy is to find all ExpressionNode and AssignmentNode from the tree and extract strings if relevant, i.e
|
||||
// Search strings in ExpressionNode -> CallNode -> tr(), set_text(), set_placeholder() etc.
|
||||
// Search strings in AssignmentNode -> text = "__", tooltip_text = "__" etc.
|
||||
|
||||
Error err;
|
||||
Ref<Resource> loaded_res = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
|
||||
ERR_FAIL_COND_V_MSG(err, err, "Failed to load " + p_path);
|
||||
|
||||
translations = r_translations;
|
||||
|
||||
Ref<GDScript> gdscript = loaded_res;
|
||||
String source_code = gdscript->get_source_code();
|
||||
|
||||
GDScriptParser parser;
|
||||
err = parser.parse(source_code, p_path, false);
|
||||
ERR_FAIL_COND_V_MSG(err, err, "Failed to parse GDScript with GDScriptParser.");
|
||||
|
||||
GDScriptAnalyzer analyzer(&parser);
|
||||
err = analyzer.analyze();
|
||||
ERR_FAIL_COND_V_MSG(err, err, "Failed to analyze GDScript with GDScriptAnalyzer.");
|
||||
|
||||
comment_data = &parser.comment_data;
|
||||
|
||||
// Traverse through the parsed tree from GDScriptParser.
|
||||
GDScriptParser::ClassNode *c = parser.get_tree();
|
||||
_traverse_class(c);
|
||||
|
||||
comment_data = nullptr;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
bool GDScriptEditorTranslationParserPlugin::_is_constant_string(const GDScriptParser::ExpressionNode *p_expression) {
|
||||
ERR_FAIL_NULL_V(p_expression, false);
|
||||
return p_expression->is_constant && p_expression->reduced_value.is_string();
|
||||
}
|
||||
|
||||
String GDScriptEditorTranslationParserPlugin::_parse_comment(int p_line, bool &r_skip) const {
|
||||
// Parse inline comment.
|
||||
if (comment_data->has(p_line)) {
|
||||
const String stripped_comment = comment_data->get(p_line).comment.trim_prefix("#").strip_edges();
|
||||
|
||||
if (stripped_comment.begins_with("TRANSLATORS:")) {
|
||||
return stripped_comment.trim_prefix("TRANSLATORS:").strip_edges(true, false);
|
||||
}
|
||||
if (stripped_comment == "NO_TRANSLATE" || stripped_comment.begins_with("NO_TRANSLATE:")) {
|
||||
r_skip = true;
|
||||
return String();
|
||||
}
|
||||
}
|
||||
|
||||
// Parse multiline comment.
|
||||
String multiline_comment;
|
||||
for (int line = p_line - 1; comment_data->has(line) && comment_data->get(line).new_line; line--) {
|
||||
const String stripped_comment = comment_data->get(line).comment.trim_prefix("#").strip_edges();
|
||||
|
||||
if (stripped_comment.is_empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (multiline_comment.is_empty()) {
|
||||
multiline_comment = stripped_comment;
|
||||
} else {
|
||||
multiline_comment = stripped_comment + "\n" + multiline_comment;
|
||||
}
|
||||
|
||||
if (stripped_comment.begins_with("TRANSLATORS:")) {
|
||||
return multiline_comment.trim_prefix("TRANSLATORS:").strip_edges(true, false);
|
||||
}
|
||||
if (stripped_comment == "NO_TRANSLATE" || stripped_comment.begins_with("NO_TRANSLATE:")) {
|
||||
r_skip = true;
|
||||
return String();
|
||||
}
|
||||
}
|
||||
|
||||
return String();
|
||||
}
|
||||
|
||||
void GDScriptEditorTranslationParserPlugin::_add_id(const String &p_id, int p_line) {
|
||||
bool skip = false;
|
||||
const String comment = _parse_comment(p_line, skip);
|
||||
if (skip) {
|
||||
return;
|
||||
}
|
||||
|
||||
translations->push_back({ p_id, String(), String(), comment });
|
||||
}
|
||||
|
||||
void GDScriptEditorTranslationParserPlugin::_add_id_ctx_plural(const Vector<String> &p_id_ctx_plural, int p_line) {
|
||||
bool skip = false;
|
||||
const String comment = _parse_comment(p_line, skip);
|
||||
if (skip) {
|
||||
return;
|
||||
}
|
||||
|
||||
translations->push_back({ p_id_ctx_plural[0], p_id_ctx_plural[1], p_id_ctx_plural[2], comment });
|
||||
}
|
||||
|
||||
void GDScriptEditorTranslationParserPlugin::_traverse_class(const GDScriptParser::ClassNode *p_class) {
|
||||
for (int i = 0; i < p_class->members.size(); i++) {
|
||||
const GDScriptParser::ClassNode::Member &m = p_class->members[i];
|
||||
// Other member types can't contain translatable strings.
|
||||
switch (m.type) {
|
||||
case GDScriptParser::ClassNode::Member::CLASS:
|
||||
_traverse_class(m.m_class);
|
||||
break;
|
||||
case GDScriptParser::ClassNode::Member::FUNCTION:
|
||||
_traverse_function(m.function);
|
||||
break;
|
||||
case GDScriptParser::ClassNode::Member::VARIABLE:
|
||||
_assess_expression(m.variable->initializer);
|
||||
if (m.variable->property == GDScriptParser::VariableNode::PROP_INLINE) {
|
||||
_traverse_function(m.variable->setter);
|
||||
_traverse_function(m.variable->getter);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptEditorTranslationParserPlugin::_traverse_function(const GDScriptParser::FunctionNode *p_func) {
|
||||
if (!p_func) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < p_func->parameters.size(); i++) {
|
||||
_assess_expression(p_func->parameters[i]->initializer);
|
||||
}
|
||||
_traverse_block(p_func->body);
|
||||
}
|
||||
|
||||
void GDScriptEditorTranslationParserPlugin::_traverse_block(const GDScriptParser::SuiteNode *p_suite) {
|
||||
if (!p_suite) {
|
||||
return;
|
||||
}
|
||||
|
||||
const Vector<GDScriptParser::Node *> &statements = p_suite->statements;
|
||||
for (int i = 0; i < statements.size(); i++) {
|
||||
const GDScriptParser::Node *statement = statements[i];
|
||||
|
||||
// BREAK, BREAKPOINT, CONSTANT, CONTINUE, and PASS are skipped because they can't contain translatable strings.
|
||||
switch (statement->type) {
|
||||
case GDScriptParser::Node::ASSERT: {
|
||||
const GDScriptParser::AssertNode *assert_node = static_cast<const GDScriptParser::AssertNode *>(statement);
|
||||
_assess_expression(assert_node->condition);
|
||||
_assess_expression(assert_node->message);
|
||||
} break;
|
||||
case GDScriptParser::Node::ASSIGNMENT: {
|
||||
_assess_assignment(static_cast<const GDScriptParser::AssignmentNode *>(statement));
|
||||
} break;
|
||||
case GDScriptParser::Node::FOR: {
|
||||
const GDScriptParser::ForNode *for_node = static_cast<const GDScriptParser::ForNode *>(statement);
|
||||
_assess_expression(for_node->list);
|
||||
_traverse_block(for_node->loop);
|
||||
} break;
|
||||
case GDScriptParser::Node::IF: {
|
||||
const GDScriptParser::IfNode *if_node = static_cast<const GDScriptParser::IfNode *>(statement);
|
||||
_assess_expression(if_node->condition);
|
||||
_traverse_block(if_node->true_block);
|
||||
_traverse_block(if_node->false_block);
|
||||
} break;
|
||||
case GDScriptParser::Node::MATCH: {
|
||||
const GDScriptParser::MatchNode *match_node = static_cast<const GDScriptParser::MatchNode *>(statement);
|
||||
_assess_expression(match_node->test);
|
||||
for (int j = 0; j < match_node->branches.size(); j++) {
|
||||
_traverse_block(match_node->branches[j]->guard_body);
|
||||
_traverse_block(match_node->branches[j]->block);
|
||||
}
|
||||
} break;
|
||||
case GDScriptParser::Node::RETURN: {
|
||||
_assess_expression(static_cast<const GDScriptParser::ReturnNode *>(statement)->return_value);
|
||||
} break;
|
||||
case GDScriptParser::Node::VARIABLE: {
|
||||
_assess_expression(static_cast<const GDScriptParser::VariableNode *>(statement)->initializer);
|
||||
} break;
|
||||
case GDScriptParser::Node::WHILE: {
|
||||
const GDScriptParser::WhileNode *while_node = static_cast<const GDScriptParser::WhileNode *>(statement);
|
||||
_assess_expression(while_node->condition);
|
||||
_traverse_block(while_node->loop);
|
||||
} break;
|
||||
default: {
|
||||
if (statement->is_expression()) {
|
||||
_assess_expression(static_cast<const GDScriptParser::ExpressionNode *>(statement));
|
||||
}
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptEditorTranslationParserPlugin::_assess_expression(const GDScriptParser::ExpressionNode *p_expression) {
|
||||
// Explore all ExpressionNodes to find CallNodes which contain translation strings, such as tr(), set_text() etc.
|
||||
// tr() can be embedded quite deep within multiple ExpressionNodes so need to dig down to search through all ExpressionNodes.
|
||||
if (!p_expression) {
|
||||
return;
|
||||
}
|
||||
|
||||
// GET_NODE, IDENTIFIER, LITERAL, PRELOAD, SELF, and TYPE are skipped because they can't contain translatable strings.
|
||||
switch (p_expression->type) {
|
||||
case GDScriptParser::Node::ARRAY: {
|
||||
const GDScriptParser::ArrayNode *array_node = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
|
||||
for (int i = 0; i < array_node->elements.size(); i++) {
|
||||
_assess_expression(array_node->elements[i]);
|
||||
}
|
||||
} break;
|
||||
case GDScriptParser::Node::ASSIGNMENT: {
|
||||
_assess_assignment(static_cast<const GDScriptParser::AssignmentNode *>(p_expression));
|
||||
} break;
|
||||
case GDScriptParser::Node::AWAIT: {
|
||||
_assess_expression(static_cast<const GDScriptParser::AwaitNode *>(p_expression)->to_await);
|
||||
} break;
|
||||
case GDScriptParser::Node::BINARY_OPERATOR: {
|
||||
const GDScriptParser::BinaryOpNode *binary_op_node = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
|
||||
_assess_expression(binary_op_node->left_operand);
|
||||
_assess_expression(binary_op_node->right_operand);
|
||||
} break;
|
||||
case GDScriptParser::Node::CALL: {
|
||||
_assess_call(static_cast<const GDScriptParser::CallNode *>(p_expression));
|
||||
} break;
|
||||
case GDScriptParser::Node::CAST: {
|
||||
_assess_expression(static_cast<const GDScriptParser::CastNode *>(p_expression)->operand);
|
||||
} break;
|
||||
case GDScriptParser::Node::DICTIONARY: {
|
||||
const GDScriptParser::DictionaryNode *dict_node = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
|
||||
for (int i = 0; i < dict_node->elements.size(); i++) {
|
||||
_assess_expression(dict_node->elements[i].key);
|
||||
_assess_expression(dict_node->elements[i].value);
|
||||
}
|
||||
} break;
|
||||
case GDScriptParser::Node::LAMBDA: {
|
||||
_traverse_function(static_cast<const GDScriptParser::LambdaNode *>(p_expression)->function);
|
||||
} break;
|
||||
case GDScriptParser::Node::SUBSCRIPT: {
|
||||
const GDScriptParser::SubscriptNode *subscript_node = static_cast<const GDScriptParser::SubscriptNode *>(p_expression);
|
||||
_assess_expression(subscript_node->base);
|
||||
if (!subscript_node->is_attribute) {
|
||||
_assess_expression(subscript_node->index);
|
||||
}
|
||||
} break;
|
||||
case GDScriptParser::Node::TERNARY_OPERATOR: {
|
||||
const GDScriptParser::TernaryOpNode *ternary_op_node = static_cast<const GDScriptParser::TernaryOpNode *>(p_expression);
|
||||
_assess_expression(ternary_op_node->condition);
|
||||
_assess_expression(ternary_op_node->true_expr);
|
||||
_assess_expression(ternary_op_node->false_expr);
|
||||
} break;
|
||||
case GDScriptParser::Node::TYPE_TEST: {
|
||||
_assess_expression(static_cast<const GDScriptParser::TypeTestNode *>(p_expression)->operand);
|
||||
} break;
|
||||
case GDScriptParser::Node::UNARY_OPERATOR: {
|
||||
_assess_expression(static_cast<const GDScriptParser::UnaryOpNode *>(p_expression)->operand);
|
||||
} break;
|
||||
default: {
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptEditorTranslationParserPlugin::_assess_assignment(const GDScriptParser::AssignmentNode *p_assignment) {
|
||||
_assess_expression(p_assignment->assignee);
|
||||
_assess_expression(p_assignment->assigned_value);
|
||||
|
||||
// Extract the translatable strings coming from assignments. For example, get_node("Label").text = "____"
|
||||
|
||||
StringName assignee_name;
|
||||
if (p_assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) {
|
||||
assignee_name = static_cast<const GDScriptParser::IdentifierNode *>(p_assignment->assignee)->name;
|
||||
} else if (p_assignment->assignee->type == GDScriptParser::Node::SUBSCRIPT) {
|
||||
const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_assignment->assignee);
|
||||
if (subscript->is_attribute && subscript->attribute) {
|
||||
assignee_name = subscript->attribute->name;
|
||||
} else if (subscript->index && _is_constant_string(subscript->index)) {
|
||||
assignee_name = subscript->index->reduced_value;
|
||||
}
|
||||
}
|
||||
|
||||
if (assignee_name != StringName() && assignment_patterns.has(assignee_name) && _is_constant_string(p_assignment->assigned_value)) {
|
||||
// If the assignment is towards one of the extract patterns (text, tooltip_text etc.), and the value is a constant string, we collect the string.
|
||||
_add_id(p_assignment->assigned_value->reduced_value, p_assignment->assigned_value->start_line);
|
||||
} else if (assignee_name == fd_filters) {
|
||||
// Extract from `get_node("FileDialog").filters = <filter array>`.
|
||||
_extract_fd_filter_array(p_assignment->assigned_value);
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptEditorTranslationParserPlugin::_assess_call(const GDScriptParser::CallNode *p_call) {
|
||||
_assess_expression(p_call->callee);
|
||||
for (int i = 0; i < p_call->arguments.size(); i++) {
|
||||
_assess_expression(p_call->arguments[i]);
|
||||
}
|
||||
|
||||
// Extract the translatable strings coming from function calls. For example:
|
||||
// tr("___"), get_node("Label").set_text("____"), get_node("LineEdit").set_placeholder("____").
|
||||
|
||||
StringName function_name = p_call->function_name;
|
||||
|
||||
// Variables for extracting tr() and tr_n().
|
||||
Vector<String> id_ctx_plural;
|
||||
id_ctx_plural.resize(3);
|
||||
bool extract_id_ctx_plural = true;
|
||||
|
||||
if (function_name == tr_func || function_name == atr_func) {
|
||||
// Extract from `tr(id, ctx)` or `atr(id, ctx)`.
|
||||
for (int i = 0; i < p_call->arguments.size(); i++) {
|
||||
if (_is_constant_string(p_call->arguments[i])) {
|
||||
id_ctx_plural.write[i] = p_call->arguments[i]->reduced_value;
|
||||
} else {
|
||||
// Avoid adding something like tr("Flying dragon", var_context_level_1). We want to extract both id and context together.
|
||||
extract_id_ctx_plural = false;
|
||||
}
|
||||
}
|
||||
if (extract_id_ctx_plural) {
|
||||
_add_id_ctx_plural(id_ctx_plural, p_call->start_line);
|
||||
}
|
||||
} else if (function_name == trn_func || function_name == atrn_func) {
|
||||
// Extract from `tr_n(id, plural, n, ctx)` or `atr_n(id, plural, n, ctx)`.
|
||||
Vector<int> indices;
|
||||
indices.push_back(0);
|
||||
indices.push_back(3);
|
||||
indices.push_back(1);
|
||||
for (int i = 0; i < indices.size(); i++) {
|
||||
if (indices[i] >= p_call->arguments.size()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (_is_constant_string(p_call->arguments[indices[i]])) {
|
||||
id_ctx_plural.write[i] = p_call->arguments[indices[i]]->reduced_value;
|
||||
} else {
|
||||
extract_id_ctx_plural = false;
|
||||
}
|
||||
}
|
||||
if (extract_id_ctx_plural) {
|
||||
_add_id_ctx_plural(id_ctx_plural, p_call->start_line);
|
||||
}
|
||||
} else if (first_arg_patterns.has(function_name)) {
|
||||
if (!p_call->arguments.is_empty() && _is_constant_string(p_call->arguments[0])) {
|
||||
_add_id(p_call->arguments[0]->reduced_value, p_call->arguments[0]->start_line);
|
||||
}
|
||||
} else if (second_arg_patterns.has(function_name)) {
|
||||
if (p_call->arguments.size() > 1 && _is_constant_string(p_call->arguments[1])) {
|
||||
_add_id(p_call->arguments[1]->reduced_value, p_call->arguments[1]->start_line);
|
||||
}
|
||||
} else if (function_name == fd_add_filter) {
|
||||
// Extract the 'JPE Images' in this example - get_node("FileDialog").add_filter("*.jpg; JPE Images").
|
||||
if (!p_call->arguments.is_empty()) {
|
||||
_extract_fd_filter_string(p_call->arguments[0], p_call->arguments[0]->start_line);
|
||||
}
|
||||
} else if (function_name == fd_set_filter) {
|
||||
// Extract from `get_node("FileDialog").set_filters(<filter array>)`.
|
||||
if (!p_call->arguments.is_empty()) {
|
||||
_extract_fd_filter_array(p_call->arguments[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptEditorTranslationParserPlugin::_extract_fd_filter_string(const GDScriptParser::ExpressionNode *p_expression, int p_line) {
|
||||
// Extract the name in "extension ; name".
|
||||
if (_is_constant_string(p_expression)) {
|
||||
PackedStringArray arr = p_expression->reduced_value.operator String().split(";", true);
|
||||
ERR_FAIL_COND_MSG(arr.size() != 2, "Argument for setting FileDialog has bad format.");
|
||||
_add_id(arr[1].strip_edges(), p_line);
|
||||
}
|
||||
}
|
||||
|
||||
void GDScriptEditorTranslationParserPlugin::_extract_fd_filter_array(const GDScriptParser::ExpressionNode *p_expression) {
|
||||
const GDScriptParser::ArrayNode *array_node = nullptr;
|
||||
|
||||
if (p_expression->type == GDScriptParser::Node::ARRAY) {
|
||||
// Extract from `["*.png ; PNG Images","*.gd ; GDScript Files"]` (implicit cast to `PackedStringArray`).
|
||||
array_node = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
|
||||
} else if (p_expression->type == GDScriptParser::Node::CALL) {
|
||||
// Extract from `PackedStringArray(["*.png ; PNG Images","*.gd ; GDScript Files"])`.
|
||||
const GDScriptParser::CallNode *call_node = static_cast<const GDScriptParser::CallNode *>(p_expression);
|
||||
if (call_node->get_callee_type() == GDScriptParser::Node::IDENTIFIER && call_node->function_name == SNAME("PackedStringArray") && !call_node->arguments.is_empty() && call_node->arguments[0]->type == GDScriptParser::Node::ARRAY) {
|
||||
array_node = static_cast<const GDScriptParser::ArrayNode *>(call_node->arguments[0]);
|
||||
}
|
||||
}
|
||||
|
||||
if (array_node) {
|
||||
for (int i = 0; i < array_node->elements.size(); i++) {
|
||||
_extract_fd_filter_string(array_node->elements[i], array_node->elements[i]->start_line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GDScriptEditorTranslationParserPlugin::GDScriptEditorTranslationParserPlugin() {
|
||||
assignment_patterns.insert("text");
|
||||
assignment_patterns.insert("placeholder_text");
|
||||
assignment_patterns.insert("tooltip_text");
|
||||
|
||||
first_arg_patterns.insert("set_text");
|
||||
first_arg_patterns.insert("set_tooltip_text");
|
||||
first_arg_patterns.insert("set_placeholder");
|
||||
first_arg_patterns.insert("add_tab");
|
||||
first_arg_patterns.insert("add_check_item");
|
||||
first_arg_patterns.insert("add_item");
|
||||
first_arg_patterns.insert("add_multistate_item");
|
||||
first_arg_patterns.insert("add_radio_check_item");
|
||||
first_arg_patterns.insert("add_separator");
|
||||
first_arg_patterns.insert("add_submenu_item");
|
||||
|
||||
second_arg_patterns.insert("set_tab_title");
|
||||
second_arg_patterns.insert("add_icon_check_item");
|
||||
second_arg_patterns.insert("add_icon_item");
|
||||
second_arg_patterns.insert("add_icon_radio_check_item");
|
||||
second_arg_patterns.insert("set_item_text");
|
||||
}
|
83
modules/gdscript/editor/gdscript_translation_parser_plugin.h
Normal file
83
modules/gdscript/editor/gdscript_translation_parser_plugin.h
Normal file
@@ -0,0 +1,83 @@
|
||||
/**************************************************************************/
|
||||
/* gdscript_translation_parser_plugin.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../gdscript_parser.h"
|
||||
#include "../gdscript_tokenizer.h"
|
||||
|
||||
#include "core/templates/hash_map.h"
|
||||
#include "core/templates/hash_set.h"
|
||||
#include "editor/translations/editor_translation_parser.h"
|
||||
|
||||
class GDScriptEditorTranslationParserPlugin : public EditorTranslationParserPlugin {
|
||||
GDCLASS(GDScriptEditorTranslationParserPlugin, EditorTranslationParserPlugin);
|
||||
|
||||
const HashMap<int, GDScriptTokenizer::CommentData> *comment_data = nullptr;
|
||||
|
||||
Vector<Vector<String>> *translations = nullptr;
|
||||
|
||||
// List of patterns used for extracting translation strings.
|
||||
StringName tr_func = "tr";
|
||||
StringName trn_func = "tr_n";
|
||||
StringName atr_func = "atr";
|
||||
StringName atrn_func = "atr_n";
|
||||
HashSet<StringName> assignment_patterns;
|
||||
HashSet<StringName> first_arg_patterns;
|
||||
HashSet<StringName> second_arg_patterns;
|
||||
// FileDialog patterns.
|
||||
StringName fd_add_filter = "add_filter";
|
||||
StringName fd_set_filter = "set_filters";
|
||||
StringName fd_filters = "filters";
|
||||
|
||||
static bool _is_constant_string(const GDScriptParser::ExpressionNode *p_expression);
|
||||
|
||||
String _parse_comment(int p_line, bool &r_skip) const;
|
||||
|
||||
void _add_id(const String &p_id, int p_line);
|
||||
void _add_id_ctx_plural(const Vector<String> &p_id_ctx_plural, int p_line);
|
||||
|
||||
void _traverse_class(const GDScriptParser::ClassNode *p_class);
|
||||
void _traverse_function(const GDScriptParser::FunctionNode *p_func);
|
||||
void _traverse_block(const GDScriptParser::SuiteNode *p_suite);
|
||||
|
||||
void _assess_expression(const GDScriptParser::ExpressionNode *p_expression);
|
||||
void _assess_assignment(const GDScriptParser::AssignmentNode *p_assignment);
|
||||
void _assess_call(const GDScriptParser::CallNode *p_call);
|
||||
|
||||
void _extract_fd_filter_string(const GDScriptParser::ExpressionNode *p_expression, int p_line);
|
||||
void _extract_fd_filter_array(const GDScriptParser::ExpressionNode *p_expression);
|
||||
|
||||
public:
|
||||
virtual Error parse_file(const String &p_path, Vector<Vector<String>> *r_translations) override;
|
||||
virtual void get_recognized_extensions(List<String> *r_extensions) const override;
|
||||
|
||||
GDScriptEditorTranslationParserPlugin();
|
||||
};
|
@@ -0,0 +1,27 @@
|
||||
# meta-description: Classic movement for gravity games (platformer, ...)
|
||||
|
||||
extends _BASE_
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
var direction := Input.get_axis("ui_left", "ui_right")
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
@@ -0,0 +1,30 @@
|
||||
# meta-description: Classic movement for gravity games (FPS, TPS, ...)
|
||||
|
||||
extends _BASE_
|
||||
|
||||
|
||||
const SPEED = 5.0
|
||||
const JUMP_VELOCITY = 4.5
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
|
||||
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
||||
if direction:
|
||||
velocity.x = direction.x * SPEED
|
||||
velocity.z = direction.z * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
velocity.z = move_toward(velocity.z, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
@@ -0,0 +1,24 @@
|
||||
# meta-description: Basic plugin template
|
||||
|
||||
@tool
|
||||
extends _BASE_
|
||||
|
||||
|
||||
func _enable_plugin() -> void:
|
||||
# Add autoloads here.
|
||||
pass
|
||||
|
||||
|
||||
func _disable_plugin() -> void:
|
||||
# Remove autoloads here.
|
||||
pass
|
||||
|
||||
|
||||
func _enter_tree() -> void:
|
||||
# Initialization of the plugin goes here.
|
||||
pass
|
||||
|
||||
|
||||
func _exit_tree() -> void:
|
||||
# Clean-up of the plugin goes here.
|
||||
pass
|
@@ -0,0 +1,10 @@
|
||||
# meta-description: Basic import script template
|
||||
|
||||
@tool
|
||||
extends _BASE_
|
||||
|
||||
|
||||
# Called by the editor when a scene has this script set as the import script in the import tab.
|
||||
func _post_import(scene: Node) -> Object:
|
||||
# Modify the contents of the scene upon import.
|
||||
return scene # Return the modified root node when you're done.
|
@@ -0,0 +1,8 @@
|
||||
# meta-description: Basic import script template (no comments)
|
||||
|
||||
@tool
|
||||
extends _BASE_
|
||||
|
||||
|
||||
func _post_import(scene: Node) -> Object:
|
||||
return scene
|
@@ -0,0 +1,9 @@
|
||||
# meta-description: Basic editor script template
|
||||
|
||||
@tool
|
||||
extends _BASE_
|
||||
|
||||
|
||||
# Called when the script is executed (using File -> Run in Script Editor).
|
||||
func _run() -> void:
|
||||
pass
|
13
modules/gdscript/editor/script_templates/Node/default.gd
Normal file
13
modules/gdscript/editor/script_templates/Node/default.gd
Normal file
@@ -0,0 +1,13 @@
|
||||
# meta-description: Base template for Node with default Godot cycle methods
|
||||
|
||||
extends _BASE_
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
3
modules/gdscript/editor/script_templates/Object/empty.gd
Normal file
3
modules/gdscript/editor/script_templates/Object/empty.gd
Normal file
@@ -0,0 +1,3 @@
|
||||
# meta-description: Empty template suitable for all Objects
|
||||
|
||||
extends _BASE_
|
@@ -0,0 +1,18 @@
|
||||
# meta-description: Base template for rich text effects
|
||||
|
||||
@tool
|
||||
# Having a class name is handy for picking the effect in the Inspector.
|
||||
class_name RichText_CLASS_
|
||||
extends _BASE_
|
||||
|
||||
|
||||
# To use this effect:
|
||||
# - Enable BBCode on a RichTextLabel.
|
||||
# - Register this effect on the label.
|
||||
# - Use [_CLASS_SNAKE_CASE_ param=2.0]hello[/_CLASS_SNAKE_CASE_] in text.
|
||||
var bbcode := "_CLASS_SNAKE_CASE_"
|
||||
|
||||
|
||||
func _process_custom_fx(char_fx: CharFXTransform) -> bool:
|
||||
var param: float = char_fx.env.get("param", 1.0)
|
||||
return true
|
8
modules/gdscript/editor/script_templates/SCsub
Normal file
8
modules/gdscript/editor/script_templates/SCsub
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env python
|
||||
from misc.utility.scons_hints import *
|
||||
|
||||
Import("env")
|
||||
|
||||
import editor.template_builders as build_template_gd
|
||||
|
||||
env.CommandNoCache("templates.gen.h", Glob("*/*.gd"), env.Run(build_template_gd.make_templates))
|
@@ -0,0 +1,51 @@
|
||||
# meta-description: Visual shader's node plugin template
|
||||
|
||||
@tool
|
||||
# Having a class name is required for a custom node.
|
||||
class_name VisualShaderNode_CLASS_
|
||||
extends _BASE_
|
||||
|
||||
|
||||
func _get_name() -> String:
|
||||
return "_CLASS_"
|
||||
|
||||
|
||||
func _get_category() -> String:
|
||||
return ""
|
||||
|
||||
|
||||
func _get_description() -> String:
|
||||
return ""
|
||||
|
||||
|
||||
func _get_return_icon_type() -> PortType:
|
||||
return PORT_TYPE_SCALAR
|
||||
|
||||
|
||||
func _get_input_port_count() -> int:
|
||||
return 0
|
||||
|
||||
|
||||
func _get_input_port_name(port: int) -> String:
|
||||
return ""
|
||||
|
||||
|
||||
func _get_input_port_type(port: int) -> PortType:
|
||||
return PORT_TYPE_SCALAR
|
||||
|
||||
|
||||
func _get_output_port_count() -> int:
|
||||
return 1
|
||||
|
||||
|
||||
func _get_output_port_name(port: int) -> String:
|
||||
return "result"
|
||||
|
||||
|
||||
func _get_output_port_type(port: int) -> PortType:
|
||||
return PORT_TYPE_SCALAR
|
||||
|
||||
|
||||
func _get_code(input_vars: Array[String], output_vars: Array[String],
|
||||
mode: Shader.Mode, type: VisualShader.Type) -> String:
|
||||
return output_vars[0] + " = 0.0;"
|
Reference in New Issue
Block a user