2020-06-23 11:48:59 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* 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"
|
|
|
|
|
2023-06-13 14:56:21 +00:00
|
|
|
#include "../gdscript.h"
|
2023-07-29 16:24:48 +00:00
|
|
|
#include "../gdscript_analyzer.h"
|
2023-06-13 14:56:21 +00:00
|
|
|
|
2020-06-23 11:48:59 +00:00
|
|
|
#include "core/io/resource_loader.h"
|
|
|
|
|
|
|
|
void GDScriptEditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const {
|
|
|
|
GDScriptLanguage::get_singleton()->get_recognized_extensions(r_extensions);
|
|
|
|
}
|
|
|
|
|
2020-07-22 22:07:35 +00:00
|
|
|
Error GDScriptEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) {
|
2023-10-22 19:46:26 +00:00
|
|
|
// Extract all translatable strings using the parsed tree from GDScriptParser.
|
2020-08-12 20:52:17 +00:00
|
|
|
// 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.
|
2022-08-25 10:42:17 +00:00
|
|
|
// Search strings in AssignmentNode -> text = "__", tooltip_text = "__" etc.
|
2020-06-23 11:48:59 +00:00
|
|
|
|
|
|
|
Error err;
|
2022-05-02 23:43:50 +00:00
|
|
|
Ref<Resource> loaded_res = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
|
2023-12-23 14:09:12 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(err, err, "Failed to load " + p_path);
|
2020-06-23 11:48:59 +00:00
|
|
|
|
2020-08-12 20:52:17 +00:00
|
|
|
ids = r_ids;
|
|
|
|
ids_ctx_plural = r_ids_ctx_plural;
|
2020-06-23 11:48:59 +00:00
|
|
|
Ref<GDScript> gdscript = loaded_res;
|
2020-07-03 18:24:54 +00:00
|
|
|
String source_code = gdscript->get_source_code();
|
2020-08-12 20:52:17 +00:00
|
|
|
|
|
|
|
GDScriptParser parser;
|
|
|
|
err = parser.parse(source_code, p_path, false);
|
2023-12-23 14:09:12 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(err, err, "Failed to parse GDScript with GDScriptParser.");
|
2023-07-29 16:24:48 +00:00
|
|
|
|
|
|
|
GDScriptAnalyzer analyzer(&parser);
|
|
|
|
err = analyzer.analyze();
|
2023-12-23 14:09:12 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(err, err, "Failed to analyze GDScript with GDScriptAnalyzer.");
|
2020-06-23 11:48:59 +00:00
|
|
|
|
2020-08-12 20:52:17 +00:00
|
|
|
// Traverse through the parsed tree from GDScriptParser.
|
|
|
|
GDScriptParser::ClassNode *c = parser.get_tree();
|
|
|
|
_traverse_class(c);
|
2020-07-03 18:24:54 +00:00
|
|
|
|
|
|
|
return OK;
|
2020-06-23 11:48:59 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 16:24:48 +00:00
|
|
|
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.get_type() == Variant::STRING || p_expression->reduced_value.get_type() == Variant::STRING_NAME);
|
|
|
|
}
|
|
|
|
|
2020-08-12 20:52:17 +00:00
|
|
|
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];
|
2023-12-12 21:19:04 +00:00
|
|
|
// Other member types can't contain translatable strings.
|
2020-08-12 20:52:17 +00:00
|
|
|
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:
|
2023-12-12 21:19:04 +00:00
|
|
|
_assess_expression(m.variable->initializer);
|
|
|
|
if (m.variable->property == GDScriptParser::VariableNode::PROP_INLINE) {
|
|
|
|
_traverse_function(m.variable->setter);
|
|
|
|
_traverse_function(m.variable->getter);
|
|
|
|
}
|
2020-08-12 20:52:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-06-23 11:48:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 20:52:17 +00:00
|
|
|
void GDScriptEditorTranslationParserPlugin::_traverse_function(const GDScriptParser::FunctionNode *p_func) {
|
2023-12-12 21:19:04 +00:00
|
|
|
if (!p_func) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-12 20:52:17 +00:00
|
|
|
|
2023-12-12 21:19:04 +00:00
|
|
|
for (int i = 0; i < p_func->parameters.size(); i++) {
|
|
|
|
_assess_expression(p_func->parameters[i]->initializer);
|
|
|
|
}
|
|
|
|
_traverse_block(p_func->body);
|
2020-08-12 20:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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++) {
|
2023-07-29 16:24:48 +00:00
|
|
|
const GDScriptParser::Node *statement = statements[i];
|
2020-08-12 20:52:17 +00:00
|
|
|
|
2023-12-12 21:19:04 +00:00
|
|
|
// BREAK, BREAKPOINT, CONSTANT, CONTINUE, and PASS are skipped because they can't contain translatable strings.
|
2020-08-12 20:52:17 +00:00
|
|
|
switch (statement->type) {
|
2023-12-12 21:19:04 +00:00
|
|
|
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;
|
2020-08-12 20:52:17 +00:00
|
|
|
case GDScriptParser::Node::IF: {
|
2023-07-29 16:24:48 +00:00
|
|
|
const GDScriptParser::IfNode *if_node = static_cast<const GDScriptParser::IfNode *>(statement);
|
2020-08-12 20:52:17 +00:00
|
|
|
_assess_expression(if_node->condition);
|
|
|
|
_traverse_block(if_node->true_block);
|
|
|
|
_traverse_block(if_node->false_block);
|
2023-12-12 21:19:04 +00:00
|
|
|
} break;
|
2020-08-12 20:52:17 +00:00
|
|
|
case GDScriptParser::Node::MATCH: {
|
2023-07-29 16:24:48 +00:00
|
|
|
const GDScriptParser::MatchNode *match_node = static_cast<const GDScriptParser::MatchNode *>(statement);
|
2020-08-12 20:52:17 +00:00
|
|
|
_assess_expression(match_node->test);
|
|
|
|
for (int j = 0; j < match_node->branches.size(); j++) {
|
2023-12-12 21:19:04 +00:00
|
|
|
_traverse_block(match_node->branches[j]->guard_body);
|
2020-08-12 20:52:17 +00:00
|
|
|
_traverse_block(match_node->branches[j]->block);
|
|
|
|
}
|
2023-12-12 21:19:04 +00:00
|
|
|
} break;
|
|
|
|
case GDScriptParser::Node::RETURN: {
|
2023-07-29 16:24:48 +00:00
|
|
|
_assess_expression(static_cast<const GDScriptParser::ReturnNode *>(statement)->return_value);
|
2023-12-12 21:19:04 +00:00
|
|
|
} 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: {
|
2020-08-12 20:52:17 +00:00
|
|
|
if (statement->is_expression()) {
|
2023-07-29 16:24:48 +00:00
|
|
|
_assess_expression(static_cast<const GDScriptParser::ExpressionNode *>(statement));
|
2020-08-12 20:52:17 +00:00
|
|
|
}
|
2023-12-12 21:19:04 +00:00
|
|
|
} break;
|
2020-06-23 11:48:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 16:24:48 +00:00
|
|
|
void GDScriptEditorTranslationParserPlugin::_assess_expression(const GDScriptParser::ExpressionNode *p_expression) {
|
2020-08-12 20:52:17 +00:00
|
|
|
// 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) {
|
2020-07-22 22:07:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-12 21:19:04 +00:00
|
|
|
// GET_NODE, IDENTIFIER, LITERAL, PRELOAD, SELF, and TYPE are skipped because they can't contain translatable strings.
|
2020-08-12 20:52:17 +00:00
|
|
|
switch (p_expression->type) {
|
|
|
|
case GDScriptParser::Node::ARRAY: {
|
2023-07-29 16:24:48 +00:00
|
|
|
const GDScriptParser::ArrayNode *array_node = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
|
2020-08-12 20:52:17 +00:00
|
|
|
for (int i = 0; i < array_node->elements.size(); i++) {
|
|
|
|
_assess_expression(array_node->elements[i]);
|
|
|
|
}
|
2023-12-12 21:19:04 +00:00
|
|
|
} break;
|
|
|
|
case GDScriptParser::Node::ASSIGNMENT: {
|
2023-07-29 16:24:48 +00:00
|
|
|
_assess_assignment(static_cast<const GDScriptParser::AssignmentNode *>(p_expression));
|
2023-12-12 21:19:04 +00:00
|
|
|
} break;
|
|
|
|
case GDScriptParser::Node::AWAIT: {
|
|
|
|
_assess_expression(static_cast<const GDScriptParser::AwaitNode *>(p_expression)->to_await);
|
|
|
|
} break;
|
2020-08-12 20:52:17 +00:00
|
|
|
case GDScriptParser::Node::BINARY_OPERATOR: {
|
2023-07-29 16:24:48 +00:00
|
|
|
const GDScriptParser::BinaryOpNode *binary_op_node = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
|
2020-08-12 20:52:17 +00:00
|
|
|
_assess_expression(binary_op_node->left_operand);
|
|
|
|
_assess_expression(binary_op_node->right_operand);
|
2023-12-12 21:19:04 +00:00
|
|
|
} break;
|
2020-08-12 20:52:17 +00:00
|
|
|
case GDScriptParser::Node::CALL: {
|
2023-12-23 14:09:12 +00:00
|
|
|
_assess_call(static_cast<const GDScriptParser::CallNode *>(p_expression));
|
2020-08-12 20:52:17 +00:00
|
|
|
} break;
|
2023-12-12 21:19:04 +00:00
|
|
|
case GDScriptParser::Node::CAST: {
|
|
|
|
_assess_expression(static_cast<const GDScriptParser::CastNode *>(p_expression)->operand);
|
|
|
|
} break;
|
2020-08-12 20:52:17 +00:00
|
|
|
case GDScriptParser::Node::DICTIONARY: {
|
2023-07-29 16:24:48 +00:00
|
|
|
const GDScriptParser::DictionaryNode *dict_node = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
|
2020-08-12 20:52:17 +00:00
|
|
|
for (int i = 0; i < dict_node->elements.size(); i++) {
|
|
|
|
_assess_expression(dict_node->elements[i].key);
|
|
|
|
_assess_expression(dict_node->elements[i].value);
|
|
|
|
}
|
2023-12-12 21:19:04 +00:00
|
|
|
} 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;
|
2020-08-12 20:52:17 +00:00
|
|
|
case GDScriptParser::Node::TERNARY_OPERATOR: {
|
2023-07-29 16:24:48 +00:00
|
|
|
const GDScriptParser::TernaryOpNode *ternary_op_node = static_cast<const GDScriptParser::TernaryOpNode *>(p_expression);
|
2020-08-12 20:52:17 +00:00
|
|
|
_assess_expression(ternary_op_node->condition);
|
|
|
|
_assess_expression(ternary_op_node->true_expr);
|
|
|
|
_assess_expression(ternary_op_node->false_expr);
|
2023-12-12 21:19:04 +00:00
|
|
|
} 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;
|
2020-08-12 20:52:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 16:24:48 +00:00
|
|
|
void GDScriptEditorTranslationParserPlugin::_assess_assignment(const GDScriptParser::AssignmentNode *p_assignment) {
|
2023-12-23 14:09:12 +00:00
|
|
|
_assess_expression(p_assignment->assignee);
|
|
|
|
_assess_expression(p_assignment->assigned_value);
|
|
|
|
|
2020-08-12 20:52:17 +00:00
|
|
|
// Extract the translatable strings coming from assignments. For example, get_node("Label").text = "____"
|
|
|
|
|
|
|
|
StringName assignee_name;
|
|
|
|
if (p_assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) {
|
2023-07-29 16:24:48 +00:00
|
|
|
assignee_name = static_cast<const GDScriptParser::IdentifierNode *>(p_assignment->assignee)->name;
|
2020-08-12 20:52:17 +00:00
|
|
|
} else if (p_assignment->assignee->type == GDScriptParser::Node::SUBSCRIPT) {
|
2023-09-14 14:41:05 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-08-12 20:52:17 +00:00
|
|
|
}
|
2020-07-22 22:07:35 +00:00
|
|
|
|
2023-09-14 14:41:05 +00:00
|
|
|
if (assignee_name != StringName() && assignment_patterns.has(assignee_name) && _is_constant_string(p_assignment->assigned_value)) {
|
2023-07-29 16:24:48 +00:00
|
|
|
// 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.
|
|
|
|
ids->push_back(p_assignment->assigned_value->reduced_value);
|
2023-12-23 14:09:12 +00:00
|
|
|
} else if (assignee_name == fd_filters) {
|
|
|
|
// Extract from `get_node("FileDialog").filters = <filter array>`.
|
|
|
|
_extract_fd_filter_array(p_assignment->assigned_value);
|
2020-08-12 20:52:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-23 14:09:12 +00:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2020-08-12 20:52:17 +00:00
|
|
|
// 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) {
|
|
|
|
// Extract from tr(id, ctx).
|
|
|
|
for (int i = 0; i < p_call->arguments.size(); i++) {
|
2023-07-29 16:24:48 +00:00
|
|
|
if (_is_constant_string(p_call->arguments[i])) {
|
|
|
|
id_ctx_plural.write[i] = p_call->arguments[i]->reduced_value;
|
2020-08-12 20:52:17 +00:00
|
|
|
} 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) {
|
|
|
|
ids_ctx_plural->push_back(id_ctx_plural);
|
|
|
|
}
|
|
|
|
} else if (function_name == trn_func) {
|
|
|
|
// Extract from tr_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;
|
|
|
|
}
|
|
|
|
|
2023-07-29 16:24:48 +00:00
|
|
|
if (_is_constant_string(p_call->arguments[indices[i]])) {
|
|
|
|
id_ctx_plural.write[i] = p_call->arguments[indices[i]]->reduced_value;
|
2020-08-12 20:52:17 +00:00
|
|
|
} else {
|
|
|
|
extract_id_ctx_plural = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (extract_id_ctx_plural) {
|
|
|
|
ids_ctx_plural->push_back(id_ctx_plural);
|
|
|
|
}
|
|
|
|
} else if (first_arg_patterns.has(function_name)) {
|
2023-12-23 14:09:12 +00:00
|
|
|
if (!p_call->arguments.is_empty() && _is_constant_string(p_call->arguments[0])) {
|
2023-07-29 16:24:48 +00:00
|
|
|
ids->push_back(p_call->arguments[0]->reduced_value);
|
2020-08-12 20:52:17 +00:00
|
|
|
}
|
|
|
|
} else if (second_arg_patterns.has(function_name)) {
|
2023-12-23 14:09:12 +00:00
|
|
|
if (p_call->arguments.size() > 1 && _is_constant_string(p_call->arguments[1])) {
|
2023-07-29 16:24:48 +00:00
|
|
|
ids->push_back(p_call->arguments[1]->reduced_value);
|
2020-08-12 20:52:17 +00:00
|
|
|
}
|
|
|
|
} else if (function_name == fd_add_filter) {
|
|
|
|
// Extract the 'JPE Images' in this example - get_node("FileDialog").add_filter("*.jpg; JPE Images").
|
2023-12-23 14:09:12 +00:00
|
|
|
if (!p_call->arguments.is_empty()) {
|
|
|
|
_extract_fd_filter_string(p_call->arguments[0]);
|
2020-07-22 22:07:35 +00:00
|
|
|
}
|
2023-12-23 14:09:12 +00:00
|
|
|
} 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]);
|
2023-05-27 02:38:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-22 22:07:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-23 14:09:12 +00:00
|
|
|
void GDScriptEditorTranslationParserPlugin::_extract_fd_filter_string(const GDScriptParser::ExpressionNode *p_expression) {
|
2020-08-12 20:52:17 +00:00
|
|
|
// Extract the name in "extension ; name".
|
2023-07-29 16:24:48 +00:00
|
|
|
if (_is_constant_string(p_expression)) {
|
2023-12-23 14:09:12 +00:00
|
|
|
PackedStringArray arr = p_expression->reduced_value.operator String().split(";", true);
|
|
|
|
ERR_FAIL_COND_MSG(arr.size() != 2, "Argument for setting FileDialog has bad format.");
|
2020-08-12 20:52:17 +00:00
|
|
|
ids->push_back(arr[1].strip_edges());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-23 14:09:12 +00:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 11:48:59 +00:00
|
|
|
GDScriptEditorTranslationParserPlugin::GDScriptEditorTranslationParserPlugin() {
|
2020-08-12 20:52:17 +00:00
|
|
|
assignment_patterns.insert("text");
|
|
|
|
assignment_patterns.insert("placeholder_text");
|
2022-08-25 10:42:17 +00:00
|
|
|
assignment_patterns.insert("tooltip_text");
|
2020-08-12 20:52:17 +00:00
|
|
|
|
|
|
|
first_arg_patterns.insert("set_text");
|
2022-08-25 10:42:17 +00:00
|
|
|
first_arg_patterns.insert("set_tooltip_text");
|
2020-08-12 20:52:17 +00:00
|
|
|
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");
|
2020-06-23 11:48:59 +00:00
|
|
|
}
|