diff options
author | Paulb23 <p_batty@hotmail.co.uk> | 2020-03-07 14:29:44 +0000 |
---|---|---|
committer | Paulb23 <p_batty@hotmail.co.uk> | 2020-07-11 15:40:00 +0100 |
commit | 156daddaaf16e36eb932452d1e30f4f77d29aae6 (patch) | |
tree | 942c5f0fe5bf8cbab99ca5534ecef4f76ea8d6b9 | |
parent | 2f1080be9b032b1cf5086201e45057baa6b1a179 (diff) |
Expose Syntax highlighter for editor plugins
-rw-r--r-- | editor/editor_node.cpp | 1 | ||||
-rw-r--r-- | editor/plugins/script_editor_plugin.cpp | 39 | ||||
-rw-r--r-- | editor/plugins/script_editor_plugin.h | 9 | ||||
-rw-r--r-- | editor/plugins/script_text_editor.cpp | 2 | ||||
-rw-r--r-- | editor/plugins/text_editor.cpp | 1 | ||||
-rw-r--r-- | modules/gdscript/editor/gdscript_highlighter.cpp | 6 | ||||
-rw-r--r-- | modules/gdscript/editor/gdscript_highlighter.h | 12 | ||||
-rw-r--r-- | modules/gdscript/register_types.cpp | 7 | ||||
-rw-r--r-- | modules/visual_script/visual_script_editor.cpp | 2 | ||||
-rw-r--r-- | scene/resources/syntax_highlighter.cpp | 9 | ||||
-rw-r--r-- | scene/resources/syntax_highlighter.h | 2 |
11 files changed, 71 insertions, 19 deletions
diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index f768a2cacf..2f669d9006 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -3602,6 +3602,7 @@ void EditorNode::register_editor_types() { ClassDB::register_class<EditorFileSystemDirectory>(); ClassDB::register_class<EditorVCSInterface>(); ClassDB::register_virtual_class<ScriptEditor>(); + ClassDB::register_virtual_class<ScriptEditorBase>(); ClassDB::register_virtual_class<EditorInterface>(); ClassDB::register_class<EditorExportPlugin>(); ClassDB::register_class<EditorResourceConversionPlugin>(); diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp index 3f17f1166f..237aefb1b9 100644 --- a/editor/plugins/script_editor_plugin.cpp +++ b/editor/plugins/script_editor_plugin.cpp @@ -63,6 +63,8 @@ void ScriptEditorBase::_bind_methods() { // TODO: This signal is no use for VisualScript. ADD_SIGNAL(MethodInfo("search_in_files_requested", PropertyInfo(Variant::STRING, "text"))); ADD_SIGNAL(MethodInfo("replace_in_files_requested", PropertyInfo(Variant::STRING, "text"))); + + BIND_VMETHOD(MethodInfo("add_syntax_highlighter", PropertyInfo(Variant::OBJECT, "highlighter"))); } static bool _is_built_in_script(Script *p_script) { @@ -2019,8 +2021,11 @@ bool ScriptEditor::edit(const RES &p_resource, int p_line, int p_col, bool p_gra if (p_resource->get_class_name() != StringName("VisualScript")) { bool highlighter_set = false; - for (int i = 0; i < syntax_highlighters_func_count; i++) { - SyntaxHighlighter *highlighter = syntax_highlighters_funcs[i](); + for (int i = 0; i < syntax_highlighters.size(); i++) { + Ref<SyntaxHighlighter> highlighter = syntax_highlighters[i]->_create(); + if (highlighter.is_null()) { + continue; + } se->add_syntax_highlighter(highlighter); if (script != nullptr && !highlighter_set) { @@ -2768,6 +2773,18 @@ Vector<Ref<Script>> ScriptEditor::get_open_scripts() const { return out_scripts; } +Array ScriptEditor::_get_open_script_editors() const { + Array script_editors; + for (int i = 0; i < tab_container->get_child_count(); i++) { + ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i)); + if (!se) { + continue; + } + script_editors.push_back(se); + } + return script_editors; +} + void ScriptEditor::set_scene_root_script(Ref<Script> p_script) { bool open_dominant = EditorSettings::get_singleton()->get("text_editor/files/open_dominant_script_on_scene_change"); @@ -2813,12 +2830,14 @@ void ScriptEditor::_open_script_request(const String &p_path) { } } -int ScriptEditor::syntax_highlighters_func_count = 0; -CreateSyntaxHighlighterFunc ScriptEditor::syntax_highlighters_funcs[ScriptEditor::SYNTAX_HIGHLIGHTER_FUNC_MAX]; +void ScriptEditor::register_syntax_highlighter(const Ref<SyntaxHighlighter> &p_syntax_highlighter) { + if (syntax_highlighters.find(p_syntax_highlighter) == -1) { + syntax_highlighters.push_back(p_syntax_highlighter); + } +} -void ScriptEditor::register_create_syntax_highlighter_function(CreateSyntaxHighlighterFunc p_func) { - ERR_FAIL_COND(syntax_highlighters_func_count == SYNTAX_HIGHLIGHTER_FUNC_MAX); - syntax_highlighters_funcs[syntax_highlighters_func_count++] = p_func; +void ScriptEditor::unregister_syntax_highlighter(const Ref<SyntaxHighlighter> &p_syntax_highlighter) { + syntax_highlighters.erase(p_syntax_highlighter); } int ScriptEditor::script_editor_func_count = 0; @@ -2927,6 +2946,12 @@ void ScriptEditor::_bind_methods() { ClassDB::bind_method("_update_members_overview", &ScriptEditor::_update_members_overview); ClassDB::bind_method("_update_recent_scripts", &ScriptEditor::_update_recent_scripts); + ClassDB::bind_method("get_current_editor", &ScriptEditor::_get_current_editor); + ClassDB::bind_method("get_open_script_editors", &ScriptEditor::_get_open_script_editors); + + ClassDB::bind_method(D_METHOD("register_syntax_highlighter", "syntax_highlighter"), &ScriptEditor::register_syntax_highlighter); + ClassDB::bind_method(D_METHOD("unregister_syntax_highlighter", "syntax_highlighter"), &ScriptEditor::unregister_syntax_highlighter); + ClassDB::bind_method(D_METHOD("get_drag_data_fw", "point", "from"), &ScriptEditor::get_drag_data_fw); ClassDB::bind_method(D_METHOD("can_drop_data_fw", "point", "data", "from"), &ScriptEditor::can_drop_data_fw); ClassDB::bind_method(D_METHOD("drop_data_fw", "point", "data", "from"), &ScriptEditor::drop_data_fw); diff --git a/editor/plugins/script_editor_plugin.h b/editor/plugins/script_editor_plugin.h index 9f9f51de5d..4ce3b0e4ad 100644 --- a/editor/plugins/script_editor_plugin.h +++ b/editor/plugins/script_editor_plugin.h @@ -236,14 +236,12 @@ class ScriptEditor : public PanelContainer { enum { SCRIPT_EDITOR_FUNC_MAX = 32, - SYNTAX_HIGHLIGHTER_FUNC_MAX = 32 }; static int script_editor_func_count; static CreateScriptEditorFunc script_editor_funcs[SCRIPT_EDITOR_FUNC_MAX]; - static int syntax_highlighters_func_count; - static CreateSyntaxHighlighterFunc syntax_highlighters_funcs[SYNTAX_HIGHLIGHTER_FUNC_MAX]; + Vector<Ref<SyntaxHighlighter> > syntax_highlighters; struct ScriptHistory { Control *control; @@ -322,6 +320,7 @@ class ScriptEditor : public PanelContainer { void _script_created(Ref<Script> p_script); ScriptEditorBase *_get_current_editor() const; + Array _get_open_script_editors() const; void _save_layout(); void _editor_settings_changed(); @@ -441,7 +440,9 @@ public: void set_live_auto_reload_running_scripts(bool p_enabled); - static void register_create_syntax_highlighter_function(CreateSyntaxHighlighterFunc p_func); + void register_syntax_highlighter(const Ref<SyntaxHighlighter> &p_syntax_highlighter); + void unregister_syntax_highlighter(const Ref<SyntaxHighlighter> &p_syntax_highlighter); + static void register_create_script_editor_function(CreateScriptEditorFunc p_func); ScriptEditor(EditorNode *p_editor); diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index a9898dafe5..875e6ef350 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -1410,6 +1410,8 @@ void ScriptTextEditor::_bind_methods() { ClassDB::bind_method("get_drag_data_fw", &ScriptTextEditor::get_drag_data_fw); ClassDB::bind_method("can_drop_data_fw", &ScriptTextEditor::can_drop_data_fw); ClassDB::bind_method("drop_data_fw", &ScriptTextEditor::drop_data_fw); + + ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &ScriptTextEditor::add_syntax_highlighter); } Control *ScriptTextEditor::get_edit_menu() { diff --git a/editor/plugins/text_editor.cpp b/editor/plugins/text_editor.cpp index 071a46b5ee..25c0da90ad 100644 --- a/editor/plugins/text_editor.cpp +++ b/editor/plugins/text_editor.cpp @@ -471,6 +471,7 @@ void TextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) { } void TextEditor::_bind_methods() { + ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &TextEditor::add_syntax_highlighter); } static ScriptEditorBase *create_editor(const RES &p_resource) { diff --git a/modules/gdscript/editor/gdscript_highlighter.cpp b/modules/gdscript/editor/gdscript_highlighter.cpp index 4be83877be..8ab3dc1d56 100644 --- a/modules/gdscript/editor/gdscript_highlighter.cpp +++ b/modules/gdscript/editor/gdscript_highlighter.cpp @@ -387,6 +387,8 @@ void GDScriptSyntaxHighlighter::_update_cache() { type_color = EDITOR_GET("text_edit/highlighting/base_type_color"); } -SyntaxHighlighter *GDScriptSyntaxHighlighter::create() { - return memnew(GDScriptSyntaxHighlighter); +Ref<SyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const { + Ref<GDScriptSyntaxHighlighter> syntax_highlighter; + syntax_highlighter.instance(); + return syntax_highlighter; } diff --git a/modules/gdscript/editor/gdscript_highlighter.h b/modules/gdscript/editor/gdscript_highlighter.h index 47ea5a3d62..c1de22167a 100644 --- a/modules/gdscript/editor/gdscript_highlighter.h +++ b/modules/gdscript/editor/gdscript_highlighter.h @@ -34,6 +34,8 @@ #include "scene/gui/text_edit.h" class GDScriptSyntaxHighlighter : public SyntaxHighlighter { + GDCLASS(GDScriptSyntaxHighlighter, SyntaxHighlighter) + private: enum Type { NONE, @@ -60,13 +62,13 @@ private: Color type_color; public: - static SyntaxHighlighter *create(); + virtual void _update_cache() override; + virtual Dictionary _get_line_syntax_highlighting(int p_line) override; - virtual void _update_cache(); - virtual Dictionary _get_line_syntax_highlighting(int p_line); + virtual String _get_name() const override; + virtual Array _get_supported_languages() const override; - virtual String _get_name() const; - virtual Array _get_supported_languages() const; + virtual Ref<SyntaxHighlighter> _create() const override; }; #endif // GDSCRIPT_HIGHLIGHTER_H diff --git a/modules/gdscript/register_types.cpp b/modules/gdscript/register_types.cpp index 884946efc1..6c4e529922 100644 --- a/modules/gdscript/register_types.cpp +++ b/modules/gdscript/register_types.cpp @@ -142,6 +142,12 @@ static void _editor_init() { gd_export.instance(); EditorExport::get_singleton()->add_export_plugin(gd_export); +#ifdef TOOLS_ENABLED + Ref<GDScriptSyntaxHighlighter> gdscript_syntax_highlighter; + gdscript_syntax_highlighter.instance(); + ScriptEditor::get_singleton()->register_syntax_highlighter(gdscript_syntax_highlighter); +#endif + #ifndef GDSCRIPT_NO_LSP register_lsp_types(); GDScriptLanguageServer *lsp_plugin = memnew(GDScriptLanguageServer); @@ -166,7 +172,6 @@ void register_gdscript_types() { ResourceSaver::add_resource_format_saver(resource_saver_gd); #ifdef TOOLS_ENABLED - ScriptEditor::register_create_syntax_highlighter_function(GDScriptSyntaxHighlighter::create); EditorNode::add_init_callback(_editor_init); gdscript_translation_parser_plugin.instance(); diff --git a/modules/visual_script/visual_script_editor.cpp b/modules/visual_script/visual_script_editor.cpp index 7a803d8930..c8ecf0c283 100644 --- a/modules/visual_script/visual_script_editor.cpp +++ b/modules/visual_script/visual_script_editor.cpp @@ -4686,6 +4686,8 @@ void VisualScriptEditor::_bind_methods() { ClassDB::bind_method("_update_members", &VisualScriptEditor::_update_members); ClassDB::bind_method("_generic_search", &VisualScriptEditor::_generic_search); + + ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &VisualScriptEditor::add_syntax_highlighter); } VisualScriptEditor::VisualScriptEditor() { diff --git a/scene/resources/syntax_highlighter.cpp b/scene/resources/syntax_highlighter.cpp index 7b78a89c1b..f4fc12fa19 100644 --- a/scene/resources/syntax_highlighter.cpp +++ b/scene/resources/syntax_highlighter.cpp @@ -65,6 +65,15 @@ TextEdit *SyntaxHighlighter::get_text_edit() { return text_edit; } +Ref<SyntaxHighlighter> SyntaxHighlighter::_create() const { + Ref<SyntaxHighlighter> syntax_highlighter; + syntax_highlighter.instance(); + if (get_script_instance()) { + syntax_highlighter->set_script(get_script_instance()->get_script()); + } + return syntax_highlighter; +} + 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); diff --git a/scene/resources/syntax_highlighter.h b/scene/resources/syntax_highlighter.h index 201bcb03e1..7059ef4ce1 100644 --- a/scene/resources/syntax_highlighter.h +++ b/scene/resources/syntax_highlighter.h @@ -56,6 +56,8 @@ public: void set_text_edit(TextEdit *p_text_edit); TextEdit *get_text_edit(); + virtual Ref<SyntaxHighlighter> _create() const; + SyntaxHighlighter() {} virtual ~SyntaxHighlighter() {} }; |