summaryrefslogtreecommitdiff
path: root/editor/plugins
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2020-07-14 09:49:02 +0200
committerGitHub <noreply@github.com>2020-07-14 09:49:02 +0200
commitca5958d1806d96e2d54e8b3336a96f0a02381a86 (patch)
treec738df29c03b2f056d7d15aea9fe7b5a4ad46cd4 /editor/plugins
parent28e8347d6cb1e5e8945ba62b9678849046038bb6 (diff)
parentbc4cee44582d2a90d0792d6b213f00be1043000b (diff)
Merge pull request #38440 from Paulb23/syntax_highlighter_refactor
Syntax highlighter refactor
Diffstat (limited to 'editor/plugins')
-rw-r--r--editor/plugins/script_editor_plugin.cpp206
-rw-r--r--editor/plugins/script_editor_plugin.h61
-rw-r--r--editor/plugins/script_text_editor.cpp197
-rw-r--r--editor/plugins/script_text_editor.h18
-rw-r--r--editor/plugins/shader_editor_plugin.cpp30
-rw-r--r--editor/plugins/shader_editor_plugin.h1
-rw-r--r--editor/plugins/text_editor.cpp70
-rw-r--r--editor/plugins/text_editor.h16
-rw-r--r--editor/plugins/visual_shader_editor_plugin.cpp23
-rw-r--r--editor/plugins/visual_shader_editor_plugin.h1
10 files changed, 349 insertions, 274 deletions
diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp
index 8386d44e69..67268e0027 100644
--- a/editor/plugins/script_editor_plugin.cpp
+++ b/editor/plugins/script_editor_plugin.cpp
@@ -51,6 +51,165 @@
#include "servers/display_server.h"
#include "text_editor.h"
+/*** SYNTAX HIGHLIGHTER ****/
+
+String EditorSyntaxHighlighter::_get_name() const {
+ ScriptInstance *si = get_script_instance();
+ if (si && si->has_method("_get_name")) {
+ return si->call("_get_name");
+ }
+ return "Unnamed";
+}
+
+Array EditorSyntaxHighlighter::_get_supported_languages() const {
+ ScriptInstance *si = get_script_instance();
+ if (si && si->has_method("_get_supported_languages")) {
+ return si->call("_get_supported_languages");
+ }
+ return Array();
+}
+
+Ref<EditorSyntaxHighlighter> EditorSyntaxHighlighter::_create() const {
+ Ref<EditorSyntaxHighlighter> syntax_highlighter;
+ syntax_highlighter.instance();
+ if (get_script_instance()) {
+ syntax_highlighter->set_script(get_script_instance()->get_script());
+ }
+ return syntax_highlighter;
+}
+
+void EditorSyntaxHighlighter::_bind_methods() {
+ ClassDB::bind_method(D_METHOD("_get_edited_resource"), &EditorSyntaxHighlighter::_get_edited_resource);
+
+ BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_name"));
+ BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_get_supported_languages"));
+ BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_get_supported_extentions"));
+}
+
+////
+
+void EditorStandardSyntaxHighlighter::_update_cache() {
+ highlighter->set_text_edit(text_edit);
+ highlighter->clear_keyword_colors();
+ highlighter->clear_member_keyword_colors();
+ highlighter->clear_color_regions();
+
+ highlighter->set_symbol_color(EDITOR_GET("text_editor/highlighting/symbol_color"));
+ highlighter->set_function_color(EDITOR_GET("text_editor/highlighting/function_color"));
+ highlighter->set_number_color(EDITOR_GET("text_editor/highlighting/number_color"));
+ highlighter->set_member_variable_color(EDITOR_GET("text_editor/highlighting/member_variable_color"));
+
+ /* Engine types. */
+ const Color type_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());
+ }
+ highlighter->add_keyword_color(n, type_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()) {
+ highlighter->add_keyword_color(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) {
+ highlighter->add_keyword_color(info.name, usertype_color);
+ }
+ }
+
+ const Ref<Script> script = _get_edited_resource();
+ if (script.is_valid()) {
+ /* Core types. */
+ const Color basetype_color = EDITOR_GET("text_editor/highlighting/base_type_color");
+ List<String> core_types;
+ script->get_language()->get_core_type_words(&core_types);
+ for (List<String>::Element *E = core_types.front(); E; E = E->next()) {
+ highlighter->add_keyword_color(E->get(), basetype_color);
+ }
+
+ /* Reserved words. */
+ const Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
+ List<String> keywords;
+ script->get_language()->get_reserved_words(&keywords);
+ for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
+ highlighter->add_keyword_color(E->get(), keyword_color);
+ }
+
+ /* 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;
+ }
+ highlighter->add_member_keyword_color(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()) {
+ highlighter->add_member_keyword_color(E->get(), member_variable_color);
+ }
+ }
+
+ /* Comments */
+ const Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
+ List<String> comments;
+ script->get_language()->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();
+ highlighter->add_color_region(beg, end, comment_color, end == "");
+ }
+
+ /* Strings */
+ const Color string_color = EDITOR_GET("text_editor/highlighting/string_color");
+ List<String> strings;
+ script->get_language()->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();
+ highlighter->add_color_region(beg, end, string_color, end == "");
+ }
+ }
+}
+
+Ref<EditorSyntaxHighlighter> EditorStandardSyntaxHighlighter::_create() const {
+ Ref<EditorStandardSyntaxHighlighter> syntax_highlighter;
+ syntax_highlighter.instance();
+ return syntax_highlighter;
+}
+
+////
+
+Ref<EditorSyntaxHighlighter> EditorPlainTextSyntaxHighlighter::_create() const {
+ Ref<EditorPlainTextSyntaxHighlighter> syntax_highlighter;
+ syntax_highlighter.instance();
+ return syntax_highlighter;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
/*** SCRIPT EDITOR ****/
void ScriptEditorBase::_bind_methods() {
@@ -63,6 +222,8 @@ void ScriptEditorBase::_bind_methods() {
// TODO: This signal is no use for VisualScript.
ADD_SIGNAL(MethodInfo("search_in_files_requested", PropertyInfo(Variant::STRING, "text")));
ADD_SIGNAL(MethodInfo("replace_in_files_requested", PropertyInfo(Variant::STRING, "text")));
+
+ BIND_VMETHOD(MethodInfo("add_syntax_highlighter", PropertyInfo(Variant::OBJECT, "highlighter")));
}
static bool _is_built_in_script(Script *p_script) {
@@ -2019,13 +2180,16 @@ bool ScriptEditor::edit(const RES &p_resource, int p_line, int p_col, bool p_gra
if (p_resource->get_class_name() != StringName("VisualScript")) {
bool highlighter_set = false;
- for (int i = 0; i < syntax_highlighters_func_count; i++) {
- SyntaxHighlighter *highlighter = syntax_highlighters_funcs[i]();
+ for (int i = 0; i < syntax_highlighters.size(); i++) {
+ Ref<EditorSyntaxHighlighter> highlighter = syntax_highlighters[i]->_create();
+ if (highlighter.is_null()) {
+ continue;
+ }
se->add_syntax_highlighter(highlighter);
if (script != nullptr && !highlighter_set) {
- List<String> languages = highlighter->get_supported_languages();
- if (languages.find(script->get_language()->get_name())) {
+ Array languages = highlighter->_get_supported_languages();
+ if (languages.find(script->get_language()->get_name()) > -1) {
se->set_syntax_highlighter(highlighter);
highlighter_set = true;
}
@@ -2768,6 +2932,18 @@ Vector<Ref<Script>> ScriptEditor::get_open_scripts() const {
return out_scripts;
}
+Array ScriptEditor::_get_open_script_editors() const {
+ Array script_editors;
+ for (int i = 0; i < tab_container->get_child_count(); i++) {
+ ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
+ if (!se) {
+ continue;
+ }
+ script_editors.push_back(se);
+ }
+ return script_editors;
+}
+
void ScriptEditor::set_scene_root_script(Ref<Script> p_script) {
bool open_dominant = EditorSettings::get_singleton()->get("text_editor/files/open_dominant_script_on_scene_change");
@@ -2813,12 +2989,18 @@ void ScriptEditor::_open_script_request(const String &p_path) {
}
}
-int ScriptEditor::syntax_highlighters_func_count = 0;
-CreateSyntaxHighlighterFunc ScriptEditor::syntax_highlighters_funcs[ScriptEditor::SYNTAX_HIGHLIGHTER_FUNC_MAX];
+void ScriptEditor::register_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter) {
+ ERR_FAIL_COND(p_syntax_highlighter.is_null());
-void ScriptEditor::register_create_syntax_highlighter_function(CreateSyntaxHighlighterFunc p_func) {
- ERR_FAIL_COND(syntax_highlighters_func_count == SYNTAX_HIGHLIGHTER_FUNC_MAX);
- syntax_highlighters_funcs[syntax_highlighters_func_count++] = p_func;
+ if (syntax_highlighters.find(p_syntax_highlighter) == -1) {
+ syntax_highlighters.push_back(p_syntax_highlighter);
+ }
+}
+
+void ScriptEditor::unregister_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter) {
+ ERR_FAIL_COND(p_syntax_highlighter.is_null());
+
+ syntax_highlighters.erase(p_syntax_highlighter);
}
int ScriptEditor::script_editor_func_count = 0;
@@ -2927,6 +3109,12 @@ void ScriptEditor::_bind_methods() {
ClassDB::bind_method("_update_members_overview", &ScriptEditor::_update_members_overview);
ClassDB::bind_method("_update_recent_scripts", &ScriptEditor::_update_recent_scripts);
+ ClassDB::bind_method("get_current_editor", &ScriptEditor::_get_current_editor);
+ ClassDB::bind_method("get_open_script_editors", &ScriptEditor::_get_open_script_editors);
+
+ ClassDB::bind_method(D_METHOD("register_syntax_highlighter", "syntax_highlighter"), &ScriptEditor::register_syntax_highlighter);
+ ClassDB::bind_method(D_METHOD("unregister_syntax_highlighter", "syntax_highlighter"), &ScriptEditor::unregister_syntax_highlighter);
+
ClassDB::bind_method(D_METHOD("get_drag_data_fw", "point", "from"), &ScriptEditor::get_drag_data_fw);
ClassDB::bind_method(D_METHOD("can_drop_data_fw", "point", "data", "from"), &ScriptEditor::can_drop_data_fw);
ClassDB::bind_method(D_METHOD("drop_data_fw", "point", "data", "from"), &ScriptEditor::drop_data_fw);
diff --git a/editor/plugins/script_editor_plugin.h b/editor/plugins/script_editor_plugin.h
index d51c95ec4b..3891af4091 100644
--- a/editor/plugins/script_editor_plugin.h
+++ b/editor/plugins/script_editor_plugin.h
@@ -47,6 +47,53 @@
#include "scene/main/timer.h"
#include "scene/resources/text_file.h"
+class EditorSyntaxHighlighter : public SyntaxHighlighter {
+ GDCLASS(EditorSyntaxHighlighter, SyntaxHighlighter)
+
+private:
+ REF edited_resourse;
+
+protected:
+ static void _bind_methods();
+
+public:
+ virtual String _get_name() const;
+ virtual Array _get_supported_languages() const;
+
+ void _set_edited_resource(const RES &p_res) { edited_resourse = p_res; }
+ REF _get_edited_resource() { return edited_resourse; }
+
+ virtual Ref<EditorSyntaxHighlighter> _create() const;
+};
+
+class EditorStandardSyntaxHighlighter : public EditorSyntaxHighlighter {
+ GDCLASS(EditorStandardSyntaxHighlighter, EditorSyntaxHighlighter)
+
+private:
+ Ref<CodeHighlighter> highlighter;
+
+public:
+ virtual void _update_cache() override;
+ virtual Dictionary _get_line_syntax_highlighting(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }
+
+ virtual String _get_name() const override { return TTR("Standard"); }
+
+ virtual Ref<EditorSyntaxHighlighter> _create() const override;
+
+ EditorStandardSyntaxHighlighter() { highlighter.instance(); }
+};
+
+class EditorPlainTextSyntaxHighlighter : public EditorSyntaxHighlighter {
+ GDCLASS(EditorPlainTextSyntaxHighlighter, EditorSyntaxHighlighter)
+
+public:
+ virtual String _get_name() const override { return TTR("Plain Text"); }
+
+ virtual Ref<EditorSyntaxHighlighter> _create() const override;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
class ScriptEditorQuickOpen : public ConfirmationDialog {
GDCLASS(ScriptEditorQuickOpen, ConfirmationDialog);
@@ -80,8 +127,8 @@ protected:
static void _bind_methods();
public:
- virtual void add_syntax_highlighter(SyntaxHighlighter *p_highlighter) = 0;
- virtual void set_syntax_highlighter(SyntaxHighlighter *p_highlighter) = 0;
+ virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) = 0;
+ virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) = 0;
virtual void apply_code() = 0;
virtual RES get_edited_resource() const = 0;
@@ -120,7 +167,6 @@ public:
ScriptEditorBase() {}
};
-typedef SyntaxHighlighter *(*CreateSyntaxHighlighterFunc)();
typedef ScriptEditorBase *(*CreateScriptEditorFunc)(const RES &p_resource);
class EditorScriptCodeCompletionCache;
@@ -236,14 +282,12 @@ class ScriptEditor : public PanelContainer {
enum {
SCRIPT_EDITOR_FUNC_MAX = 32,
- SYNTAX_HIGHLIGHTER_FUNC_MAX = 32
};
static int script_editor_func_count;
static CreateScriptEditorFunc script_editor_funcs[SCRIPT_EDITOR_FUNC_MAX];
- static int syntax_highlighters_func_count;
- static CreateSyntaxHighlighterFunc syntax_highlighters_funcs[SYNTAX_HIGHLIGHTER_FUNC_MAX];
+ Vector<Ref<EditorSyntaxHighlighter>> syntax_highlighters;
struct ScriptHistory {
Control *control;
@@ -322,6 +366,7 @@ class ScriptEditor : public PanelContainer {
void _script_created(Ref<Script> p_script);
ScriptEditorBase *_get_current_editor() const;
+ Array _get_open_script_editors() const;
void _save_layout();
void _editor_settings_changed();
@@ -441,7 +486,9 @@ public:
void set_live_auto_reload_running_scripts(bool p_enabled);
- static void register_create_syntax_highlighter_function(CreateSyntaxHighlighterFunc p_func);
+ void register_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter);
+ void unregister_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter);
+
static void register_create_script_editor_function(CreateScriptEditorFunc p_func);
ScriptEditor(EditorNode *p_editor);
diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp
index 9304683d77..1e03d9dfab 100644
--- a/editor/plugins/script_text_editor.cpp
+++ b/editor/plugins/script_text_editor.cpp
@@ -132,7 +132,7 @@ void ScriptTextEditor::apply_code() {
}
script->set_source_code(code_editor->get_text_edit()->get_text());
script->update_exports();
- _update_member_keywords();
+ code_editor->get_text_edit()->get_syntax_highlighter()->update_cache();
}
RES ScriptTextEditor::get_edited_resource() const {
@@ -155,43 +155,9 @@ void ScriptTextEditor::set_edited_resource(const RES &p_res) {
_validate_script();
}
-void ScriptTextEditor::_update_member_keywords() {
- member_keywords.clear();
- code_editor->get_text_edit()->clear_member_keywords();
- Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
-
- StringName instance_base = script->get_instance_base_type();
-
- if (instance_base == StringName()) {
- return;
- }
- 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;
- }
-
- code_editor->get_text_edit()->add_member_keyword(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()) {
- code_editor->get_text_edit()->add_member_keyword(E->get(), member_variable_color);
- }
-}
-
void ScriptTextEditor::_load_theme_settings() {
TextEdit *text_edit = code_editor->get_text_edit();
-
- text_edit->clear_colors();
+ text_edit->clear_keywords();
Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
Color completion_background_color = EDITOR_GET("text_editor/highlighting/completion_background_color");
@@ -210,9 +176,6 @@ void ScriptTextEditor::_load_theme_settings() {
Color current_line_color = EDITOR_GET("text_editor/highlighting/current_line_color");
Color line_length_guideline_color = EDITOR_GET("text_editor/highlighting/line_length_guideline_color");
Color word_highlighted_color = EDITOR_GET("text_editor/highlighting/word_highlighted_color");
- Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
- Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
- Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
Color mark_color = EDITOR_GET("text_editor/highlighting/mark_color");
Color bookmark_color = EDITOR_GET("text_editor/highlighting/bookmark_color");
Color breakpoint_color = EDITOR_GET("text_editor/highlighting/breakpoint_color");
@@ -220,13 +183,6 @@ void ScriptTextEditor::_load_theme_settings() {
Color code_folding_color = EDITOR_GET("text_editor/highlighting/code_folding_color");
Color search_result_color = EDITOR_GET("text_editor/highlighting/search_result_color");
Color search_result_border_color = EDITOR_GET("text_editor/highlighting/search_result_border_color");
- Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
- Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
- Color basetype_color = EDITOR_GET("text_editor/highlighting/base_type_color");
- Color type_color = EDITOR_GET("text_editor/highlighting/engine_type_color");
- Color usertype_color = EDITOR_GET("text_editor/highlighting/user_type_color");
- Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
- Color string_color = EDITOR_GET("text_editor/highlighting/string_color");
text_edit->add_theme_color_override("background_color", background_color);
text_edit->add_theme_color_override("completion_background_color", completion_background_color);
@@ -245,9 +201,6 @@ void ScriptTextEditor::_load_theme_settings() {
text_edit->add_theme_color_override("current_line_color", current_line_color);
text_edit->add_theme_color_override("line_length_guideline_color", line_length_guideline_color);
text_edit->add_theme_color_override("word_highlighted_color", word_highlighted_color);
- text_edit->add_theme_color_override("number_color", number_color);
- text_edit->add_theme_color_override("function_color", function_color);
- text_edit->add_theme_color_override("member_variable_color", member_variable_color);
text_edit->add_theme_color_override("bookmark_color", bookmark_color);
text_edit->add_theme_color_override("breakpoint_color", breakpoint_color);
text_edit->add_theme_color_override("executing_line_color", executing_line_color);
@@ -255,18 +208,9 @@ void ScriptTextEditor::_load_theme_settings() {
text_edit->add_theme_color_override("code_folding_color", code_folding_color);
text_edit->add_theme_color_override("search_result_color", search_result_color);
text_edit->add_theme_color_override("search_result_border_color", search_result_border_color);
- text_edit->add_theme_color_override("symbol_color", symbol_color);
text_edit->add_theme_constant_override("line_spacing", EDITOR_DEF("text_editor/theme/line_spacing", 6));
- colors_cache.symbol_color = symbol_color;
- colors_cache.keyword_color = keyword_color;
- colors_cache.basetype_color = basetype_color;
- colors_cache.type_color = type_color;
- colors_cache.usertype_color = usertype_color;
- colors_cache.comment_color = comment_color;
- colors_cache.string_color = string_color;
-
theme_loaded = true;
if (!script.is_null()) {
_set_theme_for_script();
@@ -279,98 +223,47 @@ void ScriptTextEditor::_set_theme_for_script() {
}
TextEdit *text_edit = code_editor->get_text_edit();
+ text_edit->get_syntax_highlighter()->update_cache();
- List<String> keywords;
- script->get_language()->get_reserved_words(&keywords);
+ /* add keywords for auto completion */
+ // singleton autoloads (as types, just as engine singletons are)
+ 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) {
+ text_edit->add_keyword(info.name);
+ }
+ }
- for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
- text_edit->add_keyword_color(E->get(), colors_cache.keyword_color);
- }
-
- //colorize core types
- const Color basetype_color = colors_cache.basetype_color;
- text_edit->add_keyword_color("String", basetype_color);
- text_edit->add_keyword_color("Vector2", basetype_color);
- text_edit->add_keyword_color("Vector2i", basetype_color);
- text_edit->add_keyword_color("Rect2", basetype_color);
- text_edit->add_keyword_color("Rect2i", basetype_color);
- text_edit->add_keyword_color("Vector3", basetype_color);
- text_edit->add_keyword_color("Vector3i", basetype_color);
- text_edit->add_keyword_color("Transform2D", basetype_color);
- text_edit->add_keyword_color("Plane", basetype_color);
- text_edit->add_keyword_color("Quat", basetype_color);
- text_edit->add_keyword_color("AABB", basetype_color);
- text_edit->add_keyword_color("Basis", basetype_color);
- text_edit->add_keyword_color("Transform", basetype_color);
- text_edit->add_keyword_color("Color", basetype_color);
- text_edit->add_keyword_color("StringName", basetype_color);
- text_edit->add_keyword_color("NodePath", basetype_color);
- text_edit->add_keyword_color("RID", basetype_color);
- text_edit->add_keyword_color("Object", basetype_color);
- text_edit->add_keyword_color("Callable", basetype_color);
- text_edit->add_keyword_color("Dictionary", basetype_color);
- text_edit->add_keyword_color("Array", basetype_color);
- text_edit->add_keyword_color("PackedByteArray", basetype_color);
- text_edit->add_keyword_color("PackedInt32Array", basetype_color);
- text_edit->add_keyword_color("PackedInt64Array", basetype_color);
- text_edit->add_keyword_color("PackedFloat32Array", basetype_color);
- text_edit->add_keyword_color("PackedFloat64Array", basetype_color);
- text_edit->add_keyword_color("PackedStringArray", basetype_color);
- text_edit->add_keyword_color("PackedVector2Array", basetype_color);
- text_edit->add_keyword_color("PackedVector3Array", basetype_color);
- text_edit->add_keyword_color("PackedColorArray", basetype_color);
-
- //colorize engine types
+ // engine types
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());
}
-
- text_edit->add_keyword_color(n, colors_cache.type_color);
+ text_edit->add_keyword(n);
}
- _update_member_keywords();
- //colorize user types
+ // user types
List<StringName> global_classes;
ScriptServer::get_global_class_list(&global_classes);
-
for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) {
- text_edit->add_keyword_color(E->get(), colors_cache.usertype_color);
- }
-
- //colorize singleton autoloads (as types, just as engine singletons are)
- 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) {
- text_edit->add_keyword_color(info.name, colors_cache.usertype_color);
- }
+ text_edit->add_keyword(E->get());
}
- //colorize comments
- List<String> comments;
- script->get_language()->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();
-
- text_edit->add_color_region(beg, end, colors_cache.comment_color, end == "");
+ List<String> keywords;
+ script->get_language()->get_reserved_words(&keywords);
+ for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
+ text_edit->add_keyword(E->get());
}
- //colorize strings
- List<String> strings;
- script->get_language()->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();
- text_edit->add_color_region(beg, end, colors_cache.string_color, end == "");
+ // core types
+ List<String> core_types;
+ script->get_language()->get_core_type_words(&core_types);
+ for (List<String>::Element *E = core_types.front(); E; E = E->next()) {
+ text_edit->add_keyword(E->get());
}
}
@@ -555,7 +448,7 @@ void ScriptTextEditor::_validate_script() {
if (!script->is_tool()) {
script->set_source_code(text);
script->update_exports();
- _update_member_keywords();
+ te->get_syntax_highlighter()->update_cache();
}
functions.clear();
@@ -1380,28 +1273,24 @@ void ScriptTextEditor::_edit_option_toggle_inline_comment() {
code_editor->toggle_inline_comment(delimiter);
}
-void ScriptTextEditor::add_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
- highlighters[p_highlighter->get_name()] = p_highlighter;
- highlighter_menu->add_radio_check_item(p_highlighter->get_name());
+void ScriptTextEditor::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
+ highlighters[p_highlighter->_get_name()] = p_highlighter;
+ highlighter_menu->add_radio_check_item(p_highlighter->_get_name());
}
-void ScriptTextEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
+void ScriptTextEditor::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
TextEdit *te = code_editor->get_text_edit();
- te->_set_syntax_highlighting(p_highlighter);
- if (p_highlighter != nullptr) {
- highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(p_highlighter->get_name()), true);
- } else {
- highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(TTR("Standard")), true);
- }
+ p_highlighter->_set_edited_resource(script);
+ te->set_syntax_highlighter(p_highlighter);
+ highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(p_highlighter->_get_name()), true);
}
void ScriptTextEditor::_change_syntax_highlighter(int p_idx) {
- Map<String, SyntaxHighlighter *>::Element *el = highlighters.front();
+ Map<String, Ref<EditorSyntaxHighlighter>>::Element *el = highlighters.front();
while (el != nullptr) {
highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(el->key()), false);
el = el->next();
}
- // highlighter_menu->set_item_checked(p_idx, true);
set_syntax_highlighter(highlighters[highlighter_menu->get_item_text(p_idx)]);
}
@@ -1411,6 +1300,8 @@ void ScriptTextEditor::_bind_methods() {
ClassDB::bind_method("get_drag_data_fw", &ScriptTextEditor::get_drag_data_fw);
ClassDB::bind_method("can_drop_data_fw", &ScriptTextEditor::can_drop_data_fw);
ClassDB::bind_method("drop_data_fw", &ScriptTextEditor::drop_data_fw);
+
+ ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &ScriptTextEditor::add_syntax_highlighter);
}
Control *ScriptTextEditor::get_edit_menu() {
@@ -1825,14 +1716,21 @@ ScriptTextEditor::ScriptTextEditor() {
convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize"), KEY_MASK_SHIFT | KEY_F6), EDIT_CAPITALIZE);
convert_case->connect("id_pressed", callable_mp(this, &ScriptTextEditor::_edit_option));
- highlighters[TTR("Standard")] = nullptr;
highlighter_menu = memnew(PopupMenu);
highlighter_menu->set_name("highlighter_menu");
edit_menu->get_popup()->add_child(highlighter_menu);
edit_menu->get_popup()->add_submenu_item(TTR("Syntax Highlighter"), "highlighter_menu");
- highlighter_menu->add_radio_check_item(TTR("Standard"));
highlighter_menu->connect("id_pressed", callable_mp(this, &ScriptTextEditor::_change_syntax_highlighter));
+ Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter;
+ plain_highlighter.instance();
+ add_syntax_highlighter(plain_highlighter);
+
+ Ref<EditorStandardSyntaxHighlighter> highlighter;
+ highlighter.instance();
+ add_syntax_highlighter(highlighter);
+ set_syntax_highlighter(highlighter);
+
search_menu = memnew(MenuButton);
edit_hb->add_child(search_menu);
search_menu->set_text(TTR("Search"));
@@ -1891,11 +1789,6 @@ ScriptTextEditor::ScriptTextEditor() {
}
ScriptTextEditor::~ScriptTextEditor() {
- for (const Map<String, SyntaxHighlighter *>::Element *E = highlighters.front(); E; E = E->next()) {
- if (E->get() != nullptr) {
- memdelete(E->get());
- }
- }
highlighters.clear();
}
diff --git a/editor/plugins/script_text_editor.h b/editor/plugins/script_text_editor.h
index a8d7f80e7b..6d7f84d746 100644
--- a/editor/plugins/script_text_editor.h
+++ b/editor/plugins/script_text_editor.h
@@ -83,18 +83,6 @@ class ScriptTextEditor : public ScriptEditorBase {
Vector2 color_position;
String color_args;
- void _update_member_keywords();
-
- struct ColorsCache {
- Color symbol_color;
- Color keyword_color;
- Color basetype_color;
- Color type_color;
- Color usertype_color;
- Color comment_color;
- Color string_color;
- } colors_cache;
-
bool theme_loaded;
enum {
@@ -164,7 +152,7 @@ protected:
void _notification(int p_what);
static void _bind_methods();
- Map<String, SyntaxHighlighter *> highlighters;
+ Map<String, Ref<EditorSyntaxHighlighter>> highlighters;
void _change_syntax_highlighter(int p_idx);
void _edit_option(int p_op);
@@ -190,8 +178,8 @@ protected:
public:
void _update_connected_methods();
- virtual void add_syntax_highlighter(SyntaxHighlighter *p_highlighter) override;
- virtual void set_syntax_highlighter(SyntaxHighlighter *p_highlighter) override;
+ virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
+ virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
void update_toggle_scripts_button();
virtual void apply_code() override;
diff --git a/editor/plugins/shader_editor_plugin.cpp b/editor/plugins/shader_editor_plugin.cpp
index 7dd0b8a238..3867c8538a 100644
--- a/editor/plugins/shader_editor_plugin.cpp
+++ b/editor/plugins/shader_editor_plugin.cpp
@@ -83,8 +83,6 @@ void ShaderTextEditor::reload_text() {
}
void ShaderTextEditor::_load_theme_settings() {
- get_text_edit()->clear_colors();
-
Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
Color completion_background_color = EDITOR_GET("text_editor/highlighting/completion_background_color");
Color completion_selected_color = EDITOR_GET("text_editor/highlighting/completion_selected_color");
@@ -101,9 +99,6 @@ void ShaderTextEditor::_load_theme_settings() {
Color current_line_color = EDITOR_GET("text_editor/highlighting/current_line_color");
Color line_length_guideline_color = EDITOR_GET("text_editor/highlighting/line_length_guideline_color");
Color word_highlighted_color = EDITOR_GET("text_editor/highlighting/word_highlighted_color");
- Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
- Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
- Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
Color mark_color = EDITOR_GET("text_editor/highlighting/mark_color");
Color bookmark_color = EDITOR_GET("text_editor/highlighting/bookmark_color");
Color breakpoint_color = EDITOR_GET("text_editor/highlighting/breakpoint_color");
@@ -111,9 +106,6 @@ void ShaderTextEditor::_load_theme_settings() {
Color code_folding_color = EDITOR_GET("text_editor/highlighting/code_folding_color");
Color search_result_color = EDITOR_GET("text_editor/highlighting/search_result_color");
Color search_result_border_color = EDITOR_GET("text_editor/highlighting/search_result_border_color");
- Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
- Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
- Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
get_text_edit()->add_theme_color_override("background_color", background_color);
get_text_edit()->add_theme_color_override("completion_background_color", completion_background_color);
@@ -131,9 +123,6 @@ void ShaderTextEditor::_load_theme_settings() {
get_text_edit()->add_theme_color_override("current_line_color", current_line_color);
get_text_edit()->add_theme_color_override("line_length_guideline_color", line_length_guideline_color);
get_text_edit()->add_theme_color_override("word_highlighted_color", word_highlighted_color);
- get_text_edit()->add_theme_color_override("number_color", number_color);
- get_text_edit()->add_theme_color_override("function_color", function_color);
- get_text_edit()->add_theme_color_override("member_variable_color", member_variable_color);
get_text_edit()->add_theme_color_override("mark_color", mark_color);
get_text_edit()->add_theme_color_override("bookmark_color", bookmark_color);
get_text_edit()->add_theme_color_override("breakpoint_color", breakpoint_color);
@@ -141,7 +130,11 @@ void ShaderTextEditor::_load_theme_settings() {
get_text_edit()->add_theme_color_override("code_folding_color", code_folding_color);
get_text_edit()->add_theme_color_override("search_result_color", search_result_color);
get_text_edit()->add_theme_color_override("search_result_border_color", search_result_border_color);
- get_text_edit()->add_theme_color_override("symbol_color", symbol_color);
+
+ syntax_highlighter->set_number_color(EDITOR_GET("text_editor/highlighting/number_color"));
+ syntax_highlighter->set_symbol_color(EDITOR_GET("text_editor/highlighting/symbol_color"));
+ syntax_highlighter->set_function_color(EDITOR_GET("text_editor/highlighting/function_color"));
+ syntax_highlighter->set_member_variable_color(EDITOR_GET("text_editor/highlighting/member_variable_color"));
List<String> keywords;
ShaderLanguage::get_keyword_list(&keywords);
@@ -158,13 +151,17 @@ void ShaderTextEditor::_load_theme_settings() {
}
}
+ const Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
+ syntax_highlighter->clear_keyword_colors();
for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
- get_text_edit()->add_keyword_color(E->get(), keyword_color);
+ syntax_highlighter->add_keyword_color(E->get(), keyword_color);
}
//colorize comments
- get_text_edit()->add_color_region("/*", "*/", comment_color, false);
- get_text_edit()->add_color_region("//", "", comment_color, false);
+ const Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
+ syntax_highlighter->clear_color_regions();
+ syntax_highlighter->add_color_region("/*", "*/", comment_color, false);
+ syntax_highlighter->add_color_region("//", "", comment_color, false);
}
void ShaderTextEditor::_check_shader_mode() {
@@ -236,6 +233,8 @@ void ShaderTextEditor::_bind_methods() {
}
ShaderTextEditor::ShaderTextEditor() {
+ syntax_highlighter.instance();
+ get_text_edit()->set_syntax_highlighter(syntax_highlighter);
}
/*** SCRIPT EDITOR ******/
@@ -356,7 +355,6 @@ void ShaderEditor::_editor_settings_changed() {
shader_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
shader_editor->get_text_edit()->set_draw_spaces(EditorSettings::get_singleton()->get("text_editor/indent/draw_spaces"));
shader_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_numbers"));
- shader_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting"));
shader_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
shader_editor->get_text_edit()->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line"));
shader_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink"));
diff --git a/editor/plugins/shader_editor_plugin.h b/editor/plugins/shader_editor_plugin.h
index 6ad3413dd7..904aed186a 100644
--- a/editor/plugins/shader_editor_plugin.h
+++ b/editor/plugins/shader_editor_plugin.h
@@ -44,6 +44,7 @@
class ShaderTextEditor : public CodeTextEditor {
GDCLASS(ShaderTextEditor, CodeTextEditor);
+ Ref<CodeHighlighter> syntax_highlighter;
Ref<Shader> shader;
void _check_shader_mode();
diff --git a/editor/plugins/text_editor.cpp b/editor/plugins/text_editor.cpp
index 3ceb9bfd82..d602d152fe 100644
--- a/editor/plugins/text_editor.cpp
+++ b/editor/plugins/text_editor.cpp
@@ -33,35 +33,19 @@
#include "core/os/keyboard.h"
#include "editor/editor_node.h"
-void TextEditor::add_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
- highlighters[p_highlighter->get_name()] = p_highlighter;
- highlighter_menu->add_radio_check_item(p_highlighter->get_name());
+void TextEditor::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
+ highlighters[p_highlighter->_get_name()] = p_highlighter;
+ highlighter_menu->add_radio_check_item(p_highlighter->_get_name());
}
-void TextEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
+void TextEditor::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
TextEdit *te = code_editor->get_text_edit();
- te->_set_syntax_highlighting(p_highlighter);
- if (p_highlighter != nullptr) {
- highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(p_highlighter->get_name()), true);
- } else {
- highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text("Standard"), true);
- }
-
- // little work around. GDScript highlighter goes through text_edit for colours,
- // so to remove all colours we need to set and unset them here.
- if (p_highlighter == nullptr) { // standard
- TextEdit *text_edit = code_editor->get_text_edit();
- text_edit->add_theme_color_override("number_color", colors_cache.font_color);
- text_edit->add_theme_color_override("function_color", colors_cache.font_color);
- text_edit->add_theme_color_override("number_color", colors_cache.font_color);
- text_edit->add_theme_color_override("member_variable_color", colors_cache.font_color);
- } else {
- _load_theme_settings();
- }
+ te->set_syntax_highlighter(p_highlighter);
+ highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(p_highlighter->_get_name()), true);
}
void TextEditor::_change_syntax_highlighter(int p_idx) {
- Map<String, SyntaxHighlighter *>::Element *el = highlighters.front();
+ Map<String, Ref<EditorSyntaxHighlighter>>::Element *el = highlighters.front();
while (el != nullptr) {
highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(el->key()), false);
el = el->next();
@@ -71,7 +55,7 @@ void TextEditor::_change_syntax_highlighter(int p_idx) {
void TextEditor::_load_theme_settings() {
TextEdit *text_edit = code_editor->get_text_edit();
- text_edit->clear_colors();
+ text_edit->get_syntax_highlighter()->update_cache();
Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
Color completion_background_color = EDITOR_GET("text_editor/highlighting/completion_background_color");
@@ -89,9 +73,6 @@ void TextEditor::_load_theme_settings() {
Color current_line_color = EDITOR_GET("text_editor/highlighting/current_line_color");
Color line_length_guideline_color = EDITOR_GET("text_editor/highlighting/line_length_guideline_color");
Color word_highlighted_color = EDITOR_GET("text_editor/highlighting/word_highlighted_color");
- Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
- Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
- Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
Color mark_color = EDITOR_GET("text_editor/highlighting/mark_color");
Color bookmark_color = EDITOR_GET("text_editor/highlighting/bookmark_color");
Color breakpoint_color = EDITOR_GET("text_editor/highlighting/breakpoint_color");
@@ -99,12 +80,6 @@ void TextEditor::_load_theme_settings() {
Color code_folding_color = EDITOR_GET("text_editor/highlighting/code_folding_color");
Color search_result_color = EDITOR_GET("text_editor/highlighting/search_result_color");
Color search_result_border_color = EDITOR_GET("text_editor/highlighting/search_result_border_color");
- Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
- Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
- Color basetype_color = EDITOR_GET("text_editor/highlighting/base_type_color");
- Color type_color = EDITOR_GET("text_editor/highlighting/engine_type_color");
- Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
- Color string_color = EDITOR_GET("text_editor/highlighting/string_color");
text_edit->add_theme_color_override("background_color", background_color);
text_edit->add_theme_color_override("completion_background_color", completion_background_color);
@@ -122,9 +97,6 @@ void TextEditor::_load_theme_settings() {
text_edit->add_theme_color_override("current_line_color", current_line_color);
text_edit->add_theme_color_override("line_length_guideline_color", line_length_guideline_color);
text_edit->add_theme_color_override("word_highlighted_color", word_highlighted_color);
- text_edit->add_theme_color_override("number_color", number_color);
- text_edit->add_theme_color_override("function_color", function_color);
- text_edit->add_theme_color_override("member_variable_color", member_variable_color);
text_edit->add_theme_color_override("breakpoint_color", breakpoint_color);
text_edit->add_theme_color_override("executing_line_color", executing_line_color);
text_edit->add_theme_color_override("mark_color", mark_color);
@@ -132,17 +104,8 @@ void TextEditor::_load_theme_settings() {
text_edit->add_theme_color_override("code_folding_color", code_folding_color);
text_edit->add_theme_color_override("search_result_color", search_result_color);
text_edit->add_theme_color_override("search_result_border_color", search_result_border_color);
- text_edit->add_theme_color_override("symbol_color", symbol_color);
text_edit->add_theme_constant_override("line_spacing", EDITOR_DEF("text_editor/theme/line_spacing", 6));
-
- colors_cache.font_color = text_color;
- colors_cache.symbol_color = symbol_color;
- colors_cache.keyword_color = keyword_color;
- colors_cache.basetype_color = basetype_color;
- colors_cache.type_color = type_color;
- colors_cache.comment_color = comment_color;
- colors_cache.string_color = string_color;
}
String TextEditor::get_name() {
@@ -471,6 +434,7 @@ void TextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) {
}
void TextEditor::_bind_methods() {
+ ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &TextEditor::add_syntax_highlighter);
}
static ScriptEditorBase *create_editor(const RES &p_resource) {
@@ -634,14 +598,21 @@ TextEditor::TextEditor() {
convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize")), EDIT_CAPITALIZE);
convert_case->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option));
- highlighters["Standard"] = nullptr;
highlighter_menu = memnew(PopupMenu);
highlighter_menu->set_name("highlighter_menu");
edit_menu->get_popup()->add_child(highlighter_menu);
edit_menu->get_popup()->add_submenu_item(TTR("Syntax Highlighter"), "highlighter_menu");
- highlighter_menu->add_radio_check_item(TTR("Standard"));
highlighter_menu->connect("id_pressed", callable_mp(this, &TextEditor::_change_syntax_highlighter));
+ Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter;
+ plain_highlighter.instance();
+ add_syntax_highlighter(plain_highlighter);
+
+ Ref<EditorStandardSyntaxHighlighter> highlighter;
+ highlighter.instance();
+ add_syntax_highlighter(highlighter);
+ set_syntax_highlighter(plain_highlighter);
+
MenuButton *goto_menu = memnew(MenuButton);
edit_hb->add_child(goto_menu);
goto_menu->set_text(TTR("Go To"));
@@ -666,11 +637,6 @@ TextEditor::TextEditor() {
}
TextEditor::~TextEditor() {
- for (const Map<String, SyntaxHighlighter *>::Element *E = highlighters.front(); E; E = E->next()) {
- if (E->get() != nullptr) {
- memdelete(E->get());
- }
- }
highlighters.clear();
}
diff --git a/editor/plugins/text_editor.h b/editor/plugins/text_editor.h
index 5af020eabe..5299776b56 100644
--- a/editor/plugins/text_editor.h
+++ b/editor/plugins/text_editor.h
@@ -50,16 +50,6 @@ private:
GotoLineDialog *goto_line_dialog;
- struct ColorsCache {
- Color font_color;
- Color symbol_color;
- Color keyword_color;
- Color basetype_color;
- Color type_color;
- Color comment_color;
- Color string_color;
- } colors_cache;
-
enum {
EDIT_UNDO,
EDIT_REDO,
@@ -104,7 +94,7 @@ protected:
void _make_context_menu(bool p_selection, bool p_can_fold, bool p_is_folded, Vector2 p_position);
void _text_edit_gui_input(const Ref<InputEvent> &ev);
- Map<String, SyntaxHighlighter *> highlighters;
+ Map<String, Ref<EditorSyntaxHighlighter>> highlighters;
void _change_syntax_highlighter(int p_idx);
void _load_theme_settings();
@@ -116,8 +106,8 @@ protected:
void _bookmark_item_pressed(int p_idx);
public:
- virtual void add_syntax_highlighter(SyntaxHighlighter *p_highlighter) override;
- virtual void set_syntax_highlighter(SyntaxHighlighter *p_highlighter) override;
+ virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
+ virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
virtual String get_name() override;
virtual Ref<Texture2D> get_theme_icon() override;
diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp
index c19140ae7c..ea6c5edcef 100644
--- a/editor/plugins/visual_shader_editor_plugin.cpp
+++ b/editor/plugins/visual_shader_editor_plugin.cpp
@@ -824,6 +824,8 @@ void VisualShaderEditor::_update_graph() {
if (is_expression) {
TextEdit *expression_box = memnew(TextEdit);
+ Ref<CodeHighlighter> expression_syntax_highlighter;
+ expression_syntax_highlighter.instance();
expression_node->set_control(expression_box, 0);
node->add_child(expression_box);
@@ -833,18 +835,18 @@ void VisualShaderEditor::_update_graph() {
Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
- expression_box->set_syntax_coloring(true);
+ expression_box->set_syntax_highlighter(expression_syntax_highlighter);
expression_box->add_theme_color_override("background_color", background_color);
for (List<String>::Element *E = keyword_list.front(); E; E = E->next()) {
- expression_box->add_keyword_color(E->get(), keyword_color);
+ expression_syntax_highlighter->add_keyword_color(E->get(), keyword_color);
}
expression_box->add_theme_font_override("font", get_theme_font("expression", "EditorFonts"));
expression_box->add_theme_color_override("font_color", text_color);
- expression_box->add_theme_color_override("symbol_color", symbol_color);
- expression_box->add_color_region("/*", "*/", comment_color, false);
- expression_box->add_color_region("//", "", comment_color, false);
+ expression_syntax_highlighter->set_symbol_color(symbol_color);
+ expression_syntax_highlighter->add_color_region("/*", "*/", comment_color, false);
+ expression_syntax_highlighter->add_color_region("//", "", comment_color, false);
expression_box->set_text(expression);
expression_box->set_context_menu_enabled(false);
@@ -1703,14 +1705,14 @@ void VisualShaderEditor::_notification(int p_what) {
preview_text->add_theme_color_override("background_color", background_color);
for (List<String>::Element *E = keyword_list.front(); E; E = E->next()) {
- preview_text->add_keyword_color(E->get(), keyword_color);
+ syntax_highlighter->add_keyword_color(E->get(), keyword_color);
}
preview_text->add_theme_font_override("font", get_theme_font("expression", "EditorFonts"));
preview_text->add_theme_color_override("font_color", text_color);
- preview_text->add_theme_color_override("symbol_color", symbol_color);
- preview_text->add_color_region("/*", "*/", comment_color, false);
- preview_text->add_color_region("//", "", comment_color, false);
+ syntax_highlighter->set_symbol_color(symbol_color);
+ syntax_highlighter->add_color_region("/*", "*/", comment_color, false);
+ syntax_highlighter->add_color_region("//", "", comment_color, false);
error_text->add_theme_font_override("font", get_theme_font("status_source", "EditorFonts"));
error_text->add_theme_color_override("font_color", get_theme_color("error_color", "Editor"));
@@ -2369,11 +2371,12 @@ VisualShaderEditor::VisualShaderEditor() {
preview_vbox->set_visible(preview_showed);
main_box->add_child(preview_vbox);
preview_text = memnew(TextEdit);
+ syntax_highlighter.instance();
preview_vbox->add_child(preview_text);
preview_text->set_h_size_flags(SIZE_EXPAND_FILL);
preview_text->set_v_size_flags(SIZE_EXPAND_FILL);
preview_text->set_custom_minimum_size(Size2(400 * EDSCALE, 0));
- preview_text->set_syntax_coloring(true);
+ preview_text->set_syntax_highlighter(syntax_highlighter);
preview_text->set_show_line_numbers(true);
preview_text->set_readonly(true);
diff --git a/editor/plugins/visual_shader_editor_plugin.h b/editor/plugins/visual_shader_editor_plugin.h
index dd9144c567..0601b35131 100644
--- a/editor/plugins/visual_shader_editor_plugin.h
+++ b/editor/plugins/visual_shader_editor_plugin.h
@@ -72,6 +72,7 @@ class VisualShaderEditor : public VBoxContainer {
bool shader_error;
VBoxContainer *preview_vbox;
TextEdit *preview_text;
+ Ref<CodeHighlighter> syntax_highlighter;
Label *error_text;
UndoRedo *undo_redo;