From 3fe644de86b6a9fa96bbbbaa7746c52005848d96 Mon Sep 17 00:00:00 2001 From: passivestar <60579014+passivestar@users.noreply.github.com> Date: Sun, 18 Aug 2024 22:40:24 +0400 Subject: [PATCH] Expose GDScript syntax highlighter to editor plugins --- modules/gdscript/config.py | 1 + .../doc_classes/GDScriptSyntaxHighlighter.xml | 23 +++++++++++++++++++ modules/gdscript/register_types.cpp | 7 ++++++ 3 files changed, 31 insertions(+) create mode 100644 modules/gdscript/doc_classes/GDScriptSyntaxHighlighter.xml diff --git a/modules/gdscript/config.py b/modules/gdscript/config.py index a7d5c406e9d..ecd33a5dacc 100644 --- a/modules/gdscript/config.py +++ b/modules/gdscript/config.py @@ -11,6 +11,7 @@ def get_doc_classes(): return [ "@GDScript", "GDScript", + "GDScriptSyntaxHighlighter", ] diff --git a/modules/gdscript/doc_classes/GDScriptSyntaxHighlighter.xml b/modules/gdscript/doc_classes/GDScriptSyntaxHighlighter.xml new file mode 100644 index 00000000000..63a92229015 --- /dev/null +++ b/modules/gdscript/doc_classes/GDScriptSyntaxHighlighter.xml @@ -0,0 +1,23 @@ + + + + A GDScript syntax highlighter that can be used with [TextEdit] and [CodeEdit] nodes. + + + [b]Note:[/b] This class can only be used for editor plugins because it relies on editor settings. + [codeblocks] + [gdscript] + var code_preview = TextEdit.new() + var highlighter = GDScriptSyntaxHighlighter.new() + code_preview.syntax_highlighter = highlighter + [/gdscript] + [csharp] + var codePreview = new TextEdit(); + var highlighter = new GDScriptSyntaxHighlighter(); + codePreview.SyntaxHighlighter = highlighter; + [/csharp] + [/codeblocks] + + + + diff --git a/modules/gdscript/register_types.cpp b/modules/gdscript/register_types.cpp index 59e387eece4..055f8e41102 100644 --- a/modules/gdscript/register_types.cpp +++ b/modules/gdscript/register_types.cpp @@ -165,6 +165,13 @@ void initialize_gdscript_module(ModuleInitializationLevel p_level) { gdscript_translation_parser_plugin.instantiate(); EditorTranslationParser::get_singleton()->add_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD); + } else if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) { + ClassDB::APIType prev_api = ClassDB::get_current_api(); + ClassDB::set_current_api(ClassDB::API_EDITOR); + + GDREGISTER_CLASS(GDScriptSyntaxHighlighter); + + ClassDB::set_current_api(prev_api); } #endif // TOOLS_ENABLED }