From ecc1f08386042ec7bd091bf4601c797829f0ae62 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Wed, 7 Jun 2023 18:54:19 +0200 Subject: [PATCH] Add syntax highlighting for ConfigFile/TSCN/TRES/project.godot A single highligher is used for all these formats, as they're fairly close to each other. --- editor/plugins/script_editor_plugin.cpp | 45 +++++++++++++++++++++++++ editor/plugins/script_editor_plugin.h | 22 ++++++++++++ 2 files changed, 67 insertions(+) diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp index a47a968997..f5c2c94294 100644 --- a/editor/plugins/script_editor_plugin.cpp +++ b/editor/plugins/script_editor_plugin.cpp @@ -32,6 +32,7 @@ #include "core/config/project_settings.h" #include "core/input/input.h" +#include "core/io/config_file.h" #include "core/io/file_access.h" #include "core/io/json.h" #include "core/io/resource_loader.h" @@ -302,6 +303,46 @@ Ref EditorMarkdownSyntaxHighlighter::_create() const { return syntax_highlighter; } +/// + +void EditorConfigFileSyntaxHighlighter::_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/theme/highlighting/symbol_color")); + highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color")); + // Assume that all function-style syntax is for types such as `Vector2()` and `PackedStringArray()`. + highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/base_type_color")); + + // Disable member variable highlighting as it's not relevant for ConfigFile. + highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/text_color")); + + const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color"); + highlighter->add_color_region("\"", "\"", string_color); + + // FIXME: Sections in ConfigFile must be at the beginning of a line. Otherwise, it can be an array within a line. + const Color function_color = EDITOR_GET("text_editor/theme/highlighting/function_color"); + highlighter->add_color_region("[", "]", function_color); + + const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color"); + highlighter->add_keyword_color("true", keyword_color); + highlighter->add_keyword_color("false", keyword_color); + highlighter->add_keyword_color("null", keyword_color); + highlighter->add_keyword_color("ExtResource", keyword_color); + highlighter->add_keyword_color("SubResource", keyword_color); + + const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color"); + highlighter->add_color_region(";", "", comment_color); +} + +Ref EditorConfigFileSyntaxHighlighter::_create() const { + Ref syntax_highlighter; + syntax_highlighter.instantiate(); + return syntax_highlighter; +} + //////////////////////////////////////////////////////////////////////////////// /*** SCRIPT EDITOR ****/ @@ -4450,6 +4491,10 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) { markdown_syntax_highlighter.instantiate(); register_syntax_highlighter(markdown_syntax_highlighter); + Ref config_file_syntax_highlighter; + config_file_syntax_highlighter.instantiate(); + register_syntax_highlighter(config_file_syntax_highlighter); + _update_online_doc(); } diff --git a/editor/plugins/script_editor_plugin.h b/editor/plugins/script_editor_plugin.h index 9f806309cb..784054d5b6 100644 --- a/editor/plugins/script_editor_plugin.h +++ b/editor/plugins/script_editor_plugin.h @@ -137,6 +137,28 @@ public: EditorMarkdownSyntaxHighlighter() { highlighter.instantiate(); } }; +class EditorConfigFileSyntaxHighlighter : public EditorSyntaxHighlighter { + GDCLASS(EditorConfigFileSyntaxHighlighter, EditorSyntaxHighlighter) + +private: + Ref highlighter; + +public: + virtual void _update_cache() override; + virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); } + + // While not explicitly designed for those formats, this highlighter happens + // to handle TSCN, TRES, `project.godot` well. We can expose it in case the + // user opens one of these using the script editor (which can be done using + // the All Files filter). + virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "ini", "cfg", "tscn", "tres", "godot" }; } + virtual String _get_name() const override { return TTR("ConfigFile"); } + + virtual Ref _create() const override; + + EditorConfigFileSyntaxHighlighter() { highlighter.instantiate(); } +}; + /////////////////////////////////////////////////////////////////////////////// class ScriptEditorQuickOpen : public ConfirmationDialog {