summaryrefslogtreecommitdiff
path: root/modules/gdscript/editor
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gdscript/editor')
-rw-r--r--modules/gdscript/editor/gdscript_highlighter.cpp379
-rw-r--r--modules/gdscript/editor/gdscript_highlighter.h29
-rw-r--r--modules/gdscript/editor/gdscript_translation_parser_plugin.cpp155
-rw-r--r--modules/gdscript/editor/gdscript_translation_parser_plugin.h56
4 files changed, 520 insertions, 99 deletions
diff --git a/modules/gdscript/editor/gdscript_highlighter.cpp b/modules/gdscript/editor/gdscript_highlighter.cpp
index 0aca4dbc5e..ae1f2893f1 100644
--- a/modules/gdscript/editor/gdscript_highlighter.cpp
+++ b/modules/gdscript/editor/gdscript_highlighter.cpp
@@ -29,29 +29,14 @@
/*************************************************************************/
#include "gdscript_highlighter.h"
+#include "../gdscript.h"
#include "../gdscript_tokenizer.h"
#include "editor/editor_settings.h"
-#include "scene/gui/text_edit.h"
-
-inline bool _is_symbol(CharType c) {
-
- return is_symbol(c);
-}
-
-static bool _is_text_char(CharType c) {
-
- return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
-}
static bool _is_char(CharType c) {
-
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
}
-static bool _is_number(CharType c) {
- return (c >= '0' && c <= '9');
-}
-
static bool _is_hex_symbol(CharType c) {
return ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
}
@@ -60,8 +45,8 @@ static bool _is_bin_symbol(CharType c) {
return (c == '0' || c == '1');
}
-Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) {
- Map<int, TextEdit::HighlighterInfo> color_map;
+Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) {
+ Dictionary color_map;
Type next_type = NONE;
Type current_type = NONE;
@@ -85,36 +70,140 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_
Color keyword_color;
Color color;
- int in_region = text_editor->_is_line_in_region(p_line);
- int deregion = 0;
+ color_region_cache[p_line] = -1;
+ int in_region = -1;
+ if (p_line != 0) {
+ if (!color_region_cache.has(p_line - 1)) {
+ get_line_syntax_highlighting(p_line - 1);
+ }
+ in_region = color_region_cache[p_line - 1];
+ }
- const Map<int, TextEdit::Text::ColorRegionInfo> cri_map = text_editor->_get_line_color_region_info(p_line);
- const String &str = text_editor->get_line(p_line);
+ const String &str = text_edit->get_line(p_line);
+ const int line_length = str.length();
Color prev_color;
+
+ if (in_region != -1 && str.length() == 0) {
+ color_region_cache[p_line] = in_region;
+ }
for (int j = 0; j < str.length(); j++) {
- TextEdit::HighlighterInfo highlighter_info;
+ Dictionary highlighter_info;
- if (deregion > 0) {
- deregion--;
- if (deregion == 0) {
- in_region = -1;
+ color = font_color;
+ bool is_char = !is_symbol(str[j]);
+ bool is_a_symbol = is_symbol(str[j]);
+ bool is_number = (str[j] >= '0' && str[j] <= '9');
+
+ /* color regions */
+ if (is_a_symbol || in_region != -1) {
+ int from = j;
+ for (; from < line_length; from++) {
+ if (str[from] == '\\') {
+ from++;
+ continue;
+ }
+ break;
}
- }
- if (deregion != 0) {
- if (color != prev_color) {
- prev_color = color;
- highlighter_info.color = color;
- color_map[j] = highlighter_info;
- }
- continue;
- }
+ if (from != line_length) {
+ /* check if we are in entering a region */
+ if (in_region == -1) {
+ 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;
+ }
+
+ /* search the line */
+ bool match = true;
+ const CharType *start_key = color_regions[c].start_key.c_str();
+ for (int k = 0; k < start_key_length; k++) {
+ if (start_key[k] != str[from + k]) {
+ match = false;
+ break;
+ }
+ }
+ 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) {
+ 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;
+ }
- color = font_color;
+ if (j == line_length) {
+ continue;
+ }
+ }
+
+ /* if we are in one find the end key */
+ if (in_region != -1) {
+ /* check there is enough room */
+ int chars_left = line_length - from;
+ int end_key_length = color_regions[in_region].end_key.length();
+ if (chars_left < end_key_length) {
+ continue;
+ }
+
+ /* search the line */
+ int region_end_index = -1;
+ const CharType *end_key = color_regions[in_region].start_key.c_str();
+ for (; from < line_length; from++) {
+ if (!is_a_symbol) {
+ continue;
+ }
+
+ if (str[from] == '\\') {
+ from++;
+ continue;
+ }
+
+ for (int k = 0; k < end_key_length; k++) {
+ if (end_key[k] == str[from + k]) {
+ region_end_index = from;
+ break;
+ }
+ }
+
+ if (region_end_index != -1) {
+ break;
+ }
+ }
+
+ prev_color = color_regions[in_region].color;
+ highlighter_info["color"] = color_regions[in_region].color;
+ color_map[j] = highlighter_info;
+
+ previous_type = REGION;
+ previous_text = "";
+ previous_column = j;
+ j = from;
+ if (region_end_index == -1) {
+ color_region_cache[p_line] = in_region;
+ }
- bool is_char = _is_text_char(str[j]);
- bool is_symbol = _is_symbol(str[j]);
- bool is_number = _is_number(str[j]);
+ in_region = -1;
+ prev_is_char = false;
+ prev_is_number = false;
+ continue;
+ }
+ }
+ }
// allow ABCDEF in hex notation
if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) {
@@ -136,7 +225,7 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_
// check for dot or underscore or 'x' for hex notation in floating point number or 'e' for scientific notation
if ((str[j] == '.' || str[j] == 'x' || str[j] == 'b' || str[j] == '_' || str[j] == 'e') && !in_word && prev_is_number && !is_number) {
is_number = true;
- is_symbol = false;
+ is_a_symbol = false;
is_char = false;
if (str[j] == 'x' && str[j - 1] == '0') {
@@ -154,43 +243,26 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_
is_number = false;
}
- if (is_symbol && str[j] != '.' && in_word) {
+ if (is_a_symbol && str[j] != '.' && in_word) {
in_word = false;
}
- if (is_symbol && cri_map.has(j)) {
- const TextEdit::Text::ColorRegionInfo &cri = cri_map[j];
-
- if (in_region == -1) {
- if (!cri.end) {
- in_region = cri.region;
- }
- } else {
- TextEdit::ColorRegion cr = text_editor->_get_color_region(cri.region);
- if (in_region == cri.region && !cr.line_only) { //ignore otherwise
- if (cri.end || cr.eq) {
- deregion = cr.eq ? cr.begin_key.length() : cr.end_key.length();
- }
- }
- }
- }
-
if (!is_char) {
in_keyword = false;
}
- if (in_region == -1 && !in_keyword && is_char && !prev_is_char) {
-
+ if (!in_keyword && is_char && !prev_is_char) {
int to = j;
- while (to < str.length() && _is_text_char(str[to]))
+ while (to < str.length() && !is_symbol(str[to])) {
to++;
+ }
String word = str.substr(j, to - j);
Color col = Color();
- if (text_editor->has_keyword_color(word)) {
- col = text_editor->get_keyword_color(word);
- } else if (text_editor->has_member_color(word)) {
- col = text_editor->get_member_color(word);
+ if (keywords.has(word)) {
+ col = keywords[word];
+ } else if (member_keywords.has(word)) {
+ col = member_keywords[word];
for (int k = j - 1; k >= 0; k--) {
if (str[k] == '.') {
col = Color(); //member indexing not allowed
@@ -208,9 +280,8 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_
}
if (!in_function_name && in_word && !in_keyword) {
-
int k = j;
- while (k < str.length() && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
+ while (k < str.length() && !is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
k++;
}
@@ -221,14 +292,14 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_
if (str[k] == '(') {
in_function_name = true;
- } else if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::TK_PR_VAR)) {
+ } else if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::VAR)) {
in_variable_declaration = true;
}
}
if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) {
int k = j;
- while (k > 0 && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
+ while (k > 0 && !is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
k--;
}
@@ -237,8 +308,7 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_
}
}
- if (is_symbol) {
-
+ if (is_a_symbol) {
if (in_function_name) {
in_function_args = true;
}
@@ -275,14 +345,11 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_
if (!in_node_path && in_region == -1 && str[j] == '$') {
in_node_path = true;
- } else if (in_region != -1 || (is_symbol && str[j] != '/')) {
+ } else if (in_region != -1 || (is_a_symbol && str[j] != '/')) {
in_node_path = false;
}
- if (in_region >= 0) {
- next_type = REGION;
- color = text_editor->_get_color_region(in_region).color;
- } else if (in_node_path) {
+ if (in_node_path) {
next_type = NODE_PATH;
color = node_path_color;
} else if (in_keyword) {
@@ -294,12 +361,12 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_
} else if (in_function_name) {
next_type = FUNCTION;
- if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::TK_PR_FUNCTION)) {
+ if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
color = function_definition_color;
} else {
color = function_color;
}
- } else if (is_symbol) {
+ } else if (is_a_symbol) {
next_type = SYMBOL;
color = symbol_color;
} else if (is_number) {
@@ -340,32 +407,133 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_
if (color != prev_color) {
prev_color = color;
- highlighter_info.color = color;
+ highlighter_info["color"] = color;
color_map[j] = highlighter_info;
}
}
return color_map;
}
-String GDScriptSyntaxHighlighter::get_name() const {
+String GDScriptSyntaxHighlighter::_get_name() const {
return "GDScript";
}
-List<String> GDScriptSyntaxHighlighter::get_supported_languages() {
- List<String> languages;
+Array GDScriptSyntaxHighlighter::_get_supported_languages() const {
+ Array languages;
languages.push_back("GDScript");
return languages;
}
void GDScriptSyntaxHighlighter::_update_cache() {
- font_color = text_editor->get_theme_color("font_color");
- symbol_color = text_editor->get_theme_color("symbol_color");
- function_color = text_editor->get_theme_color("function_color");
- number_color = text_editor->get_theme_color("number_color");
- member_color = text_editor->get_theme_color("member_variable_color");
+ keywords.clear();
+ member_keywords.clear();
+ color_regions.clear();
+ color_region_cache.clear();
+
+ font_color = text_edit->get_theme_color("font_color");
+ symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
+ function_color = EDITOR_GET("text_editor/highlighting/function_color");
+ number_color = EDITOR_GET("text_editor/highlighting/number_color");
+ member_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
+
+ /* Engine types. */
+ const Color types_color = EDITOR_GET("text_editor/highlighting/engine_type_color");
+ List<StringName> types;
+ ClassDB::get_class_list(&types);
+ for (List<StringName>::Element *E = types.front(); E; E = E->next()) {
+ String n = E->get();
+ if (n.begins_with("_")) {
+ n = n.substr(1, n.length());
+ }
+ keywords[n] = types_color;
+ }
+
+ /* User types. */
+ const Color usertype_color = EDITOR_GET("text_editor/highlighting/user_type_color");
+ List<StringName> global_classes;
+ ScriptServer::get_global_class_list(&global_classes);
+ for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) {
+ keywords[String(E->get())] = usertype_color;
+ }
+
+ /* Autoloads. */
+ Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
+ for (Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E; E = E->next()) {
+ const ProjectSettings::AutoloadInfo &info = E->value();
+ if (info.is_singleton) {
+ keywords[info.name] = usertype_color;
+ }
+ }
+
+ const GDScriptLanguage *gdscript = GDScriptLanguage::get_singleton();
+
+ /* Core types. */
+ const Color basetype_color = EDITOR_GET("text_editor/highlighting/base_type_color");
+ List<String> core_types;
+ gdscript->get_core_type_words(&core_types);
+ for (List<String>::Element *E = core_types.front(); E; E = E->next()) {
+ keywords[E->get()] = basetype_color;
+ }
+
+ /* Reserved words. */
+ const Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
+ List<String> keyword_list;
+ gdscript->get_reserved_words(&keyword_list);
+ for (List<String>::Element *E = keyword_list.front(); E; E = E->next()) {
+ keywords[E->get()] = keyword_color;
+ }
+
+ /* Comments */
+ const Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
+ List<String> comments;
+ gdscript->get_comment_delimiters(&comments);
+ for (List<String>::Element *E = comments.front(); E; E = E->next()) {
+ String comment = E->get();
+ String beg = comment.get_slice(" ", 0);
+ String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
+ add_color_region(beg, end, comment_color, end == "");
+ }
+
+ /* Strings */
+ const Color string_color = EDITOR_GET("text_editor/highlighting/string_color");
+ List<String> strings;
+ gdscript->get_string_delimiters(&strings);
+ for (List<String>::Element *E = strings.front(); E; E = E->next()) {
+ String string = E->get();
+ String beg = string.get_slice(" ", 0);
+ String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
+ add_color_region(beg, end, string_color, end == "");
+ }
+
+ const Ref<Script> script = _get_edited_resource();
+ if (script.is_valid()) {
+ /* Member types. */
+ const Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
+ StringName instance_base = script->get_instance_base_type();
+ if (instance_base != StringName()) {
+ List<PropertyInfo> plist;
+ ClassDB::get_property_list(instance_base, &plist);
+ for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
+ String name = E->get().name;
+ if (E->get().usage & PROPERTY_USAGE_CATEGORY || E->get().usage & PROPERTY_USAGE_GROUP || E->get().usage & PROPERTY_USAGE_SUBGROUP) {
+ continue;
+ }
+ if (name.find("/") != -1) {
+ continue;
+ }
+ member_keywords[name] = member_variable_color;
+ }
+
+ List<String> clist;
+ ClassDB::get_integer_constant_list(instance_base, &clist);
+ for (List<String>::Element *E = clist.front(); E; E = E->next()) {
+ member_keywords[E->get()] = member_variable_color;
+ }
+ }
+ }
- const String text_editor_color_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
- const bool default_theme = text_editor_color_theme == "Default";
+ const String text_edit_color_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
+ const bool default_theme = text_edit_color_theme == "Default";
if (default_theme || EditorSettings::get_singleton()->is_dark_theme()) {
function_definition_color = Color(0.4, 0.9, 1.0);
@@ -377,7 +545,7 @@ void GDScriptSyntaxHighlighter::_update_cache() {
EDITOR_DEF("text_editor/highlighting/gdscript/function_definition_color", function_definition_color);
EDITOR_DEF("text_editor/highlighting/gdscript/node_path_color", node_path_color);
- if (text_editor_color_theme == "Adaptive" || default_theme) {
+ if (text_edit_color_theme == "Adaptive" || default_theme) {
EditorSettings::get_singleton()->set_initial_value(
"text_editor/highlighting/gdscript/function_definition_color",
function_definition_color,
@@ -393,6 +561,31 @@ void GDScriptSyntaxHighlighter::_update_cache() {
type_color = EDITOR_GET("text_editor/highlighting/base_type_color");
}
-SyntaxHighlighter *GDScriptSyntaxHighlighter::create() {
- return memnew(GDScriptSyntaxHighlighter);
+void GDScriptSyntaxHighlighter::add_color_region(const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only) {
+ for (int i = 0; i < p_start_key.length(); i++) {
+ ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "color regions must start with a symbol");
+ }
+
+ if (p_end_key.length() > 0) {
+ for (int i = 0; i < p_end_key.length(); i++) {
+ ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "color regions must end with a symbol");
+ }
+ }
+
+ for (int i = 0; i < color_regions.size(); i++) {
+ ERR_FAIL_COND_MSG(color_regions[i].start_key == p_start_key, "color region with start key '" + p_start_key + "' already exists.");
+ }
+
+ ColorRegion color_region;
+ 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_regions.push_back(color_region);
+}
+
+Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {
+ Ref<GDScriptSyntaxHighlighter> syntax_highlighter;
+ syntax_highlighter.instance();
+ return syntax_highlighter;
}
diff --git a/modules/gdscript/editor/gdscript_highlighter.h b/modules/gdscript/editor/gdscript_highlighter.h
index e652fb1471..49357f3d2e 100644
--- a/modules/gdscript/editor/gdscript_highlighter.h
+++ b/modules/gdscript/editor/gdscript_highlighter.h
@@ -31,10 +31,25 @@
#ifndef GDSCRIPT_HIGHLIGHTER_H
#define GDSCRIPT_HIGHLIGHTER_H
+#include "editor/plugins/script_editor_plugin.h"
#include "scene/gui/text_edit.h"
-class GDScriptSyntaxHighlighter : public SyntaxHighlighter {
+class GDScriptSyntaxHighlighter : public EditorSyntaxHighlighter {
+ GDCLASS(GDScriptSyntaxHighlighter, EditorSyntaxHighlighter)
+
private:
+ struct ColorRegion {
+ Color color;
+ String start_key;
+ String end_key;
+ bool line_only;
+ };
+ Vector<ColorRegion> color_regions;
+ Map<int, int> color_region_cache;
+
+ Dictionary keywords;
+ Dictionary member_keywords;
+
enum Type {
NONE,
REGION,
@@ -59,14 +74,16 @@ private:
Color node_path_color;
Color type_color;
+ void add_color_region(const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only = false);
+
public:
- static SyntaxHighlighter *create();
+ virtual void _update_cache() override;
+ virtual Dictionary _get_line_syntax_highlighting(int p_line) override;
- virtual void _update_cache();
- virtual Map<int, TextEdit::HighlighterInfo> _get_line_syntax_highlighting(int p_line);
+ virtual String _get_name() const override;
+ virtual Array _get_supported_languages() const override;
- virtual String get_name() const;
- virtual List<String> get_supported_languages();
+ virtual Ref<EditorSyntaxHighlighter> _create() const override;
};
#endif // GDSCRIPT_HIGHLIGHTER_H
diff --git a/modules/gdscript/editor/gdscript_translation_parser_plugin.cpp b/modules/gdscript/editor/gdscript_translation_parser_plugin.cpp
new file mode 100644
index 0000000000..6d454e43f2
--- /dev/null
+++ b/modules/gdscript/editor/gdscript_translation_parser_plugin.cpp
@@ -0,0 +1,155 @@
+/*************************************************************************/
+/* gdscript_translation_parser_plugin.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* 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 "core/io/resource_loader.h"
+#include "modules/gdscript/gdscript.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<String> *r_extracted_strings) {
+ // Parse and match all GDScript function API that involves translation string.
+ // E.g get_node("Label").text = "something", var test = tr("something"), "something" will be matched and collected.
+
+ Error err;
+ RES loaded_res = ResourceLoader::load(p_path, "", false, &err);
+ if (err) {
+ ERR_PRINT("Failed to load " + p_path);
+ return err;
+ }
+
+ Ref<GDScript> gdscript = loaded_res;
+ String source_code = gdscript->get_source_code();
+ Vector<String> parsed_strings;
+
+ // Search translation strings with RegEx.
+ regex.clear();
+ regex.compile(String("|").join(patterns));
+ Array results = regex.search_all(source_code);
+ _get_captured_strings(results, &parsed_strings);
+
+ // Special handling for FileDialog.
+ Vector<String> temp;
+ _parse_file_dialog(source_code, &temp);
+ parsed_strings.append_array(temp);
+
+ // Filter out / and +
+ String filter = "(?:\\\\\\n|\"[\\s\\\\]*\\+\\s*\")";
+ regex.clear();
+ regex.compile(filter);
+ for (int i = 0; i < parsed_strings.size(); i++) {
+ parsed_strings.set(i, regex.sub(parsed_strings[i], "", true));
+ }
+
+ r_extracted_strings->append_array(parsed_strings);
+
+ return OK;
+}
+
+void GDScriptEditorTranslationParserPlugin::_parse_file_dialog(const String &p_source_code, Vector<String> *r_output) {
+ // FileDialog API has the form .filters = PackedStringArray(["*.png ; PNG Images","*.gd ; GDScript Files"]).
+ // First filter: Get "*.png ; PNG Images", "*.gd ; GDScript Files" from PackedStringArray.
+ regex.clear();
+ regex.compile(String("|").join(file_dialog_patterns));
+ Array results = regex.search_all(p_source_code);
+
+ Vector<String> temp;
+ _get_captured_strings(results, &temp);
+ String captured_strings = String(",").join(temp);
+
+ // Second filter: Get the texts after semicolon from "*.png ; PNG Images","*.gd ; GDScript Files".
+ String second_filter = "\"[^;]+;" + text + "\"";
+ regex.clear();
+ regex.compile(second_filter);
+ results = regex.search_all(captured_strings);
+ _get_captured_strings(results, r_output);
+ for (int i = 0; i < r_output->size(); i++) {
+ r_output->set(i, r_output->get(i).strip_edges());
+ }
+}
+
+void GDScriptEditorTranslationParserPlugin::_get_captured_strings(const Array &p_results, Vector<String> *r_output) {
+ Ref<RegExMatch> result;
+ for (int i = 0; i < p_results.size(); i++) {
+ result = p_results[i];
+ for (int j = 0; j < result->get_group_count(); j++) {
+ String s = result->get_string(j + 1);
+ // Prevent reading text with only spaces.
+ if (!s.strip_edges().empty()) {
+ r_output->push_back(s);
+ }
+ }
+ }
+}
+
+GDScriptEditorTranslationParserPlugin::GDScriptEditorTranslationParserPlugin() {
+ // Regex search pattern templates.
+ // The extra complication in the regex pattern is to ensure that the matching works when users write over multiple lines, use tabs etc.
+ const String dot = "\\.[\\s\\\\]*";
+ const String str_assign_template = "[\\s\\\\]*=[\\s\\\\]*\"" + text + "\"";
+ const String first_arg_template = "[\\s\\\\]*\\([\\s\\\\]*\"" + text + "\"[\\s\\S]*?\\)";
+ const String second_arg_template = "[\\s\\\\]*\\([\\s\\S]+?,[\\s\\\\]*\"" + text + "\"[\\s\\S]*?\\)";
+
+ // Common patterns.
+ patterns.push_back("tr" + first_arg_template);
+ patterns.push_back(dot + "text" + str_assign_template);
+ patterns.push_back(dot + "placeholder_text" + str_assign_template);
+ patterns.push_back(dot + "hint_tooltip" + str_assign_template);
+ patterns.push_back(dot + "set_text" + first_arg_template);
+ patterns.push_back(dot + "set_tooltip" + first_arg_template);
+ patterns.push_back(dot + "set_placeholder" + first_arg_template);
+
+ // Tabs and TabContainer API.
+ patterns.push_back(dot + "set_tab_title" + second_arg_template);
+ patterns.push_back(dot + "add_tab" + first_arg_template);
+
+ // PopupMenu API.
+ patterns.push_back(dot + "add_check_item" + first_arg_template);
+ patterns.push_back(dot + "add_icon_check_item" + second_arg_template);
+ patterns.push_back(dot + "add_icon_item" + second_arg_template);
+ patterns.push_back(dot + "add_icon_radio_check_item" + second_arg_template);
+ patterns.push_back(dot + "add_item" + first_arg_template);
+ patterns.push_back(dot + "add_multistate_item" + first_arg_template);
+ patterns.push_back(dot + "add_radio_check_item" + first_arg_template);
+ patterns.push_back(dot + "add_separator" + first_arg_template);
+ patterns.push_back(dot + "add_submenu_item" + first_arg_template);
+ patterns.push_back(dot + "set_item_text" + second_arg_template);
+ //patterns.push_back(dot + "set_item_tooltip" + second_arg_template); //no tr() behind this function. might be bug.
+
+ // FileDialog API - special case.
+ const String fd_text = "((?:[\\s\\\\]*\"(?:[^\"\\\\]|\\\\[\\s\\S])*(?:\"[\\s\\\\]*\\+[\\s\\\\]*\"(?:[^\"\\\\]|\\\\[\\s\\S])*)*\"[\\s\\\\]*,?)*)";
+ const String packed_string_array = "[\\s\\\\]*PackedStringArray[\\s\\\\]*\\([\\s\\\\]*\\[" + fd_text + "\\][\\s\\\\]*\\)";
+ file_dialog_patterns.push_back(dot + "add_filter[\\s\\\\]*\\(" + fd_text + "[\\s\\\\]*\\)");
+ file_dialog_patterns.push_back(dot + "filters[\\s\\\\]*=" + packed_string_array);
+ file_dialog_patterns.push_back(dot + "set_filters[\\s\\\\]*\\(" + packed_string_array + "[\\s\\\\]*\\)");
+}
diff --git a/modules/gdscript/editor/gdscript_translation_parser_plugin.h b/modules/gdscript/editor/gdscript_translation_parser_plugin.h
new file mode 100644
index 0000000000..9fa4b69f01
--- /dev/null
+++ b/modules/gdscript/editor/gdscript_translation_parser_plugin.h
@@ -0,0 +1,56 @@
+/*************************************************************************/
+/* gdscript_translation_parser_plugin.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#ifndef GDSCRIPT_TRANSLATION_PARSER_PLUGIN_H
+#define GDSCRIPT_TRANSLATION_PARSER_PLUGIN_H
+
+#include "editor/editor_translation_parser.h"
+#include "modules/regex/regex.h"
+
+class GDScriptEditorTranslationParserPlugin : public EditorTranslationParserPlugin {
+ GDCLASS(GDScriptEditorTranslationParserPlugin, EditorTranslationParserPlugin);
+
+ // Regex and search patterns that are used to match translation strings.
+ const String text = "((?:[^\"\\\\]|\\\\[\\s\\S])*(?:\"[\\s\\\\]*\\+[\\s\\\\]*\"(?:[^\"\\\\]|\\\\[\\s\\S])*)*)";
+ RegEx regex;
+ Vector<String> patterns;
+ Vector<String> file_dialog_patterns;
+
+ void _parse_file_dialog(const String &p_source_code, Vector<String> *r_output);
+ void _get_captured_strings(const Array &p_results, Vector<String> *r_output);
+
+public:
+ virtual Error parse_file(const String &p_path, Vector<String> *r_extracted_strings) override;
+ virtual void get_recognized_extensions(List<String> *r_extensions) const override;
+
+ GDScriptEditorTranslationParserPlugin();
+};
+
+#endif // GDSCRIPT_TRANSLATION_PARSER_PLUGIN_H