Selaa lähdekoodia

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.
Hugo Locurcio 2 vuotta sitten
vanhempi
commit
ecc1f08386
2 muutettua tiedostoa jossa 67 lisäystä ja 0 poistoa
  1. 45 0
      editor/plugins/script_editor_plugin.cpp
  2. 22 0
      editor/plugins/script_editor_plugin.h

+ 45 - 0
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<EditorSyntaxHighlighter> 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<EditorSyntaxHighlighter> EditorConfigFileSyntaxHighlighter::_create() const {
+	Ref<EditorConfigFileSyntaxHighlighter> 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<EditorConfigFileSyntaxHighlighter> config_file_syntax_highlighter;
+	config_file_syntax_highlighter.instantiate();
+	register_syntax_highlighter(config_file_syntax_highlighter);
+
 	_update_online_doc();
 }
 

+ 22 - 0
editor/plugins/script_editor_plugin.h

@@ -137,6 +137,28 @@ public:
 	EditorMarkdownSyntaxHighlighter() { highlighter.instantiate(); }
 };
 
+class EditorConfigFileSyntaxHighlighter : public EditorSyntaxHighlighter {
+	GDCLASS(EditorConfigFileSyntaxHighlighter, EditorSyntaxHighlighter)
+
+private:
+	Ref<CodeHighlighter> 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<EditorSyntaxHighlighter> _create() const override;
+
+	EditorConfigFileSyntaxHighlighter() { highlighter.instantiate(); }
+};
+
 ///////////////////////////////////////////////////////////////////////////////
 
 class ScriptEditorQuickOpen : public ConfirmationDialog {