diff options
author | Paulb23 <p_batty@hotmail.co.uk> | 2020-05-03 17:08:15 +0100 |
---|---|---|
committer | Paulb23 <p_batty@hotmail.co.uk> | 2020-07-11 17:09:58 +0100 |
commit | bc4cee44582d2a90d0792d6b213f00be1043000b (patch) | |
tree | 001ab82ed2bd7c6b59a7cc06807cce988c484d66 /editor | |
parent | 156daddaaf16e36eb932452d1e30f4f77d29aae6 (diff) |
Extract Syntax highlighting from TextEdit and add EditorSyntaxHighlighter
- Extacted all syntax highlighting code from text edit
- Removed enable syntax highlighting from text edit
- Added line_edited_from signal to text_edit
- Renamed get/set_syntax_highlighting to get/set_syntax_highlighter
- Added EditorSyntaxHighligher
Diffstat (limited to 'editor')
-rw-r--r-- | editor/code_editor.cpp | 21 | ||||
-rw-r--r-- | editor/code_editor.h | 3 | ||||
-rw-r--r-- | editor/editor_node.cpp | 1 | ||||
-rw-r--r-- | editor/editor_settings.cpp | 1 | ||||
-rw-r--r-- | editor/plugins/script_editor_plugin.cpp | 169 | ||||
-rw-r--r-- | editor/plugins/script_editor_plugin.h | 58 | ||||
-rw-r--r-- | editor/plugins/script_text_editor.cpp | 185 | ||||
-rw-r--r-- | editor/plugins/script_text_editor.h | 18 | ||||
-rw-r--r-- | editor/plugins/shader_editor_plugin.cpp | 30 | ||||
-rw-r--r-- | editor/plugins/shader_editor_plugin.h | 1 | ||||
-rw-r--r-- | editor/plugins/text_editor.cpp | 60 | ||||
-rw-r--r-- | editor/plugins/text_editor.h | 16 | ||||
-rw-r--r-- | editor/plugins/visual_shader_editor_plugin.cpp | 23 | ||||
-rw-r--r-- | editor/plugins/visual_shader_editor_plugin.h | 1 |
14 files changed, 327 insertions, 260 deletions
diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 9e1a5f2d43..d8648310b6 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -837,7 +837,14 @@ void CodeTextEditor::_complete_request() { } for (List<ScriptCodeCompletionOption>::Element *E = entries.front(); E; E = E->next()) { - E->get().icon = _get_completion_icon(E->get()); + ScriptCodeCompletionOption *e = &E->get(); + e->icon = _get_completion_icon(*e); + e->font_color = completion_font_color; + if (e->insert_text.begins_with("\"") || e->insert_text.begins_with("\'")) { + e->font_color = completion_string_color; + } else if (e->insert_text.begins_with("#") || e->insert_text.begins_with("//")) { + e->font_color = completion_comment_color; + } } text_editor->code_complete(entries, forced); } @@ -910,7 +917,10 @@ bool CodeTextEditor::_add_font_size(int p_delta) { } void CodeTextEditor::update_editor_settings() { - text_editor->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting")); + completion_font_color = EDITOR_GET("text_editor/highlighting/completion_font_color"); + completion_string_color = EDITOR_GET("text_editor/highlighting/string_color"); + completion_comment_color = EDITOR_GET("text_editor/highlighting/comment_color"); + text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences")); text_editor->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line")); text_editor->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type")); @@ -1407,11 +1417,8 @@ Variant CodeTextEditor::get_edit_state() { state["breakpoints"] = text_editor->get_breakpoints_array(); state["bookmarks"] = text_editor->get_bookmarks_array(); - state["syntax_highlighter"] = TTR("Standard"); - Ref<SyntaxHighlighter> syntax_highlighter = text_editor->get_syntax_highlighting(); - if (syntax_highlighter.is_valid()) { - state["syntax_highlighter"] = syntax_highlighter->_get_name(); - } + Ref<EditorSyntaxHighlighter> syntax_highlighter = text_editor->get_syntax_highlighter(); + state["syntax_highlighter"] = syntax_highlighter->_get_name(); return state; } diff --git a/editor/code_editor.h b/editor/code_editor.h index d3090b28c1..450c85c64b 100644 --- a/editor/code_editor.h +++ b/editor/code_editor.h @@ -174,6 +174,9 @@ class CodeTextEditor : public VBoxContainer { void _zoom_changed(); void _reset_zoom(); + Color completion_font_color; + Color completion_string_color; + Color completion_comment_color; CodeTextEditorCodeCompleteFunc code_complete_func; void *code_complete_ud; diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 2f669d9006..0da7e5210a 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -3603,6 +3603,7 @@ void EditorNode::register_editor_types() { ClassDB::register_class<EditorVCSInterface>(); ClassDB::register_virtual_class<ScriptEditor>(); ClassDB::register_virtual_class<ScriptEditorBase>(); + ClassDB::register_class<EditorSyntaxHighlighter>(); ClassDB::register_virtual_class<EditorInterface>(); ClassDB::register_class<EditorExportPlugin>(); ClassDB::register_class<EditorResourceConversionPlugin>(); diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 5f293f1fb3..3a1a0d5b01 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -420,7 +420,6 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { _load_default_text_editor_theme(); // Highlighting - _initial_set("text_editor/highlighting/syntax_highlighting", true); _initial_set("text_editor/highlighting/highlight_all_occurrences", true); _initial_set("text_editor/highlighting/highlight_current_line", true); _initial_set("text_editor/highlighting/highlight_type_safe_lines", true); diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp index 237aefb1b9..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() { @@ -2022,7 +2181,7 @@ 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.size(); i++) { - Ref<SyntaxHighlighter> highlighter = syntax_highlighters[i]->_create(); + Ref<EditorSyntaxHighlighter> highlighter = syntax_highlighters[i]->_create(); if (highlighter.is_null()) { continue; } @@ -2830,13 +2989,17 @@ void ScriptEditor::_open_script_request(const String &p_path) { } } -void ScriptEditor::register_syntax_highlighter(const Ref<SyntaxHighlighter> &p_syntax_highlighter) { +void ScriptEditor::register_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter) { + ERR_FAIL_COND(p_syntax_highlighter.is_null()); + if (syntax_highlighters.find(p_syntax_highlighter) == -1) { syntax_highlighters.push_back(p_syntax_highlighter); } } -void ScriptEditor::unregister_syntax_highlighter(const Ref<SyntaxHighlighter> &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); } diff --git a/editor/plugins/script_editor_plugin.h b/editor/plugins/script_editor_plugin.h index 4ce3b0e4ad..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(Ref<SyntaxHighlighter> p_highlighter) = 0; - virtual void set_syntax_highlighter(Ref<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; @@ -241,7 +287,7 @@ class ScriptEditor : public PanelContainer { static int script_editor_func_count; static CreateScriptEditorFunc script_editor_funcs[SCRIPT_EDITOR_FUNC_MAX]; - Vector<Ref<SyntaxHighlighter> > syntax_highlighters; + Vector<Ref<EditorSyntaxHighlighter>> syntax_highlighters; struct ScriptHistory { Control *control; @@ -440,8 +486,8 @@ public: void set_live_auto_reload_running_scripts(bool p_enabled); - void register_syntax_highlighter(const Ref<SyntaxHighlighter> &p_syntax_highlighter); - void unregister_syntax_highlighter(const Ref<SyntaxHighlighter> &p_syntax_highlighter); + 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); diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index 875e6ef350..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); + text_edit->add_keyword(E->get()); } - //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); - } - } - - //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,23 +1273,20 @@ void ScriptTextEditor::_edit_option_toggle_inline_comment() { code_editor->toggle_inline_comment(delimiter); } -void ScriptTextEditor::add_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter) { +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(Ref<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.is_valid()) { - 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, Ref<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(); @@ -1826,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")] = Ref<SyntaxHighlighter>(); 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")); diff --git a/editor/plugins/script_text_editor.h b/editor/plugins/script_text_editor.h index d9ba579102..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, Ref<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(Ref<SyntaxHighlighter> p_highlighter) override; - virtual void set_syntax_highlighter(Ref<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 25c0da90ad..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(Ref<SyntaxHighlighter> p_highlighter) { +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(Ref<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.is_valid()) { - 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, Ref<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() { @@ -635,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"] = Ref<SyntaxHighlighter>(); 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")); diff --git a/editor/plugins/text_editor.h b/editor/plugins/text_editor.h index 0db8f70a85..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, Ref<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(Ref<SyntaxHighlighter> p_highlighter) override; - virtual void set_syntax_highlighter(Ref<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; |