diff options
author | Paulb23 <p_batty@hotmail.co.uk> | 2020-03-07 11:17:18 +0000 |
---|---|---|
committer | Paulb23 <p_batty@hotmail.co.uk> | 2020-07-11 15:26:58 +0100 |
commit | 2f1080be9b032b1cf5086201e45057baa6b1a179 (patch) | |
tree | c6af7f72914a1ea9c88eddb80925ae174cfbd8e3 /scene | |
parent | be119c5c473be3fb73458baca7066d85869d6f1d (diff) |
Convert syntax highlighters into a resource
Diffstat (limited to 'scene')
-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 |
5 files changed, 171 insertions, 54 deletions
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 |