[Editor] Fix return of EditorTranslationParserPlugin._parse_file

Merged `_get_comments` into `_parse_file` and changed to using a
returned `Array[PackedStringArray]` instead.
This commit is contained in:
A Thousand Ships
2024-11-15 20:19:00 +01:00
committed by AThousandShips
parent e567f49cbb
commit fec3d9e68c
9 changed files with 82 additions and 111 deletions

View File

@ -39,7 +39,7 @@ void GDScriptEditorTranslationParserPlugin::get_recognized_extensions(List<Strin
GDScriptLanguage::get_singleton()->get_recognized_extensions(r_extensions);
}
Error GDScriptEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) {
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.
@ -49,11 +49,7 @@ Error GDScriptEditorTranslationParserPlugin::parse_file(const String &p_path, Ve
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);
ids = r_ids;
ids_ctx_plural = r_ids_ctx_plural;
ids_comment.clear();
ids_ctx_plural_comment.clear();
translations = r_translations;
Ref<GDScript> gdscript = loaded_res;
String source_code = gdscript->get_source_code();
@ -77,11 +73,6 @@ Error GDScriptEditorTranslationParserPlugin::parse_file(const String &p_path, Ve
return OK;
}
void GDScriptEditorTranslationParserPlugin::get_comments(Vector<String> *r_ids_comment, Vector<String> *r_ids_ctx_plural_comment) {
r_ids_comment->append_array(ids_comment);
r_ids_ctx_plural_comment->append_array(ids_ctx_plural_comment);
}
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();
@ -135,8 +126,7 @@ void GDScriptEditorTranslationParserPlugin::_add_id(const String &p_id, int p_li
return;
}
ids->push_back(p_id);
ids_comment.push_back(comment);
translations->push_back({ p_id, String(), String(), comment });
}
void GDScriptEditorTranslationParserPlugin::_add_id_ctx_plural(const Vector<String> &p_id_ctx_plural, int p_line) {
@ -146,8 +136,7 @@ void GDScriptEditorTranslationParserPlugin::_add_id_ctx_plural(const Vector<Stri
return;
}
ids_ctx_plural->push_back(p_id_ctx_plural);
ids_ctx_plural_comment.push_back(comment);
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) {

View File

@ -43,11 +43,7 @@ class GDScriptEditorTranslationParserPlugin : public EditorTranslationParserPlug
const HashMap<int, GDScriptTokenizer::CommentData> *comment_data = nullptr;
Vector<String> *ids = nullptr;
Vector<Vector<String>> *ids_ctx_plural = nullptr;
Vector<String> ids_comment;
Vector<String> ids_ctx_plural_comment;
Vector<Vector<String>> *translations = nullptr;
// List of patterns used for extracting translation strings.
StringName tr_func = "tr";
@ -81,8 +77,7 @@ class GDScriptEditorTranslationParserPlugin : public EditorTranslationParserPlug
void _extract_fd_filter_array(const GDScriptParser::ExpressionNode *p_expression);
public:
virtual Error parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) override;
virtual void get_comments(Vector<String> *r_ids_comment, Vector<String> *r_ids_ctx_plural_comment) override;
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();