Add Markdown syntax highlighting to the script editor

This commit is contained in:
Hugo Locurcio 2023-06-07 19:18:41 +02:00
parent 9425535602
commit 3eb8b0ac8c
No known key found for this signature in database
GPG Key ID: 39E8F8BE30B0A49C
2 changed files with 68 additions and 0 deletions

View File

@ -259,6 +259,52 @@ Ref<EditorSyntaxHighlighter> EditorJSONSyntaxHighlighter::_create() const {
return syntax_highlighter;
}
////
void EditorMarkdownSyntaxHighlighter::_update_cache() {
highlighter->set_text_edit(text_edit);
highlighter->clear_keyword_colors();
highlighter->clear_member_keyword_colors();
highlighter->clear_color_regions();
// Disable automatic symbolic highlights, as these don't make sense for prose.
highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/text_color"));
highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/text_color"));
highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/text_color"));
highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/text_color"));
// Headings (any level).
const Color function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");
highlighter->add_color_region("#", "", function_color);
// Bold.
highlighter->add_color_region("**", "**", function_color);
// `__bold__` syntax is not supported as color regions must begin with a symbol,
// not a character that is valid in an identifier.
// Code (both inline code and triple-backticks code blocks).
const Color code_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
highlighter->add_color_region("`", "`", code_color);
// Link (both references and inline links with URLs). The URL is not highlighted.
const Color link_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
highlighter->add_color_region("[", "]", link_color);
// Quote.
const Color quote_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
highlighter->add_color_region(">", "", quote_color, true);
// HTML comment, which is also supported in Markdown.
const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
highlighter->add_color_region("<!--", "-->", comment_color);
}
Ref<EditorSyntaxHighlighter> EditorMarkdownSyntaxHighlighter::_create() const {
Ref<EditorMarkdownSyntaxHighlighter> syntax_highlighter;
syntax_highlighter.instantiate();
return syntax_highlighter;
}
////////////////////////////////////////////////////////////////////////////////
/*** SCRIPT EDITOR ****/
@ -4331,6 +4377,10 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
json_syntax_highlighter.instantiate();
register_syntax_highlighter(json_syntax_highlighter);
Ref<EditorMarkdownSyntaxHighlighter> markdown_syntax_highlighter;
markdown_syntax_highlighter.instantiate();
register_syntax_highlighter(markdown_syntax_highlighter);
_update_online_doc();
}

View File

@ -120,6 +120,24 @@ public:
EditorJSONSyntaxHighlighter() { highlighter.instantiate(); }
};
class EditorMarkdownSyntaxHighlighter : public EditorSyntaxHighlighter {
GDCLASS(EditorMarkdownSyntaxHighlighter, 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); }
virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "md", "markdown" }; }
virtual String _get_name() const override { return TTR("Markdown"); }
virtual Ref<EditorSyntaxHighlighter> _create() const override;
EditorMarkdownSyntaxHighlighter() { highlighter.instantiate(); }
};
///////////////////////////////////////////////////////////////////////////////
class ScriptEditorQuickOpen : public ConfirmationDialog {