diff options
-rw-r--r-- | editor/code_editor.cpp | 6 | ||||
-rw-r--r-- | editor/plugins/script_editor_plugin.cpp | 4 | ||||
-rw-r--r-- | editor/plugins/script_editor_plugin.h | 4 | ||||
-rw-r--r-- | editor/plugins/script_text_editor.cpp | 24 | ||||
-rw-r--r-- | editor/plugins/script_text_editor.h | 6 | ||||
-rw-r--r-- | editor/plugins/text_editor.cpp | 23 | ||||
-rw-r--r-- | editor/plugins/text_editor.h | 6 | ||||
-rw-r--r-- | modules/gdscript/editor/gdscript_highlighter.cpp | 65 | ||||
-rw-r--r-- | modules/gdscript/editor/gdscript_highlighter.h | 6 | ||||
-rw-r--r-- | modules/visual_script/visual_script_editor.cpp | 4 | ||||
-rw-r--r-- | modules/visual_script/visual_script_editor.h | 4 | ||||
-rw-r--r-- | scene/gui/text_edit.cpp | 50 | ||||
-rw-r--r-- | scene/gui/text_edit.h | 33 | ||||
-rw-r--r-- | scene/register_scene_types.cpp | 2 | ||||
-rw-r--r-- | scene/resources/syntax_highlighter.cpp | 77 | ||||
-rw-r--r-- | scene/resources/syntax_highlighter.h | 63 |
16 files changed, 241 insertions, 136 deletions
diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 70747b4956..9e1a5f2d43 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -1408,9 +1408,9 @@ Variant CodeTextEditor::get_edit_state() { state["bookmarks"] = text_editor->get_bookmarks_array(); state["syntax_highlighter"] = TTR("Standard"); - SyntaxHighlighter *syntax_highlighter = text_editor->_get_syntax_highlighting(); - if (syntax_highlighter) { - state["syntax_highlighter"] = syntax_highlighter->get_name(); + Ref<SyntaxHighlighter> syntax_highlighter = text_editor->get_syntax_highlighting(); + if (syntax_highlighter.is_valid()) { + state["syntax_highlighter"] = syntax_highlighter->_get_name(); } return state; diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp index 8386d44e69..3f17f1166f 100644 --- a/editor/plugins/script_editor_plugin.cpp +++ b/editor/plugins/script_editor_plugin.cpp @@ -2024,8 +2024,8 @@ bool ScriptEditor::edit(const RES &p_resource, int p_line, int p_col, bool p_gra 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; } diff --git a/editor/plugins/script_editor_plugin.h b/editor/plugins/script_editor_plugin.h index d51c95ec4b..9f9f51de5d 100644 --- a/editor/plugins/script_editor_plugin.h +++ b/editor/plugins/script_editor_plugin.h @@ -80,8 +80,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<SyntaxHighlighter> p_highlighter) = 0; + virtual void set_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter) = 0; virtual void apply_code() = 0; virtual RES get_edited_resource() const = 0; diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index 9304683d77..a9898dafe5 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -1380,28 +1380,27 @@ 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<SyntaxHighlighter> 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<SyntaxHighlighter> 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); + 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); } } void ScriptTextEditor::_change_syntax_highlighter(int p_idx) { - Map<String, SyntaxHighlighter *>::Element *el = highlighters.front(); + Map<String, Ref<SyntaxHighlighter> >::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)]); } @@ -1825,7 +1824,7 @@ 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; + highlighters[TTR("Standard")] = Ref<SyntaxHighlighter>(); highlighter_menu = memnew(PopupMenu); highlighter_menu->set_name("highlighter_menu"); edit_menu->get_popup()->add_child(highlighter_menu); @@ -1891,11 +1890,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..d9ba579102 100644 --- a/editor/plugins/script_text_editor.h +++ b/editor/plugins/script_text_editor.h @@ -164,7 +164,7 @@ protected: void _notification(int p_what); static void _bind_methods(); - Map<String, SyntaxHighlighter *> highlighters; + Map<String, Ref<SyntaxHighlighter> > highlighters; void _change_syntax_highlighter(int p_idx); void _edit_option(int p_op); @@ -190,8 +190,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<SyntaxHighlighter> p_highlighter) override; + virtual void set_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter) override; void update_toggle_scripts_button(); virtual void apply_code() override; diff --git a/editor/plugins/text_editor.cpp b/editor/plugins/text_editor.cpp index 3ceb9bfd82..071a46b5ee 100644 --- a/editor/plugins/text_editor.cpp +++ b/editor/plugins/text_editor.cpp @@ -33,16 +33,16 @@ #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<SyntaxHighlighter> 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<SyntaxHighlighter> 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); + 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); } @@ -61,7 +61,7 @@ void TextEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) { } void TextEditor::_change_syntax_highlighter(int p_idx) { - Map<String, SyntaxHighlighter *>::Element *el = highlighters.front(); + Map<String, Ref<SyntaxHighlighter> >::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(); @@ -634,7 +634,7 @@ 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; + highlighters["Standard"] = Ref<SyntaxHighlighter>(); highlighter_menu = memnew(PopupMenu); highlighter_menu->set_name("highlighter_menu"); edit_menu->get_popup()->add_child(highlighter_menu); @@ -666,11 +666,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..0db8f70a85 100644 --- a/editor/plugins/text_editor.h +++ b/editor/plugins/text_editor.h @@ -104,7 +104,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<SyntaxHighlighter> > highlighters; void _change_syntax_highlighter(int p_idx); void _load_theme_settings(); @@ -116,8 +116,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<SyntaxHighlighter> p_highlighter) override; + virtual void set_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter) override; virtual String get_name() override; virtual Ref<Texture2D> get_theme_icon() override; diff --git a/modules/gdscript/editor/gdscript_highlighter.cpp b/modules/gdscript/editor/gdscript_highlighter.cpp index d0f27b632b..4be83877be 100644 --- a/modules/gdscript/editor/gdscript_highlighter.cpp +++ b/modules/gdscript/editor/gdscript_highlighter.cpp @@ -31,7 +31,6 @@ #include "gdscript_highlighter.h" #include "../gdscript_tokenizer.h" #include "editor/editor_settings.h" -#include "scene/gui/text_edit.h" inline bool _is_symbol(CharType c) { return is_symbol(c); @@ -57,8 +56,8 @@ static bool _is_bin_symbol(CharType c) { return (c == '0' || c == '1'); } -Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) { - Map<int, TextEdit::HighlighterInfo> color_map; +Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) { + Dictionary color_map; Type next_type = NONE; Type current_type = NONE; @@ -82,14 +81,14 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_ Color keyword_color; Color color; - int in_region = text_editor->_is_line_in_region(p_line); + int in_region = text_edit->_is_line_in_region(p_line); int deregion = 0; - const Map<int, TextEdit::Text::ColorRegionInfo> cri_map = text_editor->_get_line_color_region_info(p_line); - const String &str = text_editor->get_line(p_line); + const Map<int, TextEdit::Text::ColorRegionInfo> cri_map = text_edit->_get_line_color_region_info(p_line); + const String &str = text_edit->get_line(p_line); Color prev_color; for (int j = 0; j < str.length(); j++) { - TextEdit::HighlighterInfo highlighter_info; + Dictionary highlighter_info; if (deregion > 0) { deregion--; @@ -101,7 +100,7 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_ if (deregion != 0) { if (color != prev_color) { prev_color = color; - highlighter_info.color = color; + highlighter_info["color"] = color; color_map[j] = highlighter_info; } continue; @@ -163,7 +162,7 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_ in_region = cri.region; } } else { - TextEdit::ColorRegion cr = text_editor->_get_color_region(cri.region); + TextEdit::ColorRegion cr = text_edit->_get_color_region(cri.region); if (in_region == cri.region && !cr.line_only) { //ignore otherwise if (cri.end || cr.eq) { deregion = cr.eq ? cr.begin_key.length() : cr.end_key.length(); @@ -184,10 +183,10 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_ String word = str.substr(j, to - j); Color col = Color(); - if (text_editor->has_keyword_color(word)) { - col = text_editor->get_keyword_color(word); - } else if (text_editor->has_member_color(word)) { - col = text_editor->get_member_color(word); + if (text_edit->has_keyword_color(word)) { + col = text_edit->get_keyword_color(word); + } else if (text_edit->has_member_color(word)) { + col = text_edit->get_member_color(word); for (int k = j - 1; k >= 0; k--) { if (str[k] == '.') { col = Color(); //member indexing not allowed @@ -276,7 +275,7 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_ if (in_region >= 0) { next_type = REGION; - color = text_editor->_get_color_region(in_region).color; + color = text_edit->_get_color_region(in_region).color; } else if (in_node_path) { next_type = NODE_PATH; color = node_path_color; @@ -335,32 +334,32 @@ Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_ if (color != prev_color) { prev_color = color; - highlighter_info.color = color; + highlighter_info["color"] = color; color_map[j] = highlighter_info; } } return color_map; } -String GDScriptSyntaxHighlighter::get_name() const { +String GDScriptSyntaxHighlighter::_get_name() const { return "GDScript"; } -List<String> GDScriptSyntaxHighlighter::get_supported_languages() { - List<String> languages; +Array GDScriptSyntaxHighlighter::_get_supported_languages() const { + Array languages; languages.push_back("GDScript"); return languages; } void GDScriptSyntaxHighlighter::_update_cache() { - font_color = text_editor->get_theme_color("font_color"); - symbol_color = text_editor->get_theme_color("symbol_color"); - function_color = text_editor->get_theme_color("function_color"); - number_color = text_editor->get_theme_color("number_color"); - member_color = text_editor->get_theme_color("member_variable_color"); + font_color = text_edit->get_theme_color("font_color"); + symbol_color = text_edit->get_theme_color("symbol_color"); + function_color = text_edit->get_theme_color("function_color"); + number_color = text_edit->get_theme_color("number_color"); + member_color = text_edit->get_theme_color("member_variable_color"); - const String text_editor_color_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme"); - const bool default_theme = text_editor_color_theme == "Default"; + const String text_edit_color_theme = EditorSettings::get_singleton()->get("text_edit/theme/color_theme"); + const bool default_theme = text_edit_color_theme == "Default"; if (default_theme || EditorSettings::get_singleton()->is_dark_theme()) { function_definition_color = Color(0.4, 0.9, 1.0); @@ -370,22 +369,22 @@ void GDScriptSyntaxHighlighter::_update_cache() { node_path_color = Color(0.32, 0.55, 0.29); } - EDITOR_DEF("text_editor/highlighting/gdscript/function_definition_color", function_definition_color); - EDITOR_DEF("text_editor/highlighting/gdscript/node_path_color", node_path_color); - if (text_editor_color_theme == "Adaptive" || default_theme) { + EDITOR_DEF("text_edit/highlighting/gdscript/function_definition_color", function_definition_color); + EDITOR_DEF("text_edit/highlighting/gdscript/node_path_color", node_path_color); + if (text_edit_color_theme == "Adaptive" || default_theme) { EditorSettings::get_singleton()->set_initial_value( - "text_editor/highlighting/gdscript/function_definition_color", + "text_edit/highlighting/gdscript/function_definition_color", function_definition_color, true); EditorSettings::get_singleton()->set_initial_value( - "text_editor/highlighting/gdscript/node_path_color", + "text_edit/highlighting/gdscript/node_path_color", node_path_color, true); } - function_definition_color = EDITOR_GET("text_editor/highlighting/gdscript/function_definition_color"); - node_path_color = EDITOR_GET("text_editor/highlighting/gdscript/node_path_color"); - type_color = EDITOR_GET("text_editor/highlighting/base_type_color"); + function_definition_color = EDITOR_GET("text_edit/highlighting/gdscript/function_definition_color"); + node_path_color = EDITOR_GET("text_edit/highlighting/gdscript/node_path_color"); + type_color = EDITOR_GET("text_edit/highlighting/base_type_color"); } SyntaxHighlighter *GDScriptSyntaxHighlighter::create() { diff --git a/modules/gdscript/editor/gdscript_highlighter.h b/modules/gdscript/editor/gdscript_highlighter.h index e652fb1471..47ea5a3d62 100644 --- a/modules/gdscript/editor/gdscript_highlighter.h +++ b/modules/gdscript/editor/gdscript_highlighter.h @@ -63,10 +63,10 @@ public: static SyntaxHighlighter *create(); virtual void _update_cache(); - virtual Map<int, TextEdit::HighlighterInfo> _get_line_syntax_highlighting(int p_line); + virtual Dictionary _get_line_syntax_highlighting(int p_line); - virtual String get_name() const; - virtual List<String> get_supported_languages(); + virtual String _get_name() const; + virtual Array _get_supported_languages() const; }; #endif // GDSCRIPT_HIGHLIGHTER_H diff --git a/modules/visual_script/visual_script_editor.cpp b/modules/visual_script/visual_script_editor.cpp index b7ca3c882b..7a803d8930 100644 --- a/modules/visual_script/visual_script_editor.cpp +++ b/modules/visual_script/visual_script_editor.cpp @@ -4660,10 +4660,10 @@ void VisualScriptEditor::_member_option(int p_option) { } } -void VisualScriptEditor::add_syntax_highlighter(SyntaxHighlighter *p_highlighter) { +void VisualScriptEditor::add_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter) { } -void VisualScriptEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) { +void VisualScriptEditor::set_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter) { } void VisualScriptEditor::_bind_methods() { diff --git a/modules/visual_script/visual_script_editor.h b/modules/visual_script/visual_script_editor.h index 28418965b8..f39def0b32 100644 --- a/modules/visual_script/visual_script_editor.h +++ b/modules/visual_script/visual_script_editor.h @@ -288,8 +288,8 @@ protected: static void _bind_methods(); 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<SyntaxHighlighter> p_highlighter) override; + virtual void set_syntax_highlighter(Ref<SyntaxHighlighter> p_highlighter) override; virtual void apply_code() override; virtual RES get_edited_resource() const override; diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index 3860ce61e9..a56262e552 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -937,7 +937,7 @@ void TextEdit::_notification(int p_what) { break; } - Map<int, HighlighterInfo> color_map; + Dictionary color_map; if (syntax_coloring) { color_map = _get_line_syntax_highlighting(minimap_line); } @@ -980,7 +980,7 @@ void TextEdit::_notification(int p_what) { for (int j = 0; j < str.length(); j++) { if (syntax_coloring) { if (color_map.has(last_wrap_column + j)) { - current_color = color_map[last_wrap_column + j].color; + current_color = color_map[last_wrap_column + j].get("color"); if (readonly) { current_color.a = cache.font_color_readonly.a; } @@ -1060,7 +1060,7 @@ void TextEdit::_notification(int p_what) { const String &fullstr = text[line]; - Map<int, HighlighterInfo> color_map; + Dictionary color_map; if (syntax_coloring) { color_map = _get_line_syntax_highlighting(line); } @@ -1255,7 +1255,7 @@ void TextEdit::_notification(int p_what) { for (; j < str.length(); j++) { if (syntax_coloring) { if (color_map.has(last_wrap_column + j)) { - current_color = color_map[last_wrap_column + j].color; + current_color = color_map[last_wrap_column + j].get("color"); if (readonly && current_color.a > cache.font_color_readonly.a) { current_color.a = cache.font_color_readonly.a; } @@ -5017,20 +5017,20 @@ void TextEdit::_update_caches() { cache.executing_icon = get_theme_icon("MainPlay", "EditorIcons"); text.set_font(cache.font); - if (syntax_highlighter) { - syntax_highlighter->_update_cache(); + if (syntax_highlighter.is_valid()) { + syntax_highlighter->update_cache(); } } -SyntaxHighlighter *TextEdit::_get_syntax_highlighting() { +Ref<SyntaxHighlighter> TextEdit::get_syntax_highlighting() { return syntax_highlighter; } -void TextEdit::_set_syntax_highlighting(SyntaxHighlighter *p_syntax_highlighter) { +void TextEdit::set_syntax_highlighting(Ref<SyntaxHighlighter> p_syntax_highlighter) { syntax_highlighter = p_syntax_highlighter; - if (syntax_highlighter) { - syntax_highlighter->set_text_editor(this); - syntax_highlighter->_update_cache(); + if (syntax_highlighter.is_valid()) { + syntax_highlighter->set_text_edit(this); + syntax_highlighter->update_cache(); } syntax_highlighting_cache.clear(); update(); @@ -7056,6 +7056,9 @@ void TextEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("set_syntax_coloring", "enable"), &TextEdit::set_syntax_coloring); ClassDB::bind_method(D_METHOD("is_syntax_coloring_enabled"), &TextEdit::is_syntax_coloring_enabled); + ClassDB::bind_method(D_METHOD("set_syntax_highlighting", "syntax_highlighter"), &TextEdit::set_syntax_highlighting); + ClassDB::bind_method(D_METHOD("get_syntax_highlighting"), &TextEdit::get_syntax_highlighting); + ClassDB::bind_method(D_METHOD("set_highlight_current_line", "enabled"), &TextEdit::set_highlight_current_line); ClassDB::bind_method(D_METHOD("is_highlight_current_line_enabled"), &TextEdit::is_highlight_current_line_enabled); @@ -7088,6 +7091,7 @@ void TextEdit::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "readonly"), "set_readonly", "is_readonly"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_current_line"), "set_highlight_current_line", "is_highlight_current_line_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "syntax_highlighting"), "set_syntax_coloring", "is_syntax_coloring_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "syntax_highlighter", PROPERTY_HINT_RESOURCE_TYPE, "SyntaxHighlighter"), "set_syntax_highlighting", "get_syntax_highlighting"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_line_numbers"), "set_show_line_numbers", "is_show_line_numbers_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_tabs"), "set_draw_tabs", "is_drawing_tabs"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_spaces"), "set_draw_spaces", "is_drawing_spaces"); @@ -7150,7 +7154,7 @@ TextEdit::TextEdit() { wrap_at = 0; wrap_right_offset = 10; set_focus_mode(FOCUS_ALL); - syntax_highlighter = nullptr; + syntax_highlighter = Ref<SyntaxHighlighter>(NULL); _update_caches(); cache.row_height = 1; cache.line_spacing = 1; @@ -7281,18 +7285,18 @@ TextEdit::~TextEdit() { /////////////////////////////////////////////////////////////////////////////// -Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int p_line) { +Dictionary TextEdit::_get_line_syntax_highlighting(int p_line) { if (syntax_highlighting_cache.has(p_line)) { return syntax_highlighting_cache[p_line]; } - if (syntax_highlighter != nullptr) { - Map<int, HighlighterInfo> color_map = syntax_highlighter->_get_line_syntax_highlighting(p_line); + if (syntax_highlighter.is_valid()) { + Dictionary color_map = syntax_highlighter->get_line_syntax_highlighting(p_line); syntax_highlighting_cache[p_line] = color_map; return color_map; } - Map<int, HighlighterInfo> color_map; + Dictionary color_map; bool prev_is_char = false; bool prev_is_number = false; @@ -7311,7 +7315,7 @@ Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int const String &str = text[p_line]; Color prev_color; for (int j = 0; j < str.length(); j++) { - HighlighterInfo highlighter_info; + Dictionary highlighter_info; if (deregion > 0) { deregion--; @@ -7323,7 +7327,7 @@ Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int if (deregion != 0) { if (color != prev_color) { prev_color = color; - highlighter_info.color = color; + highlighter_info["color"] = color; color_map[j] = highlighter_info; } continue; @@ -7466,7 +7470,7 @@ Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int if (color != prev_color) { prev_color = color; - highlighter_info.color = color; + highlighter_info["color"] = color; color_map[j] = highlighter_info; } } @@ -7474,11 +7478,3 @@ Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int syntax_highlighting_cache[p_line] = color_map; return color_map; } - -void SyntaxHighlighter::set_text_editor(TextEdit *p_text_editor) { - text_editor = p_text_editor; -} - -TextEdit *SyntaxHighlighter::get_text_editor() { - return text_editor; -} diff --git a/scene/gui/text_edit.h b/scene/gui/text_edit.h index 68aec9cfac..b916c80377 100644 --- a/scene/gui/text_edit.h +++ b/scene/gui/text_edit.h @@ -35,17 +35,12 @@ #include "scene/gui/popup_menu.h" #include "scene/gui/scroll_bar.h" #include "scene/main/timer.h" - -class SyntaxHighlighter; +#include "scene/resources/syntax_highlighter.h" class TextEdit : public Control { GDCLASS(TextEdit, Control); public: - struct HighlighterInfo { - Color color; - }; - struct ColorRegion { Color color; String begin_key; @@ -262,7 +257,7 @@ private: } cache; Map<int, int> color_region_cache; - Map<int, Map<int, HighlighterInfo>> syntax_highlighting_cache; + Map<int, Dictionary> syntax_highlighting_cache; struct TextOperation { enum Type { @@ -305,11 +300,11 @@ private: void _do_text_op(const TextOperation &p_op, bool p_reverse); //syntax coloring - SyntaxHighlighter *syntax_highlighter; + Ref<SyntaxHighlighter> syntax_highlighter; HashMap<String, Color> keywords; HashMap<String, Color> member_keywords; - Map<int, HighlighterInfo> _get_line_syntax_highlighting(int p_line); + Dictionary _get_line_syntax_highlighting(int p_line); Vector<ColorRegion> color_regions; @@ -536,8 +531,8 @@ protected: static void _bind_methods(); public: - SyntaxHighlighter *_get_syntax_highlighting(); - void _set_syntax_highlighting(SyntaxHighlighter *p_syntax_highlighter); + Ref<SyntaxHighlighter> get_syntax_highlighting(); + void set_syntax_highlighting(Ref<SyntaxHighlighter> p_syntax_highlighter); int _is_line_in_region(int p_line); ColorRegion _get_color_region(int p_region) const; @@ -822,20 +817,4 @@ public: VARIANT_ENUM_CAST(TextEdit::MenuItems); VARIANT_ENUM_CAST(TextEdit::SearchFlags); -class SyntaxHighlighter { -protected: - TextEdit *text_editor; - -public: - virtual ~SyntaxHighlighter() {} - virtual void _update_cache() = 0; - virtual Map<int, TextEdit::HighlighterInfo> _get_line_syntax_highlighting(int p_line) = 0; - - virtual String get_name() const = 0; - virtual List<String> get_supported_languages() = 0; - - void set_text_editor(TextEdit *p_text_editor); - TextEdit *get_text_editor(); -}; - #endif // TEXT_EDIT_H diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index f40fb78056..98678cd2e3 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -163,6 +163,7 @@ #include "scene/resources/sky_material.h" #include "scene/resources/sphere_shape_3d.h" #include "scene/resources/surface_tool.h" +#include "scene/resources/syntax_highlighter.h" #include "scene/resources/text_file.h" #include "scene/resources/texture.h" #include "scene/resources/tile_set.h" @@ -346,6 +347,7 @@ void register_scene_types() { ClassDB::register_class<Tree>(); ClassDB::register_class<TextEdit>(); + ClassDB::register_class<SyntaxHighlighter>(); ClassDB::register_virtual_class<TreeItem>(); ClassDB::register_class<OptionButton>(); diff --git a/scene/resources/syntax_highlighter.cpp b/scene/resources/syntax_highlighter.cpp new file mode 100644 index 0000000000..7b78a89c1b --- /dev/null +++ b/scene/resources/syntax_highlighter.cpp @@ -0,0 +1,77 @@ +/*************************************************************************/ +/* syntax_highlighter.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "syntax_highlighter.h" + +#include "core/script_language.h" +#include "scene/gui/text_edit.h" + +Dictionary SyntaxHighlighter::get_line_syntax_highlighting(int p_line) { + return call("_get_line_syntax_highlighting", p_line); +} + +void SyntaxHighlighter::update_cache() { + call("_update_cache"); +} + +String SyntaxHighlighter::_get_name() const { + ScriptInstance *si = get_script_instance(); + if (si && si->has_method("_get_name")) { + return si->call("_get_name"); + } + return "Unamed"; +} + +Array SyntaxHighlighter::_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(); +} + +void SyntaxHighlighter::set_text_edit(TextEdit *p_text_edit) { + text_edit = p_text_edit; +} + +TextEdit *SyntaxHighlighter::get_text_edit() { + return text_edit; +} + +void SyntaxHighlighter::_bind_methods() { + ClassDB::bind_method(D_METHOD("_get_line_syntax_highlighting", "p_line"), &SyntaxHighlighter::_get_line_syntax_highlighting); + ClassDB::bind_method(D_METHOD("_update_cache"), &SyntaxHighlighter::_update_cache); + ClassDB::bind_method(D_METHOD("get_text_edit"), &SyntaxHighlighter::get_text_edit); + + BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_name")); + BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_get_supported_languages")); + BIND_VMETHOD(MethodInfo(Variant::DICTIONARY, "_get_line_syntax_highlighting", PropertyInfo(Variant::INT, "p_line"))); + BIND_VMETHOD(MethodInfo("_update_cache")); +} diff --git a/scene/resources/syntax_highlighter.h b/scene/resources/syntax_highlighter.h new file mode 100644 index 0000000000..201bcb03e1 --- /dev/null +++ b/scene/resources/syntax_highlighter.h @@ -0,0 +1,63 @@ +/*************************************************************************/ +/* syntax_highlighter.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef SYNTAX_HIGHLIGHTER_H +#define SYNTAX_HIGHLIGHTER_H + +#include "core/resource.h" + +class TextEdit; + +class SyntaxHighlighter : public Resource { + GDCLASS(SyntaxHighlighter, Resource) + +protected: + TextEdit *text_edit; + + static void _bind_methods(); + +public: + Dictionary get_line_syntax_highlighting(int p_line); + virtual Dictionary _get_line_syntax_highlighting(int p_line) { return Dictionary(); } + + void update_cache(); + virtual void _update_cache() {} + + virtual String _get_name() const; + virtual Array _get_supported_languages() const; + + void set_text_edit(TextEdit *p_text_edit); + TextEdit *get_text_edit(); + + SyntaxHighlighter() {} + virtual ~SyntaxHighlighter() {} +}; + +#endif |